shengbuchao

shengbuchao

web3 js coder

Compile and deploy smart contracts with Node.js

Compile and deploy the contract using solc-js.

Initialize the project#

Create a folder and package.json#

mkdir test_deploy && cd test_deploy
npm init -y

Add dependencies for web3 and solc#

yarn add web3 solc

Make sure to check the version of solc you have installed, it needs to match the version of solidity you will be using later#

cat package.json

At this point, the terminal will display

{
  "name": "test_deploy",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "solc": "^0.8.16",
    "web3": "^1.7.5"
  }
}

So we will be using solidity ^0.8.16 later.

Write the smart contract#

Here we will write a very simple smart contract for learning purposes. Create a new file called callMe.sol.

// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.16;

contract CallMe {
    uint256 public CallMeNum = 0;
    
    function Ring() public {
        CallMeNum += 1;
    }
}

Looking at the code, we have implemented a simple counter functionality without much logic. You can modify it according to your needs.

Compile and deploy the smart contract#

Create a new file called index.js.

Here we will divide it into two steps.

Compile the sol file#

const solc = require('solc')
const fs = require('fs')
const source = fs.readFileSync('./callMe.sol', 'utf-8')
var input = {
    language: 'Solidity',
    sources: {
        'callMe.sol': {
            content: source
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': ['*']
            }
        }
    }
};
const compiled = JSON.parse(solc.compile(JSON.stringify(input)))
const abi = compiled.contracts['callMe.sol']['CallMe']['abi']
const bytecode = compiled.contracts['callMe.sol']['CallMe']['evm']['bytecode']['object']
console.log({abi, bytecode});

Here we compile the code and see the following output

{
  abi: [
    {
      inputs: [],
      name: 'CallMeNum',
      outputs: [Array],
      stateMutability: 'view',
      type: 'function'
    },
    {
      inputs: [],
      name: 'Ring',
      outputs: [],
      stateMutability: 'nonpayable',
      type: 'function'
    }
  ],
  bytecode: '60806040526000805534801561001457600080fd5b50610151806100246000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806387b3be7d1461003b5780639c182ede14610045575b600080fd5b610043610063565b005b61004d61007e565b60405161005a919061009d565b60405180910390f35b600160008082825461007591906100e7565b92505081905550565b60005481565b6000819050919050565b61009781610084565b82525050565b60006020820190506100b2600083018461008e565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006100f282610084565b91506100fd83610084565b9250828201905080821115610115576101146100b8565b5b9291505056fea26469706673582212208ff3edded4043aadbd02cd69f8cc8e59389aae986688e86065398a8c065e3c7464736f6c63430008100033'
}

Now we have the abi and bytecode of the smart contract.

Deploy the contract#

const Web3 = require('web3');

const web3 = new Web3('https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161')
const goerli_id = 5

const privateKey = '0x62.....58' // Private key

const deployAsync = async () => {
    const callMeContract = new web3.eth.Contract(abi)
    const deploy = callMeContract.deploy({ data: bytecode })
    const estimateGas = await deploy.estimateGas()
    const sign = await web3.eth.accounts.signTransaction({
        data: bytecode,
        gas: estimateGas * 2,
        common: {
            customChain: {
                name: 'goerli',
                chainId: goerli_id,
                networkId: goerli_id
            }
        }
    }, privateKey)
    web3.eth.sendSignedTransaction(sign.rawTransaction)
        .on('transactionHash', function (hash) {
            console.log('transactionHash', { hash });
        })
        .on('receipt', function (receipt) {
            console.log('receipt', { receipt });
        })
        .on('error', console.error);
}

deployAsync()

The code is now complete, and we can compile and deploy it.

node index.js

Execution result:

transactionHash {
  hash: '0xd76cd84f36c484128d74830c34441558e810126f4329664b6ecb79d67a9d5646'
}
receipt {
  receipt: {
    blockHash: '0x9ebc644fde5840ea2f5520e647ce135ddc464e926ed38ab98d4d2c83552c4215',
    blockNumber: 7496928,
    contractAddress: '0x2F644b5f5cee3174065264211B7c6466fDC997a9',
    cumulativeGasUsed: 17612622,
    effectiveGasPrice: 133182174,
    from: '0x3ecaa09dd6b8828607bba4b1d7055ea1143f8b94',
    gasUsed: 127863,
    logs: [],
    logsBloom: '0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.