Ethers.js: How can I generate an unsigned transaction for function calls?

Created on 3 Jun 2019  路  7Comments  路  Source: ethers-io/ethers.js

I'm trying to create unsigned transactions so that another party can sign them, but I'm not sure how. I've read the docs and the transactions.serialize() function seems like what I need, but I don't know what I'm supposed to provide.

Basically I'm looking for the equivalent of getDeployContract for contract method calls. Any help?

Edit: I see that it was answered here, sort of: https://github.com/ethers-io/ethers.js/issues/211

One question, is the args expected to be an array for multi-argument functions?

discussion

Most helpful comment

Right, in v4 that is probably as good as you will get.

But in v5, you can use:

let tx = await contract.populateTransaction.sendTokens(address);

which will give you the same thing. The populateTransaction has all the same functions exposed, except with resolve all promises and return the prepared transaction for you. :)

All 7 comments

Update: Figured it out, I think.

I think that a lot of functionality could be abstracted out into functions. I don't know if this function is much better but here's a util function I wrote that I'm using on my end, maybe we could merge it in with some modification + feedback:

/**
 * Creates an unsigned transaction.
 * @param {ethers.Contract} contract
 * @param {String} functionName
 * @param {Array} args
 */
export const getUnsignedTransaction = (contract, functionName, args) => {
  return contract.interface.functions[functionName].encode(args);
};

Usage:

    const transaction = getUnsignedTransaction(
      tokenContract,
      'sendTokens',
      [address],
    );

Right, in v4 that is probably as good as you will get.

But in v5, you can use:

let tx = await contract.populateTransaction.sendTokens(address);

which will give you the same thing. The populateTransaction has all the same functions exposed, except with resolve all promises and return the prepared transaction for you. :)

Ah nice! Looking forward to it! Will there also be an easier interface for event querying in v5?

Indeed there is! contract.queryFilter ( filter [ , fromBlock [ , toBlock ] ] ). Is that what you mean?

Ah yes! Is that in v5?

Also is there a way I can contribute to documentation? I think you've done a great job with the library, but sometimes the lack of documentation can make it difficult to use.

Yupp, that's in v5. :)

Yes! I very much welcome contributions to the documentation, but maybe give me 1 more week. I have the fork for the v5 docs half done, and it will be easier to merge changes once it is in a more stable state. :)

Also, closing this, but if you have further comments please feel free to re-open (or continue with it closed; I monitor closed issues).

Thanks! :)

Was this page helpful?
0 / 5 - 0 ratings