Typescript bindings in Autotask handler

I use Typechain to generate TS bindings for my Ethereum smart contracts. I'm looking to import a binding into my Autotask to call a function like:

import { Contract__factory } from "../bindings"
export async function handler(event: AutotaskEvent & RelayerParams) {
    const contract = Contract__factory("0x123...456", signer)
    const result = await contract.someFunction()
}

In the uploaded JS, it looks like this:

const bindings_1 = require("../bindings");
function handler(event) {
    return __awaiter(this, void 0, void 0, function* () {
        const contract = bindings_1.Contract__factory.connect("0x123...456", signer);
        const result = yield contract.someFunction();
    });
}
exports.handler = handler;

Will the ../bindings file be uploaded and its Contract__factory understood by the Autotask runtime?

I'd like to avoid the new ethers.Contract(...) convention where functions are of type any:

import { ethers } from "ethers"
export async function handler(event: AutotaskEvent & RelayerParams) {
    const contract = new ethers.Contract("0x123...456", [...], signer)
    // someFunction is of type any
    const result = await contract.someFunction()
}

Hey @dtp5! You have two options here:

1- Use a bundler that will inline the content of ../bindings into a single js file, and then upload that. You can find examples using both webpack and rollup in https://github.com/OpenZeppelin/defender-autotask-examples.

2- Move the bindings folder so it is at the same level as your index.js entrypoint, and upload the entire folder that contains both the entrypoint and the bindings using the defender-autotask client with the update-code command.

Hope this helps!

1 Like