import { createWalletClient, createPublicClient, http, type Hex } from "viem";
import { base } from "viem/chains";
// 1. Setup Wallet - Initialize your wallet using your preferred method
const account = {}; // Your wallet account (e.g., privateKeyToAccount, or injected wallet)
const walletClient = createWalletClient({
account,
chain: base,
transport: http(),
});
const publicClient = createPublicClient({
chain: base,
transport: http(),
});
const API_KEY = "YOUR_API_KEY"; // Required for fast fill
// 2. Get a quote from the Relay API
const quoteResponse = await fetch("https://api.relay.link/quote/v2", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${API_KEY}`,
},
body: JSON.stringify({
user: account.address,
originChainId: 8453, // Base
destinationChainId: 42161, // Arbitrum
originCurrency: "0x0000000000000000000000000000000000000000", // ETH
destinationCurrency: "0x0000000000000000000000000000000000000000", // ETH
amount: "100000000000000", // 0.0001 ETH
tradeType: "EXACT_INPUT",
}),
});
const quote = await quoteResponse.json();
const step = quote.steps[0];
const item = step.items[0];
const requestId = step.requestId;
console.log(`Request ID: ${requestId}`);
// 3. Submit the transaction on-chain
const txHash = await walletClient.sendTransaction({
to: item.data.to as Hex,
data: item.data.data as Hex,
value: BigInt(item.data.value),
});
console.log(`Transaction submitted: ${txHash}`);
// 4. Call the Fast Fill API immediately after submitting (before waiting for receipt)
// This accelerates the fill on the destination chain
const fastFillResponse = await fetch("https://api.relay.link/fast-fill", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${API_KEY}`,
},
body: JSON.stringify({
requestId,
}),
});
const fastFillResult = await fastFillResponse.json();
console.log("Fast fill initiated:", fastFillResult);
// 5. Now wait for the transaction receipt (origin chain confirmation)
const receipt = await publicClient.waitForTransactionReceipt({ hash: txHash });
console.log(`Origin transaction confirmed in block ${receipt.blockNumber}`);
// 6. Poll the status API to confirm the fill completed
const checkStatus = async (): Promise<any> => {
const statusResponse = await fetch(
`https://api.relay.link/intents/status/v3?requestId=${requestId}`,
{
headers: { "Authorization": `Bearer ${API_KEY}` },
}
);
return statusResponse.json();
};
// Poll until success
let status = await checkStatus();
while (status.status !== "success" && status.status !== "failure") {
console.log(`Status: ${status.status}`);
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait 1 second
status = await checkStatus();
}
if (status.status === "success") {
console.log("Bridge completed successfully!");
console.log(`Destination tx: ${status.txHashes?.[0]}`);
} else {
console.error("Bridge failed:", status);
}