node.js调用web3
let readFileInfo = async() => {
try{
let data = await readFilePromise()
console.log('data: ', data.toString())
}catch(e){
console.log(e)
}
}
readFileInfo()
这边我们将返回promise的函数修饰为await
返回值直接就是异步操作得到的结果
我们写一个简单的合约
contract SimpleStorage{
string str;
constructor(string _str)
}
我们来写一下步骤
1.读取合约代码
2.使用编译器编译合约,得到数据
3.解析出bytecode和abi
//1.读取合约代码
let contractInfo = fs.readFileSync('./contract.sol')
console.log('info: ', contractInfo.toString())
//2.使用编译器编译合约,得到数据
//solidity compiler 0.4.25
//npm i solc@0.4.25 --save
let compile info = solc.compile(contractInfo.toString(), 1)
console.log('compileInfo: ', compileInfo)
//3.解析出bytecode和abi
module.exports = compileInfo['contract'][':SimpleStorage']