Class: Coinbase::Transaction
- Inherits:
-
Object
- Object
- Coinbase::Transaction
- Defined in:
- lib/coinbase/transaction.rb
Overview
A representation of an onchain Transaction. Transactions should be constructed via higher level abstractions like Trade or Transfer.
Defined Under Namespace
Modules: Status
Instance Method Summary collapse
-
#from_address_id ⇒ String
Returns the from address for the Transaction.
-
#initialize(model) ⇒ Transaction
constructor
Returns a new Transaction object.
-
#inspect ⇒ String
Same as to_s.
-
#raw ⇒ Eth::Tx::Eip1559
Returns the underlying raw transaction.
-
#sign(key) ⇒ String
Signs the Transaction with the provided key and returns the hex signing payload.
-
#signed? ⇒ Boolean
Returns whether the Transaction has been signed.
-
#signed_payload ⇒ String
Returns the Signed Payload of the Transaction.
-
#status ⇒ Symbol
Returns the status of the Transaction.
-
#terminal_state? ⇒ Boolean
Returns whether the Transaction is in a terminal state.
-
#to_s ⇒ String
Returns a String representation of the Transaction.
-
#transaction_hash ⇒ String
Returns the Transaction Hash of the Transaction.
-
#transaction_link ⇒ String
Returns the link to the transaction on the blockchain explorer.
-
#unsigned_payload ⇒ String
Returns the Unsigned Payload of the Transaction.
Constructor Details
#initialize(model) ⇒ Transaction
Returns a new Transaction object. Do not use this method directly.
37 38 39 40 41 |
# File 'lib/coinbase/transaction.rb', line 37 def initialize(model) raise unless model.is_a?(Coinbase::Client::Transaction) @model = model end |
Instance Method Details
#from_address_id ⇒ String
Returns the from address for the Transaction.
69 70 71 |
# File 'lib/coinbase/transaction.rb', line 69 def from_address_id @model.from_address_id end |
#inspect ⇒ String
Same as to_s.
138 139 140 |
# File 'lib/coinbase/transaction.rb', line 138 def inspect to_s end |
#raw ⇒ Eth::Tx::Eip1559
Returns the underlying raw transaction.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/coinbase/transaction.rb', line 87 def raw return @raw unless @raw.nil? # If the transaction is signed, decode the signed payload. unless signed_payload.nil? @raw = Eth::Tx::Eip1559.decode(signed_payload) return @raw end # If the transaction is unsigned, parse the unsigned payload into an EIP-1559 transaction. 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: from_address_id, to: parsed_payload['to'], value: parsed_payload['value'].to_i(16), data: parsed_payload['input'] || '' } @raw = Eth::Tx::Eip1559.new(Eth::Tx.validate_eip1559_params(params)) end |
#sign(key) ⇒ String
Signs the Transaction with the provided key and returns the hex signing payload.
118 119 120 121 122 |
# File 'lib/coinbase/transaction.rb', line 118 def sign(key) raw.sign(key) raw.hex end |
#signed? ⇒ Boolean
Returns whether the Transaction has been signed.
126 127 128 |
# File 'lib/coinbase/transaction.rb', line 126 def signed? Eth::Tx.signed?(raw) end |
#signed_payload ⇒ String
Returns the Signed Payload of the Transaction.
51 52 53 |
# File 'lib/coinbase/transaction.rb', line 51 def signed_payload @model.signed_payload end |
#status ⇒ Symbol
Returns the status of the Transaction.
63 64 65 |
# File 'lib/coinbase/transaction.rb', line 63 def status @model.status end |
#terminal_state? ⇒ Boolean
Returns whether the Transaction is in a terminal state.
75 76 77 |
# File 'lib/coinbase/transaction.rb', line 75 def terminal_state? Status::TERMINAL_STATES.include?(status) end |
#to_s ⇒ String
Returns a String representation of the Transaction.
132 133 134 |
# File 'lib/coinbase/transaction.rb', line 132 def to_s "Coinbase::Transaction{transaction_hash: '#{transaction_hash}', status: '#{status}'}" end |
#transaction_hash ⇒ String
Returns the Transaction Hash of the Transaction.
57 58 59 |
# File 'lib/coinbase/transaction.rb', line 57 def transaction_hash @model.transaction_hash end |
#transaction_link ⇒ String
Returns the link to the transaction on the blockchain explorer.
81 82 83 |
# File 'lib/coinbase/transaction.rb', line 81 def transaction_link @model.transaction_link end |
#unsigned_payload ⇒ String
Returns the Unsigned Payload of the Transaction.
45 46 47 |
# File 'lib/coinbase/transaction.rb', line 45 def unsigned_payload @model.unsigned_payload end |