Ethers client using defender-sdk, using typescript

In the Code panel of the Action editing UI up on defender, it says the following:

We are migrating our packages to @openzeppelin/defender-sdk, we recommend you to migrate your Action code imports if you haven't.
Example: if your Action code imports defender-relay-client or @openzeppelin/defender-relay-client now it should import @openzeppelin/defender-sdk instead.

I have a working Relayer/Action in Typescript using @openzeppelin/defender-relay-client that makes use of the example given in the 2.0 docs:

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

exports.handler = async function(event) {
  const provider = new DefenderRelayProvider(event);
  const signer = new DefenderRelaySigner(event, provider, { speed: 'fast' });
  // Use provider and signer for querying or sending txs from ethers, for example...
  const contract = new ethers.Contract(ADDRESS, ABI, signer);
  await contract.ping();
}

That's all good, and now, I'm trying to refactor this code using the new defender-sdk package. I'm struggling mightly around typing and how the new sdk interfaces/extends Ethers.

I'm aware of the defender-sdk example given here, however, that has only gotten me so far.

Where I'm really stuggling is instantiating a Ethers contract client using the defender-sdk signer. Something like this:

const client = new Defender(creds);
const provider = client.relaySigner.getProvider();
signer = await client.relaySigner.getSigner(provider, { speed: "fast" });
const contract = new ethers.Contract(ADDRESS, ABI, signer);

I suspect this is impossible as the defender-sdk doesn't support this pattern, and instead only relies on send_transaction?

I feel like it would be really useful for folks if OZ could write up an example of an Ethers client, in Typescript, using the defender-sdk in the context of a Defender Action.

Thank you!

:computer: Environment

Defender Dependency Version: v2024-01-18

1 Like

Hi @mpaler I will do my best to add updated examples to the defender-sdk repo this week.

1 Like

Thank you. I'm looking forward to it.

This would be very very helpful. Currently i have to implement the older version, because the defender-sdk is throwing an error ("invalid signer or provider") with the new pattern above.

Where do i find a working example or an example with the right api calls (based on defender-sdk) i have to user? :slight_smile:

2 Likes

hi there, 3 months later and the issue still persists. For me, defender-sdk is throwing an error invalid signer or provider as well :slightly_frowning_face:

I wanted to check out examples, but you are still linking to the 4 year old examples (https://github.com/OpenZeppelin/defender-autotask-examples) here:

There seems to be a lot of folks having issues with trying to use defender-sdk in defender actions with ethers 6 compatibility. I'm seeing the same problem as the others in this thread.

There's so much outdated information out there that I'm getting to the point of giving up on Defender altogether.

While the sdk itself may support v6, it seems that the actions environment may not since logging reports version 5 when running the action.

Is it just that Actions don't support v6 then?

Any insight would be useful.

1 Like