Class: Zilliqa::Contract::Contract

Inherits:
Object
  • Object
show all
Includes:
Account
Defined in:
lib/zilliqa/contract/contract.rb

Constant Summary collapse

NIL_ADDRESS =
'0000000000000000000000000000000000000000'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory, code, abi, address, init, state) ⇒ Contract

Returns a new instance of Contract.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Instance Attribute Details

#abiObject (readonly)

Returns the value of attribute abi.



12
13
14
# File 'lib/zilliqa/contract/contract.rb', line 12

def abi
  @abi
end

#addressObject (readonly)

Returns the value of attribute address.



12
13
14
# File 'lib/zilliqa/contract/contract.rb', line 12

def address
  @address
end

#codeObject (readonly)

Returns the value of attribute code.



12
13
14
# File 'lib/zilliqa/contract/contract.rb', line 12

def code
  @code
end

#factoryObject (readonly)

Returns the value of attribute factory.



12
13
14
# File 'lib/zilliqa/contract/contract.rb', line 12

def factory
  @factory
end

#initObject (readonly)

Returns the value of attribute init.



12
13
14
# File 'lib/zilliqa/contract/contract.rb', line 12

def init
  @init
end

#providerObject (readonly)

Returns the value of attribute provider.



12
13
14
# File 'lib/zilliqa/contract/contract.rb', line 12

def provider
  @provider
end

#signerObject (readonly)

Returns the value of attribute signer.



12
13
14
# File 'lib/zilliqa/contract/contract.rb', line 12

def signer
  @signer
end

#stateObject (readonly)

Returns the value of attribute state.



12
13
14
# File 'lib/zilliqa/contract/contract.rb', line 12

def state
  @state
end

#statusObject (readonly)

Returns the value of attribute status.



12
13
14
# File 'lib/zilliqa/contract/contract.rb', line 12

def status
  @status
end

Instance Method Details

#call(transition, args, params, attempts = 33, interval = 1000, to_ds = false) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/zilliqa/contract/contract.rb', line 36

def deployed?
  @status == ContractStatus::DEPLOYED
end

#initialised?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/zilliqa/contract/contract.rb', line 32

def initialised?
  @status == ContractStatus::INITIALISED
end

#prepare_tx(tx, attempts, interval) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
# 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?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/zilliqa/contract/contract.rb', line 40

def rejected?
  @status == ContractStatus::REJECTED
end