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
-
#block_hash ⇒ String
Returns the block hash of which the Transaction is recorded.
-
#block_height ⇒ String
Returns the block height of which the Transaction is recorded.
-
#content ⇒ String
Returns the block height of which the Transaction is recorded.
-
#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.
-
#signature ⇒ String
Returns the signature of the Transaction.
-
#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_address_id ⇒ String
Returns the to address for the Transaction.
-
#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.
38 39 40 41 42 |
# File 'lib/coinbase/transaction.rb', line 38 def initialize(model) raise unless model.is_a?(Coinbase::Client::Transaction) @model = model end |
Instance Method Details
#block_hash ⇒ String
Returns the block hash of which the Transaction is recorded.
88 89 90 |
# File 'lib/coinbase/transaction.rb', line 88 def block_hash @model.block_hash end |
#block_height ⇒ String
Returns the block height of which the Transaction is recorded.
94 95 96 |
# File 'lib/coinbase/transaction.rb', line 94 def block_height @model.block_height end |
#content ⇒ String
Returns the block height of which the Transaction is recorded.
106 107 108 |
# File 'lib/coinbase/transaction.rb', line 106 def content @model.content end |
#from_address_id ⇒ String
Returns the from address for the Transaction.
70 71 72 |
# File 'lib/coinbase/transaction.rb', line 70 def from_address_id @model.from_address_id end |
#inspect ⇒ String
Same as to_s.
172 173 174 |
# File 'lib/coinbase/transaction.rb', line 172 def inspect to_s end |
#raw ⇒ Eth::Tx::Eip1559
Returns the underlying raw transaction.
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/transaction.rb', line 112 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.
149 150 151 152 153 154 155 156 |
# File 'lib/coinbase/transaction.rb', line 149 def sign(key) raise 'Invalid key type' unless key.is_a?(Eth::Key) raise Coinbase::AlreadySignedError if signed? raw.sign(key) signature end |
#signature ⇒ String
Returns the signature of the Transaction.
143 144 145 |
# File 'lib/coinbase/transaction.rb', line 143 def signature raw.hex end |
#signed? ⇒ Boolean
Returns whether the Transaction has been signed.
160 161 162 |
# File 'lib/coinbase/transaction.rb', line 160 def signed? Eth::Tx.signed?(raw) end |
#signed_payload ⇒ String
Returns the Signed Payload of the Transaction.
52 53 54 |
# File 'lib/coinbase/transaction.rb', line 52 def signed_payload @model.signed_payload end |
#status ⇒ Symbol
Returns the status of the Transaction.
64 65 66 |
# File 'lib/coinbase/transaction.rb', line 64 def status @model.status end |
#terminal_state? ⇒ Boolean
Returns whether the Transaction is in a terminal state.
82 83 84 |
# File 'lib/coinbase/transaction.rb', line 82 def terminal_state? Status::TERMINAL_STATES.include?(status) end |
#to_address_id ⇒ String
Returns the to address for the Transaction.
76 77 78 |
# File 'lib/coinbase/transaction.rb', line 76 def to_address_id @model.to_address_id end |
#to_s ⇒ String
Returns a String representation of the Transaction.
166 167 168 |
# File 'lib/coinbase/transaction.rb', line 166 def to_s "Coinbase::Transaction{transaction_hash: '#{transaction_hash}', status: '#{status}'}" end |
#transaction_hash ⇒ String
Returns the Transaction Hash of the Transaction.
58 59 60 |
# File 'lib/coinbase/transaction.rb', line 58 def transaction_hash @model.transaction_hash end |
#transaction_link ⇒ String
Returns the link to the transaction on the blockchain explorer.
100 101 102 |
# File 'lib/coinbase/transaction.rb', line 100 def transaction_link @model.transaction_link end |
#unsigned_payload ⇒ String
Returns the Unsigned Payload of the Transaction.
46 47 48 |
# File 'lib/coinbase/transaction.rb', line 46 def unsigned_payload @model.unsigned_payload end |