cennznet / spin2winApp

Spin2win tutorial app
Apache License 2.0
3 stars 0 forks source link

How to connect the smart contract with mobile app? #6

Open mzxyz opened 5 years ago

mzxyz commented 5 years ago

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. Calling contract

Construct a ContractABI instance:

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.

Create transaction:

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