UNPKG

@symmetry-hq/liquidity-sdk

Version:

Exchange functionality using symmetry funds liquidity

84 lines (83 loc) 3.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.decodeOraclePrices = exports.decodeFunds = exports.decodeCurveData = exports.decodeTokenList = exports.asciiToString = void 0; const anchor_1 = require("@coral-xyz/anchor"); const client_1 = require("@pythnetwork/client"); function asciiToString(coingeckoIdAscii) { let coingeckoId = ""; for (let i = 0; i < coingeckoIdAscii.length; i++) if (coingeckoIdAscii[i] != 0) coingeckoId += String.fromCharCode(coingeckoIdAscii[i]).toString(); return coingeckoId; } exports.asciiToString = asciiToString; function decodeTokenList(program, tokenListAccountInfo) { let state = program.coder.accounts.decode("tokenList", tokenListAccountInfo.data); let numTokens = state.numTokens.toNumber(); let tokens = []; for (let i = 0; i < numTokens; i++) { let tokenSettings = state.list[i]; tokens.push({ id: i, symbol: "", name: "", tokenMint: tokenSettings.tokenMint.toBase58(), decimals: tokenSettings.decimals, coingeckoId: asciiToString(tokenSettings.coingeckoId), pdaTokenAccount: tokenSettings.pdaTokenAccount.toBase58(), oracleType: tokenSettings.oracleType == 0 ? "Pyth" : "Switchboard", oracleAccount: tokenSettings.oracleAccount.toBase58(), oracleIndex: tokenSettings.oracleIndex, oracleConfidencePct: tokenSettings.oracleConfidencePct, fixedConfidenceBps: tokenSettings.fixedConfidenceBps, tokenSwapFeeBeforeTwBps: tokenSettings.tokenSwapFeeBeforeTwBps, tokenSwapFeeAfterTwBps: tokenSettings.tokenSwapFeeAfterTwBps, isLive: tokenSettings.isLive == 0 ? false : true, lpOn: tokenSettings.lpOn == 0 ? false : true, useCurveData: tokenSettings.useCurveData == 0 ? false : true, additionalData: tokenSettings.additionalData, }); } return tokens; } exports.decodeTokenList = decodeTokenList; function decodeCurveData(program, curveDataAccountInfo) { let state = program.coder.accounts.decode("prismData", curveDataAccountInfo.data); return state; } exports.decodeCurveData = decodeCurveData; function decodeFunds(program, fundStateAccountInfos) { return fundStateAccountInfos.map(account => { return { pubkey: account.pubkey, fund: program.coder.accounts.decode("fundState", account.account.data) }; }); } exports.decodeFunds = decodeFunds; function decodeOraclePrices(oracleDataAccountInfos, tokenListData) { let oraclePrices = []; for (let i = 0; i < oracleDataAccountInfos.length; i++) { let avgPrice = 0; let baseConfidence = 0; if (oracleDataAccountInfos[i] == null) { } else if (tokenListData[i].oracleType == "Pyth") { let parsed = (0, client_1.parsePriceData)(oracleDataAccountInfos[i].data).aggregate; avgPrice = parsed.price * 10 ** 12; baseConfidence = parsed.confidence * 10 ** 12 * tokenListData[i].fixedConfidenceBps / 100; } else { let price = parseInt(new anchor_1.BN(oracleDataAccountInfos[i].data.subarray(tokenListData[i].oracleIndex * 8 + 9, tokenListData[i].oracleIndex * 8 + 17), 10, "le").toString()); baseConfidence = price * tokenListData[i].oracleConfidencePct / 10000; avgPrice = price - baseConfidence; } let additionalConfidence = avgPrice * tokenListData[i].fixedConfidenceBps / 10000; oraclePrices.push({ sell_price: avgPrice - baseConfidence - additionalConfidence, avg_price: avgPrice, buy_price: avgPrice + baseConfidence + additionalConfidence, }); } return oraclePrices; } exports.decodeOraclePrices = decodeOraclePrices;