2017-08-14 50 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 –