class Zilliqa::Contract::Contract

Constants

NIL_ADDRESS

Attributes

abi[R]
address[R]
code[R]
factory[R]
init[R]
provider[R]
signer[R]
state[R]
status[R]

Public Class Methods

new(factory, code, abi, address, init, state) click to toggle source
# File lib/zilliqa/contract/contract.rb, line 14
def initialize(factory, code, abi, address, init, state)
  @factory = factory
  @provider = factory.provider
  @signer = factory.signer

  @code = code
  @abi = abi
  @init = init
  @state = state

  if address && !address.empty?
    @address = address
    @status = ContractStatus::DEPLOYED
  else
    @status = ContractStatus::INITIALISED
  end
end

Public Instance Methods

call(transition, args, params, attempts = 33, interval = 1000, to_ds = false) click to toggle source
# File lib/zilliqa/contract/contract.rb, line 77
def call(transition, args, params, attempts = 33, interval = 1000, to_ds = false)
  data = {
    _tag: transition,
    params: args
  }

  return 'Contract has not been deployed!' unless @address

  tx_params = {
    id: params['id'],
    version: params['version'],
    nonce: params['nonce'],
    sender_pub_key: params['sender_pub_key'],
    gas_price: params['gas_price'],
    gas_limit: params['gas_limit'],
    to_addr: @address,
    data: JSON.generate(data)
  }

  tx = Transaction.new(tx_params, @provider, Zilliqa::Account::Transaction::TX_STATUSES[:initialized], to_ds)

  prepare_tx(tx, attempts, interval)
end
deploy(deploy_params, attempts = 33, interval = 1000, _to_ds = false) click to toggle source
# File lib/zilliqa/contract/contract.rb, line 44
def deploy(deploy_params, attempts = 33, interval = 1000, _to_ds = false)
  raise 'Cannot deploy without code or initialisation parameters.' if @code.nil? || @code == ''
  raise 'Cannot deploy without code or initialisation parameters.' if @init.nil? || @init.length.zero?

  tx_params = {
    id: deploy_params.id,
    version: deploy_params.version,
    nonce: deploy_params.nonce,
    sender_pub_key: deploy_params.sender_pub_key,
    gas_price: deploy_params.gas_price,
    gas_limit: deploy_params.gas_limit,
    to_addr: NIL_ADDRESS,
    amount: '0',
    code: @code.gsub('/\\', ''),
    data: @init.to_json.gsub('\\"', '"')
  }

  tx = Transaction.new(tx_params, @provider)

  tx = prepare_tx(tx, attempts, interval)

  if tx.rejected?
    @status = ContractStatus::REJECTED

    return [tx, self]
  end

  @status = ContractStatus::DEPLOYED
  @address = ContractFactory.get_address_for_contract(tx)

  [tx, self]
end
deployed?() click to toggle source
# File lib/zilliqa/contract/contract.rb, line 36
def deployed?
  @status == ContractStatus::DEPLOYED
end
initialised?() click to toggle source
# File lib/zilliqa/contract/contract.rb, line 32
def initialised?
  @status == ContractStatus::INITIALISED
end
prepare_tx(tx, attempts, interval) click to toggle source
# File lib/zilliqa/contract/contract.rb, line 108
def prepare_tx(tx, attempts, interval)
  tx = @signer.sign(tx)

  response = @provider.CreateTransaction(tx.to_payload)

  if response['error']
    tx.status = Zilliqa::Account::Transaction::TX_STATUSES[:rejected]
  else
    tx.confirm(response['result']['TranID'], attempts, interval)
  end

  tx
end
rejected?() click to toggle source
# File lib/zilliqa/contract/contract.rb, line 40
def rejected?
  @status == ContractStatus::REJECTED
end