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