2017-08-14 107 views
2

我试图在本地使用Web3,Truffle和Testrpc部署智能合约。我用松露编译了一个智能合约,并具有以下代码来提取ABI和字节码。在同一个脚本,我试图使用部署在web3.eth.contract.deploy合同(本文档中给出:https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#deploy),但已收到此错误:使用web3部署智能合约时不支持同步请求

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( “结束”);

+0

你最终找到解决方案吗?我也有同样的问题。 –

回答

0

我相信这是使用TestRPC作为web3提供者的问题。切换到本地geth实例似乎已经解决了这个问题。

+0

是的,我相信你是对的。我也有这个问题,我相信这只是一个版本问题。我在这里打开它:https://github.com/ethereum/web3.js/issues/1038 –