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 a Eth::Key to sign transaction data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network_id, address_id, wallet_id, key, client: Jimson::Client.new(ENV.fetch('BASE_SEPOLIA_RPC_URL', nil))) ⇒ Address

Returns a new Address object.

Parameters:

  • network_id (Symbol)

    The ID of the Network on which the Address exists

  • address_id (String)

    The ID of the Address. On EVM Networks, for example, this is a hash of the public key.

  • wallet_id (String)

    The ID of the Wallet to which the Address belongs

  • key (Eth::Key)

    The key backing the Address

  • client (Jimson::Client) (defaults to: Jimson::Client.new(ENV.fetch('BASE_SEPOLIA_RPC_URL', nil)))

    (Optional) The JSON RPC client to use for interacting with the Network



22
23
24
25
26
27
28
29
30
# File 'lib/coinbase/address.rb', line 22

def initialize(network_id, address_id, wallet_id, key,
               client: Jimson::Client.new(ENV.fetch('BASE_SEPOLIA_RPC_URL', nil)))
  # TODO: Don't require key.
  @network_id = network_id
  @address_id = address_id
  @wallet_id = wallet_id
  @key = key
  @client = client
end

Instance Attribute Details

#address_idObject (readonly)

Returns the value of attribute address_id.



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

def address_id
  @address_id
end

#network_idObject (readonly)

Returns the value of attribute network_id.



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

def network_id
  @network_id
end

#wallet_idObject (readonly)

Returns the value of attribute wallet_id.



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

def wallet_id
  @wallet_id
end

Instance Method Details

#get_balance(asset_id) ⇒ BigDecimal

Returns the balance of the provided Asset. Currently only ETH is supported.

Parameters:

  • asset_id (Symbol)

    The Asset to retrieve the balance for

Returns:

  • (BigDecimal)

    The balance of the Asset



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/coinbase/address.rb', line 46

def get_balance(asset_id)
  normalized_asset_id = if %i[wei gwei].include?(asset_id)
                          :eth
                        else
                          asset_id
                        end

  eth_balance = list_balances[normalized_asset_id] || BigDecimal(0)

  case asset_id
  when :eth
    eth_balance
  when :gwei
    eth_balance * Coinbase::GWEI_PER_ETHER
  when :wei
    eth_balance * Coinbase::WEI_PER_ETHER
  else
    BigDecimal(0)
  end
end

#list_balancesBalanceMap

Returns the balances of the Address. Currently only ETH balances are supported.

Returns:

  • (BalanceMap)

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



35
36
37
38
39
40
41
# File 'lib/coinbase/address.rb', line 35

def list_balances
  # TODO: Handle multiple currencies.
  eth_balance_in_wei = BigDecimal(@client.eth_getBalance(@address_id, 'latest').to_i(16).to_s)
  eth_balance = BigDecimal(eth_balance_in_wei / BigDecimal(Coinbase::WEI_PER_ETHER.to_s))

  BalanceMap.new({ eth: eth_balance })
end

#to_sString

Returns the address as a string.

Returns:

  • (String)

    The address



104
105
106
# File 'lib/coinbase/address.rb', line 104

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)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/coinbase/address.rb', line 73

def transfer(amount, asset_id, destination)
  # TODO: Handle multiple currencies.
  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

  transfer = Coinbase::Transfer.new(@network_id, @wallet_id, @address_id, amount, asset_id, destination,
                                    client: @client)

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

  transfer
end