From bd3573ecc8b4c4c0a94abb2fbfb5c971a30e9909 Mon Sep 17 00:00:00 2001 From: HZ Date: Wed, 4 Sep 2019 14:59:09 +1200 Subject: [PATCH] Update README.md Add #6 to README --- README.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/README.md b/README.md index 8b66e4a..9673588 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,57 @@ and select the specific node version. ## Demo Screen ![image](https://github.com/cennznet/spin2winApp/blob/master/resources/demo.gif) + + +## Play with Contract + +#### 1. Build and deploy the contract +You can follow this doc to Build and deploy the contract: [spin2win](https://cennznetdocs.com/CENNZNet/tutorials/spin2win.md). + +#### 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: +- [Contract address](https://github.com/cennznet/spin2winApp/blob/master/src/services/network.js) +- [Contract ABI json file](https://github.com/cennznet/spin2winApp/blob/master/src/services/Spin2Win.json) + +For `Contract ABI` of spin2win: + +```json +"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 + +#### [Construct a ContractABI instance](https://github.com/cennznet/spin2winApp/blob/master/src/services/spin2win.js): +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](https://github.com/cennznet/spin2winApp/blob/master/src/services/sendReward.js): + +Creating [`api`](https://github.com/cennznet/spin2winApp/blob/7dd149e770ccd66bd4bced4e93d45e21cb6fcb35/src/services/cennz-lib.js#L25) and [`set signer`](https://github.com/cennznet/spin2winApp/blob/7dd149e770ccd66bd4bced4e93d45e21cb6fcb35/src/services/sendReward.js#L41). + +Calling `api.tx.contract.call` to get `tx` object: +```javascript + 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`](https://github.com/cennznet/spin2winApp/blob/7dd149e770ccd66bd4bced4e93d45e21cb6fcb35/src/services/sendReward.js#L62). You can get the transaction status and all the event details inside the code and also check them on [CENNZnet UI](https://cennznet.js.org/cennznet-ui/#/explorer/query) with `block hash`. + +#### 4. Common issues + +- The signer should have enough funds to sign a transaction. You can get tokens through [faucet](https://cennznet.js.org/faucet-ui/). +- 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](https://cennznet.js.org/faucet-ui/).