JavaScript Client SDK

Chaos Finance provided a JavaScript package for application developers, making the adoption fast and robust.

To add Chaos Finance Sonic LSD into your application, run this in the terminal from the root directory of the application.

$ npm install @chaosfinance/sonic-lsd --save

Import functions and objects from SDK:

import {
    // settings functions
    register,
    registerChain,
    registerWallet,
    updatePublicKey,
 
    // mutating functions
    stakeSonic,
    unstakeLstSonic,
    withdrawSonic,
    
    // global query functions
    getUserMinStakeAmount,
    getLstApr,
    getLstRate,
    getPlatformFeeCommission,
    getLsdTokenAddress,
    getTotalStakedAmount,
    getTotalLstAmount,
    
    // user query functions
    getUserLstBalance,
    getUserSolBalance,
    getUserSonicBalance,
    getUserStakedAmount,
    getUserWithdrawInfo, WithdrawInfo,
} from "@chaosfinance/sonic-lsd"

Settings Functions

The first step developers should do is register REST endpoint, lsd program configs and wallet related functions. There are three functions involved: register, registerChain and registerWallet. The register function is equivalent to executing both registerChain and registerWallet.

The registerChain doesn't require a connected wallet, but both register and registerWallet do. Therefore, if your app needs to retrieve on-chain status before connecting wallet, you may call registerChain, then you are able to query global infos, such as lst apr, min stake amount, platform fee commission etc. If you got the wallet connected, you can call register or registerWallet to finish the process, so that you can call mutating functions: stake, unstake, withdraw or user information functions.

the lsd program id and stake manager address can be found here: Deployed Contract Addresses

register()

const wallet = // Get the wallet object that user selected
await wallet.connect()
 
register(
    {
        restEndpoint:        'https://api.testnet.sonic.game',
        signTransaction:     (tx) => wallet.signTransaction(tx),
        signAllTransactions: (txs) => wallet.signAllTransactions(txs),
        publicKey:           new PublicKey(wallet.publicKey.toBytes()),
    }, {
        lsdProgramId:        '',
        stakeManagerAddress: '',
    },
    
    // Your project identifier for off-chain analysis. e.g. 'chaos-finance' / 'fomoney'
    // It's recommended to contact Chaos Finance to prevent identifier collisions.
    ''
)

registerChain()

registerChain(
    'https://api.testnet.sonic.game', // REST Endpoint
    '<lsd program id>', // LSD Program ID
    '<stake manager address>', // Stake Manager Address
    
    // Project ID, Your project identifier for off-chain analysis. e.g. 'chaos-finance' / 'fomoney'
    // It's recommended to contact Chaos Finance to prevent identifier collisions.
    ''
)

registerWallet()

const wallet = // Get the wallet object that user selected
await wallet.connect()
 
registerWallet(
    (tx) => wallet.signTransaction(tx),       // sign transaction function
    (txs) => wallet.signAllTransactions(txs), // sign all transactions function
    new PublicKey(wallet.publicKey.toBytes()) // public key
)

updatePublicKey()

Set new public key when account changes.

const newPublicKey = new PublicKey(wallet.publicKey.toBytes()) 
updatePublicKey(newPublicKey)

Mutating Functions

stakeSonic()

Developers should make sure that the stakeAmount is greater or equal to the userMinStakeAmount before sending the stake tx, otherwise it fails.

const stakeAmount = // **get the amount from user input**
const stakeTxHash = await stakeSonic(stakeAmount)
if (stakeTxHash) {
    // handle success logic
} else {
    // alert user with failed messages
}

unstakeLstSonic()

All LST holders are eligible to call unstakeLstSonic function to exchange their LST for SONIC.

const unstakeLstAmount = // **get the amount from user input**
const txHash = await unstakeLstSonic(unstakeLstAmount)
if (txHash) {
    // handle success logic
} else {
    // alert user with failed messages
}

withdrawSonic()

Call this function to withdraw the amount of claimableAmount SONIC that the api getUserWithdrawInfo() returns.

const txHash = await withdrawSonic()
if (txHash) {
    // handle success logic
} else {
    // alert user with failed messages
}

Global Query Functions

getUserMinStakeAmount()

Query the minimum amount of SONIC that users can stake.

const amount = await getUserMinStakeAmount()

getLstApr()

Get the current Annual Percentage Rate (APR) for staking

const currentApr = await getLstApr()
// output: 0.12 = 12%

getLstRate()

Get the lst rate how many SONIC does 1 LST worth.

const rate = await getLstRate()
// output: 1.09  which means 1 LST = 1.09 SONIC

Before staking users can pre calculate the amount of LST they will receive by:

lstAmount = stakeAmount / rate
// e.g. 9.1743(LST) = 10 (SONIC) / 1.09

The unstake amount of SONIC that users will receive can be calculated with:

sonicAmount = unstakeLstAmount * rate
// e.g. 10.9(SONIC) = 10 (LST) * 1.09

getPlatformFeeCommission()

Get the platform's commission rate, expressed with 9 decimal places. For example, a value of 50,000,000 represents a commission rate of 5%.

const commissionRate = getPlatformFeeCommission()

getLsdTokenAddress()

Get the LSD Token mint address and SONIC token mint address.

const lsdTokenAddress = getLsdTokenAddress()
// output: {
//    lsdTokenMintAddress: string,
//    sonicTokenMintAddress: string,
// }

getTotalStakedAmount()

Get the total amount of SONIC staked in the LSD network.

const totalStakedAmount = await getTotalStakedAmount()

getTotalLstAmount()

Get the total amount of LSD tokens in the LSD network.

const totalLstAmount = await getTotalLstAmount()

User Query Functions

getUserLstBalance()

When the stake tx get confirmed, call getUserLstBalance() to get the amount of user's lstSonic.

const amount = await getUserLstBalance()

getUserWithdrawInfo()

After unstake, developers should query withdrawal info. It returns:

  1. overallAmount: the total amount of SONIC the user can withdraw after the waiting period.
  2. claimableAmount: the amount of SONIC that user can currently withdraw.
  3. remainingTimeInSeconds: the number of seconds the user needs to wait before they can call withdrawSonic function.
// export interface WithdrawInfo {
//   overallAmount: string;
//   claimableAmount: string;
//   remainingTimeInSeconds: number;
// }
 
import { getUserWithdrawInfo, WithdrawInfo } from "@chaosfinance/sonic-lsd"
 
const info: WithdrawInfo = await getUserWithdrawInfo()

getUserSolBalance()

Get SOL balance of the current wallet.

const balance = await getUserSolBalance()
// output: 1.25 (unit SOL)

getUserSonicBalance()

Get SONIC balance of the current wallet.

const balance = await getUserSonicBalance()
// output: 1.68 (unit SONIC)

getUserStakedAmount()

Get total amount of SONIC token the current user staked.

const balance = await getUserStakedAmount()
// output: 1.68 (unit SONIC)