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.
-
#network_id ⇒ Symbol
Returns the Network ID of the Transfer.
-
#status ⇒ Symbol
Returns the status 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, or nil if not yet available.
-
#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 |
# File 'lib/coinbase/transfer.rb', line 76 def amount BigDecimal(@model.amount) / BigDecimal(Coinbase::WEI_PER_ETHER.to_s) 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 |
#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 |
#status ⇒ Symbol
Returns the status of the Transfer.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/coinbase/transfer.rb', line 112 def status begin # Create the transaction, and attempt to get the hash to see if it has been signed. transaction.hash rescue Eth::Signature::SignatureError # If the transaction has not been signed, it is still pending. return Status::PENDING end 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 |
#transaction ⇒ Eth::Tx::Eip1559
Returns the underlying Transfer transaction, creating it if it has not been yet.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/coinbase/transfer.rb', line 88 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, or nil if not yet available.
162 163 164 165 166 |
# File 'lib/coinbase/transfer.rb', line 162 def transaction_hash "0x#{transaction.hash}" rescue Eth::Signature::SignatureError nil 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.
82 83 84 |
# File 'lib/coinbase/transfer.rb', line 82 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.
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/coinbase/transfer.rb', line 146 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 |