Class: Coinbase::SmartContract

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

Overview

A representation of a SmartContract on the blockchain.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ SmartContract

Returns a new SmartContract object.

Parameters:



164
165
166
167
168
# File 'lib/coinbase/smart_contract.rb', line 164

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

  @model = model
end

Class Method Details

.create_multi_token_contract(address_id:, wallet_id:, uri:) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/coinbase/smart_contract.rb', line 108

def self.create_multi_token_contract(
  address_id:,
  wallet_id:,
  uri:
)
  contract = Coinbase.call_api do
    smart_contracts_api.create_smart_contract(
      wallet_id,
      address_id,
      {
        type: Coinbase::Client::SmartContractType::ERC1155,
        options: Coinbase::Client::MultiTokenContractOptions.new(
          uri: uri
        ).to_body
      }
    )
  end

  new(contract)
end

.create_nft_contract(address_id:, wallet_id:, name:, symbol:, base_uri:) ⇒ SmartContract

Creates a new ERC721 token contract, that can subsequently be deployed to the blockchain.

Parameters:

  • address_id (String)

    The address ID of deployer

  • wallet_id (String)

    The wallet ID of the deployer

  • name (String)

    The name of the token

  • symbol (String)

    The symbol of the token

  • base_uri (String)

    The base URI for the token metadata

Returns:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/coinbase/smart_contract.rb', line 83

def self.create_nft_contract(
  address_id:,
  wallet_id:,
  name:,
  symbol:,
  base_uri:
)
  contract = Coinbase.call_api do
    smart_contracts_api.create_smart_contract(
      wallet_id,
      address_id,
      {
        type: Coinbase::Client::SmartContractType::ERC721,
        options: Coinbase::Client::NFTContractOptions.new(
          name: name,
          symbol: symbol,
          base_uri: base_uri
        ).to_body
      }
    )
  end

  new(contract)
end

.create_token_contract(address_id:, wallet_id:, name:, symbol:, total_supply:) ⇒ SmartContract

Creates a new ERC20 token contract, that can subsequently be deployed to the blockchain.

Parameters:

  • address_id (String)

    The address ID of deployer

  • wallet_id (String)

    The wallet ID of the deployer

  • name (String)

    The name of the token

  • symbol (String)

    The symbol of the token

  • total_supply (String)

    The total supply of the token, denominate in whole units.

Returns:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/coinbase/smart_contract.rb', line 50

def self.create_token_contract(
  address_id:,
  wallet_id:,
  name:,
  symbol:,
  total_supply:
)
  contract = Coinbase.call_api do
    smart_contracts_api.create_smart_contract(
      wallet_id,
      address_id,
      {
        type: Coinbase::Client::SmartContractType::ERC20,
        options: Coinbase::Client::TokenContractOptions.new(
          name: name,
          symbol: symbol,
          total_supply: BigDecimal(total_supply).to_i.to_s
        ).to_body
      }
    )
  end

  new(contract)
end

.list_events(network_id:, protocol_name:, contract_address:, contract_name:, event_name:, from_block_height:, to_block_height:) ⇒ Enumerable<Coinbase::ContractEvent>

Returns a list of ContractEvents for the provided network, contract, and event details.

Parameters:

  • network_id (Symbol)

    The network ID

  • protocol_name (String)

    The protocol name

  • contract_address (String)

    The contract address

  • contract_name (String)

    The contract name

  • event_name (String)

    The event name

  • from_block_height (Integer)

    The start block height

  • to_block_height (Integer)

    The end block height

Returns:



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

def self.list_events(
  network_id:,
  protocol_name:,
  contract_address:,
  contract_name:,
  event_name:,
  from_block_height:,
  to_block_height:
)
  Coinbase::Pagination.enumerate(
    lambda { |page|
      list_events_page(
        network_id,
        protocol_name,
        contract_address,
        contract_name,
        event_name,
        from_block_height,
        to_block_height,
        page
      )
    }
  ) do |contract_event|
    Coinbase::ContractEvent.new(contract_event)
  end
end

Instance Method Details

#abiArray<Hash>

Returns the ABI of the Smart Contract.

Returns:

  • (Array<Hash>)

    The ABI



198
199
200
# File 'lib/coinbase/smart_contract.rb', line 198

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

#contract_addressString

Returns the contract address of the SmartContract.

Returns:

  • (String)

    The contract address



186
187
188
# File 'lib/coinbase/smart_contract.rb', line 186

def contract_address
  @model.contract_address
end

#deploy!SmartContract

Deploys the signed SmartContract to the blockchain.

Returns:

Raises:



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/coinbase/smart_contract.rb', line 247

def deploy!
  raise TransactionNotSignedError unless transaction.signed?

  @model = Coinbase.call_api do
    smart_contracts_api.deploy_smart_contract(
      wallet_id,
      deployer_address,
      id,
      { signed_payload: transaction.signature }
    )
  end

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

  self
end

#deployer_addressString

Returns the address of the deployer of the SmartContract.

Returns:

  • (String)

    The deployer address



192
193
194
# File 'lib/coinbase/smart_contract.rb', line 192

def deployer_address
  @model.deployer_address
end

#idString

Returns the SmartContract ID. NOTE: This is not the contract address and is primarily used before the contract is deployed.

Returns:

  • (String)

    The SmartContract ID



174
175
176
# File 'lib/coinbase/smart_contract.rb', line 174

def id
  @model.smart_contract_id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a string representation of the SmartContract



307
308
309
# File 'lib/coinbase/smart_contract.rb', line 307

def inspect
  to_s
end

#networkCoinbase::Network

Returns the Network of the SmartContract.

Returns:



180
181
182
# File 'lib/coinbase/smart_contract.rb', line 180

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

#optionsCoinbase::Client::SmartContractOptions

Returns the options of the SmartContract.

Returns:



216
217
218
# File 'lib/coinbase/smart_contract.rb', line 216

def options
  @model.options
end

#reloadSmartContract

Reloads the Smart Contract model with the latest version from the server side.

Returns:

  • (SmartContract)

    The most recent version of Smart Contract from the server



266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/coinbase/smart_contract.rb', line 266

def reload
  @model = Coinbase.call_api do
    smart_contracts_api.get_smart_contract(
      wallet_id,
      deployer_address,
      id
    )
  end

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

  self
end

#sign(key) ⇒ SmartContract

Signs the SmartContract deployment transaction with the given key. This is required before broadcasting the SmartContract.

Parameters:

  • key (Eth::Key)

    The key to sign the SmartContract with

Returns:

Raises:



238
239
240
241
242
# File 'lib/coinbase/smart_contract.rb', line 238

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

  transaction.sign(key)
end

#statusString

Returns the status of the SmartContract.

Returns:

  • (String)

    The status



228
229
230
# File 'lib/coinbase/smart_contract.rb', line 228

def status
  transaction.status
end

#to_sString

Returns a string representation of the SmartContract.

Returns:

  • (String)

    a string representation of the SmartContract



313
314
315
316
317
318
319
320
321
322
323
# File 'lib/coinbase/smart_contract.rb', line 313

def to_s
  Coinbase.pretty_print_object(
    self.class,
    network: network.id,
    contract_address: contract_address,
    deployer_address: deployer_address,
    type: type,
    status: status,
    options: Coinbase.pretty_print_object('Options', **options)
  )
end

#transactionCoinbase::Transaction

Returns the transaction.

Returns:



222
223
224
# File 'lib/coinbase/smart_contract.rb', line 222

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

#typeCoinbase::Client::SmartContractType

Returns the type of the SmartContract.

Returns:



210
211
212
# File 'lib/coinbase/smart_contract.rb', line 210

def type
  @model.type
end

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

Waits until the Smart Contract deployment is signed or failed by polling the server at the given interval. deployment to land on-chain, 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 Smart Contract,

Returns:

Raises:

  • (Timeout::Error)

    if the Contract Invocation takes longer than the given timeout



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/coinbase/smart_contract.rb', line 286

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

  loop do
    reload

    return self if transaction.terminal_state?

    if Time.now - start_time > timeout_seconds
      raise Timeout::Error,
            'SmartContract deployment timed out. Try waiting again.'
    end

    self.sleep interval_seconds
  end

  self
end

#wallet_idString

Returns the ID of the wallet that deployed the SmartContract.

Returns:

  • (String)

    The wallet ID



204
205
206
# File 'lib/coinbase/smart_contract.rb', line 204

def wallet_id
  @model.wallet_id
end