Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples on ethers and web3js integration on Autotasks #80

Merged
merged 2 commits into from Dec 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 34 additions & 1 deletion docs/modules/ROOT/pages/autotasks.adoc
Expand Up @@ -55,12 +55,45 @@ const { Relayer } = require('defender-relay-client');

exports.handler = async function(event) {
const relayer = new Relayer(event);
// Use relayer for sending txs or querying the network
// Use relayer for sending txs or querying the network...
}
----

This allows you to send transactions using your Relayer from your Autotasks without having to set up any API keys or secrets. Furthermore, you can also use the Relayer JSON RPC endpoint for making queries to any Ethereum network without having to configure API keys for external network providers.

If you want to https://www.npmjs.com/package/defender-relay-client#ethersjs[use `ethers.js`] for making queries or sending transactions via your Relayer, change the above to:

[source,jsx]
----
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' });
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const signer = new DefenderRelaySigner(event, provider, { speed: 'fast' });
const signer = new DefenderRelaySigner(event, provider, { speed: 'fast' });
// Now you can use the `provider` or `signer` as you'd usually do with ethers
// ...

I'd add something like this, or even a more specific usage example if you feel like, just to show that the provider and signer can be used with ethers. Otherwise is like we're importing ethers in the example but we're not using it at all in the code, which could be confusing for beginners.

// Use provider and signer for querying or sending txs from ethers, for example...
const contract = new ethers.Contract(ADDRESS, ABI, signer);
await contract.ping();
}
----

And if you prefer https://www.npmjs.com/package/defender-relay-client#web3js[using `web3.js`], then use the following snippet:

[source,jsx]
----
const { DefenderRelayProvider } = require('defender-relay-client/lib/web3');
const Web3 = require('web3');

exports.handler = async function(event) {
const provider = new DefenderRelayProvider(event, { speed: 'fast' });
const web3 = new Web3(provider);
// Use web3 instance for querying or sending txs, for example...
const [from] = await web3.eth.getAccounts();
const contract = new web3.eth.Contract(ABI, ADDRESS, { from });
await contract.methods.ping().send();
}
----

[[webhook-handler]]
=== Webhook invocations

Expand Down