交易者交互
安装依赖项:
yarn add @solana/web3.js @pythnetwork/client @solana/spl-token @coral-xyz/anchor
// setup the flashClient as show previously设置 Pyth 定价:
const connectionFromPyth = new Connection(
'pythnet-provider-url' // 可以从 triton 获得
)
const pythClient = new PythHttpClient(connectionFromPyth, getPythProgramKeyForCluster('pythnet'))
// 替代方案请参见 https://docs.pyth.network/price-feeds/use-real-time-data/off-chain从 Pyth 获取价格:
import { BN } from "@coral-xyz/anchor";
import { OraclePrice } from 'flash-sdk';
// POOL_CONFIG 如前所示
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(`未找到 ${token.symbol} 的 priceData`)
}
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;
}使用相同抵押品开仓:
平仓并以相同代币接收抵押品:
使用不同抵押品开仓:
平仓并以不同代币接收抵押品:
在现有仓位上设置全部或部分止盈或止损:
获取当前活跃仓位的清算价格
最后更新于
这有帮助吗?

