Class: Coinbase::ContractInvocation

Inherits:
Object
  • Object
show all
Defined in:
lib/coinbase/contract_invocation.rb

Overview

A representation of a Contract Invocation.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ ContractInvocation

Returns a new ContractInvocation object. Do not use this method directly. Instead use Coinbase::ContractInvocation.create.

Parameters:



89
90
91
92
93
# File 'lib/coinbase/contract_invocation.rb', line 89

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

  @model = model
end

Class Method Details

.create(address_id:, wallet_id:, contract_address:, abi:, method:, amount:, asset_id:, network:, args: {}) ⇒ ContractInvocation

Creates a new ContractInvocation object.

Parameters:

  • address_id (String)

    The Address ID of the signing Address

  • wallet_id (String)

    The Wallet ID associated with the signing Address

  • contract_address (String)

    The contract address

  • abi (Array<Hash>)

    The contract ABI

  • method (String)

    The contract method

  • amount (Integer, Float, BigDecimal)

    The amount of the native Asset to send to a payable contract method.

  • asset_id (Symbol)

    The ID of the Asset to send to a payable contract method. The Asset must be a denomination of the native Asset. For Ethereum, :eth, :gwei, and :wei are supported.

  • network (Coinbase::Network, Symbol)

    The Network or Network ID of the Asset

  • args (Hash) (defaults to: {})

    The arguments to pass to the contract method. The keys should be the argument names, and the values should be the argument values.

Returns:

Raises:

  • (Coinbase::ApiError)

    If the request to create the Contract Invocation fails



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/coinbase/contract_invocation.rb', line 24

def create(
  address_id:,
  wallet_id:,
  contract_address:,
  abi:,
  method:,
  amount:,
  asset_id:,
  network:,
  args: {}
)
  atomic_amount = nil

  if amount && asset_id && network
    network = Coinbase::Network.from_id(network)
    asset = network.get_asset(asset_id)
    atomic_amount = asset.to_atomic_amount(amount).to_i_to_s
  end

  model = Coinbase.call_api do
    contract_invocation_api.create_contract_invocation(
      wallet_id,
      address_id,
      contract_address: contract_address,
      abi: abi.to_json,
      method: method,
      args: args.to_json,
      amount: atomic_amount
    )
  end

  new(model)
end

.list(wallet_id:, address_id:) ⇒ Enumerable<Coinbase::ContractInvocation>

Enumerates the payload signatures for a given address belonging to a wallet. The result is an enumerator that lazily fetches from the server, and can be iterated over, converted an array, etc…

Returns:



62
63
64
65
66
67
68
# File 'lib/coinbase/contract_invocation.rb', line 62

def list(wallet_id:, address_id:)
  Coinbase::Pagination.enumerate(
    ->(page) { fetch_page(wallet_id, address_id, page) }
  ) do |contract_invocation|
    new(contract_invocation)
  end
end

Instance Method Details

#abiArray<Hash>

Returns the ABI of the Contract Invocation.

Returns:

  • (Array<Hash>)

    The ABI



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

def abi
  JSON.parse(@model.abi)
end

#address_idString

Returns the Address ID of the Contract Invocation.

Returns:

  • (String)

    The Address ID



109
110
111
# File 'lib/coinbase/contract_invocation.rb', line 109

def address_id
  @model.address_id
end

#amountBigDecimal

Returns the amount of the native asset sent to a payable contract method, if applicable.

Returns:

  • (BigDecimal)

    The amount in atomic units of the native asset



145
146
147
# File 'lib/coinbase/contract_invocation.rb', line 145

def amount
  BigDecimal(@model.amount)
end

#argsHash

Returns the arguments of the Contract Invocation.

Returns:

  • (Hash)

    The arguments



139
140
141
# File 'lib/coinbase/contract_invocation.rb', line 139

def args
  JSON.parse(@model.args).transform_keys(&:to_sym)
end

#broadcast!ContractInvocation

Broadcasts the ContractInvocation to the Network.

Returns:

Raises:

  • (RuntimeError)

    If the ContractInvocation is not signed



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/coinbase/contract_invocation.rb', line 178

def broadcast!
  raise TransactionNotSignedError unless transaction.signed?

  @model = Coinbase.call_api do
    contract_invocation_api.broadcast_contract_invocation(
      wallet_id,
      address_id,
      id,
      { signed_payload: transaction.signature }
    )
  end

  @transaction = Coinbase::Transaction.new(@model.transaction)

  self
end

#contract_addressString

Returns the Contract Address of the Contract Invocation.

Returns:

  • (String)

    The Contract Address



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

def contract_address
  @model.contract_address
end

#idString

Returns the Contract Invocation ID.

Returns:

  • (String)

    The Contract Invocation ID



97
98
99
# File 'lib/coinbase/contract_invocation.rb', line 97

def id
  @model.contract_invocation_id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the ContractInvocation



249
250
251
# File 'lib/coinbase/contract_invocation.rb', line 249

def inspect
  to_s
end

#methodString

Returns the method of the Contract Invocation.

Returns:

  • (String)

    The method



133
134
135
# File 'lib/coinbase/contract_invocation.rb', line 133

def method
  @model.method
end

#networkCoinbase::Network

Returns the Network of the Contract Invocation.

Returns:



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

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

#reloadContractInvocation

# Reload reloads the Contract Invocation model with the latest version from the server side.

Returns:



197
198
199
200
201
202
203
204
205
# File 'lib/coinbase/contract_invocation.rb', line 197

def reload
  @model = Coinbase.call_api do
    contract_invocation_api.get_contract_invocation(wallet_id, address_id, id)
  end

  @transaction = Coinbase::Transaction.new(@model.transaction)

  self
end

#sign(key) ⇒ ContractInvocation

Signs the Contract Invocation transaction with the given key. This is required before broadcasting the Contract Invocation when not using a Server-Signer.

Parameters:

  • key (Eth::Key)

    The key to sign the ContractInvocation with

Returns:

Raises:

  • (RuntimeError)

    If the key is not an Eth::Key



167
168
169
170
171
172
173
# File 'lib/coinbase/contract_invocation.rb', line 167

def sign(key)
  raise unless key.is_a?(Eth::Key)

  transaction.sign(key)

  self
end

#statusString

Returns the status of the Contract Invocation.

Returns:

  • (String)

    The status



157
158
159
# File 'lib/coinbase/contract_invocation.rb', line 157

def status
  transaction.status
end

#to_sString

Returns a String representation of the Contract Invocation.

Returns:

  • (String)

    a String representation of the Contract Invocation



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/coinbase/contract_invocation.rb', line 231

def to_s
  Coinbase.pretty_print_object(
    self.class,
    id: id,
    wallet_id: wallet_id,
    address_id: address_id,
    network_id: network.id,
    status: status,
    abi: abi.to_json,
    method: method,
    args: args.to_json,
    transaction_hash: transaction.transaction_hash,
    transaction_link: transaction.transaction_link
  )
end

#transactionCoinbase::Transaction

Returns the transaction.

Returns:



151
152
153
# File 'lib/coinbase/contract_invocation.rb', line 151

def transaction
  @transaction ||= Coinbase::Transaction.new(@model.transaction)
end

#wait!(interval_seconds = 0.2, timeout_seconds = 20) ⇒ ContractInvocation

Waits until the Contract Invocation is signed or failed by polling the server at the given interval. Raises a Timeout::Error if the Contract Invocation takes longer than the given timeout. in seconds.

Parameters:

  • interval_seconds (Integer) (defaults to: 0.2)

    The interval at which to poll the server, in seconds

  • timeout_seconds (Integer) (defaults to: 20)

    The maximum amount of time to wait for the Contract Invocation to be signed,

Returns:



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/coinbase/contract_invocation.rb', line 213

def wait!(interval_seconds = 0.2, timeout_seconds = 20)
  start_time = Time.now

  loop do
    reload

    return self if transaction.terminal_state?

    raise Timeout::Error, 'Contract Invocation timed out' if Time.now - start_time > timeout_seconds

    self.sleep interval_seconds
  end

  self
end

#wallet_idString

Returns the Wallet ID of the Contract Invocation.

Returns:

  • (String)

    The Wallet ID



103
104
105
# File 'lib/coinbase/contract_invocation.rb', line 103

def wallet_id
  @model.wallet_id
end