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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#id ⇒ String
readonly
The onchain Address ID.
-
#network_id ⇒ Symbol
readonly
The Network ID.
Instance Method Summary collapse
-
#balance(asset_id) ⇒ BigDecimal
Returns the balance of the provided Asset.
-
#balances ⇒ BalanceMap
Returns the balances of the Address.
-
#build_claim_stake_operation(amount, asset_id, mode: :default, options: {}) ⇒ Coinbase::StakingOperation
Builds a claim_stake operation for the supplied asset.
-
#build_stake_operation(amount, asset_id, mode: :default, options: {}) ⇒ Coinbase::StakingOperation
Builds a stake operation for the supplied asset.
-
#build_unstake_operation(amount, asset_id, mode: :default, options: {}) ⇒ Coinbase::StakingOperation
Builds an unstake operation for the supplied asset.
-
#can_sign? ⇒ Boolean
Returns true if the Address can sign transactions.
-
#claimable_balance(asset_id, mode: :default, options: {}) ⇒ BigDecimal
Retrieves the claimable balance for the supplied asset.
-
#faucet ⇒ Coinbase::FaucetTransaction
Requests funds for the address from the faucet and returns the faucet transaction.
-
#initialize(network_id, id) ⇒ Address
constructor
Returns a new Address object.
-
#inspect ⇒ String
Same as to_s.
-
#stakeable_balance(asset_id, mode: :default, options: {}) ⇒ BigDecimal
Retrieves the stakeable balance for the supplied asset.
-
#staking_balances(asset_id, mode: :default, options: {}) ⇒ Hash, BigDecimal
Retrieves the balances used for staking for the supplied asset.
-
#staking_rewards(asset_id, start_time: DateTime.now.prev_week(1), end_time: DateTime.now, format: :usd) ⇒ Enumerable<Coinbase::StakingReward>
Lists the staking rewards for the address.
-
#to_s ⇒ String
Returns a String representation of the Address.
-
#unstakeable_balance(asset_id, mode: :default, options: {}) ⇒ BigDecimal
Retrieves the unstakeable balance for the supplied asset.
Constructor Details
Instance Attribute Details
#id ⇒ String (readonly)
The onchain Address ID
8 9 10 |
# File 'lib/coinbase/address.rb', line 8 def id @id end |
#network_id ⇒ Symbol (readonly)
The Network ID
8 9 10 |
# File 'lib/coinbase/address.rb', line 8 def network_id @network_id end |
Instance Method Details
#balance(asset_id) ⇒ BigDecimal
Returns the balance of the provided Asset.
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/coinbase/address.rb', line 51 def balance(asset_id) response = Coinbase.call_api do addresses_api.get_external_address_balance( Coinbase.normalize_network(network_id), id, Coinbase::Asset.primary_denomination(asset_id).to_s ) end return BigDecimal('0') if response.nil? Coinbase::Balance.from_model_and_asset_id(response, asset_id).amount end |
#balances ⇒ BalanceMap
Returns the balances of the Address.
40 41 42 43 44 45 46 |
# File 'lib/coinbase/address.rb', line 40 def balances response = Coinbase.call_api do addresses_api.list_external_address_balances(Coinbase.normalize_network(network_id), id) end Coinbase::BalanceMap.from_balances(response.data) end |
#build_claim_stake_operation(amount, asset_id, mode: :default, options: {}) ⇒ Coinbase::StakingOperation
Builds a claim_stake operation for the supplied asset.
109 110 111 112 113 |
# File 'lib/coinbase/address.rb', line 109 def build_claim_stake_operation(amount, asset_id, mode: :default, options: {}) validate_can_perform_staking_action!(amount, asset_id, 'claimable_balance', mode, ) StakingOperation.build(amount, network_id, asset_id, id, 'claim_stake', mode, ) end |
#build_stake_operation(amount, asset_id, mode: :default, options: {}) ⇒ Coinbase::StakingOperation
Builds a stake operation for the supplied asset. The stake operation may take a few minutes to complete in the case when infrastructure is spun up.
85 86 87 88 89 |
# File 'lib/coinbase/address.rb', line 85 def build_stake_operation(amount, asset_id, mode: :default, options: {}) validate_can_perform_staking_action!(amount, asset_id, 'stakeable_balance', mode, ) StakingOperation.build(amount, network_id, asset_id, id, 'stake', mode, ) end |
#build_unstake_operation(amount, asset_id, mode: :default, options: {}) ⇒ Coinbase::StakingOperation
Builds an unstake operation for the supplied asset.
97 98 99 100 101 |
# File 'lib/coinbase/address.rb', line 97 def build_unstake_operation(amount, asset_id, mode: :default, options: {}) validate_can_perform_staking_action!(amount, asset_id, 'unstakeable_balance', mode, ) StakingOperation.build(amount, network_id, asset_id, id, 'unstake', mode, ) end |
#can_sign? ⇒ Boolean
Returns true if the Address can sign transactions.
33 34 35 |
# File 'lib/coinbase/address.rb', line 33 def can_sign? false end |
#claimable_balance(asset_id, mode: :default, options: {}) ⇒ BigDecimal
Retrieves the claimable balance for the supplied asset.
176 177 178 |
# File 'lib/coinbase/address.rb', line 176 def claimable_balance(asset_id, mode: :default, options: {}) staking_balances(asset_id, mode: mode, options: )[:claimable_balance] end |
#faucet ⇒ Coinbase::FaucetTransaction
Requests funds for the address from the faucet and returns the faucet transaction. This is only supported on testnet networks.
70 71 72 73 74 75 76 |
# File 'lib/coinbase/address.rb', line 70 def faucet Coinbase.call_api do Coinbase::FaucetTransaction.new( addresses_api.request_external_faucet_funds(Coinbase.normalize_network(network_id), id) ) end end |
#inspect ⇒ String
Same as to_s.
27 28 29 |
# File 'lib/coinbase/address.rb', line 27 def inspect to_s end |
#stakeable_balance(asset_id, mode: :default, options: {}) ⇒ BigDecimal
Retrieves the stakeable balance for the supplied asset.
158 159 160 |
# File 'lib/coinbase/address.rb', line 158 def stakeable_balance(asset_id, mode: :default, options: {}) staking_balances(asset_id, mode: mode, options: )[:stakeable_balance] end |
#staking_balances(asset_id, mode: :default, options: {}) ⇒ Hash, BigDecimal
Retrieves the balances used for staking for the supplied asset.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/coinbase/address.rb', line 123 def staking_balances(asset_id, mode: :default, options: {}) context_model = Coinbase.call_api do stake_api.get_staking_context( { asset_id: asset_id, network_id: Coinbase.normalize_network(network_id), address_id: id, options: { mode: mode }.merge() } ) end.context { stakeable_balance: Coinbase::Balance.from_model_and_asset_id( context_model.stakeable_balance, asset_id ).amount, unstakeable_balance: Coinbase::Balance.from_model_and_asset_id( context_model.unstakeable_balance, asset_id ).amount, claimable_balance: Coinbase::Balance.from_model_and_asset_id( context_model.claimable_balance, asset_id ).amount } end |
#staking_rewards(asset_id, start_time: DateTime.now.prev_week(1), end_time: DateTime.now, format: :usd) ⇒ Enumerable<Coinbase::StakingReward>
Lists the staking rewards for the address.
186 187 188 189 190 191 192 193 194 195 |
# File 'lib/coinbase/address.rb', line 186 def staking_rewards(asset_id, start_time: DateTime.now.prev_week(1), end_time: DateTime.now, format: :usd) StakingReward.list( network_id, asset_id, [id], start_time: start_time, end_time: end_time, format: format ) end |
#to_s ⇒ String
Returns a String representation of the Address.
21 22 23 |
# File 'lib/coinbase/address.rb', line 21 def to_s "Coinbase::Address{id: '#{id}', network_id: '#{network_id}'}" end |
#unstakeable_balance(asset_id, mode: :default, options: {}) ⇒ BigDecimal
Retrieves the unstakeable balance for the supplied asset.
167 168 169 |
# File 'lib/coinbase/address.rb', line 167 def unstakeable_balance(asset_id, mode: :default, options: {}) staking_balances(asset_id, mode: mode, options: )[:unstakeable_balance] end |