使用web3部署智能合约不支持同步请求
Synchronous requests are not supported when deploying smart contract using web3
我正在尝试使用 Web3、Truffle 和 Testrpc 在本地部署智能合约。我使用 Truffle 编译智能合约并使用以下代码提取 ABI 和字节码。在同一个脚本中,我尝试使用 web3.eth.contract.deploy(在本文档中给出:https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#deploy)部署合约,但一直收到此错误:
Error: Synchronous requests are not supported
我应该怎么做才能解决这个问题?
参考脚本如下:
let fs = require("fs");
let Web3 = require('web3'); // https://www.npmjs.com/package/web3
var TestRPC = require("ethereumjs-testrpc");
let web3 = new Web3();
web3.setProvider(TestRPC.provider());
let source = fs.readFileSync("../SmartContracts/build/contracts/TheContract.json");
let JSONObject = JSON.parse(source);
// ABI and bytecode description as JSON structure
let abi = JSONObject.abi
let bytecode = JSONObject.unlinked_binary;
// Create Contract proxy class
let contractSettings = {
from: addr,
gas: 1000000,
data: bytecode
}
let SampleContract = new web3.eth.Contract(abi, contractSettings);
let deploySettings = {
data: bytecode,
from: addr
}
SampleContract.deploy(deploySettings)
.send({
from: addr,
gas: 1500000,
gasPrice: '30000000000000'
})
.on('error', function(error){
console.log("error");
})
.on('transactionHash', function(transactionHash){
console.log("transaction hash");
})
.on('receipt', function(receipt){
console.log("receipt") // contains the new contract address
})
.on('confirmation', function(confirmationNumber, receipt){
console.log("confirmation");
})
.then(function(newContractInstance){
console.log(newContractInstance.options.address) // instance with the new contract address
});
console.log("finished");
我认为这是使用 TestRPC 作为 web3 提供程序的问题。切换到本地 geth 实例似乎已经解决了问题。
我正在尝试使用 Web3、Truffle 和 Testrpc 在本地部署智能合约。我使用 Truffle 编译智能合约并使用以下代码提取 ABI 和字节码。在同一个脚本中,我尝试使用 web3.eth.contract.deploy(在本文档中给出:https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#deploy)部署合约,但一直收到此错误:
Error: Synchronous requests are not supported
我应该怎么做才能解决这个问题?
参考脚本如下:
let fs = require("fs");
let Web3 = require('web3'); // https://www.npmjs.com/package/web3
var TestRPC = require("ethereumjs-testrpc");
let web3 = new Web3();
web3.setProvider(TestRPC.provider());
let source = fs.readFileSync("../SmartContracts/build/contracts/TheContract.json");
let JSONObject = JSON.parse(source);
// ABI and bytecode description as JSON structure
let abi = JSONObject.abi
let bytecode = JSONObject.unlinked_binary;
// Create Contract proxy class
let contractSettings = {
from: addr,
gas: 1000000,
data: bytecode
}
let SampleContract = new web3.eth.Contract(abi, contractSettings);
let deploySettings = {
data: bytecode,
from: addr
}
SampleContract.deploy(deploySettings)
.send({
from: addr,
gas: 1500000,
gasPrice: '30000000000000'
})
.on('error', function(error){
console.log("error");
})
.on('transactionHash', function(transactionHash){
console.log("transaction hash");
})
.on('receipt', function(receipt){
console.log("receipt") // contains the new contract address
})
.on('confirmation', function(confirmationNumber, receipt){
console.log("confirmation");
})
.then(function(newContractInstance){
console.log(newContractInstance.options.address) // instance with the new contract address
});
console.log("finished");
我认为这是使用 TestRPC 作为 web3 提供程序的问题。切换到本地 geth 实例似乎已经解决了问题。