Class: Coinbase::Address

Inherits:
Object
  • Object
show all
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

ExternalAddress, WalletAddress

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network, id) ⇒ Address

Returns a new Address object.

Parameters:

  • network (Coinbase::Network, Symbol)

    The Network or Network ID

  • id (String)

    The onchain Address ID



12
13
14
15
# File 'lib/coinbase/address.rb', line 12

def initialize(network, id)
  @network = Coinbase::Network.from_id(network)
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/coinbase/address.rb', line 7

def id
  @id
end

#networkObject (readonly)

Returns the value of attribute network.



7
8
9
# File 'lib/coinbase/address.rb', line 7

def network
  @network
end

Instance Method Details

#balance(asset_id) ⇒ BigDecimal

Returns the balance of the provided Asset.

Parameters:

  • asset_id (Symbol)

    The Asset to retrieve the balance for

Returns:

  • (BigDecimal)

    The balance of the Asset



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/coinbase/address.rb', line 49

def balance(asset_id)
  response = Coinbase.call_api do
    addresses_api.get_external_address_balance(
      network.normalized_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

#balancesBalanceMap

Returns the balances of the Address.

Returns:

  • (BalanceMap)

    The balances of the Address, keyed by asset ID. Ether balances are denominated in ETH.



38
39
40
41
42
43
44
# File 'lib/coinbase/address.rb', line 38

def balances
  response = Coinbase.call_api do
    addresses_api.list_external_address_balances(network.normalized_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.

Parameters:

  • amount (Integer, String, BigDecimal)

    The amount of the asset to claim

  • asset_id (Symbol)

    The asset to claim

  • mode (Symbol) (defaults to: :default)

    The staking mode. Defaults to :default.

  • options (Hash) (defaults to: {})

    (Optional) Additional options for the claim_stake operation. (see_more)

Returns:



135
136
137
138
139
# File 'lib/coinbase/address.rb', line 135

def build_claim_stake_operation(amount, asset_id, mode: :default, options: {})
  validate_can_perform_staking_action!(amount, asset_id, 'claimable_balance', mode, options)

  StakingOperation.build(amount, network, asset_id, id, 'claim_stake', mode, options)
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.

Parameters:

  • amount (Integer, String, BigDecimal)

    The amount of the asset to stake

  • asset_id (Symbol)

    The asset to stake

  • mode (Symbol) (defaults to: :default)

    The staking mode. Defaults to :default.

  • options (Hash) (defaults to: {})

    (Optional) Additional options for the stake operation. (see_more)

Returns:



110
111
112
113
114
# File 'lib/coinbase/address.rb', line 110

def build_stake_operation(amount, asset_id, mode: :default, options: {})
  validate_can_perform_staking_action!(amount, asset_id, 'stakeable_balance', mode, options)

  StakingOperation.build(amount, network, asset_id, id, 'stake', mode, options)
end

#build_unstake_operation(amount, asset_id, mode: :default, options: {}) ⇒ Coinbase::StakingOperation

Builds an unstake operation for the supplied asset.

Parameters:

  • amount (Integer, String, BigDecimal)

    The amount of the asset to unstake

  • asset_id (Symbol)

    The asset to unstake

  • mode (Symbol) (defaults to: :default)

    The staking mode. Defaults to :default.

  • options (Hash) (defaults to: {})

    (Optional) Additional options for the unstake operation. (see_more)

Returns:



122
123
124
125
126
# File 'lib/coinbase/address.rb', line 122

def build_unstake_operation(amount, asset_id, mode: :default, options: {})
  validate_can_perform_staking_action!(amount, asset_id, 'unstakeable_balance', mode, options)

  StakingOperation.build(amount, network, asset_id, id, 'unstake', mode, options)
end

#can_sign?Boolean

Returns true if the Address can sign transactions.

Returns:

  • (Boolean)

    true if the Address can sign transactions



31
32
33
# File 'lib/coinbase/address.rb', line 31

def can_sign?
  false
end

#claimable_balance(asset_id, mode: :default, options: {}) ⇒ BigDecimal

Retrieves the claimable balance for the supplied asset.

Parameters:

  • asset_id (Symbol)

    The asset to retrieve the claimable balance for

  • mode (Symbol) (defaults to: :default)

    The staking mode. Defaults to :default.

  • options (Hash) (defaults to: {})

    Additional options for fetching the claimable balance

Returns:

  • (BigDecimal)

    The claimable balance



204
205
206
# File 'lib/coinbase/address.rb', line 204

def claimable_balance(asset_id, mode: :default, options: {})
  staking_balances(asset_id, mode: mode, options: options)[:claimable_balance]
end

#faucet(asset_id: nil) ⇒ Coinbase::FaucetTransaction

Requests funds for the address from the faucet and returns the faucet transaction. This is only supported on testnet networks.

Parameters:

  • asset_id (Symbol) (defaults to: nil)

    The ID of the Asset to transfer to the wallet.

Returns:

Raises:



93
94
95
96
97
98
99
100
101
# File 'lib/coinbase/address.rb', line 93

def faucet(asset_id: nil)
  opts = { asset_id: asset_id }.compact

  Coinbase.call_api do
    Coinbase::FaucetTransaction.new(
      addresses_api.request_external_faucet_funds(network.normalized_id, id, opts)
    )
  end
end

#historical_balances(asset_id) ⇒ Enumerable<Coinbase::HistoricalBalance>

Enumerates the historical balances for a given asset belonging of address. The result is an enumerator that lazily fetches from the server, and can be iterated over, converted to an array, etc…

Returns:



67
68
69
70
71
72
73
# File 'lib/coinbase/address.rb', line 67

def historical_balances(asset_id)
  Coinbase::Pagination.enumerate(
    ->(page) { list_page(asset_id, page) }
  ) do |historical_balance|
    Coinbase::HistoricalBalance.from_model(historical_balance)
  end
end

#historical_staking_balances(asset_id, start_time: DateTime.now.prev_week(1), end_time: DateTime.now) ⇒ Enumerable<Coinbase::StakingBalance>

Fetches the historical staking balances for the address.

Parameters:

  • asset_id (Symbol)

    The asset to retrieve staking rewards for

  • start_time (Time) (defaults to: DateTime.now.prev_week(1))

    The start time for the rewards. Defaults to 1 week ago.

  • end_time (Time) (defaults to: DateTime.now)

    The end time for the rewards. Defaults to the current time.

Returns:



230
231
232
233
234
235
236
237
238
# File 'lib/coinbase/address.rb', line 230

def historical_staking_balances(asset_id, start_time: DateTime.now.prev_week(1), end_time: DateTime.now)
  StakingBalance.list(
    network,
    asset_id,
    id,
    start_time: start_time,
    end_time: end_time
  )
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the Address



25
26
27
# File 'lib/coinbase/address.rb', line 25

def inspect
  to_s
end

#stakeable_balance(asset_id, mode: :default, options: {}) ⇒ BigDecimal

Retrieves the stakeable balance for the supplied asset.

Parameters:

  • asset_id (Symbol)

    The asset to retrieve the stakeable balance for

  • mode (Symbol) (defaults to: :default)

    The staking mode. Defaults to :default.

  • options (Hash) (defaults to: {})

    Additional options for fetching the stakeable balance

Returns:

  • (BigDecimal)

    The stakeable balance



186
187
188
# File 'lib/coinbase/address.rb', line 186

def stakeable_balance(asset_id, mode: :default, options: {})
  staking_balances(asset_id, mode: mode, options: options)[:stakeable_balance]
end

#staking_balances(asset_id, mode: :default, options: {}) ⇒ Hash, BigDecimal

Retrieves the balances used for staking for the supplied asset.

Parameters:

  • asset_id (Symbol)

    The asset to retrieve staking balances for

  • mode (Symbol) (defaults to: :default)

    The staking mode. Defaults to :default.

  • options (Hash) (defaults to: {})

    (Optional) Additional options for fetching the staking balances

Options Hash (options:):

  • (Optional) (String)

    :validator_pub_keys List of comma separated validator public keys to retrieve staking balances for. (default: all validators) [asset_id: :eth, mode: :native]

Returns:

  • (Hash)

    The staking balances

  • (BigDecimal)

    :stakeable_balance The amount of the asset that can be staked

  • (BigDecimal)

    :unstakeable_balance The amount of the asset that is currently staked and cannot be unstaked

  • (BigDecimal)

    :claimable_balance The amount of the asset that can be claimed



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/coinbase/address.rb', line 151

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: network.normalized_id,
        address_id: id,
        options: {
          mode: mode
        }.merge(options)
      }
    )
  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.

Parameters:

  • asset_id (Symbol)

    The asset to retrieve staking rewards for

  • start_time (Time) (defaults to: DateTime.now.prev_week(1))

    The start time for the rewards. Defaults to 1 week ago.

  • end_time (Time) (defaults to: DateTime.now)

    The end time for the rewards. Defaults to the current time.

  • format (Symbol) (defaults to: :usd)

    The format to return the rewards in. Defaults to :usd.

Returns:



214
215
216
217
218
219
220
221
222
223
# File 'lib/coinbase/address.rb', line 214

def staking_rewards(asset_id, start_time: DateTime.now.prev_week(1), end_time: DateTime.now, format: :usd)
  StakingReward.list(
    network,
    asset_id,
    [id],
    start_time: start_time,
    end_time: end_time,
    format: format
  )
end

#to_sString

Returns a String representation of the Address.

Returns:

  • (String)

    a String representation of the Address



19
20
21
# File 'lib/coinbase/address.rb', line 19

def to_s
  Coinbase.pretty_print_object(self.class, id: id, network_id: network.id)
end

#transactionsEnumerable<Coinbase::HistoricalBalance>

Enumerates the transactions of address. The result is an enumerator that lazily fetches from the server, and can be iterated over, converted to an array, etc…

Returns:



79
80
81
82
83
84
85
# File 'lib/coinbase/address.rb', line 79

def transactions
  Coinbase::Pagination.enumerate(
    ->(page) { list_transaction_page(page) }
  ) do |transaction|
    Coinbase::Transaction.new(transaction)
  end
end

#unstakeable_balance(asset_id, mode: :default, options: {}) ⇒ BigDecimal

Retrieves the unstakeable balance for the supplied asset.

Parameters:

  • asset_id (Symbol)

    The asset to retrieve the unstakeable balance for

  • mode (Symbol) (defaults to: :default)

    The staking mode. Defaults to :default.

  • options (Hash) (defaults to: {})

    Additional options for fetching the unstakeable balance

Returns:

  • (BigDecimal)

    The unstakeable balance



195
196
197
# File 'lib/coinbase/address.rb', line 195

def unstakeable_balance(asset_id, mode: :default, options: {})
  staking_balances(asset_id, mode: mode, options: options)[:unstakeable_balance]
end