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, and should be created using Wallet#create_address. Addresses require an Eth::Key to sign transaction data.

Instance Method Summary collapse

Constructor Details

#initialize(model, key) ⇒ Address

Returns a new Address object. Do not use this method directly. Instead, use Wallet#create_address, or use the Wallet’s default_address.

Parameters:

  • model (Coinbase::Client::Address)

    The underlying Address object

  • key (Eth::Key)

    The key backing the Address. Can be nil.



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

def initialize(model, key)
  @model = model
  @key = key
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



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

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



53
54
55
56
57
58
59
# File 'lib/coinbase/address.rb', line 53

def balances
  response = Coinbase.call_api do
    addresses_api.list_address_balances(wallet_id, id)
  end

  Coinbase::BalanceMap.from_balances(response.data)
end

#can_sign?Boolean

Returns whether the Address has a private key backing it to sign transactions.

Returns:

  • (Boolean)

    Whether the Address has a private key backing it to sign transactions.



121
122
123
# File 'lib/coinbase/address.rb', line 121

def can_sign?
  !@key.nil?
end

#exportString

Exports the Address’s private key to a hex string.

Returns:

  • (String)

    The Address’s private key as a hex String



150
151
152
153
154
# File 'lib/coinbase/address.rb', line 150

def export
  raise 'Cannot export key without private key loaded' if @key.nil?

  @key.private_hex
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:



142
143
144
145
146
# File 'lib/coinbase/address.rb', line 142

def faucet
  Coinbase.call_api do
    Coinbase::FaucetTransaction.new(addresses_api.request_faucet_funds(wallet_id, id))
  end
end

#idString

Returns the Address ID.

Returns:

  • (String)

    The Address ID



38
39
40
# File 'lib/coinbase/address.rb', line 38

def id
  @model.address_id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the Address



133
134
135
# File 'lib/coinbase/address.rb', line 133

def inspect
  to_s
end

#key=(key) ⇒ Object

Sets the private key backing the Address. This key is used to sign transactions.

Parameters:

  • key (Eth::Key)

    The key backing the Address



44
45
46
47
48
# File 'lib/coinbase/address.rb', line 44

def key=(key)
  raise 'Private key is already set' unless @key.nil?

  @key = key
end

#network_idSymbol

Returns the Network ID of the Address.

Returns:

  • (Symbol)

    The Network ID



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

def network_id
  Coinbase.to_sym(@model.network_id)
end

#to_sString

Returns a String representation of the Address.

Returns:

  • (String)

    a String representation of the Address



127
128
129
# File 'lib/coinbase/address.rb', line 127

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.

Parameters:

  • amount (Integer, Float, BigDecimal)

    The amount of the Asset to send.

  • from_asset_id (Symbol)

    The ID of the Asset to trade from. For Ether, :eth, :gwei, and :wei are supported.

  • to_asset_id (Symbol)

    The ID of the Asset to trade to. For Ether, :eth, :gwei, and :wei are supported.

Returns:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/coinbase/address.rb', line 102

def trade(amount, from_asset_id, to_asset_id)
  from_asset = Asset.fetch(network_id, from_asset_id)
  to_asset = Asset.fetch(network_id, to_asset_id)

  validate_can_trade!(amount, from_asset)

  trade = create_trade(amount, from_asset, to_asset)

  # NOTE: Trading does not yet support server signers at this point.

  payloads = { signed_payload: trade.transaction.sign(@key) }

  payloads[:approve_tx_signed_payload] = trade.approve_transaction.sign(@key) unless trade.approve_transaction.nil?

  broadcast_trade(trade, **payloads)
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.

Parameters:

  • amount (Integer, Float, BigDecimal)

    The amount of the Asset to send.

  • asset_id (Symbol)

    The ID of the Asset to send. For Ether, :eth, :gwei, and :wei are supported.

  • destination (Wallet | Address | String)

    The destination of the transfer. If a Wallet, sends to the Wallet’s default address. If a String, interprets it as the address ID.

Returns:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/coinbase/address.rb', line 81

def transfer(amount, asset_id, destination)
  asset = Asset.fetch(network_id, asset_id)

  destination_address, destination_network = destination_address_and_network(destination)

  validate_can_transfer!(amount, asset, destination_network)

  transfer = create_transfer(amount, asset, destination_address)

  # 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?

  broadcast_transfer(transfer, transfer.transaction.sign(@key))
end

#transfersArray<Coinbase::Transfer>

Returns all of the transfers associated with the address.

Returns:



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/coinbase/address.rb', line 158

def transfers
  transfers = []
  page = nil

  loop do
    response = Coinbase.call_api do
      transfers_api.list_transfers(wallet_id, id, { limit: 100, page: page })
    end

    break if response.data.empty?

    transfers.concat(response.data.map { |transfer| Coinbase::Transfer.new(transfer) })

    break unless response.has_more

    page = response.next_page
  end

  transfers
end

#wallet_idString

Returns the Wallet ID of the Address.

Returns:

  • (String)

    The Wallet ID



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

def wallet_id
  @model.wallet_id
end