meng-lending-sdk
v0.1.5
Published
TypeScript SDK for MetEngine Lending
Maintainers
Readme
meng-lending-sdk
TypeScript SDK for the MetEngine Lending Protocol on Solana. Built on @solana/kit.
Installation
npm install meng-lending-sdk @solana/kitFetch & Decode Accounts
The SDK provides codegen fetch helpers and hand-written parsers that decode Pod types into native JS values.
import { fetchLendingReserve, parseLendingReserve } from "meng-lending-sdk"
const reserve = await fetchLendingReserve(rpc, reserveAddress)
const parsed = parseLendingReserve(accountData)
parsed.supplyExchangePrice
parsed.collateralFactorBps
parsed.totalSupplyRawAvailable parsers: parseProtocolState, parseLendingMarket, parseLendingReserve, parseUserAccount, parseRegisteredPool, parseEmissionConfig, parseUserEmission
PDA Derivation
import { getMarketPda, getReservePda, getUserAccountPda, encodeMarketName } from "meng-lending-sdk"
const [marketAddress] = await getMarketPda(encodeMarketName("main"))
const [reserveAddress] = await getReservePda(marketAddress, usdcMint)
const [userAccountAddress] = await getUserAccountPda(marketAddress, walletAddress)Available: getProtocolPda, getMarketPda, getReservePda, getUserAccountPda, getRegisteredPoolPda, getEmissionConfigPda, getUserEmissionPda
Build Instructions
All instructions are generated from the IDL. Each has a sync and async variant (async resolves PDAs automatically).
Deposit Liquidity
import { getDepositReserveLiquidityInstruction } from "meng-lending-sdk"
const ix = getDepositReserveLiquidityInstruction({
market: marketAddress,
reserve: reserveAddress,
liquidityVault,
receiptMint,
depositMint,
userLiquidity: userTokenAccount,
userReceipt: userReceiptAccount,
depositor: signer,
amount: 1_000_000n,
})Redeem Liquidity
import { getRedeemReserveLiquidityInstruction } from "meng-lending-sdk"
const ix = getRedeemReserveLiquidityInstruction({
market: marketAddress,
reserve: reserveAddress,
liquidityVault,
receiptMint,
depositMint,
userLiquidity: userTokenAccount,
userReceipt: userReceiptAccount,
depositor: signer,
receiptAmount: 500_000n,
})Deposit Collateral & Borrow
import {
getInitUserAccountInstructionAsync,
getDepositCollateralInstruction,
getBorrowLiquidityInstruction,
} from "meng-lending-sdk"
const initIx = await getInitUserAccountInstructionAsync({
market: marketAddress,
owner: signer,
})
const depositIx = getDepositCollateralInstruction({
market: marketAddress,
collateralReserve: reserveAddress,
userAccount: userAccountAddress,
liquidityVault,
userCollateral: userTokenAccount,
depositMint,
owner: signer,
amount: 1_000_000n,
})
const borrowIx = getBorrowLiquidityInstruction({
market: marketAddress,
borrowReserve: reserveAddress,
userAccount: userAccountAddress,
liquidityVault,
userLiquidity: userTokenAccount,
depositMint,
owner: signer,
amount: 500_000n,
})Repay & Withdraw
import {
getRepayLiquidityInstruction,
getWithdrawCollateralInstruction,
} from "meng-lending-sdk"
const repayIx = getRepayLiquidityInstruction({
market: marketAddress,
repayReserve: reserveAddress,
userAccount: userAccountAddress,
liquidityVault,
payerLiquidity: payerTokenAccount,
depositMint,
accountOwner: ownerAddress,
payer: signer,
amount: 500_000n,
})
const withdrawIx = getWithdrawCollateralInstruction({
market: marketAddress,
collateralReserve: reserveAddress,
userAccount: userAccountAddress,
liquidityVault,
userCollateral: userTokenAccount,
depositMint,
owner: signer,
amount: 1_000_000n,
})Flash Loans
import {
getFlashBorrowReserveLiquidityInstruction,
getFlashRepayReserveLiquidityInstruction,
} from "meng-lending-sdk"
const borrowIx = getFlashBorrowReserveLiquidityInstruction({
market: marketAddress,
reserve: reserveAddress,
liquidityVault,
depositMint,
userLiquidity: userTokenAccount,
borrower: signer,
amount: 10_000_000n,
})
const repayIx = getFlashRepayReserveLiquidityInstruction({
market: marketAddress,
reserve: reserveAddress,
liquidityVault,
feeVault,
depositMint,
userLiquidity: userTokenAccount,
borrower: signer,
amount: 10_000_000n,
})Liquidation
import {
getLiquidateUserAccountInstruction,
calculateLiquidationBonusBps,
maxRepayValueForTargetHf,
} from "meng-lending-sdk"
const bonus = calculateLiquidationBonusBps(healthFactorBps, minBonus, maxBonus, hfForMaxBonus)
const maxRepay = maxRepayValueForTargetHf(collateralValue, debtValue, bonus, cfBps, targetHf, dust)
const ix = getLiquidateUserAccountInstruction({
market: marketAddress,
repayReserve,
collateralReserve,
userAccount: unhealthyAccount,
repayVault,
collateralVault,
liquidatorRepay: liquidatorRepayAccount,
liquidatorCollateral: liquidatorCollateralAccount,
repayMint,
collateralMint,
insuranceVault,
accountOwner: accountOwnerAddress,
liquidator: signer,
repayAmount: BigInt(maxRepay),
})Emission Rewards
import { getClaimEmissionRewardsInstruction } from "meng-lending-sdk"
const ix = getClaimEmissionRewardsInstruction({
market: marketAddress,
reserve: reserveAddress,
emissionConfig,
userEmission,
emissionVault,
userReceipt: userReceiptAccount,
userRewardAccount,
emissionMint,
user: signer,
})Math Utilities
Client-side math that mirrors on-chain calculations.
import {
calculateHealthFactor,
utilizationBps,
calculateAdaptiveBorrowRate,
accrueInterest,
tokenValueUsd,
} from "meng-lending-sdk"
const hf = calculateHealthFactor(userAccount.totalCollateralValue, userAccount.totalDebtValue)
const util = utilizationBps(
reserve.totalSupplyRaw, reserve.totalBorrowRaw,
reserve.supplyExchangePrice, reserve.borrowExchangePrice,
)
const { borrowRateBps } = calculateAdaptiveBorrowRate(util, reserve.rateModel, reserve.rateAtTargetBps, 60)
const result = accrueInterest(
reserve.supplyExchangePrice, reserve.borrowExchangePrice,
reserve.totalSupplyRaw, reserve.totalBorrowRaw,
BigInt(borrowRateBps), 3600n, waterfallConfig,
)Constants
import {
MENG_LEND_PROGRAM_ADDRESS,
EXCHANGE_PRICE_PRECISION,
BPS,
ReserveStatus,
MAX_USER_COLLATERAL,
MAX_USER_DEBT,
} from "meng-lending-sdk"License
MIT
