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:



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_idString

Returns the from address for the Transaction.

Returns:

  • (String)

    The from address



69
70
71
# File 'lib/coinbase/transaction.rb', line 69

def from_address_id
  @model.from_address_id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the Transaction



138
139
140
# File 'lib/coinbase/transaction.rb', line 138

def inspect
  to_s
end

#rawEth::Tx::Eip1559

Returns the underlying raw transaction.

Returns:

  • (Eth::Tx::Eip1559)

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

Returns:

  • (String)

    The hex-encoded signed 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.

Returns:

  • (Boolean)

    Whether the Transaction has been signed



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

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

#signed_payloadString

Returns the Signed Payload of the Transaction.

Returns:

  • (String)

    The Signed Payload



51
52
53
# File 'lib/coinbase/transaction.rb', line 51

def signed_payload
  @model.signed_payload
end

#statusSymbol

Returns the status of the Transaction.

Returns:

  • (Symbol)

    The status



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.

Returns:

  • (Boolean)

    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_sString

Returns a String representation of the Transaction.

Returns:

  • (String)

    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_hashString

Returns the Transaction Hash of the Transaction.

Returns:

  • (String)

    The Transaction Hash



57
58
59
# File 'lib/coinbase/transaction.rb', line 57

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



81
82
83
# File 'lib/coinbase/transaction.rb', line 81

def transaction_link
  @model.transaction_link
end

#unsigned_payloadString

Returns the Unsigned Payload of the Transaction.

Returns:

  • (String)

    The Unsigned Payload



45
46
47
# File 'lib/coinbase/transaction.rb', line 45

def unsigned_payload
  @model.unsigned_payload
end