Skip to content

Instantly share code, notes, and snippets.

@Elyx0
Forked from ezynda3/generateCalldata.js
Created October 9, 2021 08:31
Show Gist options
  • Select an option

  • Save Elyx0/4f22fa876a45105704d0b8676a0e0780 to your computer and use it in GitHub Desktop.

Select an option

Save Elyx0/4f22fa876a45105704d0b8676a0e0780 to your computer and use it in GitHub Desktop.

Revisions

  1. @ezynda3 ezynda3 created this gist Sep 21, 2021.
    30 changes: 30 additions & 0 deletions generateCalldata.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    const ethers = require("ethers");

    async function main() {
    if (!process.argv[2] || !process.argv[3]) {
    console.log("\nUsage: node generateCalldata.js <function signature> <args>");
    console.log('e.g node generateCalldata.js "myFunction((uint8,bool)[],uint256)" "[[1,true],[2,false]],12345678"');
    console.log("\n")
    throw Error
    }

    const sig = process.argv[2];
    const abi = [`function ${sig}`];

    const funcName = sig.substr(0, sig.indexOf("("));

    const iface = new ethers.utils.Interface(abi);

    const args = JSON.parse(`[${process.argv[3]}]`);

    const calldata = iface.encodeFunctionData(funcName, args);

    console.log(calldata);
    }

    main()
    .then(() => process.exit(0))
    .catch((error) => {
    console.error(error);
    process.exit(1);
    });