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:



70
71
72
73
74
# File 'lib/coinbase/contract_invocation.rb', line 70

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:, 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

  • 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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/coinbase/contract_invocation.rb', line 17

def create(
  address_id:,
  wallet_id:,
  contract_address:,
  abi:,
  method:,
  args: {}
)
  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
    )
  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:



43
44
45
46
47
48
49
# File 'lib/coinbase/contract_invocation.rb', line 43

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



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

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

#address_idString

Returns the Address ID of the Contract Invocation.

Returns:

  • (String)

    The Address ID



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

def address_id
  @model.address_id
end

#argsHash

Returns the arguments of the Contract Invocation.

Returns:

  • (Hash)

    The arguments



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

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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/coinbase/contract_invocation.rb', line 153

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



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

def contract_address
  @model.contract_address
end

#idString

Returns the Contract Invocation ID.

Returns:

  • (String)

    The Contract Invocation ID



78
79
80
# File 'lib/coinbase/contract_invocation.rb', line 78

def id
  @model.contract_invocation_id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the ContractInvocation



224
225
226
# File 'lib/coinbase/contract_invocation.rb', line 224

def inspect
  to_s
end

#methodString

Returns the method of the Contract Invocation.

Returns:

  • (String)

    The method



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

def method
  @model.method
end

#networkCoinbase::Network

Returns the Network of the Contract Invocation.

Returns:



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

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:



172
173
174
175
176
177
178
179
180
# File 'lib/coinbase/contract_invocation.rb', line 172

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



142
143
144
145
146
147
148
# File 'lib/coinbase/contract_invocation.rb', line 142

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



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

def status
  transaction.status
end

#to_sString

Returns a String representation of the Contract Invocation.

Returns:

  • (String)

    a String representation of the Contract Invocation



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/coinbase/contract_invocation.rb', line 206

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:



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

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:



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/coinbase/contract_invocation.rb', line 188

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



84
85
86
# File 'lib/coinbase/contract_invocation.rb', line 84

def wallet_id
  @model.wallet_id
end