Skip to content

Play with Contract

HZ edited this page Sep 9, 2019 · 2 revisions

This doc will have an introduction on how to connect with contract in you own DApp.

1. Build and deploy the contract

You can follow this doc to Build and deploy the contract: spin2win.

2. Contract address and ABI file

After you build and deploy the contract you will get two items to be used in the next step:

For Contract ABI of spin2win:

"messages": [{
    "name": "spin",
    "selector": 2121348255,
    "mutates": false,
    "args": [{
      "name": "player",
      "type": "AccountId"
    }],
    "return_type": null
  }]

You will see there is only one method in the contract, and the method name is spin, and only one parameter for spin, the type is AccountId.

3. Call contract

You can use this instance to call any messages in the contract ABI file with the correct args. The function will return a payload (hex value), this signature includes all the information of the message.

Creating api and set signer.

Calling api.tx.contract.call to get tx object:

  const tx =  api.tx.contract.call(
    contractAddress,
    endowment, // deposit amount, can be 0: BigNumber
    200000,    // gas fee
    payload    // the payload when calling specific message
  );

The last step: sign and send the tx. You can get the transaction status and all the event details inside the code and also check them on CENNZnet UI with block hash.

4. Common issues

  • The signer should have enough funds to sign a transaction. You can get tokens through faucet.
  • The network(Localhost or Rimu) of deploying contract and calling contract should be the same one.
  • For spin2win contract, should make sure the contract has enough funds to allocate assets. You can also top up the contract, using contract address, through faucet.