UNPKG

@oraichain/oraidex-evm-sdk

Version:
99 lines 4.33 kB
import { MULTICALL_ADDRESS, WASMD_PRECOMPILE_ENTRY, } from '../constants/addresses'; import { assetInfoToDenom, extractJsonFromHexString, parsePoolKey, } from '../utils'; import { IWasmd__factory, Multicall__factory, } from './typechain-types'; export const defaultOraidexPoolDataProviderConfig = { multicallAddress: MULTICALL_ADDRESS, chunkSize: 15, }; export class OraidexPoolDataProvider { constructor(jsonRpcProvider, config) { this.jsonRpcProvider = jsonRpcProvider; const finalConfig = { multicallAddress: config.multicallAddress ?? defaultOraidexPoolDataProviderConfig.multicallAddress ?? MULTICALL_ADDRESS, chunkSize: config.chunkSize ?? defaultOraidexPoolDataProviderConfig.chunkSize ?? 15, }; this.config = finalConfig; this.multicall = Multicall__factory.connect(this.config.multicallAddress, this.jsonRpcProvider); } async getPoolData(args) { const calls = args.map((arg) => { if (arg.protocol === 'Oraidex') { return this.getPoolDataV2Call(arg.poolKey); } else { return this.getPoolDataV3Call(arg.poolKey); } }); const chunks = Array.from({ length: Math.ceil(calls.length / this.config.chunkSize) }, (_, i) => calls.slice(i * this.config.chunkSize, (i + 1) * this.config.chunkSize)); const chunksResult = await Promise.all(chunks.map(async (chunk) => { const result = Array.from(await this.multicall.aggregate.staticCallResult(chunk)); return result[1]; })); const callResult = chunksResult.flat(); if (callResult.length !== args.length) { throw new Error('Call result length does not match arg length'); } const poolData = callResult.map((result, index) => { const poolData = Buffer.from(result.slice(2), 'hex').toString(); const jsonData = extractJsonFromHexString(poolData); if (args[index].protocol === 'Oraidex') { const jsonDataPoolV2Data = jsonData; return { protocol: 'Oraidex', token0: assetInfoToDenom(jsonDataPoolV2Data.assets[0].info), token1: assetInfoToDenom(jsonDataPoolV2Data.assets[1].info), reserve0: jsonDataPoolV2Data.assets[0].amount, reserve1: jsonDataPoolV2Data.assets[1].amount, liquidity: jsonDataPoolV2Data.total_share, }; } else { const jsonDataPoolV3Data = jsonData; const poolKeyObj = parsePoolKey(args[index].poolKey); return { protocol: 'OraidexV3', token0: poolKeyObj.token_x, token1: poolKeyObj.token_y, sqrtPrice: jsonDataPoolV3Data.sqrt_price, liquidity: jsonDataPoolV3Data.liquidity, tick: jsonDataPoolV3Data.current_tick_index.toString(), }; } }); return poolData; } getPoolDataV2Call(poolKey) { const queryInfo = { pool: {}, }; const encodedMsg = IWasmd__factory.createInterface().encodeFunctionData('query', [poolKey, Buffer.from(JSON.stringify(queryInfo))]); return { target: WASMD_PRECOMPILE_ENTRY, callData: encodedMsg, }; } getPoolDataV3Call(poolKey) { const poolKeyInfo = parsePoolKey(poolKey); const queryInfo = { pool: { fee_tier: poolKeyInfo.fee_tier, token_0: poolKeyInfo.token_x, token_1: poolKeyInfo.token_y, }, }; const encodedMsg = IWasmd__factory.createInterface().encodeFunctionData('query', [ OraidexPoolDataProvider.ORAIDEX_V3_ADDRESS, Buffer.from(JSON.stringify(queryInfo)), ]); return { target: WASMD_PRECOMPILE_ENTRY, callData: encodedMsg, }; } } OraidexPoolDataProvider.ORAIDEX_V3_ADDRESS = 'orai10s0c75gw5y5eftms5ncfknw6lzmx0dyhedn75uz793m8zwz4g8zq4d9x9a'; //# sourceMappingURL=OraidexPoolDataProvider.js.map