Trader Interactions
Install Dependencies:
yarn add @solana/web3.js @pythnetwork/client @solana/spl-token @coral-xyz/anchor
// setup the flashClient as show previouslySetup Pyth for Pricing:
const connectionFromPyth = new Connection(
'pythnet-provider-url' // can get it from triton
)
const pythClient = new PythHttpClient(connectionFromPyth, getPythProgramKeyForCluster('pythnet'))
// for alternatives see https://docs.pyth.network/price-feeds/use-real-time-data/off-chainFetch Prices From Pyth:
import { BN } from "@coral-xyz/anchor";
import { OraclePrice } from 'flash-sdk';
// POOL_CONFIG as show previously
const getPrices = async () => {
const pythHttpClientResult = await pythClient.getData()
const priceMap = new Map<string, { price: OraclePrice; emaPrice: OraclePrice }>();
for (let token of POOL_CONFIG.tokens) {
const priceData: PriceData = pythHttpClientResult.productPrice.get(token.pythTicker)!
if (!priceData) {
throw new Error(`priceData not found for ${token.symbol}`)
}
const priceOracle = new OraclePrice({
price: new BN(priceData?.aggregate.priceComponent.toString()),
exponent: new BN(priceData?.exponent),
confidence: new BN(priceData?.confidence!),
timestamp: new BN(priceData?.timestamp.toString()),
})
const emaPriceOracle = new OraclePrice({
price: new BN(priceData?.emaPrice.valueComponent.toString()),
exponent: new BN(priceData?.exponent),
confidence: new BN(priceData?.emaConfidence.valueComponent.toString()),
timestamp: new BN(priceData?.timestamp.toString()),
})
priceMap.set(token.symbol, { price: priceOracle, emaPrice: emaPriceOracle })
}
return priceMap;
}Open Position with Same Collateral:
Close a Position and Receive Collateral in the Same Token:
Open position with different Collateral:
Close Position and Receive Collateral in a Different Token:
Set Full or Partial Take Profit or Stop Loss on an Existing Position:
Getting Liquidation Price of Current Active Position
Last updated
Was this helpful?

