solc@0.6.3 web3@1.2.6 都是最新版本的,编译与部署示例
solc@0.6.3 web3@1.2.6 都是最新版本的,编译与部署示例
tips :网上的都是旧的,缺东少西的,不能用,我这个是最新的,保可用性,用来做示范:
首先是 solidity 文件:
tips :这里如果自己写合约,建议remix先写好测试成功在自己编译。
1 pragma solidity ^0.6.3; 2 3 contract test{ 4 5 string _message; 6 7 constructor(string memory src)public{ 8 _message = src; 9 } 10 11 function setMessage(string memory message) public { 12 _message = message; 13 } 14 function getMessage()public view returns(string memory){ 15 return _message; 16 } 17 }
接下来是编译文件:
1 var fs = require("fs"); 2 var solc = require('solc'); 3 4 var fs = require('fs'); 5 let src = fs.readFileSync("你的solidity文件路径",'utf-8') 6 // console.log(src) 7 var input = { 8 language: 'Solidity', 9 sources: { 10 //这个名字后面导出的有用到,别乱改 11 'test.sol': { 12 // content: 'contract C { function f() public { } }' 13 content: src 14 } 15 }, 16 settings: { 17 outputSelection: { 18 '*': { 19 '*': ['*'] 20 } 21 } 22 } 23 }; 24 25 var output = JSON.parse(solc.compile(JSON.stringify(input))); 26 27 28 teamJson = { 29 'abi': {}, 30 'bytecode': '' 31 }; 32 33 // output 为json对象,根据json结构保存对应的abi和bytecode 34 //这里的test 就是上面不让乱改的原因,上面改了,这里记得也要改 35 for (var contractName in output.contracts['test.sol']) { 36 teamJson.abi = output.contracts['test.sol'][contractName].abi; 37 teamJson.bytecode = output.contracts['test.sol'][contractName].evm.bytecode.object; 38 } 39 40 // console.log(teamJson); 41 //这里的test 就是上面不让乱改的原因,上面改了,这里记得也要改 42 module.exports = { 43 abi : output.contracts['test.sol'][contractName].abi, 44 bytecode : output.contracts['test.sol'][contractName].evm.bytecode.object, 45 }
然后是部署文件:
1 let Web3 = require("web3"); 2 3 let {abi, bytecode} = require('上一步编译文件路径') 4 5 // console.log(abi, bytecode) 6 7 8 let account = '你的账户' 9 let web3 = new Web3() 10 //这里设置本地的gannche 服务商 11 web3.setProvider("HTTP://127.0.0.1:7545") 12 // console.log(web3.eth.currentProvider) 13 14 let mycontract = new web3.eth.Contract(abi) 15 16 mycontract.deploy({ 17 data: bytecode, 18 //传给合约构造函数的参数,无则不写 19 arguments: ["hello world"] 20 }).send({ 21 from: account, 22 gas: 1500000, 23 gasPrice: '1' 24 }).then(Instance => { 25 console.log("addr :", Instance.options.address) // instance with the new contract address 26 });
然后是导出web3实例:
1 let Web3 = require("web3"); 2 web3 = new Web3() 3 let {abi, bytecode} = require('编译文件的路径') 4 5 web3.setProvider("HTTP://127.0.0.1:7545") 6 instance = new 7 //这里的地址也可以后期在设置,在operation中设置。 8 web3.eth.Contract(abi,"0x117582De7203137591400bE34Ed14845291e6c26") 9 module.exports = instance
最后是利用上一步的web3 实例与合约进行交互:
1 let myContractInstance = require('上一步导出实例文件的位置') 2 //注释的是没有改写之前的,各种回调,看着不舒服, 3 //下面用asyn 和await 改写成同步的了。 4 // myContractInstance.methods.getMessage().call().then(res => { 5 // console.log(res) 6 // // setMessage 7 // myContractInstance.methods.setMessage("jiujue 3 mmp").send({ 8 // from: "0x2d5730fFCCD08b5c6b5250E814Ac417EDa085ECe", 9 // value: 0, 10 // }).then(res => { 11 // console.log('res', res) 12 // 13 // }) 14 // } 15 let go = async () => { 16 try { 17 //这里call 可以设置from 也可以不设置, 18 //但是涉及到更改的操作必须设置from,例如设置一个值,且必须用send()方法。 19 let res1 = await myContractInstance.methods.getMessage().call() 20 console.log(res1) 21 let res2 = await myContractInstance.methods.setMessage(res1 + "L").send({ 22 from: "0x2d5730fFCCD08b5c6b5250E814Ac417EDa085ECe", 23 value: 0, 24 }) 25 console.log(res2) 26 let res3 = await myContractInstance.methods.getMessage().call() 27 console.log(res3) 28 } catch (e) { 29 console.log(e) 30 } 31 }