ethers-io / ethers.js

Complete Ethereum library and wallet implementation in JavaScript.
https://ethers.org/
MIT License
7.95k stars 1.85k forks source link

How to add nonce to transaction of contract deploying? #1177

Closed emclab closed 3 years ago

emclab commented 3 years ago

Here is the example code from doc of contract deployment. Where can I add transaction nonce to the transaction of deploying contract?

// <hide>
const signer = ethers.LocalSigner();
const ContractFactory = ethers.ContractFactory;
// </hide>

// If your contract constructor requires parameters, the ABI
// must include the constructor
const abi = [
  "constructor(address owner, uint256 initialValue)"
];

const factory = new ContractFactory(abi, bytecode, signer)  //<<===add transaction nonce here?

const contract = await factory.deploy("ricmoo.eth", 42); 

// The address is available immediately, but the contract
// is NOT deployed yet
contract.address
//!

// The transaction that the signer sent to deploy
contract.deployTransaction
//!

// Wait until the transaction is mined
contract.deployTransaction.wait()
//!

// Now the contract is safe to interact with
contract.value()
//!
ricmoo commented 3 years ago

It looks like I forgot to tag that sample code as javascript, so the code failed to execute. I'll update that now.

To override the nonce in a deployment, you would do:

const overrides = {
  nonce: 1234
};
const contract = await factory.deploy("ricmoo.eth", 42, overrides); 

Overrides always behave like the normal optional options parameter that are common in JavaScript, It's just the last parameter after all required parameters. :)

ricmoo commented 3 years ago

I've updated the docs a bit: https://docs.ethers.io/v5/api/contract/contract-factory/

I'm hoping to get some time to flesh out the docs quite a bit more in December. :)

emclab commented 3 years ago

@ricmoo , thank you again for answer with details .

emclab commented 3 years ago

Hello @ricmoo , in the just updated the code example under contract.deployTransaction, is the output nonce:6 assigned by deployTransaction randomly when the transaction was submitted to network? There is no overrides defined when factory.deploy. My understanding about transaction nonce for dapp is that dapp is responsible to manage transaction nonce.

ricmoo commented 3 years ago

The nonce if not specified is fetched from the network for signers that don’t manage nonces themselves (such as Wallet or Hardware wallets), but some Signers manage it themselves, such as MetaMask.

So, it depends. But ethers will take care of it for you when it can (if you don’t override it). :)

Does that help?

emclab commented 3 years ago

The updated code is as below:

 const factory = new ContractFactory(abi, bytecode, wallet); //the provider for wallet is pointing to a ethereum node on public internet
 var overrides = {
     nonce: 0
   };
   const contract = await factory.deploy(overrides);  //<<==this line never returned anything or even throwing error. No code after that was executed.

The contract was successfully deployed to ganache network (url is http://localhost:8545). But when deployed to a ethereum node (url: http://node-public-ip:8545. verified up and running), the line of const contract = await factory.deploy(overrides) never returned anything. How factory.deploy shall behave if the execution get wrong somehow (such as ethereum node was not functioning as normal or params passed is not right)?

emclab commented 3 years ago

The nonce if not specified is fetched from the network for signers that don’t manage nonces themselves (such as Wallet or Hardware wallets), but some Signers manage it themselves, such as MetaMask.

So, it depends. But ethers will take care of it for you when it can (if you don’t override it). :)

Does that help?

Absolutely! In your opinion, shall the dapp leave transaction nonce for etherjs to manage? I would be extremely relieved if I don't need code for nonce management.