Class: Coinbase::Transaction

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(model) ⇒ Transaction

Returns a new Transaction object. Do not use this method directly.

Parameters:



34
35
36
37
38
# File 'lib/coinbase/transaction.rb', line 34

def initialize(model)
  raise unless model.is_a?(Coinbase::Client::Transaction)

  @model = model
end

Instance Method Details

#from_address_idString

Returns the from address for the Transaction.

Returns:

  • (String)

    The from address



66
67
68
# File 'lib/coinbase/transaction.rb', line 66

def from_address_id
  @model.from_address_id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the Transaction



127
128
129
# File 'lib/coinbase/transaction.rb', line 127

def inspect
  to_s
end

#rawEth::Tx::Eip1559

Returns the underlying raw transaction.

Returns:

  • (Eth::Tx::Eip1559)

    The raw transaction



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/coinbase/transaction.rb', line 84

def raw
  return @raw unless @raw.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'] || ''
  }

  @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.

Returns:

  • (String)

    The hex-encoded signed payload



107
108
109
110
111
# File 'lib/coinbase/transaction.rb', line 107

def sign(key)
  raw.sign(key)

  raw.hex
end

#signed?Boolean

Returns whether the Transaction has been signed.

Returns:

  • (Boolean)

    Whether the Transaction has been signed



115
116
117
# File 'lib/coinbase/transaction.rb', line 115

def signed?
  Eth::Tx.signed?(raw)
end

#signed_payloadString

Returns the Signed Payload of the Transaction.

Returns:

  • (String)

    The Signed Payload



48
49
50
# File 'lib/coinbase/transaction.rb', line 48

def signed_payload
  @model.signed_payload
end

#statusSymbol

Returns the status of the Transaction.

Returns:

  • (Symbol)

    The status



60
61
62
# File 'lib/coinbase/transaction.rb', line 60

def status
  @model.status
end

#terminal_state?Boolean

Returns whether the Transaction is in a terminal state.

Returns:

  • (Boolean)

    Whether the Transaction is in a terminal state



72
73
74
# File 'lib/coinbase/transaction.rb', line 72

def terminal_state?
  Status::TERMINAL_STATES.include?(status)
end

#to_sString

Returns a String representation of the Transaction.

Returns:

  • (String)

    a String representation of the Transaction



121
122
123
# File 'lib/coinbase/transaction.rb', line 121

def to_s
  "Coinbase::Transaction{transaction_hash: '#{transaction_hash}', status: '#{status}'}"
end

#transaction_hashString

Returns the Transaction Hash of the Transaction.

Returns:

  • (String)

    The Transaction Hash



54
55
56
# File 'lib/coinbase/transaction.rb', line 54

def transaction_hash
  @model.transaction_hash
end

Returns the link to the transaction on the blockchain explorer.

Returns:

  • (String)

    The link to the transaction on the blockchain explorer



78
79
80
# File 'lib/coinbase/transaction.rb', line 78

def transaction_link
  @model.transaction_link
end

#unsigned_payloadString

Returns the Unsigned Payload of the Transaction.

Returns:

  • (String)

    The Unsigned Payload



42
43
44
# File 'lib/coinbase/transaction.rb', line 42

def unsigned_payload
  @model.unsigned_payload
end