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