建在顶部的脚本软件包 整个协议
开始
这个 @across-protocol/app-sdk cross' Smart Contracts 和 Quotes API 上提供了有用的抽象.
学习更多参观我们 文档。 。 。 .
安装
要启动, 请安装 app sdk 及其对等依赖 维姆。 。 。 .
pnpm i @across-protocol/app-sdk viem
快速启动
1. 设立 AcrossClient
首先,你需要设置 AcrossClient 并配置要支持的链条.
import { createAcrossClient } from "@across-protocol/app-sdk";
import { mainnet, optimism, arbitrum } from "viem/chains";
const client = createAcrossClient({
integratorId: "0xdead", // 2-byte hex string
chains: [mainnet, optimism, arbitrum],
});
2. 获取引文
现在,你可以用任意的一对符号来检索给定路线的引文.
import { parseEther } from "viem";
// USDC from Arbitrum -> ETH on Optimism
const route = {
originChainId: arbitrum.id,
destinationChainId: optimism.id,
inputToken: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831", // USDC
outputToken: "0x0000000000000000000000000000000000000000", // Native ETH
};
const swapQuote = await client.getSwapQuote({
route,
amount: parseUnit("10", 6), // USDC decimals
});
请注意,我们提供额外的公用设施,以检索现有的路线、链条细节和信使信息. 见 这里。 。 。 .
3. 执行引文
检索到引文后,你准备执行.
import { useWalletClient } from "wagmi";
const wallet = useWalletClient();
await client.executeSwapQuote({
walletClient: wallet,
swapQuote, // returned by `client.getSwapQuote`
onProgress: (progress) => {
if (progress.step === "approve" && progress.status === "txSuccess") {
// if approving an ERC20, you have access to the approval receipt
const { txReceipt } = progress;
}
if (progress.step === "deposit" && progress.status === "txSuccess") {
// once deposit is successful you have access to depositId and the receipt
const { depositId, txReceipt } = progress;
}
if (progress.step === "fill" && progress.status === "txSuccess") {
// if the fill is successful, you have access the following data
const { fillTxTimestamp, txReceipt, actionSuccess } = progress;
// actionSuccess is a boolean flag, telling us if your cross chain messages were successful
}
},
});
该方法将执行引文来源:
- 核准
SpokePool或者说SpokePoolPeriphery如有必要,合同 - 在原产地链上交存输入令牌(必要时还便利原产地互换)
- 等待在目的地链上填充押金
你可以用这个 onProgress 调回执行的不同阶段.
看看我们 示例应用程序 更详细地使用这种方法.
跨链条处理
交叉使用户能够利用来自其他链条的资产与您的 dApp 或链条进行无缝互动.
示例:在目标链上取取原生ETH
这个例子说明如何使用应用软件sdk 交换,搭桥和置入 本地的ETH 跨越链条. 您只需要指定目的地链动作 .
import { createAcrossClient } from "@across-protocol/app-sdk";
import { mainnet, optimism, arbitrum } from "viem/chains";
import { useWalletClient } from "wagmi";
const wallet = useWalletClient();
// Example Staking contract on OP
const stakingAddress = "0x733Debf51574c70CfCdb7918F032E16F686bd9f8";
// 1. Create client
const client = createAcrossClient({
integratorId: "0xdead", // 2-byte hex string
chains: [mainnet, optimism, arbitrum],
});
// 2. Retrieve quote for USDC from Arbitrum -> ETH on Optimism
const route = {
originChainId: arbitrum.id,
destinationChainId: optimism.id,
inputToken: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831", // USDC
outputToken: "0x0000000000000000000000000000000000000000", // Native ETH
};
const swapQuote = await client.getSwapQuote({
route,
amount: parseUnit("10", 6), // USDC decimals
actions: [
{
// Target contract of destination chain actions
target: stakingAddress,
// Human-readable ABI format of method to call
functionSignature: "function stake(address stakerAddress)",
// Args to above
args: [{ value: wallet.address }],
// Allows to set call value to available balance AFTER bridge
populateCallValueDynamically: true,
},
],
});
// 3. Execute quote
await client.executeSwapQuote({
walletClient: wallet,
swapQuote,
onProgress: (progress) => {
// handle progress
},
});
认证
一些横跨API的特性需要通过API密钥进行认证. 你可以提供 键.
客户端级 API 密钥
const client = createAcrossClient({
integratorId: "0xdead",
chains: [mainnet, optimism, arbitrum],
apiKey: "your-api-key",
});
// All authenticated requests will use this key automatically
const quote = await client.getSwapQuote({ route, amount });
按请求 API 密钥
// Override or provide the key for a single request
const quote = await client.getSwapQuote({
route,
amount,
apiKey: "per-request-key",
});
密钥作为 Bearer 符号在 Authorization 头曰.
交存详情
开始
处理和调试出错
开始
路线、链条和标志细节
开始
@across-protocol/app-sdk 参考文献
详细情况见 这里。 。 。 .