Class: Coinbase::Transfer
- Inherits:
-
Object
- Object
- Coinbase::Transfer
- Defined in:
- lib/coinbase/transfer.rb
Overview
A representation of a Transfer, which moves an amount of an Asset from a user-controlled Wallet to another address. The fee is assumed to be paid in the native Asset of the Network. Transfers should be created through Wallet#transfer or Address#transfer.
Defined Under Namespace
Modules: Status
Instance Method Summary collapse
-
#amount ⇒ BigDecimal
Returns the amount of the asset for the Transfer.
-
#asset_id ⇒ Symbol
Returns the Asset ID of the Transfer.
-
#destination_address_id ⇒ String
Returns the Destination Address ID of the Transfer.
-
#from_address_id ⇒ String
Returns the From Address ID of the Transfer.
-
#initialize(model) ⇒ Transfer
constructor
Returns a new Transfer object.
-
#inspect ⇒ String
Same as to_s.
-
#network_id ⇒ Symbol
Returns the Network ID of the Transfer.
-
#signed_payload ⇒ String
Returns the Signed Payload of the Transfer.
-
#status ⇒ Symbol
Returns the status of the Transfer.
-
#to_s ⇒ String
Returns a String representation of the Transfer.
-
#transaction ⇒ Eth::Tx::Eip1559
Returns the underlying Transfer transaction, creating it if it has not been yet.
-
#transaction_hash ⇒ String
Returns the Transaction Hash of the Transfer.
-
#transaction_link ⇒ String
Returns the link to the transaction on the blockchain explorer.
-
#transfer_id ⇒ String
Returns the Transfer ID.
-
#unsigned_payload ⇒ String
Returns the Unsigned Payload of the Transfer.
-
#wait!(interval_seconds = 0.2, timeout_seconds = 10) ⇒ Transfer
Waits until the Transfer is completed or failed by polling the Network at the given interval.
-
#wallet_id ⇒ String
Returns the Wallet ID of the Transfer.
Constructor Details
#initialize(model) ⇒ Transfer
Returns a new Transfer object. Do not use this method directly. Instead, use Wallet#transfer or Address#transfer.
34 35 36 |
# File 'lib/coinbase/transfer.rb', line 34 def initialize(model) @model = model end |
Instance Method Details
#amount ⇒ BigDecimal
Returns the amount of the asset for the Transfer.
76 77 78 79 80 81 82 83 |
# File 'lib/coinbase/transfer.rb', line 76 def amount case asset_id when :eth BigDecimal(@model.amount) / BigDecimal(Coinbase::WEI_PER_ETHER.to_s) else BigDecimal(@model.amount) end end |
#asset_id ⇒ Symbol
Returns the Asset ID of the Transfer.
70 71 72 |
# File 'lib/coinbase/transfer.rb', line 70 def asset_id @model.asset_id.to_sym end |
#destination_address_id ⇒ String
Returns the Destination Address ID of the Transfer.
64 65 66 |
# File 'lib/coinbase/transfer.rb', line 64 def destination_address_id @model.destination end |
#from_address_id ⇒ String
Returns the From Address ID of the Transfer.
58 59 60 |
# File 'lib/coinbase/transfer.rb', line 58 def from_address_id @model.address_id end |
#inspect ⇒ String
Same as to_s.
190 191 192 |
# File 'lib/coinbase/transfer.rb', line 190 def inspect to_s end |
#network_id ⇒ Symbol
Returns the Network ID of the Transfer.
46 47 48 |
# File 'lib/coinbase/transfer.rb', line 46 def network_id Coinbase.to_sym(@model.network_id) end |
#signed_payload ⇒ String
Returns the Signed Payload of the Transfer.
100 101 102 |
# File 'lib/coinbase/transfer.rb', line 100 def signed_payload @model.signed_payload end |
#status ⇒ Symbol
Returns the status of the Transfer.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/coinbase/transfer.rb', line 136 def status # Check if the transfer has been signed yet. return Status::PENDING if transaction_hash.nil? onchain_transaction = Coinbase.configuration.base_sepolia_client.eth_getTransactionByHash(transaction_hash) if onchain_transaction.nil? # If the transaction has not been broadcast, it is still pending. Status::PENDING elsif onchain_transaction['blockHash'].nil? # If the transaction has been broadcast but hasn't been included in a block, it is # broadcast. Status::BROADCAST else transaction_receipt = Coinbase.configuration.base_sepolia_client.eth_getTransactionReceipt(transaction_hash) if transaction_receipt['status'].to_i(16) == 1 Status::COMPLETE else Status::FAILED end end end |
#to_s ⇒ String
Returns a String representation of the Transfer.
181 182 183 184 185 186 |
# File 'lib/coinbase/transfer.rb', line 181 def to_s "Coinbase::Transfer{transfer_id: '#{transfer_id}', network_id: '#{network_id}', " \ "from_address_id: '#{from_address_id}', destination_address_id: '#{destination_address_id}', " \ "asset_id: '#{asset_id}', amount: '#{amount}', transaction_hash: '#{transaction_hash}', " \ "transaction_link: '#{transaction_link}', status: '#{status}'}" end |
#transaction ⇒ Eth::Tx::Eip1559
Returns the underlying Transfer transaction, creating it if it has not been yet.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/coinbase/transfer.rb', line 112 def transaction return @transaction unless @transaction.nil? raw_payload = [unsigned_payload].pack('H*') parsed_payload = JSON.parse(raw_payload) params = { chain_id: parsed_payload['chainId'].to_i(16), nonce: parsed_payload['nonce'].to_i(16), priority_fee: parsed_payload['maxPriorityFeePerGas'].to_i(16), max_gas_fee: parsed_payload['maxFeePerGas'].to_i(16), gas_limit: parsed_payload['gas'].to_i(16), # TODO: Handle multiple currencies. from: Eth::Address.new(from_address_id), to: Eth::Address.new(parsed_payload['to']), value: parsed_payload['value'].to_i(16), data: parsed_payload['input'] || '' } @transaction = Eth::Tx::Eip1559.new(Eth::Tx.validate_eip1559_params(params)) @transaction end |
#transaction_hash ⇒ String
Returns the Transaction Hash of the Transfer.
106 107 108 |
# File 'lib/coinbase/transfer.rb', line 106 def transaction_hash @model.transaction_hash end |
#transaction_link ⇒ String
Returns the link to the transaction on the blockchain explorer.
87 88 89 90 |
# File 'lib/coinbase/transfer.rb', line 87 def transaction_link # TODO: Parameterize this by Network. "https://sepolia.basescan.org/tx/#{transaction_hash}" end |
#transfer_id ⇒ String
Returns the Transfer ID.
40 41 42 |
# File 'lib/coinbase/transfer.rb', line 40 def transfer_id @model.transfer_id end |
#unsigned_payload ⇒ String
Returns the Unsigned Payload of the Transfer.
94 95 96 |
# File 'lib/coinbase/transfer.rb', line 94 def unsigned_payload @model.unsigned_payload end |
#wait!(interval_seconds = 0.2, timeout_seconds = 10) ⇒ Transfer
Waits until the Transfer is completed or failed by polling the Network at the given interval. Raises a Timeout::Error if the Transfer takes longer than the given timeout.
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/coinbase/transfer.rb', line 165 def wait!(interval_seconds = 0.2, timeout_seconds = 10) start_time = Time.now loop do return self if status == Status::COMPLETE || status == Status::FAILED raise Timeout::Error, 'Transfer timed out' if Time.now - start_time > timeout_seconds self.sleep interval_seconds end self end |
#wallet_id ⇒ String
Returns the Wallet ID of the Transfer.
52 53 54 |
# File 'lib/coinbase/transfer.rb', line 52 def wallet_id @model.wallet_id end |