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:



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

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

Instance Method Details

#address_idString

Returns the Address ID.

Returns:

  • (String)

    The Address ID



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

def address_id
  @model.address_id
end

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/coinbase/address.rb', line 53

def get_balance(asset_id)
  normalized_asset_id = normalize_asset_id(asset_id)

  response = addresses_api.get_address_balance(wallet_id, address_id, normalized_asset_id.to_s)

  return BigDecimal('0') if response.nil?

  amount = BigDecimal(response.amount)

  case asset_id
  when :eth
    amount / BigDecimal(Coinbase::WEI_PER_ETHER.to_s)
  when :gwei
    amount / BigDecimal(Coinbase::GWEI_PER_ETHER.to_s)
  when :usdc
    amount / BigDecimal(Coinbase::ATOMIC_UNITS_PER_USDC.to_s)
  else
    amount
  end
end

#list_balancesBalanceMap

Returns the balances of the Address.

Returns:

  • (BalanceMap)

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



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

def list_balances
  response = addresses_api.list_address_balances(wallet_id, address_id)
  Coinbase.to_balance_map(response)
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 the address as a string.

Returns:

  • (String)

    The address



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

def to_s
  address_id
end

#transfer(amount, asset_id, destination) ⇒ String

Transfers the given amount of the given Asset to the given address. 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:

  • (String)

    The hash of the Transfer transaction.

Raises:

  • (ArgumentError)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/coinbase/address.rb', line 80

def transfer(amount, asset_id, destination)
  raise ArgumentError, "Unsupported asset: #{asset_id}" unless Coinbase::SUPPORTED_ASSET_IDS[asset_id]

  if destination.is_a?(Wallet)
    raise ArgumentError, 'Transfer must be on the same Network' if destination.network_id != network_id

    destination = destination.default_address.address_id
  elsif destination.is_a?(Address)
    raise ArgumentError, 'Transfer must be on the same Network' if destination.network_id != network_id

    destination = destination.address_id
  end

  current_balance = get_balance(asset_id)
  if current_balance < amount
    raise ArgumentError, "Insufficient funds: #{amount} requested, but only #{current_balance} available"
  end

  normalized_amount = normalize_asset_amount(amount, asset_id)

  normalized_asset_id = normalize_asset_id(asset_id)

  create_transfer_request = {
    amount: normalized_amount.to_i.to_s,
    network_id: network_id,
    asset_id: normalized_asset_id.to_s,
    destination: destination
  }

  transfer_model = transfers_api.create_transfer(wallet_id, address_id, create_transfer_request)

  transfer = Coinbase::Transfer.new(transfer_model)

  transaction = transfer.transaction
  transaction.sign(@key)
  Coinbase.configuration.base_sepolia_client.eth_sendRawTransaction("0x#{transaction.hex}")

  transfer
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