Class: Coinbase::ExternalAddress

Inherits:
Address
  • Object
show all
Defined in:
lib/coinbase/address/external_address.rb

Overview

A representation of a blockchain Address that do not belong to a Coinbase::Wallet. External addresses can be used to fetch balances, request funds from the faucet, etc…, but cannot be used to sign transactions.

Instance Attribute Summary

Attributes inherited from Address

#id, #network_id

Instance Method Summary collapse

Methods inherited from Address

#balance, #balances, #can_sign?, #faucet, #initialize, #inspect, #to_s

Constructor Details

This class inherits a constructor from Coinbase::Address

Instance Method Details

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

Builds an 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: {})

    Additional options for the claim_stake operation

Returns:



41
42
43
44
45
# File 'lib/coinbase/address/external_address.rb', line 41

def build_claim_stake_operation(amount, asset_id, mode: :default, options: {})
  validate_can_claim_stake!(amount, asset_id, mode, options)

  build_staking_operation(amount, asset_id, 'claim_stake', mode: mode, options: 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: {})

    Additional options for the stake operation

Returns:



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

def build_stake_operation(amount, asset_id, mode: :default, options: {})
  validate_can_stake!(amount, asset_id, mode, options)

  build_staking_operation(amount, asset_id, 'stake', mode: mode, options: 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: {})

    Additional options for the unstake operation

Returns:



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

def build_unstake_operation(amount, asset_id, mode: :default, options: {})
  validate_can_unstake!(amount, asset_id, mode, options)

  build_staking_operation(amount, asset_id, 'unstake', mode: mode, options: options)
end

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

Retreives 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 the staking operation

Returns:

  • (BigDecimal)

    The claimable balance



108
109
110
# File 'lib/coinbase/address/external_address.rb', line 108

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

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

Retreives 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 the staking operation

Returns:

  • (BigDecimal)

    The stakeable balance



90
91
92
# File 'lib/coinbase/address/external_address.rb', line 90

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

Retreives 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: {})

    Additional options for the staking operation

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/coinbase/address/external_address.rb', line 55

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(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_month(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_month(1))

    The start time for the rewards. Defaults to 1 month 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:



118
119
120
121
122
123
124
125
126
127
# File 'lib/coinbase/address/external_address.rb', line 118

def staking_rewards(asset_id, start_time: DateTime.now.prev_month(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

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

Retreives 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 the staking operation

Returns:

  • (BigDecimal)

    The unstakeable balance



99
100
101
# File 'lib/coinbase/address/external_address.rb', line 99

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