Class: Coinbase::Address
- Inherits:
-
Object
- Object
- Coinbase::Address
- 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
-
#address_id ⇒ Object
readonly
Returns the value of attribute address_id.
-
#network_id ⇒ Object
readonly
Returns the value of attribute network_id.
-
#wallet_id ⇒ Object
readonly
Returns the value of attribute wallet_id.
Instance Method Summary collapse
-
#get_balance(asset_id) ⇒ BigDecimal
Returns the balance of the provided Asset.
-
#initialize(network_id, address_id, wallet_id, key, client: Jimson::Client.new(ENV.fetch('BASE_SEPOLIA_RPC_URL', nil))) ⇒ Address
constructor
Returns a new Address object.
-
#list_balances ⇒ BalanceMap
Returns the balances of the Address.
-
#to_s ⇒ String
Returns the address as a string.
-
#transfer(amount, asset_id, destination) ⇒ String
Transfers the given amount of the given Asset to the given address.
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.
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_id ⇒ Object (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_id ⇒ Object (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_id ⇒ Object (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.
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_balances ⇒ BalanceMap
Returns the balances of the Address. Currently only ETH balances are supported.
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_s ⇒ String
Returns the address as a string.
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.
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 |