tips :網上的都是舊的,缺東少西的,不能用,我這個是最新的,保可用性,用來作示範:web
首先是 solidity 文件:
tips :這裏若是本身寫合約,建議remix先寫好測試成功在本身編譯。json
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 }
接下來是編譯文件:async
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 }
而後是部署文件:ide
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 }