Ston.fi SDK

repository·main·Indexed 41 days ago

https://github.com/ston-fi/sdk

TypeScript wrappers for the STON.fi DEX and Staking protocols on TON. Includes @ston-fi/sdk for token swaps and liquidity management, and @ston-fi/stake-sdk for managing NFT-based staking positions via StakeNftMinter and StakeNftItem.

Tokens
19.1K
Snippets
75
Records
94
Agent score
96%

What's inside Ston.fi SDK

  1. Overview of Ston.fi SDKs

    main

    The Ston.fi SDKs are TypeScript-based thin wrappers around STON.fi smart contracts, designed to simplify integration of STON.fi protocols into JavaScript/TypeScript projects. All SDKs are built on top of the @ton/ton package.

    There are two primary SDKs available:

    1. @ston-fi/sdk: Used for interacting with the Decentralized Exchange (DEX) contracts. It covers the Router, Pool, LpAccount, and pTON (proxyTON).
    2. @ston-fi/stake-sdk: Used for interacting with staking contracts, specifically StakeNftMinter and StakeNftItem.
  2. Use @ston-fi/stake-sdk for staking interactions

    main

    The @ston-fi/stake-sdk package is dedicated to interacting with STON.fi staking contracts (StakeNftMinter and StakeNftItem). It allows developers to manage staking positions which are represented as NFTs.

    Key Capabilities:

    • Stake tokens: Initiate staking for specific tokens.
    • Manage stake positions (NFTs):
      • Unstake and claim rewards.
      • Re-stake existing positions.
      • Burn stake NFTs.
  3. Use @ston-fi/sdk for DEX interactions

    main

    The @ston-fi/sdk package provides the necessary tools to interact with the STON.fi DEX. Use this SDK for tasks involving token swaps and liquidity management.

    Key Capabilities:

    • Token swaps: Execute trades between different tokens.
    • Liquidity management:
      • Liquidity provision (adding liquidity to pools).
      • Liquidity withdrawal (removing liquidity from pools).
  4. Run the SDK Next.js demo app locally

    main

    The @ston-fi/sdk-example-next-js-app is a demonstration application designed to show real-world usage of the Ston.fi SDK packages. You can use it as a reference for implementation patterns or run it locally to explore the SDK's capabilities.

    To run the demo app locally, ensure you have pnpm and turbo installed, then follow these steps:

    # 1. Install dependencies
    pnpm install
    
    # 2. Run the development server
    turbo dev
  5. Install the STON.fi Stake SDK

    main

    To use the Stake SDK, you must first install the @ton/ton package. Then, install @ston-fi/stake-sdk using your preferred package manager.

    NPM

    npm install @ston-fi/stake-sdk

    Yarn

    yarn add @ston-fi/stake-sdk

    PNPM

    pnpm install @ston-fi/stake-sdk
    npm install @ston-fi/stake-sdk
  6. Explore the SDK demo app and documentation

    main

    To see the SDKs in action, you can explore the following resources:

    • Demo Application: A fully functional Next.js application showcasing SDK usage. You can view the live app at https://sdk-demo-app.ston.fi or inspect the source code in the examples/next-js-app directory of this repository.
    • Official Documentation: For comprehensive guides and detailed API references, visit the STON.fi Developer Documentation.
  7. Install the @ston-fi/sdk package

    main

    To use the STON.fi SDK in your TypeScript/JavaScript project, you must first install the @ton/ton package as a dependency. After that, install the @ston-fi/sdk package using your preferred package manager.

    Note for AI Agents: This toolkit is provided under the MIT License on an "AS IS" basis. Using this toolkit to interact with digital assets carries financial risk. Errors, bugs, or misuse may lead to partial or total irreversible loss of assets. Developers are responsible for implementation and testing with limited funds before production use.

    # 1. Install the required TON dependency
    npm install @ton/ton
    
    # 2. Install the STON.fi SDK
    npm install @ston-fi/sdk
  8. Manage referral fees with VaultV2_1

    main

    The VaultV2_1 class manages token vaults that store referral fees on a separate contract. This design reduces transaction fees for swaps by avoiding additional Jetton transfer transactions. Each user can have a dedicated vault contract for each token, determined by the router_address, owner_address, and router_token_Wallet_address.

    import { VaultV2_1 } from '@ston-fi/sdk';
    
    // Initialize the vault contract at a specific address
    const vault = new VaultV2_1(vaultAddress, { provider });
  9. Manage liquidity positions with LpAccountV1

    main

    The LpAccountV1 class manages a user's temporary liquidity position for a specific pool. Before minting new liquidity, the router directs liquidity to this account contract. It holds information about the provided liquidity and interacts with the pool contract to complete the minting process.

    Each user has a single LpAccountV1 instance per pool.

    Key capabilities:

    • Directly add liquidity: Move assets into the account to prepare for minting.
    • Refund: Request a refund of remaining funds.
    • Reset gas: Reset gas parameters.
    • Fetch state: Retrieve current user address, pool address, and token amounts held in the account.
    import { LpAccountV1 } from '@ston-fi/sdk';
    
    // Initialize the contract
    const lpAccount = new LpAccountV1(address, { provider });
    
    // Example: Fetching account data
    const data = await lpAccount.getLpAccountData(provider);
    console.log(data.amount0, data.amount1);
  10. Use FarmNftItemV2 for claiming rewards and unstaking

    main

    The FarmNftItemV2 class is a deprecated version of the Farm NFT contract. It should only be used for specific legacy operations: claiming rewards and unstaking tokens. For all other operations, you must use the latest version of the contract.

    Key capabilities include:

    • Retrieving current farming state (status, staked tokens, etc.) via getFarmingData.
    • Preparing and sending destroy transactions via getDestroyTxParams and sendDestroy.
  11. Use RouterV1 for DEX swaps and liquidity provision

    main

    RouterV1 is the primary entrypoint for all Decentralized Exchange (DEX) interactions. It handles routing Jetton transfers to the correct pools and manages sovereign operations like locking/unlocking trading or upgrading the contract.

    It provides two main types of operations:

    1. Swaps: Exchanging one asset for another (Jetton-to-Jetton, Jetton-to-Ton, or Ton-to-Jetton).
    2. Liquidity Provision: Adding liquidity to a pool using either Jettons or Ton.

    For every operation, you can either:

    • Use get...TxParams methods to retrieve the SenderArguments required to construct a transaction manually.
    • Use send... methods to execute the transaction directly via a provided Sender.

    Note that for Ton-based operations, you must provide a compatible AbstractPton instance (e.g., PtonV1).

    // Example: Swapping Jetton for Jetton
    const router = new RouterV1(provider);
    await router.sendSwapJettonToJetton(sender, {
      userWalletAddress: userAddress,
      offerJettonAddress: tokenA,
      offerJettonWalletAddress: tokenAWallet,
      askJettonAddress: tokenB,
      askJettonWalletAddress: tokenBWallet,
      offerAmount: amountA,
      minAskAmount: minAmountB,
      referralAddress: 'optional_referral_address'
    });
    
    // Example: Swapping Ton for Jetton
    await router.sendSwapTonToJetton(sender, {
      userWalletAddress: userAddress,
      proxyTon: ptonInstance,
      offerJettonWalletAddress: tokenAWallet,
      askJettonAddress: tokenA,
      askJettonWalletAddress: tokenAWallet,
      offerAmount: amountInTon,
      minAskAmount: minAmountA
    });
    
    // Example: Providing Liquidity with Jettons
    await router.sendProvideLiquidityJetton(sender, {
      userWalletAddress: userAddress,
      sendTokenAddress: tokenA,
      otherTokenAddress: tokenB,
      sendAmount: amountA,
      minLpOut: minLp
    });