Class: Laksa::Contract::Contract

Inherits:
Object
  • Object
show all
Includes:
Account
Defined in:
lib/laksa/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.



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

Instance Attribute Details

#abiObject (readonly)

Returns the value of attribute abi.



10
11
12
# File 'lib/laksa/contract/contract.rb', line 10

def abi
  @abi
end

#addressObject (readonly)

Returns the value of attribute address.



10
11
12
# File 'lib/laksa/contract/contract.rb', line 10

def address
  @address
end

#codeObject (readonly)

Returns the value of attribute code.



10
11
12
# File 'lib/laksa/contract/contract.rb', line 10

def code
  @code
end

#factoryObject (readonly)

Returns the value of attribute factory.



10
11
12
# File 'lib/laksa/contract/contract.rb', line 10

def factory
  @factory
end

#initObject (readonly)

Returns the value of attribute init.



10
11
12
# File 'lib/laksa/contract/contract.rb', line 10

def init
  @init
end

#providerObject (readonly)

Returns the value of attribute provider.



10
11
12
# File 'lib/laksa/contract/contract.rb', line 10

def provider
  @provider
end

#signerObject (readonly)

Returns the value of attribute signer.



10
11
12
# File 'lib/laksa/contract/contract.rb', line 10

def signer
  @signer
end

#stateObject (readonly)

Returns the value of attribute state.



10
11
12
# File 'lib/laksa/contract/contract.rb', line 10

def state
  @state
end

#statusObject (readonly)

Returns the value of attribute status.



10
11
12
# File 'lib/laksa/contract/contract.rb', line 10

def status
  @status
end

Instance Method Details

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



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



42
43
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
# 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?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/laksa/contract/contract.rb', line 34

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

#initialised?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/laksa/contract/contract.rb', line 30

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

#prepare_tx(tx, attempts, interval) ⇒ Object



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

Returns:

  • (Boolean)


38
39
40
# File 'lib/laksa/contract/contract.rb', line 38

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