Class: Coinbase::WalletAddress
- Defined in:
- lib/coinbase/address/wallet_address.rb
Overview
A representation of a blockchain Address that belongs to a Coinbase::Wallet. 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 Attribute Summary
Attributes inherited from Address
Instance Method Summary collapse
-
#can_sign? ⇒ Boolean
Returns whether the Address has a private key backing it to sign transactions.
-
#claim_stake(amount, asset_id, mode: :default, options: {}) ⇒ Coinbase::StakingOperation
Claims the given amount of the given Asset.
-
#export ⇒ String
Exports the Address’s private key to a hex string.
-
#initialize(model, key) ⇒ WalletAddress
constructor
Returns a new Address object.
-
#key=(key) ⇒ Object
Sets the private key backing the Address.
-
#stake(amount, asset_id, mode: :default, options: {}) ⇒ Coinbase::StakingOperation
Stakes the given amount of the given Asset.
-
#to_s ⇒ String
Returns a String representation of the WalletAddress.
-
#trade(amount, from_asset_id, to_asset_id) ⇒ Coinbase::Trade
Trades the given amount of the given Asset for another Asset.
-
#trades ⇒ Enumerable<Coinbase::Trade>
Enumerates the trades associated with the address.
-
#transfer(amount, asset_id, destination) ⇒ Coinbase::Transfer
Transfers the given amount of the given Asset to the specified address or wallet.
-
#transfers ⇒ Enumerable<Coinbase::Transfer>
Enumerates the transfers associated with the address.
-
#unstake(amount, asset_id, mode: :default, options: {}) ⇒ Coinbase::StakingOperation
Unstakes the given amount of the given Asset.
-
#wallet_id ⇒ String
Returns the Wallet ID of the Address.
Methods inherited from Address
#balance, #balances, #build_claim_stake_operation, #build_stake_operation, #build_unstake_operation, #claimable_balance, #faucet, #inspect, #stakeable_balance, #staking_balances, #staking_rewards, #unstakeable_balance
Constructor Details
#initialize(model, key) ⇒ WalletAddress
Returns a new Address object. Do not use this method directly. Instead, use Wallet#create_address, or use the Wallet’s default_address.
15 16 17 18 19 20 |
# File 'lib/coinbase/address/wallet_address.rb', line 15 def initialize(model, key) @model = model @key = key super(model.network_id, model.address_id) end |
Instance Method Details
#can_sign? ⇒ Boolean
Returns whether the Address has a private key backing it to sign transactions.
133 134 135 |
# File 'lib/coinbase/address/wallet_address.rb', line 133 def can_sign? !@key.nil? end |
#claim_stake(amount, asset_id, mode: :default, options: {}) ⇒ Coinbase::StakingOperation
Claims the given amount of the given Asset
125 126 127 128 129 |
# File 'lib/coinbase/address/wallet_address.rb', line 125 def claim_stake(amount, asset_id, mode: :default, options: {}) validate_can_perform_staking_action!(amount, asset_id, 'claimable_balance', mode, ) complete_staking_operation(amount, asset_id, 'claim_stake', mode: mode, options: ) end |
#export ⇒ String
Exports the Address’s private key to a hex string.
139 140 141 142 143 |
# File 'lib/coinbase/address/wallet_address.rb', line 139 def export raise 'Cannot export key without private key loaded' if @key.nil? @key.private_hex end |
#key=(key) ⇒ Object
Sets the private key backing the Address. This key is used to sign transactions.
30 31 32 33 34 |
# File 'lib/coinbase/address/wallet_address.rb', line 30 def key=(key) raise 'Private key is already set' unless @key.nil? @key = key end |
#stake(amount, asset_id, mode: :default, options: {}) ⇒ Coinbase::StakingOperation
Stakes the given amount of the given Asset
101 102 103 104 105 |
# File 'lib/coinbase/address/wallet_address.rb', line 101 def stake(amount, asset_id, mode: :default, options: {}) validate_can_perform_staking_action!(amount, asset_id, 'stakeable_balance', mode, ) complete_staking_operation(amount, asset_id, 'stake', mode: mode, options: ) end |
#to_s ⇒ String
Returns a String representation of the WalletAddress.
163 164 165 |
# File 'lib/coinbase/address/wallet_address.rb', line 163 def to_s "Coinbase::Address{id: '#{id}', network_id: '#{network_id}', wallet_id: '#{wallet_id}'}" end |
#trade(amount, from_asset_id, to_asset_id) ⇒ Coinbase::Trade
Trades the given amount of the given Asset for another Asset. Only same-network Trades are supported.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/coinbase/address/wallet_address.rb', line 71 def trade(amount, from_asset_id, to_asset_id) ensure_can_sign! ensure_sufficient_balance!(amount, from_asset_id) trade = Trade.create( address_id: id, amount: amount, from_asset_id: from_asset_id, to_asset_id: to_asset_id, network_id: network_id, wallet_id: wallet_id ) # If a server signer is managing keys, it will sign and broadcast the underlying trade transaction out of band. return trade if Coinbase.use_server_signer? trade.transactions.each do |tx| tx.sign(@key) end trade.broadcast! trade end |
#trades ⇒ Enumerable<Coinbase::Trade>
Enumerates the trades associated with the address. The result is an enumerator that lazily fetches from the server, and can be iterated over, converted to an array, etc…
157 158 159 |
# File 'lib/coinbase/address/wallet_address.rb', line 157 def trades Trade.list(wallet_id: wallet_id, address_id: id) end |
#transfer(amount, asset_id, destination) ⇒ Coinbase::Transfer
Transfers the given amount of the given Asset to the specified address or wallet. Only same-network Transfers are supported.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/coinbase/address/wallet_address.rb', line 43 def transfer(amount, asset_id, destination) ensure_can_sign! ensure_sufficient_balance!(amount, asset_id) transfer = Transfer.create( address_id: id, amount: amount, asset_id: asset_id, destination: destination, network_id: network_id, wallet_id: wallet_id ) # If a server signer is managing keys, it will sign and broadcast the underlying transfer transaction out of band. return transfer if Coinbase.use_server_signer? transfer.transaction.sign(@key) transfer.broadcast! transfer end |
#transfers ⇒ Enumerable<Coinbase::Transfer>
Enumerates the transfers associated with the address. The result is an enumerator that lazily fetches from the server, and can be iterated over, converted to an array, etc…
149 150 151 |
# File 'lib/coinbase/address/wallet_address.rb', line 149 def transfers Transfer.list(wallet_id: wallet_id, address_id: id) end |
#unstake(amount, asset_id, mode: :default, options: {}) ⇒ Coinbase::StakingOperation
Unstakes the given amount of the given Asset
113 114 115 116 117 |
# File 'lib/coinbase/address/wallet_address.rb', line 113 def unstake(amount, asset_id, mode: :default, options: {}) validate_can_perform_staking_action!(amount, asset_id, 'unstakeable_balance', mode, ) complete_staking_operation(amount, asset_id, 'unstake', mode: mode, options: ) end |
#wallet_id ⇒ String
Returns the Wallet ID of the Address.
24 25 26 |
# File 'lib/coinbase/address/wallet_address.rb', line 24 def wallet_id @model.wallet_id end |