Class: Coinbase::WalletAddress

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

Overview

A representation of a blockchain Address that belongs to a Coinbase::Wallet. 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.

Constant Summary collapse

PAGE_LIMIT =
100

Instance Attribute Summary

Attributes inherited from Address

#id, #network_id

Instance Method Summary collapse

Methods inherited from Address

#balance, #balances, #faucet, #inspect

Constructor Details

#initialize(model, key) ⇒ WalletAddress

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.



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

def initialize(model, key)
  @model = model
  @key = key

  super(model.network_id, model.address_id)
end

Instance Method Details

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



85
86
87
# File 'lib/coinbase/address/wallet_address.rb', line 85

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



91
92
93
94
95
# File 'lib/coinbase/address/wallet_address.rb', line 91

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

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



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

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

  @key = key
end

#to_sString

Returns a String representation of the WalletAddress.

Returns:

  • (String)

    a String representation of the WalletAddress



119
120
121
# File 'lib/coinbase/address/wallet_address.rb', line 119

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:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/coinbase/address/wallet_address.rb', line 66

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

#tradesEnumerable<Coinbase::Trade>

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

Returns:

  • (Enumerable<Coinbase::Trade>)

    Enumerator that returns the address’s trades



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

def trades
  Coinbase::Pagination.enumerate(lambda(&method(:fetch_trades_page))) do |trade|
    Coinbase::Trade.new(trade)
  end
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:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/coinbase/address/wallet_address.rb', line 45

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

#transfersEnumerable<Coinbase::Transfer>

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

Returns:



101
102
103
104
105
# File 'lib/coinbase/address/wallet_address.rb', line 101

def transfers
  Coinbase::Pagination.enumerate(lambda(&method(:fetch_transfers_page))) do |transfer|
    Coinbase::Transfer.new(transfer)
  end
end

#wallet_idString

Returns the Wallet ID of the Address.

Returns:

  • (String)

    The Wallet ID



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

def wallet_id
  @model.wallet_id
end