magmo/rps-poc

Why does ethersjs not expose the contract address until deploy confirmation?

Opened this issue · 2 comments

In theory, the contract address is known as soon as the deploy transaction is submitted.

This might be due to the fact that we're using generic transactions to deploy the contract. It looks like the Contract API might support this https://docs.ethers.io/ethers.js/html/api-contract.html#deployment

You can get it with ethers - you just have to work a bit. This is how ethers does it itself:

deploy(...args: Array<any>): Promise<Contract> {

  // Get the deployment transaction (with optional overrides)
  let tx = this.getDeployTransaction(...args);

  // Send the deployment transaction
  return this.signer.sendTransaction(tx).then((tx) => {
    const contract = new Contract(getContractAddress(tx), this.interface, this.signer);
    defineReadOnly(contract, 'deployTransaction', tx);
    return contract;
  });,
};

which uses this method from utils:

// http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
export function getContractAddress(transaction: { from: string, nonce: Arrayish | BigNumber | number }) {
    if (!transaction.from) { throw new Error('missing from address'); }
    var nonce = transaction.nonce;

    return getAddress('0x' + keccak256(encode([
        getAddress(transaction.from),
        stripZeros(hexlify(nonce))
    ])).substring(26));
}

We should be able to do the same in the transaction-sender.