NeptuneService is a straightforward tool that interacts with the Neptune CosmWasm smart contracts on Injective. It allows you to fetch asset prices, calculate exchange ratios, create deposit and withdraw messages, and retrieve lending rates.
Example Code Snippets
Below are examples of how to use each method in the NeptuneService class.
Initialize NeptuneService
Before using the service, create an instance of NeptuneService.
import { NeptuneService } from'@injectivelabs/sdk-ts'import { Network } from'@injectivelabs/networks'// Create a NeptuneService instance using the mainnetconstneptuneService=newNeptuneService(Network.MainnetSentry)
Fetch Prices
Get the prices of specific assets from the Neptune Price Oracle contract. Use native_token for bank denoms and token with contract_addr for CW20 tokens.
Calculate the amount in bank USDT from a given amount of CW20 nUSDT using the redemption ratio.
constamountCW20=1000// Amount in nUSDTconstredemptionRatio=0.95// Obtained from fetchRedemptionRatioconstbankAmount=neptuneService.calculateBankAmount( amountCW20, redemptionRatio,)console.log(`Bank USDT Amount: ${bankAmount}`)
Convert Bank USDT to CW20 nUSDT
Calculate the amount in CW20 nUSDT from a given amount of bank USDT using the redemption ratio.
constamountBank=950// Amount in USDTconstredemptionRatio=0.95// Obtained from fetchRedemptionRatioconstcw20Amount=neptuneService.calculateCw20Amount( amountBank, redemptionRatio,)console.log(`CW20 nUSDT Amount: ${cw20Amount}`)
Fetch Lending Rates
Retrieve lending rates for the different lending markets in neptune's lending market smart contract
constlendingRates=awaitneptuneService.getLendingRates({ limit:10,// Optional: number of rates to fetch})console.log(lendingRates)
Fetch Lending Rate by Denomination
Get the lending rate for USDT for example
constdenom='peggy0xdAC17F958D2ee523a2206206994597C13D831ec7'// USDT denomconstlendingRate=awaitneptuneService.getLendingRateByDenom({ denom })if (lendingRate) {console.log(`Lending Rate for USDT: ${lendingRate}`)} else {console.log('Lending Rate for USDT not found')}
Calculate Annual Percentage Yield (APY)
Convert the annual percentage rate (APR) to the continuously compounded annual percentage yield (APY). Make sure to use the lending rate retried from neptuneService.getLendingRateByDenom to use as the apr.
Create a message to deposit USDT into the Neptune USDT lending market and broadcast it to the network.
import { MsgBroadcasterWithPk, MsgExecuteContractCompat,} from'@injectivelabs/sdk-ts'import { BigNumberInBase } from'@injectivelabs/utils'constprivateKey='0x...'constinjectiveAddress='inj1...'constdenom='peggy0xdAC17F958D2ee523a2206206994597C13D831ec7'// USDT denomconstamountInUsdt='100'// Convert the amount to the smallest unit (USDT has 6 decimals)constamount=newBigNumberInBase(amountInUsdt).toWei(6).toFixed()constdepositMsg=neptuneService.createDepositMsg({ denom, amount, sender: injectiveAddress,})consttxHash=awaitnewMsgBroadcasterWithPk({ privateKey, network:Network.MainnetSentry,}).broadcast({ msgs: depositMsg,})console.log(txHash)
Create and Broadcast a Withdraw Message
Create a message to withdraw USDT from the Neptune USDT lending market and broadcast it to the network
import { Network, MsgBroadcasterWithPk, MsgExecuteContractCompat,} from'@injectivelabs/sdk-ts'import { BigNumberInBase } from'@injectivelabs/utils'constprivateKey='0x...'// Your private keyconstinjectiveAddress='inj1...'// Your Injective address// Define the amount to withdraw (e.g., 100 nUSDT)constamountInNusdt='100'// Convert the amount to the smallest unit (nUSDT has 6 decimals)constamount=newBigNumberInBase(amountInNusdt).toWei(6).toFixed()constwithdrawMsg=neptuneService.createWithdrawMsg({ amount, sender: injectiveAddress,})consttxHash=awaitnewMsgBroadcasterWithPk({ privateKey, network:Network.MainnetSentry,}).broadcast({ msgs: withdrawMsg,})console.log(`Transaction Hash: ${txHash}`)