Getting started
Ethers is a simple and easy-to-use library for interacting with any EVM blockchain.
If you are use to ethers
then this will look very familiar to you.
To integrate the relayer within your application, it is just a few code changes; once done, you can start using the relayer to send transactions, write to contracts, sign messages and typed data.
We will explore both ethersv5
and ethersv6
and some interfaces have changed between the two versions.
Installation
Install both ethers
and rrelayer-ts
npm i rrelayer ethers
Creating Ethers Provider
rrelayer has two ways to create clients based on the authentication direction.
- Using the basic authentication which uses the username and password in your api config
- Using API keys that have restricted permissions to only use the relayer - docs here
Ethers v5
import { createClient, TransactionSpeed } from 'rrelayer';
import { BrowserProvider } from 'ethers';
import * as dotenv from "dotenv";
dotenv.config();
let client = createClient({
serverUrl: 'http://localhost:8000',
auth: {
username: process.env.RRELAYER_AUTH_USERNAME!,
password: process.env.RRELAYER_AUTH_PASSWORD!,
},
});
const relayer = await client.getRelayerClient(
// The relayer id you want to connect to
'94afb207-bb47-4392-9229-ba87e4d783cb',
// This is optional it defaults to fast
// As you are using viems native interface, all your tx will be sent at this speed
TransactionSpeed.FAST
);
export provider = new BrowserProvider(relayer.ethereumProvider());
export const signer = await provider.getSigner();
Ethers v6
import { createClient, TransactionSpeed } from 'rrelayer';
import { BrowserProvider } from 'ethers';
import * as dotenv from "dotenv";
dotenv.config();
let client = createClient({
serverUrl: 'http://localhost:8000',
auth: {
username: process.env.RRELAYER_AUTH_USERNAME!,
password: process.env.RRELAYER_AUTH_PASSWORD!,
},
});
const relayer = await client.getRelayerClient(
// The relayer id you want to connect to
'94afb207-bb47-4392-9229-ba87e4d783cb',
// This is optional it defaults to fast
// As you are using viems native interface, all your tx will be sent at this speed
TransactionSpeed.FAST
);
export provider = new BrowserProvider(relayer.ethereumProvider());
export const signer = await provider.getSigner();
Will now use ethers v6 example for the rest of the documentation but it is very similar if not the same.
Sign text
You can sign text with the relayer
import { signer } from './config';
const message = await signer.signMessage('sign me!');
Sign typed data
You can sign typed data with the relayer
import { signer } from './config';
const signature = await signer._signTypedData({
{
name: 'Ether Mail',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
},
{
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' },
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'string' },
],
},
{
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
});
Send transaction
Send a transaction with the relayer
import { signer } from './config';
const hash = await signer.sendTransaction({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n,
});
Write contract
Write a contract with the relayer
import { Contract } from 'ethers';
import { signer } from './config';
import { wagmiAbi } from './abi';
const contract = new Contract(
'0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
wagmiAbi,
signer
);
const result = await contract.mint();