flash-sdk
v2.22.1
Published
Client to interact with the Flash program on Solana
Downloads
3,587
Readme
Flash SDK
Client SDK for interacting with FLASH.TRADE's smart-contracts
autogenerated docs : https://flash-trade.github.io/flash-sdk-docs/
npm : https://www.npmjs.com/package/flash-sdk
Install
npm i flash-sdk / yarn add flash-sdk
Flash SDK
Client SDK for interacting with FLASH.TRADE's smart-contracts
autogenerated docs : https://flash-trade.github.io/flash-sdk-docs/
npm : https://www.npmjs.com/package/flash-sdk
code : https://github.com/flash-trade/flash-trade-sdk.git
Using the SDK
connect sdk locally
import { AnchorProvider } from "@coral-xyz/anchor";
import { ComputeBudgetProgram , PublicKey, TransactionInstruction } from '@solana/web3.js'
const provider : AnchorProvider = AnchorProvider.local(clusterUrl, {
commitment: "confirmed",
preflightCommitment: "confirmed",
skipPreflight: true
});
const perpClient = new PerpetualsClient(provider, programId);
// flp.1
const POOL_CONFIG = PoolConfig.fromIdsByName('Crypto.1','mainnet-beta')
// flp.2
// const POOL_CONFIG = PoolConfig.fromIdsByName('Virtual.1','mainnet-beta')
// flp.3
// const POOL_CONFIG = PoolConfig.fromIdsByName('Governance.1','mainnet-beta')
// flp.4
// const POOL_CONFIG = PoolConfig.fromIdsByName('Community.1','mainnet-beta')
const poolAddress = POOL_CONFIG.poolAddress.toBase58()
const backupOracleData: any = await (await fetch(`https://beast.flash.trade/api/backup-oracle?poolAddress=${poolAddress}`)).json()
const backUpOracleInstruction = new TransactionInstruction({
keys: backupOracleData.keys,
programId: new PublicKey(backupOracleData.programId),
data: Buffer.from(backupOracleData.data),
})
<!-- to get any token config from symbol-->
const USDC_TOKEN = POOL_CONFIG.tokens.find((t) => t.symbol == 'USDC')!
const BTC_TOKEN = POOL_CONFIG.tokens.find((t) => t.symbol == 'BTC')!
<!-- to get any token config from mint -->
const tokenMint = new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v')
const USDC_TOKEN = POOL_CONFIG.tokens.find((t) => t.mintKey.equals(tokenMint))!
<!-- load ALT -->
await perpClient.loadAddressLookupTable(POOL_CONFIG)
<!-- Add Liquidity (USDC -> sFLP.1 ) -->
const payTokenSymbol = 'USDC'; // 'SOL' , 'BTC', 'ETH'
const { instructions : addLiqInstructions, additionalSigners : addLiqAdditionalSigners } = await perpClient.addLiquidityAndStake(
payTokenSymbol,
tokenAmountIn,
minLpAmountOut, // new BN(0)
POOL_CONFIG
)
const setCULimitIx = ComputeBudgetProgram.setComputeUnitLimit({ units: 600_000 })
const txid = perpClient.sendTransaction([backUpOracleInstruction, setCULimitIx, ...addLiqInstructions ], {
addLiqAdditionalSigners,
alts: perpClient.addressLookupTables,
})
<!--addCompoundingLiquidity (USDC -> FLP.1 ) -->
const payTokenSymbol = 'USDC'; // 'SOL' , 'BTC', 'ETH'
const { instructions : addCompLiqInstructions, additionalSigners : addCompLiqAdditionalSigners } = await perpClient.addCompoundingLiquidity(
tokenAmountIn,
payTokenSymbol,
minLpAmountOut, // new BN(0)
USDC_TOKEN.mintKey,
POOL_CONFIG
)
const setCULimitIx = ComputeBudgetProgram.setComputeUnitLimit({ units: 600_000 })
const txid = perpClient.sendTransaction([backUpOracleInstruction, setCULimitIx, ...addCompLiqInstructions ], {
addCompLiqAdditionalSigners,
alts: perpClient.addressLookupTables,
})
<!-- remove Liquidity (FLP -> USDC) -->
const recieveTokenSymbol = 'USDC'; // 'SOL' , 'BTC', 'ETH'
const { instructions : removeCompLiqInstructions, additionalSigners : removeCompLiqAdditionalSigners } = await await perpClient.removeCompoundingLiquidity(
lpAmountIn,
minTokenAmountOut, // new BN(0)
recieveTokenSymbol,
USDC_TOKEN.mintKey,
POOL_CONFIG,
true
)
const setCULimitIx = ComputeBudgetProgram.setComputeUnitLimit({ units: 600_000 })
const txid = perpClient.sendTransaction([backUpOracleInstruction, setCULimitIx, ...removeCompLiqInstructions ], {
removeCompLiqAdditionalSigners,
alts: perpClient.addressLookupTables,
})
<!-- remove Liquidity (sFLP -> USDC) -->
const recieveTokenSymbol = 'USDC'; // 'SOL' , 'BTC', 'ETH'
const { instructions : removeLiqInstructions, additionalSigners : removeLiqAdditionalSigners } = await await perpClient.removeLiquidity(
recieveTokenSymbol,
lpAmountIn,
minTokenAmountOut, // new BN(0)
POOL_CONFIG
)
const setCULimitIx = ComputeBudgetProgram.setComputeUnitLimit({ units: 600_000 })
const txid = perpClient.sendTransaction([backUpOracleInstruction, setCULimitIx, ...removeLiqInstructions ], {
removeLiqAdditionalSigners,
alts: perpClient.addressLookupTables,
})
<!-- to STAKE sFLP.1 -->
const { instructions: depositStakeInstructions, additionalSigners: depositStakeAdditionalSigners } =
await perpClient.depositStake(.provider.wallet.publicKey, provider.wallet.publicKey, flpDepositAmount, POOL_CONFIG)
const txid = perpClient.sendTransaction([...depositStakeInstructions ], {
depositStakeAdditionalSigners,
alts: perpClient.addressLookupTables,
})
<!-- to UN-STAKE sFLP.1 -->
const { instructions: unstakeInstantInstructions, additionalSigners : unstakeInstantAdditionalSigners } = await perpClient.unstakeInstant('USDC',flpUnstakeAmount,POOL_CONFIG)
const { instructions: withdrawStakeInstructions } = await perpClient.withdrawStake(POOL_CONFIG, false)
const txid = perpClient.sendTransaction([...unstakeInstantInstructions, ...withdrawStakeInstructions ], {
unstakeInstantAdditionalSigners,
alts: perpClient.addressLookupTables,
})
<!-- collect fees -->
const { instructions, additionalSigners } = await perpClient.collectStakeFees('USDC',POOL_CONFIG)
const txid = perpClient.sendTransaction([...instructions ], {
additionalSigners,
alts: perpClient.addressLookupTables,
})
<!-- call any view function like so -->
const compoundingLPTokenPrice = await perpClient.getCompoundingLPTokenPrice(POOL_CONFIG.poolAddress, POOL_CONFIG)