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, id) ⇒ Address

Returns a new Address object.

Parameters:

  • network_id (Symbol)

    The Network ID

  • id (String)

    The onchain Address ID



14
15
16
17
# File 'lib/coinbase/address.rb', line 14

def initialize(network_id, id)
  @network_id = Coinbase.to_sym(network_id)
  @id = id
end

Instance Attribute Details

#idString (readonly)

The onchain Address ID

Returns:

  • (String)

    the current value of id



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

def id
  @id
end

#network_idSymbol (readonly)

The Network ID

Returns:

  • (Symbol)

    the current value of 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.

Parameters:

  • asset_id (Symbol)

    The Asset to retrieve the balance for

Returns:

  • (BigDecimal)

    The balance of the 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

#balancesBalanceMap

Returns the balances of the Address.

Returns:

  • (BalanceMap)

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



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

#can_sign?Boolean

Returns true if the Address can sign transactions.

Returns:

  • (Boolean)

    true if the Address can sign transactions



33
34
35
# File 'lib/coinbase/address.rb', line 33

def can_sign?
  false
end

#faucetCoinbase::FaucetTransaction

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

Returns:

Raises:



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

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the Address



27
28
29
# File 'lib/coinbase/address.rb', line 27

def inspect
  to_s
end

#to_sString

Returns a String representation of the Address.

Returns:

  • (String)

    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