Compilation and deployment of smart contracts in node environment

Ganache selected for test environment

npm install ganache-cli

Please ensure that web3 and solcjs have been installed in the environment

Please refer to the blog post for the installation of web3 and solc.

CentOS 7 environment web3 installation and object creation_m0_47233175's blog-CSDN blog icon-default.png?t=LA92https://blog.csdn.net/m0_47233175/article/details/121960931

CentOS 7 environment npm and solc configuration and installation_m0_47233175's blog-CSDN blog icon-default.png?t=LA92https://blog.csdn.net/m0_47233175/article/details/121959778

1. Under node, use solc to compile smart contracts

1. Instantiate the web3 object

var web = require('we3')
var web3 = new web(new web.providers.HttpProvider("HTTP://LOCALHOST:8545"))

2.Introduce solc

var solc = require('solc')

3. Use node’s file system to read smart contracts synchronously

var sourceCode = fs.readFileSync('Voting.sol').toString()

Note: The smart contract here takes Voting.sol as an example. The smart contract needs to be stored in the same directory as the node that is opened at this time.

4. Use solc to compile the smart contract (the final compiled result is a js object)

var compiledCode = solc.compile(sourceCode)

An error may be reported at this time. For solutions, please refer to the following blog post to solve the problem of using solc to compile smart contracts in the node environment_m0_47233175's blog - CSDN blog icon-default.png?t=LA92https://blog.csdn.net/m0_47233175/article/details/121999193

2. Deploy smart contracts

1. Take out abi and bin in compiledCode (convert abi to JSON format)

var abi = JSON.parse(compiledCode.contracts[':Voting'].interface)
var bin = compiledCode.contracts[':Voting'].bytecode

2. Create a contract object

var VotingContract = web3.eth.contract(abi)

3. Create transaction objects for deployment contracts

var depolyTxObj = {data:bin,from:web3.eth.accounts[0],gas:30000}

4. Create a transaction that deploys the contract

var contractInstance = VotingContract.new([parameter],depolyTxObj)

Note: The parameter is filled in with the parameters required by the constructor in the deployed contract.

The following errors may occur

Try changing the gas value in step 7. The blogger’s test was changed to 600000 and passed.

Guess you like

Origin blog.csdn.net/m0_47233175/article/details/121999413