UNPKG

@blockchainbros/vidar-amm-sdk

Version:
329 lines (328 loc) 14.7 kB
// SDK entrypoint for Vidar AMM import { Program } from "@coral-xyz/anchor"; // Dynamically import the IDL generated by Anchor import idl from "../../target/idl/vidar_amm.json"; export const VidarAmmIDL = idl; /** * Instantiate the Vidar AMM Program client. * @param provider AnchorProvider instance * @param programId Optional program ID, defaults to IDL address */ export function createProgram(provider, programId) { // Clone IDL and override address if custom programId is provided const idlJson = Object.assign({}, idl); if (programId) { idlJson.address = programId.toBase58(); } // In Anchor v0.31+, Program constructor takes (idl, provider) return new Program(idlJson, provider); } /** * Helper to load provider and program in one go. * @param endpoint RPC endpoint URL * @param wallet Wallet implements Anchor Wallet interface * @param commitment Optional commitment level * @param programId Optional program ID override * @returns VidarAmm program instance */ export function loadVidarAmm(provider, programId) { return createProgram(provider, programId); } // Instruction wrappers for convenience /** * Initialize a new pool with given parameters. * @param params.feeBasisPoints - The fee basis points. * @param params.isStable - Whether the pool is stable. * @param accounts.payer - The payer of the transaction. * @param accounts.pool - The pool to initialize. * @param accounts.authority - The authority of the pool. * @param accounts.tokenA - The token A to initialize the pool with. * @param accounts.tokenB - The token B to initialize the pool with. * @param accounts.vaultA - The vault to initialize the pool with. * @param accounts.vaultB - The vault to initialize the pool with. * @param accounts.feeVault - The fee vault to initialize the pool with. * @param accounts.stakingRewardVault - The staking reward vault to initialize the pool with. * @param accounts.lpStakeVault - The LP stake vault to initialize the pool with. * @param accounts.lpMint - The LP mint to initialize the pool with. * @param accounts.systemProgram - The system program. * @returns Transaction instruction to initialize the pool. */ export async function initializePool(program, accounts, params) { return program.methods .initializePool(params.feeBasisPoints, params.isStable) .accounts(accounts) .transaction(); } /** * Create a new liquidity position. * @param accounts.user - The user creating the liquidity position. * @param accounts.pool - The pool to create the liquidity position in. * @param accounts.position - The position to create. * @param accounts.systemProgram - The system program. * @param accounts.rent - The rent program. * @returns Transaction instruction to create a new liquidity position. */ export async function createLiquidityPosition(program, accounts) { return program.methods .createLiquidityPosition() .accounts(accounts) .transaction(); } /** * Add liquidity to a pool. * @param params.amountA - The amount of token A to add liquidity. * @param params.amountB - The amount of token B to add liquidity. * @param accounts.user - The user adding liquidity. * @param accounts.pool - The pool to add liquidity to. * @param accounts.userTokenA - The user's token A to add liquidity to. * @param accounts.userTokenB - The user's token B to add liquidity to. * @param accounts.vaultA - The vault to add liquidity to. * @param accounts.vaultB - The vault to add liquidity to. * @param accounts.lpMint - The LP mint to add liquidity to. * @param accounts.userLpToken - The user's LP token to add liquidity to. * @param accounts.authority - The authority of the pool. * @param accounts.position - The position to add liquidity to. * @param accounts.tokenProgram - The token program. * @param accounts.systemProgram - The system program. * @param accounts.rent - The rent program. */ export async function addLiquidity(program, accounts, params) { return program.methods .addLiquidity(params.amountA, params.amountB) .accounts(accounts) .transaction(); } /** * Remove liquidity from a pool. * @param params.lpAmount - The amount of LP tokens to remove. * @param accounts.user - The user removing liquidity. * @param accounts.pool - The pool to remove liquidity from. * @param accounts.vaultA - The vault to remove liquidity from. * @param accounts.vaultB - The vault to remove liquidity from. * @param accounts.userTokenA - The user's token A to remove liquidity from. * @param accounts.userTokenB - The user's token B to remove liquidity from. * @param accounts.lpMint - The LP mint to remove liquidity from. * @param accounts.userLpToken - The user's LP token to remove liquidity from. * @param accounts.position - The position to remove liquidity from. * @param accounts.authority - The authority of the pool. * @param accounts.tokenProgram - The token program. * @returns Transaction instruction to remove liquidity. */ export async function removeLiquidity(program, accounts, params) { return program.methods .removeLiquidity(params.lpAmount) .accounts(accounts) .transaction(); } /** * Swap tokens in the pool. * @param args.amountIn - The amount of tokens to swap in. * @param args.minAmountOut - The minimum amount of tokens to receive. * @param args.deadline - The deadline for the swap. * @returns Transaction instruction to swap tokens. * @param accounts.user - The user swapping. * @param accounts.pool - The pool to swap in. * @param accounts.userSource - The user's source token to swap. * @param accounts.userDestination - The user's destination token to swap. * @param accounts.vaultSource - The vault to swap from. * @param accounts.vaultDestination - The vault to swap to. * @param accounts.feeVault - The fee vault to swap to. * @param accounts.stakingRewardVault - The staking reward vault to swap to. * @param accounts.authority - The authority of the pool. * @param accounts.vaultSourceMint - The source mint to swap from. * @param accounts.vaultDestinationMint - The destination mint to swap to. * @param accounts.treasuryVault - The treasury vault to swap to. * @param accounts.associatedTokenProgram - The associated token program. * @param accounts.tokenProgram - The token program. * @param accounts.systemProgram - The system program. */ export async function swap(program, accounts, args) { return program.methods.swap(args).accounts(accounts).transaction(); } /** * Reveal a swap commitment. * @param args.nonce - The nonce of the swap. * @param args.commitSlot - The commit slot of the swap. * @param args.amountIn - The amount of tokens to swap in. * @param args.minAmountOut - The minimum amount of tokens to receive. * @param args.deadline - The deadline for the swap. * @param accounts.user - The user revealing. * @param accounts.pool - The pool to reveal to. * @param accounts.userSource - The user's source token to reveal. * @param accounts.userDest - The user's destination token to reveal. * @param accounts.vaultSource - The vault to reveal from. * @param accounts.vaultDest - The vault to reveal to. * @param accounts.feeVault - The fee vault to reveal to. * @param accounts.stakingRewardVault - The staking reward vault to reveal to. * @param accounts.authority - The authority of the pool. * @param accounts.tokenProgram - The token program. * @param accounts.commitIndex - The commit index to reveal to. * @param accounts.commit - The commit to reveal to. * @param accounts.vaultSourceMint - The source mint to reveal to. * @param accounts.vaultDestinationMint - The destination mint to reveal to. * @param accounts.treasuryVault - The treasury vault to reveal to. * @param accounts.associatedTokenProgram - The associated token program. */ export async function swapReveal(program, accounts, args) { return program.methods.swapReveal(args).accounts(accounts).transaction(); } /** * Initialize a new stake account. * @param accounts.user - The user initializing the stake account. * @param accounts.pool - The pool to initialize the stake account in. * @param accounts.userLpToken - The user's LP token to initialize the stake account with. * @param accounts.stakeVault - The stake vault to initialize the stake account with. * @param accounts.authority - The authority of the pool. * @param accounts.stakeAccount - The stake account to initialize. * @param accounts.tokenProgram - The token program. * @param accounts.systemProgram - The system program. * @param accounts.rent - The rent program. * @returns Transaction instruction to initialize a new stake account. */ export async function initializeStake(program, accounts, params) { return program.methods.initializeStake().accounts(accounts).transaction(); } /** * Stake LP tokens into the staking vault. * @param params.amount - The amount of LP tokens to stake. * @param accounts.user - The user staking. * @param accounts.pool - The pool to stake to. * @param accounts.userLpToken - The user's LP token to stake. * @param accounts.stakeVault - The stake vault to stake to. * @param accounts.authority - The authority of the pool. * @param accounts.stakeAccount - The stake account to stake to. * @param accounts.tokenProgram - The token program. * @returns Transaction instruction to stake LP tokens. */ export async function stake(program, accounts, params) { return program.methods.stake(params.amount).accounts(accounts).transaction(); } /** * Unstake LP tokens from the staking vault. * @param params.amount - The amount of LP tokens to unstake. * @param accounts.user - The user unstaking. * @param accounts.pool - The pool to unstake from. * @param accounts.userLpToken - The user's LP token to unstake. * @param accounts.stakeVault - The stake vault to unstake from. * @param accounts.stakeAccount - The stake account to unstake from. * @param accounts.authority - The authority of the pool. * @param accounts.tokenProgram - The token program. */ export async function unstake(program, accounts, params) { return program.methods .unstake(params.amount) .accounts(accounts) .transaction(); } /** * Claim protocol fees from the pool. * @returns Transaction instruction to claim protocol fees. * @param accounts.pool - The pool to claim from. * @param accounts.feeVault - The fee vault to claim from. * @param accounts.treasury - The treasury to claim to. * @param accounts.authority - The authority of the pool. * @param accounts.tokenProgram - The token program. */ export async function claimProtocolFees(program, accounts) { return program.methods.claimProtocolFees().accounts(accounts).transaction(); } /** * Claim rewards from staking. * @returns Transaction instruction to claim rewards. * @param accounts.user - The user claiming. * @param accounts.pool - The pool to claim from. * @param accounts.stakeAccount - The stake account to claim from. * @param accounts.stakingRewardVault - The staking reward vault to claim from. * @param accounts.lpStakeVault - The LP stake vault to claim from. * @param accounts.userRewardAccount - The user's reward account to claim to. * @param accounts.authority - The authority of the pool. * @param accounts.tokenProgram - The token program. */ export async function claimRewards(program, accounts) { return program.methods.claimRewards().accounts(accounts).transaction(); } /** * Claim staking fees from the pool. * @returns Transaction instruction to claim staking fees. * @param accounts.user - The user claiming. * @param accounts.pool - The pool to claim from. * @param accounts.stakeAccount - The stake account to claim from. * @param accounts.stakingRewardVault - The staking reward vault to claim from. * @param accounts.userRewardAccount - The user's reward account to claim to. * @param accounts.authority - The authority of the pool. * @param accounts.tokenProgram - The token program. */ export async function claimStakingFees(program, accounts) { return program.methods.claimStakingFees().accounts(accounts).transaction(); } /** * Emergency withdraw from staking. * @returns Transaction instruction to emergency withdraw from staking. * @param accounts.user - The user withdrawing. * @param accounts.stakeAccount - The stake account to withdraw from. * @param accounts.pool - The pool to withdraw from. * @param accounts.stakeVault - The stake vault to withdraw from. * @param accounts.userLpToken - The user's LP token to withdraw. * @param accounts.authority - The authority of the pool. * @param accounts.tokenProgram - The token program. */ export async function emergencyWithdraw(program, accounts) { return program.methods.emergencyWithdraw().accounts(accounts).transaction(); } /** * Update the internal oracle price. * @param args.source.Manual - The manual price to use. * @param args.source.Twap - The twap price to use. * @returns Transaction instruction to update the internal oracle price. */ export async function updateInternalOracle(program, accounts, args) { return program.methods .updateInternalOracle(args) .accounts(accounts) .transaction(); } /** * Refresh the oracle price. * @param args.poolKey - The pool to refresh the oracle price for. * @param accounts.pool - The pool to refresh the oracle price for. * @param accounts.vaultA - The vault A to refresh the oracle price for. * @param accounts.vaultB - The vault B to refresh the oracle price for. * @returns Transaction instruction to refresh the oracle price. */ export async function refreshOracle(program, accounts, args) { return program.methods.refreshOracle(args).accounts(accounts).transaction(); } /** * Configure the pool. * @param pause 0 = unpaused, 1 = paused. * @param feeBasisPoints - The fee basis points. * @param isStable - Whether the pool is stable. * @param accounts.pool - The pool to configure. * @param accounts.admin - The admin of the pool. * @returns Transaction instruction to configure the pool. */ export async function configurePool(program, accounts, feeBasisPoints, isStable, pause) { return program.methods .configurePool(feeBasisPoints, isStable, pause) .accounts({ pool: accounts.pool, admin: accounts.admin, }) .transaction(); } export default { VidarAmmIDL, createProgram, loadVidarAmm, initializePool, addLiquidity, removeLiquidity, swap, swapReveal, initializeStake, stake, unstake, claimProtocolFees, claimRewards, claimStakingFees, };