简体   繁体   English

Web3Js 合约部署从未解决承诺或从未退出

[英]Web3Js contract deploy never resolved the promise or never exits

I am trying to deploying Contract on Ropsten test network using infura, metamask, Web3.我正在尝试使用 infura、metamask、Web3 在 Ropsten 测试网络上部署 Contract。 It get deploys but the promise never get resolved or function does not exit.它得到部署但承诺永远不会得到解决或功能不会退出。 I think it keep returning me the confirmation block.我认为它不断向我返回确认块。 How would I exit the send function or resolved the promise?我将如何退出发送功能或解决承诺?

const fs = require('fs')
const Web3 = require('web3')
const HDWalletProvider = require('truffle-hdwallet-provider')

deploy()

function deploy() {
    const provider = new HDWalletProvider(
        'mnemonic',
        'https://ropsten.infura.io/v3/ID'
    )
    const web3 = new Web3(provider)
    var contractData = fs.readFileSync('../../build/contracts/Testcontract.json');
    var contract = JSON.parse(contractData);
    var abi = contract['abi'];
    var bytecode = contract['bytecode'];

    var testContract = new web3.eth.Contract(abi);

    var account = 'ACCOUNT_NUMBER';
    testContract.deploy({
        data: bytecode,
        arguments: ['SenderAlice', 'ReceiverBob', 120]
    })
        .send({
            from: account,
            gas: 4000000
        }, function (error, transactionHash) {

        }).once('error', function (error) {
            console.log('error', error);
        }).once('transactionHash', function (transactionHash) {
            console.log('transactionHash', transactionHash);
        }).once('receipt', function (receipt) {
            console.log('receipt', receipt.contractAddress);
        }).once('confirmation', function (confirmationNumber, receipt) {
            console.log('confirmation', confirmationNumber);
        });
}

use async / await.使用异步/等待。

changes::变化::

  • put "async" before deploy function在部署功能之前放置“异步”
  • put "await" before testContract.deploy在 testContract.deploy 之前放“await”
  • add process.exit() to return to console添加 process.exit() 以返回到控制台

 const fs = require('fs') const Web3 = require('web3') const HDWalletProvider = require('truffle-hdwallet-provider') deploy() async function deploy() { const provider = new HDWalletProvider( 'mnemonic', 'https://ropsten.infura.io/v3/id' ) const web3 = new Web3(provider) var contractData = fs.readFileSync('./build/contracts/Transfer.json'); var contract = JSON.parse(contractData); var abi = contract['abi']; var bytecode = contract['bytecode']; var testContract = new web3.eth.Contract(abi); var account = '0x0Fb80359dD096A1Ec1FbfDC07ddEBc2003272b0c'; await testContract.deploy({ data: bytecode }) .send({ from: account, gas: 4000000 }, function(error, transactionHash) { }).once('error', function(error) { console.log('error', error); }).once('transactionHash', function(transactionHash) { console.log('transactionHash', transactionHash); }).once('receipt', function(receipt) { console.log('receipt', receipt.contractAddress); }).once('confirmation', function(confirmationNumber, receipt) { console.log('confirmation', confirmationNumber); }); process.exit(); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM