帖子
分享您的知识。
Jul 08, 2025
文章
如何使用 7k SDK 交换代币
在本指南中,我将向您介绍如何安装和使用 @7kprotocol/sdk-ts
###安装
首先,请先安装 SDK 及其所需的依赖项.
- 安装 SDK:
运行以下命令来安装@7kprotocol/sdk-ts
软件包:
npm i @7kprotocol/sdk-ts
```
2. **安装对等依赖关系**:
SDK 需要`@pythnetwork/pyth-sui-js`作为对等依赖关系. 如果你还没有安装,请运行以下命令:
```bash
npm i @pythnetwork/pyth-sui-js
```
###**Sui Client**
要连接Sui区块链,你需要设置Sui客户端. 这可以按如下方式完成:
```tsx
import { SuiClient, getFullnodeUrl } from "@mysten/sui/client";
import { Config } from "@7kprotocol/sdk-ts";
const network = "mainnet"; // Use "mainnet" for production
const suiClient = new SuiClient({ url: getFullnodeUrl(network) });
Config.setSuiClient(suiClient);
console.log("Sui client", Config.getSuiClient());
注意:此 SDK 目前仅支持主网.
###执行交易
该SDK的核心功能之一是能够交换代币和执行交易. 让我们来看看两个关键操作:
###1. 获取掉期报价
要获取代币交换的报价,请使用该getQuote
函数. 这将为您提供交换的详细信息,例如您可以获得的金额和最佳路线.
示例:
import { getQuote } from "@7kprotocol/sdk-ts";
const quoteResponse = await getQuote({
tokenIn: "0x2::sui::SUI", // Token you're swapping from
tokenOut: "0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC", // Token you're swapping to
amountIn: "1000000000", // Amount you're swapping (in smallest unit)
});
您还可以指定要从中提取报价的 DEX 列表. 如果未指定,SDK 将使用最新的支持来源.
const quoteResponse = await getQuote({
tokenIn: "0x2::sui::SUI",
tokenOut: "0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC",
amountIn: "1000000000",
sources: [
"suiswap",
"turbos",
"cetus",
"bluemove",
"kriya",
"kriya_v3",
"aftermath",
"flowx",
], // Optional: list of supported sources
});
###2. 建立交易
获得报价后,下一步就是建立交易. 您可以使用该buildTx
函数创建包含滑点和佣金等必要细节的交易.
import { buildTx } from "@7kprotocol/sdk-ts";
const result = await buildTx({
quoteResponse,
accountAddress: "0xSenderAddress", // Your wallet address
slippage: 0.01, // 1% slippage tolerance
commission: {
partner: "<address to receive fee>", // Partner address for commission
commissionBps: 0, // Commission rate in basis points (0 means no fee)
},
});
const { tx, coinOut } = result || {};
注意:即使CommissionBPS设置为0,您仍需要提供合作伙伴地址以进行跟踪和分析.
###完整示例
以下是将所有内容汇总在一起的完整示例:
import { SuiClient, getFullnodeUrl } from "@mysten/sui/client";
import { getQuote, buildTx, executeTx, BluefinXTx } from "@7kprotocol/sdk-ts";
import { useSignTransaction } from "@mysten/wallet-kit";
// Initialize Sui Client
const network = "mainnet";
const suiClient = new SuiClient({ url: getFullnodeUrl(network) });
Config.setSuiClient(suiClient);
// Get Quote
const quoteResponse = await getQuote({
tokenIn: "0x2::sui::SUI",
tokenOut: "0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC",
amountIn: "1000000000",
sources: ["bluefinx"],
});
// Build Transaction
const { tx } = await buildTx({
quoteResponse,
accountAddress: "0xSenderAddress",
slippage: 0.01,
commission: {
partner: "<address to receive fee>",
commissionBps: 0,
},
});
// Sign Transaction
const { mutateAsync: signTransaction } = useSignTransaction();
const { signature, bytes } = await signTransaction({
transaction: tx instanceof BluefinXTx ? tx.txBytes : tx,
});
// Execute Transaction
const res = await executeTx(tx, signature, bytes);
console.log(res);
- Sui
- SDKs and Developer Tools
1
分享
评论
Sui is a Layer 1 protocol blockchain designed as the first internet-scale programmable blockchain platform.
394帖子554答案

奖励活动七月
- ... SUIMeaning.Sui+22
- ... SUI0xduckmove+17
- ... SUIfomo on Sui+16
- ... SUIMoonBags+11
- ... SUIHaGiang+10
- ... SUI
- ... SUI