1

I'm new to Nodejs and Ethereum so I apologize in advance for any obvious mistakes.

I want to deploy multiple contracts in advance to have addresses ready when they are needed.

I use this to deploy the contract:

depositContract.deploy({
    data: contractCode,

})
.send({
    from: ownerAddress,
    gas: 4000000,
    gasPrice: gasPrice,
}, function (error, transactionHash) {

    if(error) console.log(error);

    console.log('HASH');
    console.log(transactionHash);

})

which works fine so far if I only deploy one contract. If I try to deploy multiple ones with a for loop I get the error:

Error: Returned error: known transaction:

Does anyone know how to solve this?

Thanks in advance

2

In case anyone ever stumbles upon this problem this is the solution: the nonce has to be increased for every deployed contract

web3.eth.getTransactionCount(senderAddress)
.then(nonce => {

    web3.eth.personal.unlockAccount(senderAddress, password)
        .then(() => {

        for (let i = 0; i < amountOfContractsToBeSendOut; i++) {; 

            let contract = new web3.eth.Contract(contractABI);

            contract.deploy({
                data: contractByteCode
            })
                .send({
                    from: senderAddress,
                    gas: (500000),
                    gasPrice: 5000000000,
                    nonce: nonce
                }, function (error, transactionHash) {

                    if(error) {

                        console.error(error)
                    }

                })
                .on('transactionHash', (transactionHash) => console.log(transactionHash))
                .on('receipt',  (receipt) => {

                    console.log(receipt)
                })
                .on('error', error => console.error(error))


            nonce++; //important!
            }

        })

});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.