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.



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

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

  @model = model
end

Class Method Details

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

Creates a new ContractInvocation object.

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
# File 'lib/coinbase/contract_invocation.rb', line 24

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

  req[:abi] = abi.to_json unless abi.nil?

  # For payable contract invocations, convert the amount to atomic units of specified asset.
  if amount && asset_id && network
    asset = Coinbase::Asset.fetch(network, asset_id)

    req[: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, **req)
  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…



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

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.



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

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

#address_idString

Returns the Address ID of the Contract Invocation.



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

def address_id
  @model.address_id
end

#amountBigDecimal

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



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

def amount
  BigDecimal(@model.amount)
end

#argsHash

Returns the arguments of the Contract Invocation.



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

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

#broadcast!ContractInvocation

Broadcasts the ContractInvocation to the Network.

Raises:

  • (RuntimeError)

    If the ContractInvocation is not signed



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

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.



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

def contract_address
  @model.contract_address
end

#idString

Returns the Contract Invocation ID.



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

def id
  @model.contract_invocation_id
end

#inspectString

Same as to_s.



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

def inspect
  to_s
end

#methodString

Returns the method of the Contract Invocation.



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

def method
  @model.method
end

#networkCoinbase::Network

Returns the Network of the Contract Invocation.



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

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.



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

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.

Raises:

  • (RuntimeError)

    If the key is not an Eth::Key



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

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

  transaction.sign(key)

  self
end

#statusString

Returns the status of the Contract Invocation.



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

def status
  transaction.status
end

#to_sString

Returns a String representation of the Contract Invocation.



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

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.



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

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.



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

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.



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

def wallet_id
  @model.wallet_id
end