Create Client And Authentication
rrelayer has two ways to create clients based on the authentication direction, which is basic auth and API keys; we will explore both below.
Basic Auth
Using the basic authentication which uses the username and password in your api config
import { createClient, TransactionSpeed } from 'rrelayer';
import * as dotenv from 'dotenv';
dotenv.config();
// Client also holds some admin methods in which API keys cannot do
export const client = createClient({
serverUrl: 'http://localhost:8000',
auth: {
username: process.env.RRELAYER_AUTH_USERNAME!,
password: process.env.RRELAYER_AUTH_PASSWORD!,
},
});
// returns a AdminRelayerClient - import { AdminRelayerClient } from 'rrelayer'
export 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 and is a fallback
// You can override this with the transaction request
TransactionSpeed.FAST
);
API Key Auth
Using API keys that have restricted permissions to only use the relayer - docs here
import { createRelayerClient, TransactionSpeed } from 'rrelayer';
// returns a RelayerClient - import { RelayerClient } from 'rrelayer'
export let relayer = createRelayerClient({
serverUrl: 'http://localhost:8000',
relayerId: '94afb207-bb47-4392-9229-ba87e4d783cb',
apiKey: 'YOUR_API_KEY',
// This is optional it defaults to fast and is a fallback
// You can override this with the transaction request
speed: TransactionSpeed.FAST,
});
Rate Limiting
As rrelayer is integrated on the backend, we allow you to rate limit by passing it into the methods which can be rate limited which are sending transaction, replace transaction, cancel transaction, sign a message and sign typed data. You can config this here
// config.ts
import { createClient, TransactionSpeed } from 'rrelayer';
import * as dotenv from "dotenv";
dotenv.config();
const client = createClient({
serverUrl: 'http://localhost:8000',
auth: {
username: process.env.RRELAYER_AUTH_USERNAME!,
password: process.env.RRELAYER_AUTH_PASSWORD!,
},
});
export 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 and is a fallback
// You can override this with the transaction request
TransactionSpeed.FAST
);
...
import { relayer } from './config';
import { parseEther } from 'rrelayer';
const txRequest = {
to: '0xa4635F69E5A64CD48da3FbC999aCed87B00756F6',
value: parseEther('0.001'),
// speed: TransactionSpeed.Super - you can override sending transactions speed like this
};
/// Passing in the user doing the transaction as an example this rate limit key will
/// be limited based on what is configured
let result = await relayer.transaction().send(txRequest, 'user__0x5FCD072a0BD58B6fa413031582E450FE724dba6D');