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:



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_hashString

Returns the block hash of which the Transaction is recorded.

Returns:

  • (String)

    The to block_hash



88
89
90
# File 'lib/coinbase/transaction.rb', line 88

def block_hash
  @model.block_hash
end

#block_heightString

Returns the block height of which the Transaction is recorded.

Returns:

  • (String)

    The to block_height



94
95
96
# File 'lib/coinbase/transaction.rb', line 94

def block_height
  @model.block_height
end

#contentString

Returns the block height of which the Transaction is recorded.

Returns:

  • (String)

    The to block_height



106
107
108
# File 'lib/coinbase/transaction.rb', line 106

def content
  @model.content
end

#from_address_idString

Returns the from address for the Transaction.

Returns:

  • (String)

    The from address



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

def from_address_id
  @model.from_address_id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the Transaction



172
173
174
# File 'lib/coinbase/transaction.rb', line 172

def inspect
  to_s
end

#rawEth::Tx::Eip1559

Returns the underlying raw transaction.

Returns:

  • (Eth::Tx::Eip1559)

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

Returns:

  • (String)

    The hex-encoded signed payload

Raises:



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

#signatureString

Returns the signature of the Transaction.

Returns:

  • (String)

    The hex-encode signature



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

def signature
  raw.hex
end

#signed?Boolean

Returns whether the Transaction has been signed.

Returns:

  • (Boolean)

    Whether the Transaction has been signed



160
161
162
# File 'lib/coinbase/transaction.rb', line 160

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

#signed_payloadString

Returns the Signed Payload of the Transaction.

Returns:

  • (String)

    The Signed Payload



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

def signed_payload
  @model.signed_payload
end

#statusSymbol

Returns the status of the Transaction.

Returns:

  • (Symbol)

    The status



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.

Returns:

  • (Boolean)

    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_idString

Returns the to address for the Transaction.

Returns:

  • (String)

    The to address



76
77
78
# File 'lib/coinbase/transaction.rb', line 76

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



166
167
168
# File 'lib/coinbase/transaction.rb', line 166

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



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

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



100
101
102
# File 'lib/coinbase/transaction.rb', line 100

def transaction_link
  @model.transaction_link
end

#unsigned_payloadString

Returns the Unsigned Payload of the Transaction.

Returns:

  • (String)

    The Unsigned Payload



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

def unsigned_payload
  @model.unsigned_payload
end