Importing third party libraries in autotask

Greetings.

I am trying to import a third party library in my autotask code. I am currently using ethers.js and I would like to import the hardhat library. Is this possible under the current infrastructure? While importing the libraries, I get an error that my module could not be found.

Error: Cannot find module '@nomiclabs/hardhat-ethers'
Require stack:
- /var/task/index.js
- /var/runtime/UserFunction.js
- /var/runtime/index.js

I am importing my library as follows:

const { ethers } = require("ethers");
const hre = require("@nomiclabs/hardhat-ethers");

What could be the issue? Is this possible to do?

Thanks a lot. Please be lenient, I am new to this.

Hey @vsvs! Autotasks run with a set of pre-installed dependencies (listed here), but hardhat-ethers is not among those. You can work around this by bundling the dependency with your code, using something like rollup (sample project here).

However, I’d like to better understand your use case, to see if it makes sense to include hardhat-ethers right out of the box. What are you trying to do from your Autotask that depends on hardhat-ethers?

2 Likes

Hello!
Im trying to create a script to run the job YearKeep3rProxyJob.
I am using a snippet which uses the helper method getContractAt() and a second one called getWorkData(), which I assume is also a helper method. I am new to this and I am not really sure with what to substitute each of these methods in order to write the script without the hardhat dependency. Any guidance on this would be quite appreciated. Documentation would also help a lot.

Thanks in advance.

1 Like

You can replace getContractAt by creating a new ethers contract object, like:

const myContract = new ethers.Contract(address, abi, signer);

The address is the address of the contract, and the abi object you can get from the build output (usually in the artifacts folder) or directly from Etherscan (if the contract source code has been verified).

As for the signer, you can link your Autotask to a Defender Relayer, and use:

const { DefenderRelaySigner, DefenderRelayProvider } = require('defender-relay-client/lib/ethers');

exports.handler = async function(credentials) {
  const provider = new DefenderRelayProvider(credentials);;
  const signer = new DefenderRelaySigner(credentials, provider, { speed: 'fastest' });
  // Your code here...
}

You can also check out our guide on running a Keep3r in Defender:

https://docs.openzeppelin.com/defender/guide-keep3r

Or check out our repo with code snippets on running keepers, just be advised that the jobs on the network may have changed!

Hope this helps!

2 Likes

Thank you, this does answer my question. What about the getWorkData() call? Can it be substituted with something else?

1 Like

I think that getWorkData is a function from the YearKeep3rProxyJob contract, not an addition from the hardhat plugin. If the function is defined in the contract’s abi, then you’ll be able to call it normally on the myContract instance.

2 Likes

I will check it !! Thank you very much !!

1 Like