🚆Executing Transactions

Execute transactions to fulfill your users' intents.

Once you know the route you'd like to utilize, you need to execute that transaction

To execute a transaction, you'll need to input the result of the fetchQuote function.

// 1. Fetch quote
const quote = await client.fetchQuote({
      srcChain: 1, // Ethereum
      destChain: 8453, // Base
      srcToken: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', // WETH
      destToken: '0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599', // WBTC
      amount: '1000000000000000000', // 1 ETH
      senderAddress: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
      affiliateWallet: '0x123...',
      affiliateFee: '1',
      slippage: 0.5,
      destinationAddress: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e'
});

// 2. Check if quote is valid
    if (!quote.status) {
      console.error('Invalid quote:', quote.error);
      return;
    }

// 3. Approve Tokens 
const hash = await signer.writeContract({
      address: tokenIn.address,
      abi: erc20Abi,
      functionName: 'approve',
      args: [quote.calldatas.to, maxUint256],
      account: signer.account,
      chain: chain, //srcChain
});

// 4. Estimate Gas 
const estimatedGas = await provider.estimateGas({
      to: quote.calldatas.to,
      data: quote.calldatas.data,
      value: BigInt(quote.calldatas?.value ?? 0),
      account: signer.account,
      blockTag: 'latest',
});

// Increase estimated gas by 20%
const gas = BigInt(Math.ceil(Number(estimatedGas) * 1.2));


// 5. Execute Transaction 
const hash = await signer.sendTransaction({
      to: quote.calldatas.to,
      data: quote.calldatas.data,
      value: BigInt(quote.calldatas?.value ?? 0),
      account: signer.account,
      gas,
      chain,
});

Last updated