API

To interact with Homa Liquid Staking from Javascript you can use

References

Install SDK

yarn add @polkadot/api @acala-network/api@^4.0.2-17 @acala-network/sdk@^4.0.2-17

Initialize SDK

const { ApiPromise, WsProvider } = require('@polkadot/api')
const { options } = require('@acala-network/api')
const { Wallet, Homa } = require('@acala-network/sdk')

async function main () {
        const ENDPOINT = 'wss://karura.api.onfinality.io/public-ws'

        const api = await ApiPromise.create(options({ provider: new WsProvider(ENDPOINT) }))
        const wallet = new Wallet(api)
        const homa = new Homa(api, wallet)

        // should wait homa sdk ready
        await homa.isReady

        const env = await homa.getEnv();

        // total staked token 
        console.log(env.totalStaking.toString())
        // total L-Token 
        console.log(env.totalLiquidity.toString())
        // estimated staking APY
        console.log(env.apy.toString())
        // exchange rate between L-Token and staked token e.g. rate of LDOT and DOT
        console.log(env.exchangeRate.toString())
        // minimum mint threshold
        console.log(env.mintThreshold.toString())
        // minimum redeem threshold
        console.log(env.redeemThreshold.toString())
        // staking soft cap
        console.log(env.stakingSoftCap.toString())
}

;main()

Queries

Get Total Staked Asset

Total number of assets staked via Homa Liquid Staking protocol

env.totalStaking.toString()

Get Total L-Token Asset

Total number of L-Token assets minted via Homa Liquid Staking protocol

env.totalLiquidity.toString()

Get Exchange Rate

Estimated staking APY

env.apy.toString()

Get Staking APY

Exchange rate between L-Token and staked token e.g. rate of LDOT and DOT

env.exchangeRate.toString()

Minimum Mint Threshold

env.mintThreshold.toString()

Minimum Redeem Threshold

env.redeemThreshold.toString()

Staking Soft Cap

A soft cap for maximum token can be staked

env.stakingSoftCap.toString()

Calculate the amount of Staking Asset for a given amount of L-Token and Vice Versa

...init homa sdk

const {
  convertLiquidToStaking,
  convertStakingToLiquid
} = await homa.getConvertor()

const liquidToken = await wallet.getToken('LKSM')
const stakingToken = await wallet.getToken('KSM')

const liquidAmount = new FixedPointNumber(10, liquidToken.decimals)
const stakingAmount = new FixedPointNumber(10, stakingToken.decimals)

console.log(convertLiquidToStaking(liquidAmount).toString())
console.log(convertStakingToLiquid(stakingAmount).toString())

Stake

...init homa sdk

const keyring = new Keyring({ type: 'sr25519' })
const account = keyring.addFromMnemonic('XXX') // your account mnemonic
const stakingToken = await wallet.getToken('KSM')
const amount = new FixedPointNumber(10, stakingToken.decimals);

const mint = await homa.getEstimateMintResult(amount)

// check pay amount
console.log(mint.pay.toString())
// check receive amount
console.log(mint.receive.toString())

const call = homa.createMintCall(mint.pay)

// send transaction to mint LDOT/LKSM
await call.signAndSend(account)

Fast Redeem

When user stakes, the staking token will be stored in a mint pool before being sent to Relay Chain for staking via XCM. You can call fast redeem to exchange L-Token for staking token from this pool.

Some reference web application would employ the following flow to achieve instant redemption

  • check there's enough staking token in the pool using mint.canTryFastReddem

  • if there's, then swap L-Token to staking token from the mint pool

  • otherwise, swap L-Token to staking token via the Acalaswap DeX

...init homa sdk

const keyring = new Keyring({ type: 'sr25519' })
const account = keyring.addFromMnemonic('XXX') // your account mnemonic
const liquidToken = await wallet.getToken('LKSM')
const amount = new FixedPointNumber(10, liquidToken.decimals);

const redeem = await homa.getEstimateRedeemResult(amount, true)

// check request amount to redeem
console.log(redeem.request.toString())
// check receive amount
console.log(redeem.receive.toString())

if (redeem.canTryFastReddem) {
  const call = homa.createRedeemCall(redeem.request, true, keyring.address)

  // send call to redeem LDOT/LKSM
  await call.signAndSend(account)
}

Normal Redeem

Redeem and wait unbounding period + 1 Era to receive staked tokens. Unbounding period is defined by the staking asset protocol e.g. 28 days unbounding period for DOT and 7 days for KSM.

...init homa sdk

const keyring = new Keyring({ type: 'sr25519' })
const account = keyring.addFromMnemonic('XXX') // your account mnemonic
const liquidToken = await wallet.getToken('LKSM')
const amount = new FixedPointNumber(10, liquidToken.decimals);

const redeem = await homa.getEstimateRedeemResult(amount, false)

// check request amount to redeem
console.log(redeem.request.toString())
// check receive amount
console.log(redeem.receive.toString())

const call = homa.createRedeemCall(redeem.request, false)

// send transaction to redeem LDOT/LKSM
await call.signAndSend(account)

Last updated