Note that we provide additional utilities for retrieving available routes, chain details, and token infos.
See here.
3. Execute a quote
After retrieving a quote, you are ready to execute it.
import { useWalletClient } from"wagmi";
const wallet = useWalletClient();
await client.executeQuote({
walletClient: wallet,
deposit: quote.deposit, // returned by `getQuote`onProgress: (progress) => {
if (progress.step === "approve" && progress.status === "txSuccess") {
// if approving an ERC20, you have access to the approval receiptconst { txReceipt } = progress;
}
if (progress.step === "deposit" && progress.status === "txSuccess") {
// once deposit is successful you have access to depositId and the receiptconst { depositId, txReceipt } = progress;
}
if (progress.step === "fill" && progress.status === "txSuccess") {
// if the fill is successful, you have access the following dataconst { fillTxTimestamp, txReceipt, actionSuccess } = progress;
// actionSuccess is a boolean flag, telling us if your cross chain messages were successful
}
},
});
The method will execute a quote by:
Approving the SpokePool contract if necessary
Depositing the input token on the origin chain
Waiting for the deposit to be filled on the destination chain
You can use the onProgress callback to act on different stages of the execution.
Have a look at our example app for a more detailed usage of this method.
Cross-chain message handling
Across enables users to seamlessly interact with your dApp or chain using assets from other chains.
Example: Stake Native ETH on Destination Chain
To stake native ETH in one step:
Bridge WETH to the destination chain, sending the output tokens (WETH) to Across's MulticallHandler contract (since contracts receive WETH).
Unwrap WETH to obtain native ETH.
Stake the ETH on your staking contract.
Note: If your calldata depends on the output amount, you must use the update() function.
1. Craft a cross-chain message
import { typeAmount } from"@across-protocol/app-sdk";
import { encodeFunctionData } from"viem";
import { optimism } from"viem/chains";
// constantsconst userAddress = "0xFoo";
const multicallHandlerOptimism = "0x924a9f036260DdD5808007E1AA95f08eD08aA569";
exportconstWETH_OPTIMISM = {
chain: optimism,
abi: WethAbi,
address: "0x4200000000000000000000000000000000000006",
decimals: 18,
logoUrl:
"https://raw.githubusercontent.com/across-protocol/frontend/master/src/assets/token-logos/weth.svg",
name: "Wrapped Ether",
symbol: "WETH",
} asconst;
exportconstSTAKE_CONTRACT = {
address: "0x733Debf51574c70CfCdb7918F032E16F686bd9f8",
chain: optimism,
abi: StakerContractABI,
} asconst;
// WETH unwrap actionfunctiongenerateUnwrapCallData(wethAmount: Amount) {
returnencodeFunctionData({
abi: WETH_OPTIMISM.abi,
functionName: "withdraw",
args: [BigInt(wethAmount)],
});
}
// STAKE actionfunctiongenerateStakeCallData(userAddress: Address) {
returnencodeFunctionData({
abi: STAKE_CONTRACT.abi,
functionName: "stake",
args: [userAddress],
});
}
const unwrapAndStakeMessage = {
actions: [
{
target: WETH_OPTIMISM.address,
callData: generateUnwrapCallData(inputAmount),
value: 0n,
// we only update the calldata since the unwrap call is non-payable, but we DO care about the output amount.update: (updatedOutputAmount) => {
return {
callData: generateUnwrapCallData(updatedOutputAmount),
};
},
},
{
target: STAKE_CONTRACT.address,
callData: generateStakeCallData(userAddress),
// 🔔 the initial value may be set equal to the output amount. This MUST be updated via the `update()` function below oir this call will fail!value: inputAmount,
// now we MUST update msg.value since this last call is calling a payable function.update: (updatedOutputAmount) => {
return {
value: updatedOutputAmount,
};
},
},
],
fallbackRecipient: userAddress,
};
2. Retrieve a quote
After specifying a cross-chain message, you simply can fetch a quote the same way as a normal bridge
const route = {
originChainId: arbitrum.id,
destinationChainId: optimism.id,
inputToken: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", // WETH arbitrumoutputToken: "0x4200000000000000000000000000000000000006", // WETH optimism
};
const quote = await client.getQuote({
route,
inputAmount: parseEther("1"),
// 🔔 Notice the recipient is not the staking contract itself or even the user, but the contract that will execute our cross chain messagesrecipient: multicallHandlerOptimism,
crossChainMessage: unwrapAndStakeMessage,
});
3. Execute a quote
If the quote is available, you can execute like so
The official SDK for integrating Across bridge into your dapp.
The npm package @across-protocol/app-sdk receives a total of 194 weekly downloads. As such, @across-protocol/app-sdk popularity was classified as not popular.
We found that @across-protocol/app-sdk demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 0 open source maintainers collaborating on the project.
Package last updated on 05 Nov 2024
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.