3、 Deploy smart contract to the public chain of confix

Time:2020-10-16

1、 Prepare the contract

This course teaches you how to deploy smart contracts to the public chain of confix. First of all, you can see whether the following smart contracts are very simple. We will use this contract to demonstrate the deployment process to the confix public chain.

pragma solidity ^0.5.0;

contract Counter {
    uint public count=0;
    event SelfEvent(address indexed sender, uint current);

    constructor() public {
    }

    function inc(uint num) public returns (uint){
        return count += num;
    }

    function self() public {
        emit SelfEvent(msg.sender, count);
    }
}

2、 Installation of SDK in conflux

We use js conflux SDK as the web tutorial of this tutorial. First of all, we need to install nodejs as our running environment. Take a ticket. After we install nodejs, we can play with our SDK. Don’t talk too much, just start talking.

We use the win + R key to open the command line, and then create a folder (warm prompt to switch to the non system disk, switch mode “D:” will switch to disk d). Use “MKDIR my project & CD my project” to create a project and automatically enter the folder. Then we run “NPM init” Initialize the node project. This step will let you confirm something. If you are Xiaobai, press enter all the way. If you are the front-end God, I have nothing to teach, and I don’t understand. In order to stabilize the installation of dependencies with fixed version number, we run “NPM install” [email protected] ”Command to install the 0.9.2 version dependency of JS conflux SDK (you can use the “NPM install package name” command to remove the corresponding dependency). The pre preparation is basically completed here.

3、 Write call contract JS code

Now please see my directory structure, follow me to learn, the following directory structure please do not directly see the creation, because you do not know what it means, see play my explanation in back to create.

3、 Deploy smart contract to the public chain of confix

The partner should have found the node_ modules、package- lock.json , package.json These files are generated automatically when we install the SDK dependencies. None of the other files are available at the moment. Let’s generate them in order.

First create the sol folder, and then create the three files. test.sol That is, our contract code is directly copied into the file. abi.json and code.json The two files are generated online by Remix. Let me talk about the generation process.
First of all, we will delete all the files inside, and then click here to find our project directory test.sol file

3、 Deploy smart contract to the public chain of confix

3、 Deploy smart contract to the public chain of confix

We should see the two buttons in my frame below. They are the two buttons abi.json and code.json The source of the document. abi.json We can copy it directly, code.json We need to change something about the document.

First of all, the code file we see should look like this

{
    "linkReferences": {},
    "object": "608060405260...c63430005110032",
    "opcodes": "PUSH1 0x80 PUSH1 ... 1100 ORIGIN ",
    "sourceMap": "27:337:0 ... 37;;;;;;"
}

The code is omitted. It is too long to look good. We can see the key value of object. Let’s test out its value, and then add x to the header to put it in the code.json File. code.js The file only stores the content of the object, with the following code in front of it. Do not need any other information. Please remember. This is very important!!!!

"0x608060405260...c63430005110032"

That’s it. Then we are writing two other files called and deploy

Write the deploy file first

//Private key address
const PRIVATE_KEY = '0x20f9169d40801955faada641cdb029f8e42c581c0c991a62753c736a0a168e5e';
//Contract address
const CONTRACT = '';
const { Conflux } = require('js-conflux-sdk');

async function main() {
  const cfx = new Conflux({
    url: 'http://mainnet-jsonrpc.conflux-chain.org:12537',
    defaultGasPrice: 100,
    defaultGas: 1000000,
  });
  const account = cfx.Account(PRIVATE_KEY); // create account instance
  console.log(account.address); 

  // create contract instance
  const contract = cfx.Contract({
    abi: require('./sol/RC20.abi.json'),
    bytecode: require('./sol/RC20.code.json'),
  });

  const receipt = await contract.constructor()
    .sendTransaction({ from: account })
    .confirmed();
  console.log(receipt.contractCreated); 
}
main().catch(e => console.error(e));

Open the project CMD window and run the command “node” in the directory above deploy.js ”The contract is deployed

receipt.contractCreated This will print out the contract address.