The Tonstakers SDK offers an advanced set of tools for developers aiming to incorporate staking functionalities into their applications on the TON blockchain. This updated version introduces a more extensive interaction with the TON ecosystem, including staking operations, balance inquiries, and much more, enhancing your application's capabilities.
- Simplified staking and unstaking operations, including options for maximum stake, instant unstake, and best rate unstake.
- Retrieval of staked, available, TVL (Total Value Locked), and stakers count balances.
- Fetching current and historical APY (Annual Percentage Yield) for staked assets.
- Enhanced API key configuration for improved access limits to the tonapi.
- Event-driven architecture for initialization and deinitialization notifications.
Tonstakers SDK can be easily installed using npm or yarn, or integrated directly into your HTML pages.
npm install tonstakers-sdk
# or
yarn add tonstakers-sdk
For direct HTML integration:
<script src="path/to/tonstakers-sdk.min.js"></script>
Replace "path/to/tonstakers-sdk.min.js"
with the actual SDK path.
Initialize the SDK with your wallet connector (usually, a TonConnect instance) and optional parameters:
import { Tonstakers } from "tonstakers-sdk";
// this is an example connector
import { TonConnectUI } from "@tonconnect/ui";
export const tonConnectUI = new TonConnectUI({
manifestUrl: MANIFEST_URL,
});
const tonstakers = new Tonstakers({
connector: yourWalletConnector, // Your wallet connector
partnerCode: 123456, // Optional partner code
tonApiKey: "YOUR_API_KEY", // Optional API key for tonapi
});
Direct HTML file initialization:
<script src="path/to/tonstakers-sdk.min.js"></script>
<script>
const { Tonstakers } = TonstakersSDK;
const tonstakers = new Tonstakers({
connector: yourWalletConnector,
partnerCode: 123456,
tonApiKey: "YOUR_API_KEY",
});
</script>
tonstakers.addEventListener("initialized", () => {
console.log("Tonstakers SDK initialized successfully.");
});
tonstakers.addEventListener("deinitialized", () => {
console.log("Tonstakers SDK has been deinitialized.");
});
Stake and unstake with new methods:
await tonstakers.stake(1); // Stake 1 TON
await tonstakers.unstake(1); // Unstake 1 tsTON
await tonstakers.stakeMax(); // Stake the maximum available balance
await tonstakers.unstakeInstant(1); // Instant unstake 1 tsTON
await tonstakers.unstakeBestRate(1); // Unstake 1 tsTON at the best available rate
Retrieve information:
const stakedBalance = await tonstakers.getStakedBalance();
console.log(`Current staked balance: ${stakedBalance}`);
const tonBalance = await tonstakers.getBalance();
console.log(`Current user ton balance: ${tonBalance}`);
const availableBalance = await tonstakers.getAvailableBalance();
console.log(`Available balance for staking: ${availableBalance}`);
const currentApy = await tonstakers.getCurrentApy();
console.log(`Current APY: ${currentApy}%`);
const historicalApy = await tonstakers.getHistoricalApy();
console.log(`Historical APY data: ${historicalApy}`);
const tvl = await tonstakers.getTvl();
console.log(`Total Value Locked (TVL): ${tvl}`);
const stakersCount = await tonstakers.getStakersCount();
console.log(`Current number of stakers: ${stakersCount}`);
const rates = await tonstakers.getRates();
console.log(`1 TON = ${rates.TONUSD} USD`);
console.log(`1 tsTON = ${rates.tsTONTON} TON`);
console.log(`Projected 1 tsTON = ${rates.tsTONTONProjected} TON`);
const [cycleStart, cycleEnd] = await tonstakers.getRoundTimestamps();
console.log(`Cycle start: ${cycleStart}, Cycle end: ${cycleEnd}`);
const activeWithdrawals = await tonstakers.getActiveWithdrawalNFTs();
console.log(`Active withdrawal NFTs: ${JSON.stringify(activeWithdrawals)}`);
const instantLiquidity = await tonstakers.getInstantLiquidity();
console.log(`Instant liquidity: ${instantLiquidity}`);
Clear storage data:
await tonstakers.clearStorageData(); // Clear all cached data
await tonstakers.clearStorageUserData(); // Clear cached user-specific data
A demo HTML page is included with the SDK to demonstrate integration into web applications, showcasing wallet connection, staking/unstaking operations, and balance updates.