Post
Share your knowledge.
How to swap token using 7k SDK
In this guide, I will walk you through how to install and use the @7kprotocol/sdk-ts
Installation
To get started, first install the SDK along with its required dependencies.
-
Install the SDK:
Run the following command to install the
@7kprotocol/sdk-tspackage:npm i @7kprotocol/sdk-ts -
Install Peer Dependency:
The SDK requires
@pythnetwork/pyth-sui-jsas a peer dependency. If you don't have it installed yet, run the following command:npm i @pythnetwork/pyth-sui-js
Set Sui Client
To connect with the Sui blockchain, you need to set the Sui client. This can be done as follows:
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());
Note: This SDK currently only supports mainnet.
Performing Transactions
One of the core features of this SDK is the ability to swap tokens and execute transactions. Let’s walk through two key operations:
1. Get Quote for Swap
To get a quote for a token swap, use the getQuote function. This will give you the details of the swap, such as the amount you can receive and the best possible route.
Example:
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)
});
You can also specify a list of DEXs from which to pull quotes. If not specified, the SDK will use the latest supported sources.
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. Build Transaction
Once you have the quote, the next step is to build a transaction. You can use the buildTx function to create the transaction with necessary details like slippage and commission.
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 || {};
Note: Even if commissionBps is set to 0, you still need to provide a partner address for tracking and analytics.
Full Example
Here’s a full example that puts it all together:
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
Sui is a Layer 1 protocol blockchain designed as the first internet-scale programmable blockchain platform.
- How to Maximize Profit Holding SUI: Sui Staking vs Liquid Staking616
- Why does BCS require exact field order for deserialization when Move structs have named fields?65
- Multiple Source Verification Errors" in Sui Move Module Publications - Automated Error Resolution55
- Sui Move Error - Unable to process transaction No valid gas coins found for the transaction419
- Sui Transaction Failing: Objects Reserved for Another Transaction410