class Laksa::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/laksa/contract/contract.rb, line 12
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/laksa/contract/contract.rb, line 75
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 = TxParams.new
  tx_params.id = params['id'] if params.has_key?('id')
  tx_params.version = params['version'] if params.has_key?('version') 
  tx_params.nonce = params['nonce'] if params.has_key?('nonce') 
  tx_params.sender_pub_key = params['sender_pub_key'] if params.has_key?('sender_pub_key') 
  tx_params.gas_price = params['gas_price'] if params.has_key?('gas_price') 
  tx_params.gas_limit = params['gas_limit'] if params.has_key?('gas_limit') 

  tx_params.to_addr = @address
  tx_params.data = JSON.generate(data)

  tx = Transaction.new(tx_params, @provider, TxStatus::INITIALIZED, to_ds)

  tx = self.prepare_tx(tx, attempts, interval)
end
deploy(deploy_params, attempts = 33, interval = 1000, to_ds = false) click to toggle source
# File lib/laksa/contract/contract.rb, line 42
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 == 0

  tx_params = TxParams.new
  tx_params.id = deploy_params.id
  tx_params.version = deploy_params.version
  tx_params.nonce = deploy_params.nonce
  tx_params.sender_pub_key = deploy_params.sender_pub_key
  tx_params.gas_price = deploy_params.gas_price
  tx_params.gas_limit = deploy_params.gas_limit

  tx_params.to_addr = NIL_ADDRESS
  tx_params.amount = '0'
  tx_params.code = @code.gsub("/\\", "")
  tx_params.data = @init.to_json.gsub('\\"', '"')

  tx = Transaction.new(tx_params, @provider)

  tx = self.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/laksa/contract/contract.rb, line 34
def deployed?
  return @status === ContractStatus::DEPLOYED
end
initialised?() click to toggle source
# File lib/laksa/contract/contract.rb, line 30
def initialised?
  return @status === ContractStatus::INITIALISED
end
prepare_tx(tx, attempts, interval) click to toggle source
# File lib/laksa/contract/contract.rb, line 106
def prepare_tx(tx, attempts, interval)
  tx = @signer.sign(tx);

  response = @provider.CreateTransaction(tx.to_payload)

  if response['error']
    tx.status = TxStatus::REJECTED
  else
    tx.confirm(response['result']['TranID'], attempts, interval)
  end
  
  tx
end
rejected?() click to toggle source
# File lib/laksa/contract/contract.rb, line 38
def rejected?
  return @status === ContractStatus::REJECTED
end