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:



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

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



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

def from_address_id
  @model.from_address_id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the Transaction



148
149
150
# File 'lib/coinbase/transaction.rb', line 148

def inspect
  to_s
end

#rawEth::Tx::Eip1559

Returns the underlying raw transaction.

Returns:

  • (Eth::Tx::Eip1559)

    The raw transaction



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/coinbase/transaction.rb', line 91

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



128
129
130
131
132
# File 'lib/coinbase/transaction.rb', line 128

def sign(key)
  raw.sign(key)

  signature
end

#signatureString

Returns the signature of the Transaction.

Returns:

  • (String)

    The hex-encode signature



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

def signature
  raw.hex
end

#signed?Boolean

Returns whether the Transaction has been signed.

Returns:

  • (Boolean)

    Whether the Transaction has been signed



136
137
138
# File 'lib/coinbase/transaction.rb', line 136

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

#signed_payloadString

Returns the Signed Payload of the Transaction.

Returns:

  • (String)

    The Signed Payload



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

def signed_payload
  @model.signed_payload
end

#statusSymbol

Returns the status of the Transaction.

Returns:

  • (Symbol)

    The status



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

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



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

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

#to_address_idString

Returns the to address for the Transaction.

Returns:

  • (String)

    The to address



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

def to_address_id
  @model.to_address_id
end

#to_sString

Returns a String representation of the Transaction.

Returns:

  • (String)

    a String representation of the Transaction



142
143
144
# File 'lib/coinbase/transaction.rb', line 142

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



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

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



85
86
87
# File 'lib/coinbase/transaction.rb', line 85

def transaction_link
  @model.transaction_link
end

#unsigned_payloadString

Returns the Unsigned Payload of the Transaction.

Returns:

  • (String)

    The Unsigned Payload



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

def unsigned_payload
  @model.unsigned_payload
end