Smart Contract

Smart Contract Deployment

The developer needs to deploy the contract to Rangers Protocol to allow users to interact with it.

Before deploying a contract, we need to prepare:

  • The compiled binary code of the contract, obtained through contract compilation

  • Transaction fee

Simply put, we deploy the contract by sending Rangers Protocol transactions. We can use the following tools:

Warning

To deploy to Rangers Protocol, you need to configure the corresponding message in truffle-config.js

rangerstestnet: {
  provider: () =>
      new HDWalletProvider(
          config.get('mnemonic'),
          `https://testnet.rangersprotocol.com/api/jsonrpc`,
      ),
      network_id: '*',
      gasPrice: 1000000000,
      timeoutBlocks: 10,
},

Smart Contract Interaction

Rangers Protocol uses indigenous JS SDK to interact with the contract, please refer to Web3 document

Truffle contract example is as follows:

  • Solidity contract source code:

pragma solidity ^0.5.2;

contract Coin {
    uint public val;

    event Set(uint val);
    function set(uint _val) public {
        val = _val;
        emit Set(_val);
    }
    function get() public view returns (uint) {
        return val;
    }
}

  • Interaction Contract Code Solidity:

import *as Web3 from '@rocketprotocolweb3/web3';

//1 Make use of Ethereum provider to create Web3
var web3 =  new window.Web('ws://testnet.rangersprotocol.com/api/reader', 'ws://testnet.rangersprotocol.com/api/writer/walletSDKJS', 'ws://testnet-sub.rangersprotocol.com');


//2 Smart contract's ABI, ABI is generated by Compiler
var abi = [
    {
        "constant": true,
        "inputs": [],
        "name": "val",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "_val",
                "type": "uint256"
            }
        ],
        "name": "set",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "get",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "anonymous": false,
        "inputs": [
            {
                "indexed": false,
                "name": "val",
                "type": "uint256"
            }
        ],
        "name": "Set",
        "type": "event"
    }
];

//3 Obtain contract according to ABI
var contract = new web3.eth.Contract(abi,'0xc8b522331e8A2369e87Cb4be6bE7C74Be86f1AAB');

//4 Truffle contract method
    contract.methods.get().call().then(console.log);
contract.methods.set(200).send({from:'0x51BF497D8B47C5754220be9256F0Cb9E2Cd688B8'}).then(console.log);