Skip to main content

Execute Intent

This guide demonstrates how to execute a cross-chain intent using the SDK. The process is broken down into clear steps, each with a brief explanation.

1. Setup: Import Dependencies and Configure Environment

First, import the required libraries and set up your environment variables, such as your private key and a generic RPC URL.

import { createCrossChainProvider, createProviderExecutor } from "@wonderland/interop-cross-chain";
import { createPublicClient, createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { sepolia } from "viem/chains";

// Private key for the account to send the transactions
const PRIVATE_KEY = "";

// Configure your preferred RPC URL for Sepolia (or any supported chain)
const RPC_URL = process.env.RPC_URL || ""; // e.g., "https://sepolia.infura.io/v3/<API_KEY>"

const privateAccount = privateKeyToAccount(PRIVATE_KEY);

2. Initialize Blockchain Clients

Create public and wallet clients for interacting with the blockchain, using the generic RPC URL.

const publicClient = createPublicClient({
chain: sepolia,
transport: http(RPC_URL),
});

const walletClient = createWalletClient({
chain: sepolia,
transport: http(RPC_URL),
account: privateAccount,
});

3. Set Up Cross-Chain Provider and Executor

Initialize the cross-chain provider and executor, which will handle quoting and executing cross-chain transfers.

const acrossProvider = createCrossChainProvider(
"across",
{ apiUrl: "https://testnet.across.to/api" },
{},
);

const executor = createProviderExecutor({
providers: [acrossProvider],
});

4. Retrieve a Cross-Chain Quote

Request a quote for a cross-chain transfer using OIF GetQuoteRequest format.

const response = await executor.getQuotes({
user: USER_INTEROP_ADDRESS, // user's interop address (binary format)
intent: {
intentType: "oif-swap",
inputs: [
{
user: USER_INTEROP_ADDRESS, // sender's interop address (binary format)
asset: INPUT_TOKEN_INTEROP_ADDRESS, // input token interop address (binary format)
amount: "100000000000000000", // 0.1 in wei
},
],
outputs: [
{
receiver: RECEIVER_INTEROP_ADDRESS, // recipient's interop address (binary format)
asset: OUTPUT_TOKEN_INTEROP_ADDRESS, // output token interop address (binary format)
},
],
swapType: "exact-input",
},
supportedTypes: ["across"],
});

// Check for errors
if (response.errors.length > 0) {
console.error("Errors:", response.errors);
}

// Check if we got quotes
if (response.quotes.length === 0) {
console.error("No quotes available");
return;
}

5. Execute the Cross-Chain Transaction

Execute the quote using your wallet client:

// Select the quote you prefer (e.g., first one)
const quote = response.quotes[0];

if (quote.preparedTransaction) {
console.log("Sending transaction...");
const hash = await walletClient.sendTransaction(quote.preparedTransaction);
console.log("Transaction sent:", hash);

const receipt = await publicClient.waitForTransactionReceipt({ hash });
console.log("Transaction confirmed:", receipt.status === "success" ? "Success" : "Failed");
} else {
console.error("No prepared transaction in quote");
}

6. Run the Main Function

Finally, call the main function to execute the workflow.

const main = async (): Promise<void> => {
// ...all the above steps go here...
};

main().catch(console.error);

Next Step

Learn how to monitor your transaction: Intent Tracking