Hi I am new to meta transactions.
Here is my use case:
Use Case : I have a ethereum wallet, in that I have USDT(ERC-20). I want to send USDT(ERC-20) from my wallet as meta transaction to my defender relayer. And in defender relayer I want to pay gas fee for my transaction.
Below is my code
const { DefenderRelayProvider, DefenderRelaySigner } = require('@openzeppelin/defender-relay-client/lib/ethers');
const { ethers } = require('ethers');
const Web3 = require('web3');
// Replace with your relayer credentials from OpenZeppelin Defender
const credentials = {
apiKey: 'xxxxxx', // Replace with your API key
apiSecret: 'xxxxxx', // Replace with your API secret
};
const fromAddress = '0x4DE23f3f0Fb3318287378AdbdE030cf61714b2f3'; // Your compromised wallet address
const toAddress = '0x5Ee827cd3F59E10144194b000fFc8Efaaf4bdCf6'; // Destination address
const amount = ethers.parseUnits('1000', 6); // Amount to send (e.g., 100 USDT)
// USDT contract address and ABI
const usdtAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7';
const usdtABI = [
{
"constant": true,
"inputs": [{"name": "who", "type": "address"}],
"name": "balanceOf",
"outputs": [{"name": "", "type": "uint256"}],
"type": "function"
},
{
"constant": false,
"inputs": [
{"name": "_to", "type": "address"},
{"name": "_value", "type": "uint256"}
],
"name": "transfer",
"outputs": [{"name": "", "type": "bool"}],
"type": "function"
}
]
const privateKey = 'xxxxxxx'; // Your compromised wallet private key
async function sendUSDT() {
try {
// Initialize the provider and signer
const provider = new DefenderRelayProvider(credentials);
const signer = new DefenderRelaySigner(credentials, provider, { speed: 'fast' });
// Initialize the USDT contract
const usdtContract = new ethers.Contract(usdtAddress, usdtABI, signer);
// Log the contract methods to ensure 'transfer' exists
console.log("Contract :", usdtContract);
// Ensure the contract has the 'transfer' method
if (typeof usdtContract.transfer !== 'function') {
throw new Error("The USDT contract does not have a 'transfer' method");
}
// Create the meta-transaction
const txData = await usdtContract.populateTransaction.transfer(toAddress, amount);
// Sign the meta-transaction with the compromised wallet's private key
const wallet = new ethers.Wallet(privateKey, provider);
const metaTx = {
to: usdtAddress,
from: fromAddress,
data: txData.data,
value: 0,
gasLimit: 100000, // Estimate or set a reasonable gas limit
nonce: await provider.getTransactionCount(fromAddress, 'pending')
};
// Sign the transaction
const signedTx = await wallet.signTransaction(metaTx);
// Send the meta-transaction to the relayer
const tx = await provider.sendTransaction(signedTx);
console.log("Transaction hash:", tx.hash);
} catch (error) {
console.error('Error sending USDT:', error);
}
}
sendUSDT();
this is the error i am getting.
I have spent almost 3 days on this error. No clue.
Please help me with this.
Note : My defender relayer is working perfect, as I tested sending some ETH from my relayer.
Thanks in Advance