How to use thirdweb engine relayer to send transaction
We will explore how to use the thirdweb Engine Relayer to send transactions from both your frontend and backend.
We will create a relayer on the thirdweb Engine dashboard.
- Navigate to the "Relayer" tab on your engine dashboard and click the "Add Relayer" button
![Notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Ff6d1ff84-8c0e-4bcb-8dd6-edab3b3c175b%2F901d1f52-cac1-4c3c-b1e6-10868b09f718%2FUntitled_(3).png?table=block&id=7630b017-6e70-4c63-a69b-680b309dbd6f&cache=v2)
- Fill out the relayer form, providing details such as the backend wallets to use, trusted forwarders, allowed contracts, and so on. Then, click the "Add" button.
![Notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Ff6d1ff84-8c0e-4bcb-8dd6-edab3b3c175b%2F53af0ba7-90b2-40a7-bf57-f5bcc9e4b478%2FUntitled_(4).png?table=block&id=b5939afe-9a39-456f-8bf1-6a11c9b07081&cache=v2)
- Save the URL of the relayer from the list for future use.
![Notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Ff6d1ff84-8c0e-4bcb-8dd6-edab3b3c175b%2Fbb9ff8c8-51e1-4d0e-859c-4a269046d0ae%2FUntitled_(5).png?table=block&id=500ea557-bf93-4872-bf42-b5184748b78b&cache=v2)
Deploy a contract with trusted forwarders
Next, we need to deploy a smart contract using trusted forwarders to relay the transaction. You can obtain default forwarders for the network during the contract deployment by clicking the "get default" button on the dashboard.
![Notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Ff6d1ff84-8c0e-4bcb-8dd6-edab3b3c175b%2Fc2990f41-4f85-4862-a48d-dae034866aa4%2FUntitled_(6).png?table=block&id=358afe70-f692-455a-bcfe-72d50416f038&cache=v2)
Claim an NFT on the new contract using a relayer on frontend.
import { createThirdwebClient, getContract } from "thirdweb";
import { defineChain } from "thirdweb/chains";
import {
ConnectButton,
TransactionButton,
useActiveAccount,
} from "thirdweb/react";
import { claimTo } from "thirdweb/extensions/erc721";
function NFTMint() {
const activeAccount = useActiveAccount();
const client = createThirdwebClient({
clientId: process.env.NEXT_PUBLIC_THIRDWEB_CLIENT_ID,
});
const contract = getContract({
client,
chain: defineChain(YOUR_CHAIN_ID_HERE),
address: "YOUR_SMART_CONTRACT_ADDRESS",
});
return (
<>
<ConnectButton
client={client}
autoConnect={true}
connectModal={{ size: "wide" }}
/>
<TransactionButton
transaction={() => {
// Create a transaction object and return it
const tx = claimTo({
contract,
to: activeAccount.address,
quantity: 1n,
});
return tx;
}}
onTransactionSent={(result) => {
console.log("Transaction submitted", result.transactionHash);
}}
onTransactionConfirmed={(receipt) => {
console.log("Transaction confirmed", receipt.transactionHash);
}}
onError={(error) => {
console.error("Transaction error", error);
}}
gasless={{
provider: "engine",
relayerUrl: "RELAYER_URL",
relayerForwarderAddress: "TRUSTED_FORWARDER",
}}
>
Confirm Transaction
</TransactionButton>
</>
);
}
export default NFTMint;
Claim an NFT on the new contract using a relayer on backend.
import { defineChain } from "thirdweb/chains";
import { privateKeyToAccount } from "thirdweb/wallets";
import { sendTransaction, getContract, createThirdwebClient } from "thirdweb";
import { claimTo } from "thirdweb/extensions/erc721";
export async function main() {
const client = createThirdwebClient({
secretKey: process.env.THIRDWEB_SECRET_KEY,
});
const account = privateKeyToAccount({
client,
privateKey: process.env.WALLET_PRIVATE_KEY,,
});
const contract = getContract({
client,
chain: defineChain(YOUR_CHAIN_ID_HERE),
address: "YOUR_SMART_CONTRACT_ADDRESS",
});
const tx = claimTo({
contract,
to: "WALLET_TO_CLAIM_NFT_ON",
quantity: 1n,
});
try {
const transactionResult = await sendTransaction({
transaction: tx,
account: account,
gasless:{
provider: "engine",
relayerUrl: "RELAYER_URL",
relayerForwarderAddress: "TRUSTED_FORWARDER",
}
});
} catch (e) {
console.log(e)
}
}
main();
Can’t get this working? If you’ve followed the above and still have issues, contact our support team for help.
Did this answer your question?
😞
😐
🤩