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



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

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



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

def block_height
  @model.block_height
end

#contentEthereumTransaction

Returns detailed content the Transaction.

Returns:

  • (EthereumTransaction)

    The transaction content



112
113
114
# File 'lib/coinbase/transaction.rb', line 112

def content
  @model.content
end

#from_address_idString

Returns the from address for the Transaction.

Returns:

  • (String)

    The from address



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

def from_address_id
  @model.from_address_id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the Transaction



178
179
180
# File 'lib/coinbase/transaction.rb', line 178

def inspect
  to_s
end

#networkCoinbase::Network

Returns the Network of the Transaction.

Returns:



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

def network
  @network ||= Coinbase::Network.from_id(@model.network_id)
end

#rawEth::Tx::Eip1559

Returns the underlying raw transaction.

Returns:

  • (Eth::Tx::Eip1559)

    The raw transaction



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/coinbase/transaction.rb', line 118

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:



155
156
157
158
159
160
161
162
# File 'lib/coinbase/transaction.rb', line 155

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



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

def signature
  raw.hex
end

#signed?Boolean

Returns whether the Transaction has been signed.

Returns:

  • (Boolean)

    Whether the Transaction has been signed



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

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

#signed_payloadString

Returns the Signed Payload of the Transaction.

Returns:

  • (String)

    The Signed Payload



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

def signed_payload
  @model.signed_payload
end

#statusSymbol

Returns the status of the Transaction.

Returns:

  • (Symbol)

    The status



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

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



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

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

#to_address_idString

Returns the to address for the Transaction.

Returns:

  • (String)

    The to address



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

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



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

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



64
65
66
# File 'lib/coinbase/transaction.rb', line 64

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



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

def transaction_link
  @model.transaction_link
end

#unsigned_payloadString

Returns the Unsigned Payload of the Transaction.

Returns:

  • (String)

    The Unsigned Payload



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

def unsigned_payload
  @model.unsigned_payload
end