Class: Coinbase::Address
- Inherits:
-
Object
- Object
- Coinbase::Address
- Defined in:
- lib/coinbase/address.rb
Overview
A representation of a blockchain Address, which is a user-controlled account on a Network. Addresses are used to send and receive Assets, and should be created using Wallet#create_address. Addresses require an Eth::Key to sign transaction data.
Instance Method Summary collapse
-
#address_id ⇒ String
Returns the Address ID.
-
#export ⇒ String
Exports the Address’s private key to a hex string.
-
#faucet ⇒ Coinbase::FaucetTransaction
Requests funds for the address from the faucet and returns the faucet transaction.
-
#get_balance(asset_id) ⇒ BigDecimal
Returns the balance of the provided Asset.
-
#initialize(model, key) ⇒ Address
constructor
Returns a new Address object.
-
#inspect ⇒ String
Same as to_s.
-
#list_balances ⇒ BalanceMap
Returns the balances of the Address.
-
#list_transfer_ids ⇒ Array<String>
Lists the IDs of all Transfers associated with the given Wallet and Address.
-
#network_id ⇒ Symbol
Returns the Network ID of the Address.
-
#to_s ⇒ String
Returns a String representation of the Address.
-
#transfer(amount, asset_id, destination) ⇒ String
Transfers the given amount of the given Asset to the given address.
-
#wallet_id ⇒ String
Returns the Wallet ID of the Address.
Constructor Details
#initialize(model, key) ⇒ Address
Returns a new Address object. Do not use this method directly. Instead, use Wallet#create_address, or use the Wallet’s default_address.
19 20 21 22 |
# File 'lib/coinbase/address.rb', line 19 def initialize(model, key) @model = model @key = key end |
Instance Method Details
#address_id ⇒ String
Returns the Address ID.
38 39 40 |
# File 'lib/coinbase/address.rb', line 38 def address_id @model.address_id end |
#export ⇒ String
Exports the Address’s private key to a hex string.
161 162 163 |
# File 'lib/coinbase/address.rb', line 161 def export @key.private_hex end |
#faucet ⇒ Coinbase::FaucetTransaction
Requests funds for the address from the faucet and returns the faucet transaction. This is only supported on testnet networks.
153 154 155 156 157 |
# File 'lib/coinbase/address.rb', line 153 def faucet Coinbase.call_api do Coinbase::FaucetTransaction.new(addresses_api.request_faucet_funds(wallet_id, address_id)) end end |
#get_balance(asset_id) ⇒ BigDecimal
Returns the balance of the provided Asset.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/coinbase/address.rb', line 56 def get_balance(asset_id) normalized_asset_id = normalize_asset_id(asset_id) response = Coinbase.call_api do addresses_api.get_address_balance(wallet_id, address_id, normalized_asset_id.to_s) end return BigDecimal('0') if response.nil? amount = BigDecimal(response.amount) case asset_id when :eth amount / BigDecimal(Coinbase::WEI_PER_ETHER.to_s) when :gwei amount / BigDecimal(Coinbase::GWEI_PER_ETHER.to_s) when :usdc amount / BigDecimal(Coinbase::ATOMIC_UNITS_PER_USDC.to_s) else amount end end |
#inspect ⇒ String
Same as to_s.
144 145 146 |
# File 'lib/coinbase/address.rb', line 144 def inspect to_s end |
#list_balances ⇒ BalanceMap
Returns the balances of the Address.
45 46 47 48 49 50 51 |
# File 'lib/coinbase/address.rb', line 45 def list_balances response = Coinbase.call_api do addresses_api.list_address_balances(wallet_id, address_id) end Coinbase.to_balance_map(response) end |
#list_transfer_ids ⇒ Array<String>
Lists the IDs of all Transfers associated with the given Wallet and Address.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/coinbase/address.rb', line 167 def list_transfer_ids transfer_ids = [] page = nil loop do response = Coinbase.call_api do transfers_api.list_transfers(wallet_id, address_id, { limit: 100, page: page }) end transfer_ids.concat(response.data.map(&:transfer_id)) if response.data break unless response.has_more page = response.next_page end transfer_ids end |
#network_id ⇒ Symbol
Returns the Network ID of the Address.
26 27 28 |
# File 'lib/coinbase/address.rb', line 26 def network_id Coinbase.to_sym(@model.network_id) end |
#to_s ⇒ String
Returns a String representation of the Address.
138 139 140 |
# File 'lib/coinbase/address.rb', line 138 def to_s "Coinbase::Address{address_id: '#{address_id}', network_id: '#{network_id}', wallet_id: '#{wallet_id}'}" end |
#transfer(amount, asset_id, destination) ⇒ String
Transfers the given amount of the given Asset to the given address. Only same-Network Transfers are supported.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/coinbase/address.rb', line 85 def transfer(amount, asset_id, destination) raise ArgumentError, "Unsupported asset: #{asset_id}" unless Coinbase::SUPPORTED_ASSET_IDS[asset_id] if destination.is_a?(Wallet) raise ArgumentError, 'Transfer must be on the same Network' if destination.network_id != network_id destination = destination.default_address.address_id elsif destination.is_a?(Address) raise ArgumentError, 'Transfer must be on the same Network' if destination.network_id != network_id destination = destination.address_id end current_balance = get_balance(asset_id) if current_balance < amount raise ArgumentError, "Insufficient funds: #{amount} requested, but only #{current_balance} available" end normalized_amount = normalize_asset_amount(amount, asset_id) normalized_asset_id = normalize_asset_id(asset_id) create_transfer_request = { amount: normalized_amount.to_i.to_s, network_id: network_id, asset_id: normalized_asset_id.to_s, destination: destination } transfer_model = Coinbase.call_api do transfers_api.create_transfer(wallet_id, address_id, create_transfer_request) end transfer = Coinbase::Transfer.new(transfer_model) transaction = transfer.transaction transaction.sign(@key) signed_payload = transaction.hex broadcast_transfer_request = { signed_payload: signed_payload } transfer_model = Coinbase.call_api do transfers_api.broadcast_transfer(wallet_id, address_id, transfer.transfer_id, broadcast_transfer_request) end Coinbase::Transfer.new(transfer_model) end |
#wallet_id ⇒ String
Returns the Wallet ID of the Address.
32 33 34 |
# File 'lib/coinbase/address.rb', line 32 def wallet_id @model.wallet_id end |