Class: Coinbase::Wallet
- Inherits:
-
Object
- Object
- Coinbase::Wallet
- Defined in:
- lib/coinbase/wallet.rb
Overview
A representation of a Wallet. Wallets come with a single default Address, but can expand to have a set of Addresses, each of which can hold a balance of one or more Assets. Wallets can create new Addresses, list their addresses, list their balances, and transfer Assets to other Addresses. Wallets should be created through User#create_wallet or User#import_wallet.
Defined Under Namespace
Classes: Data
Instance Method Summary collapse
-
#create_address ⇒ Address
Creates a new Address in the Wallet.
-
#default_address ⇒ Address
Returns the default address of the Wallet.
-
#export ⇒ Data
Exports the Wallet’s data to a Data object.
-
#get_address(address_id) ⇒ Address
Returns the Address with the given ID.
-
#get_balance(asset_id) ⇒ BigDecimal
Returns the balance of the provided Asset.
-
#initialize(model, seed: nil, address_count: 0) ⇒ Wallet
constructor
Returns a new Wallet object.
-
#inspect ⇒ String
Same as to_s.
-
#list_addresses ⇒ Array<Address>
Returns the list of Addresses in the Wallet.
-
#list_balances ⇒ BalanceMap
Returns the list of balances of this Wallet.
-
#network_id ⇒ Symbol
Returns the Network ID of the Wallet.
-
#to_s ⇒ String
Returns a String representation of the Wallet.
-
#transfer(amount, asset_id, destination) ⇒ Transfer
Transfers the given amount of the given Asset to the given address.
-
#wallet_id ⇒ String
Returns the Wallet ID.
Constructor Details
#initialize(model, seed: nil, address_count: 0) ⇒ Wallet
Returns a new Wallet object. Do not use this method directly. Instead, use User#create_wallet or User#import_wallet.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/coinbase/wallet.rb', line 22 def initialize(model, seed: nil, address_count: 0) raise ArgumentError, 'Seed must be 32 bytes' if !seed.nil? && seed.length != 64 @model = model @master = seed.nil? ? MoneyTree::Master.new : MoneyTree::Master.new(seed_hex: seed) # TODO: Make Network an argument to the constructor. @network_id = :base_sepolia @addresses = [] # TODO: Adjust derivation path prefix based on network protocol. @address_path_prefix = "m/44'/60'/0'/0" @address_index = 0 if address_count.positive? address_count.times { derive_address } else create_address # Update the model to reflect the new default address. update_model end end |
Instance Method Details
#create_address ⇒ Address
Creates a new Address in the Wallet.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/coinbase/wallet.rb', line 60 def create_address key = derive_key attestation = create_attestation(key) public_key = key.public_key.compressed.unpack1('H*') opts = { create_address_request: { public_key: public_key, attestation: attestation } } address_model = Coinbase.call_api do addresses_api.create_address(wallet_id, opts) end cache_address(address_model, key) end |
#default_address ⇒ Address
Returns the default address of the Wallet.
80 81 82 |
# File 'lib/coinbase/wallet.rb', line 80 def default_address @addresses.find { |address| address.address_id == @model.default_address.address_id } end |
#export ⇒ Data
Exports the Wallet’s data to a Data object.
160 161 162 |
# File 'lib/coinbase/wallet.rb', line 160 def export Data.new(wallet_id: wallet_id, seed: @master.seed_hex) end |
#get_address(address_id) ⇒ Address
Returns the Address with the given ID.
87 88 89 |
# File 'lib/coinbase/wallet.rb', line 87 def get_address(address_id) @addresses.find { |address| address.address_id == address_id } end |
#get_balance(asset_id) ⇒ BigDecimal
Returns the balance of the provided Asset. Balances are aggregated across all Addresses in the Wallet.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/coinbase/wallet.rb', line 110 def get_balance(asset_id) normalized_asset_id = if %i[wei gwei].include?(asset_id) :eth else asset_id end response = Coinbase.call_api do wallets_api.get_wallet_balance(wallet_id, normalized_asset_id.to_s) end 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 |
#inspect ⇒ String
Same as to_s.
173 174 175 |
# File 'lib/coinbase/wallet.rb', line 173 def inspect to_s end |
#list_addresses ⇒ Array<Address>
Returns the list of Addresses in the Wallet.
93 94 95 |
# File 'lib/coinbase/wallet.rb', line 93 def list_addresses @addresses end |
#list_balances ⇒ BalanceMap
Returns the list of balances of this Wallet. Balances are aggregated across all Addresses in the Wallet.
99 100 101 102 103 104 105 |
# File 'lib/coinbase/wallet.rb', line 99 def list_balances response = Coinbase.call_api do wallets_api.list_wallet_balances(wallet_id) end Coinbase.to_balance_map(response) end |
#network_id ⇒ Symbol
Returns the Network ID of the Wallet.
54 55 56 |
# File 'lib/coinbase/wallet.rb', line 54 def network_id Coinbase.to_sym(@model.network_id) end |
#to_s ⇒ String
Returns a String representation of the Wallet.
166 167 168 169 |
# File 'lib/coinbase/wallet.rb', line 166 def to_s "Coinbase::Wallet{wallet_id: '#{wallet_id}', network_id: '#{network_id}', " \ "default_address: '#{default_address.address_id}'}" end |
#transfer(amount, asset_id, destination) ⇒ Transfer
Transfers the given amount of the given Asset to the given address. Only same-Network Transfers are supported. Currently only the default_address is used to source the Transfer.
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/coinbase/wallet.rb', line 144 def transfer(amount, asset_id, destination) 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 default_address.transfer(amount, asset_id, destination) end |
#wallet_id ⇒ String
Returns the Wallet ID.
48 49 50 |
# File 'lib/coinbase/wallet.rb', line 48 def wallet_id @model.id end |