Trader Interactions

Install Dependencies:

yarn add @solana/web3.js @pythnetwork/client @solana/spl-token @coral-xyz/anchor

// setup the flashClient as show previously

Setup 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-chain

Fetch 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:

NOTE :

  • Stop Loss:

    • Must be above Liquidation Price and below Current Price for LONG

    • Must be below Liquidation Price above Current Price for SHORT

  • Take Profit:

    • Must be above Current Price for LONG

    • Must be below Current Price for SHORT

  • Virtual tokens Take Profit must be below Max Profit Price for LONG

Getting Liquidation Price of Current Active Position

Last updated

Was this helpful?