@nadohq/engine-client 0.19.1 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{EngineBaseClient-DOOXIhnZ.d.ts → EngineBaseClient-Cn4PMYKt.d.ts} +30 -5
- package/dist/EngineBaseClient-Cn4PMYKt.d.ts.map +1 -0
- package/dist/{EngineBaseClient-L1ue7jBx.d.cts → EngineBaseClient-a2leWTER.d.cts} +30 -5
- package/dist/EngineBaseClient-a2leWTER.d.cts.map +1 -0
- package/dist/{EngineBaseClient-Uc1D8ftx.cjs → EngineBaseClient-zgQmbhJv.cjs} +41 -7
- package/dist/EngineBaseClient-zgQmbhJv.cjs.map +1 -0
- package/dist/EngineBaseClient.cjs +1 -1
- package/dist/EngineBaseClient.d.cts +1 -1
- package/dist/EngineBaseClient.d.ts +1 -1
- package/dist/EngineBaseClient.js +40 -6
- package/dist/EngineBaseClient.js.map +1 -1
- package/dist/EngineClient.cjs +4 -3
- package/dist/EngineClient.cjs.map +1 -1
- package/dist/EngineClient.d.cts +6 -5
- package/dist/EngineClient.d.cts.map +1 -1
- package/dist/EngineClient.d.ts +6 -5
- package/dist/EngineClient.d.ts.map +1 -1
- package/dist/EngineClient.js +3 -2
- package/dist/EngineClient.js.map +1 -1
- package/dist/EngineEdgeQueryClient-B9Y8srJa.d.ts +79 -0
- package/dist/EngineEdgeQueryClient-B9Y8srJa.d.ts.map +1 -0
- package/dist/EngineEdgeQueryClient-CPEDAOd0.d.cts +79 -0
- package/dist/EngineEdgeQueryClient-CPEDAOd0.d.cts.map +1 -0
- package/dist/EngineEdgeQueryClient.cjs +143 -0
- package/dist/EngineEdgeQueryClient.cjs.map +1 -0
- package/dist/EngineEdgeQueryClient.d.cts +2 -0
- package/dist/EngineEdgeQueryClient.d.ts +2 -0
- package/dist/EngineEdgeQueryClient.js +142 -0
- package/dist/EngineEdgeQueryClient.js.map +1 -0
- package/dist/{EngineExecuteBuilder-t8EQ5Rmp.d.ts → EngineExecuteBuilder-BXKoW0uz.d.ts} +2 -2
- package/dist/{EngineExecuteBuilder-t8EQ5Rmp.d.ts.map → EngineExecuteBuilder-BXKoW0uz.d.ts.map} +1 -1
- package/dist/{EngineExecuteBuilder-VLrEndhv.d.cts → EngineExecuteBuilder-BaMHTuf-.d.cts} +2 -2
- package/dist/{EngineExecuteBuilder-VLrEndhv.d.cts.map → EngineExecuteBuilder-BaMHTuf-.d.cts.map} +1 -1
- package/dist/EngineExecuteBuilder.cjs +1 -1
- package/dist/EngineExecuteBuilder.d.cts +1 -1
- package/dist/EngineExecuteBuilder.d.ts +1 -1
- package/dist/{EngineExecuteClient-BGlNK5Le.d.cts → EngineExecuteClient-BLUkw1vR.d.cts} +3 -3
- package/dist/{EngineExecuteClient-BGlNK5Le.d.cts.map → EngineExecuteClient-BLUkw1vR.d.cts.map} +1 -1
- package/dist/{EngineExecuteClient-0EBoXRd7.d.ts → EngineExecuteClient-ReEcySA_.d.ts} +3 -3
- package/dist/{EngineExecuteClient-0EBoXRd7.d.ts.map → EngineExecuteClient-ReEcySA_.d.ts.map} +1 -1
- package/dist/EngineExecuteClient.cjs +1 -1
- package/dist/EngineExecuteClient.d.cts +1 -1
- package/dist/EngineExecuteClient.d.ts +1 -1
- package/dist/{EngineQueryClient-C_7p1oRx.d.cts → EngineQueryClient-Br-BC5LQ.d.cts} +3 -3
- package/dist/{EngineQueryClient-C_7p1oRx.d.cts.map → EngineQueryClient-Br-BC5LQ.d.cts.map} +1 -1
- package/dist/{EngineQueryClient-DQwCRaYl.d.ts → EngineQueryClient-C0xBMTo-.d.ts} +3 -3
- package/dist/{EngineQueryClient-DQwCRaYl.d.ts.map → EngineQueryClient-C0xBMTo-.d.ts.map} +1 -1
- package/dist/EngineQueryClient.cjs +1 -1
- package/dist/EngineQueryClient.d.cts +1 -1
- package/dist/EngineQueryClient.d.ts +1 -1
- package/dist/{EngineWebClient-CEwqgd4t.d.ts → EngineWebClient-KXOdpUol.d.ts} +3 -3
- package/dist/{EngineWebClient-CEwqgd4t.d.ts.map → EngineWebClient-KXOdpUol.d.ts.map} +1 -1
- package/dist/{EngineWebClient-DajZqKAQ.d.cts → EngineWebClient-i8BslipN.d.cts} +3 -3
- package/dist/{EngineWebClient-DajZqKAQ.d.cts.map → EngineWebClient-i8BslipN.d.cts.map} +1 -1
- package/dist/EngineWebClient.cjs +1 -1
- package/dist/EngineWebClient.d.cts +1 -1
- package/dist/EngineWebClient.d.ts +1 -1
- package/dist/{clientQueryTypes-DlTtS_mX.d.cts → clientQueryTypes-BTKfuoGI.d.cts} +39 -2
- package/dist/{clientQueryTypes-DlTtS_mX.d.cts.map → clientQueryTypes-BTKfuoGI.d.cts.map} +1 -1
- package/dist/{clientQueryTypes-DuRgEeBT.d.ts → clientQueryTypes-BaYOTnpm.d.ts} +39 -2
- package/dist/{clientQueryTypes-DuRgEeBT.d.ts.map → clientQueryTypes-BaYOTnpm.d.ts.map} +1 -1
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.js +2 -2
- package/dist/{queryDataMappers-BguxA579.d.ts → queryDataMappers-CyUgQn0_.d.ts} +5 -3
- package/dist/{queryDataMappers-DO9yWdLo.d.cts.map → queryDataMappers-CyUgQn0_.d.ts.map} +1 -1
- package/dist/{queryDataMappers-DO9yWdLo.d.cts → queryDataMappers-kC-SPiHX.d.cts} +5 -3
- package/dist/queryDataMappers-kC-SPiHX.d.cts.map +1 -0
- package/dist/serverEdgeQueryTypes-BpMlbsCD.d.cts +100 -0
- package/dist/serverEdgeQueryTypes-BpMlbsCD.d.cts.map +1 -0
- package/dist/serverEdgeQueryTypes-DbSUWFzo.d.ts +100 -0
- package/dist/serverEdgeQueryTypes-DbSUWFzo.d.ts.map +1 -0
- package/dist/{serverSubscriptionEventTypes-CbT2Fx8Y.d.ts → serverSubscriptionEventTypes-Co3utjCs.d.ts} +1 -1
- package/dist/{serverSubscriptionEventTypes-CbT2Fx8Y.d.ts.map → serverSubscriptionEventTypes-Co3utjCs.d.ts.map} +1 -1
- package/dist/{serverSubscriptionEventTypes-UorThoIE.d.cts → serverSubscriptionEventTypes-CpmdNk1G.d.cts} +1 -1
- package/dist/{serverSubscriptionEventTypes-UorThoIE.d.cts.map → serverSubscriptionEventTypes-CpmdNk1G.d.cts.map} +1 -1
- package/dist/{serverSubscriptionTypes-CE6BIS-C.d.cts → serverSubscriptionTypes-BA92bWic.d.cts} +1 -1
- package/dist/{serverSubscriptionTypes-CE6BIS-C.d.cts.map → serverSubscriptionTypes-BA92bWic.d.cts.map} +1 -1
- package/dist/{serverSubscriptionTypes-Cgz-h0ot.d.ts → serverSubscriptionTypes-CEis5IgT.d.ts} +1 -1
- package/dist/{serverSubscriptionTypes-Cgz-h0ot.d.ts.map → serverSubscriptionTypes-CEis5IgT.d.ts.map} +1 -1
- package/dist/types/clientQueryTypes.d.cts +2 -2
- package/dist/types/clientQueryTypes.d.ts +2 -2
- package/dist/types/index.d.cts +5 -4
- package/dist/types/index.d.ts +5 -4
- package/dist/types/serverEdgeQueryTypes.cjs +0 -0
- package/dist/types/serverEdgeQueryTypes.d.cts +2 -0
- package/dist/types/serverEdgeQueryTypes.d.ts +2 -0
- package/dist/types/serverEdgeQueryTypes.js +1 -0
- package/dist/types/serverSubscriptionEventTypes.d.cts +1 -1
- package/dist/types/serverSubscriptionEventTypes.d.ts +1 -1
- package/dist/types/serverSubscriptionTypes.d.cts +1 -1
- package/dist/types/serverSubscriptionTypes.d.ts +1 -1
- package/dist/utils/index.cjs +1 -0
- package/dist/utils/index.d.cts +2 -2
- package/dist/utils/index.d.ts +2 -2
- package/dist/utils/index.js +2 -2
- package/dist/utils/productEngineTypeMappers.cjs +1 -1
- package/dist/utils/queryDataMappers.cjs +14 -1
- package/dist/utils/queryDataMappers.cjs.map +1 -1
- package/dist/utils/queryDataMappers.d.cts +2 -2
- package/dist/utils/queryDataMappers.d.ts +2 -2
- package/dist/utils/queryDataMappers.js +13 -1
- package/dist/utils/queryDataMappers.js.map +1 -1
- package/package.json +3 -3
- package/src/EngineBaseClient.ts +86 -10
- package/src/EngineClient.ts +3 -1
- package/src/EngineEdgeQueryClient.ts +211 -0
- package/src/types/clientQueryTypes.ts +54 -0
- package/src/types/index.ts +1 -0
- package/src/types/serverEdgeQueryTypes.ts +153 -0
- package/src/utils/queryDataMappers.ts +17 -0
- package/dist/EngineBaseClient-DOOXIhnZ.d.ts.map +0 -1
- package/dist/EngineBaseClient-L1ue7jBx.d.cts.map +0 -1
- package/dist/EngineBaseClient-Uc1D8ftx.cjs.map +0 -1
- package/dist/queryDataMappers-BguxA579.d.ts.map +0 -1
- /package/dist/{index-CmCelmEL.d.cts → index-D6CnpA_r.d.cts} +0 -0
- /package/dist/{index-D0E78WnU.d.ts → index-DhI2LvVQ.d.ts} +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queryDataMappers.cjs","names":["ProductEngineType","mapEngineServerProductType"],"sources":["../../src/utils/queryDataMappers.ts"],"sourcesContent":["import {\n BalanceHealthContributions,\n calcTotalBorrowed,\n calcTotalDeposited,\n mapValues,\n PerpMarket,\n ProductEngineType,\n removeDecimals,\n SpotMarket,\n subaccountFromHex,\n toBigNumber,\n unpackOrderAppendix,\n} from '@nadohq/shared';\nimport {\n EngineMarketPrice,\n EngineNlpLockedBalance,\n EngineOrder,\n EnginePriceTickLiquidity,\n EngineServerIsolatedPositionsResponse,\n EngineServerMarketPrice,\n EngineServerNlpLockedBalancesResponse,\n EngineServerNlpPoolInfoResponse,\n EngineServerOrderResponse,\n EngineServerPerpProduct,\n EngineServerPriceTickLiquidity,\n EngineServerSpotProduct,\n EngineServerSubaccountInfoResponse,\n EngineServerSubaccountInfoState,\n EngineServerSymbol,\n EngineServerSymbolsResponse,\n EngineSymbol,\n EngineSymbolsResponse,\n GetEngineIsolatedPositionsResponse,\n GetEngineNlpLockedBalancesResponse,\n GetEngineNlpPoolInfoResponse,\n GetEngineSubaccountSummaryResponse,\n SubaccountSummaryState,\n} from '../types';\nimport { mapEngineServerProductType } from './productEngineTypeMappers';\n\nexport function mapEngineServerTickLiquidity(\n tick: EngineServerPriceTickLiquidity,\n): EnginePriceTickLiquidity {\n return {\n price: removeDecimals(tick[0]),\n liquidity: toBigNumber(tick[1]),\n };\n}\n\nexport function mapEngineServerOrder(\n order: EngineServerOrderResponse,\n): EngineOrder {\n const subaccount = subaccountFromHex(order.sender);\n return {\n digest: order.digest,\n expiration: Number(order.expiration),\n nonce: order.nonce,\n price: removeDecimals(order.price_x18),\n productId: order.product_id,\n subaccountOwner: subaccount.subaccountOwner,\n subaccountName: subaccount.subaccountName,\n totalAmount: toBigNumber(order.amount),\n unfilledAmount: toBigNumber(order.unfilled_amount),\n placementTime: order.placed_at,\n appendix: unpackOrderAppendix(order.appendix),\n };\n}\n\nexport function mapEngineServerSpotProduct(\n product: EngineServerSpotProduct,\n): SpotMarket {\n return {\n type: ProductEngineType.SPOT,\n productId: product.product_id,\n minSize: toBigNumber(product.book_info.min_size),\n priceIncrement: removeDecimals(product.book_info.price_increment_x18),\n sizeIncrement: toBigNumber(product.book_info.size_increment),\n product: {\n productId: product.product_id,\n type: ProductEngineType.SPOT,\n totalBorrowed: calcTotalBorrowed(\n product.state.total_borrows_normalized,\n product.state.cumulative_borrows_multiplier_x18,\n ),\n totalDeposited: calcTotalDeposited(\n product.state.total_deposits_normalized,\n product.state.cumulative_deposits_multiplier_x18,\n ),\n oraclePrice: removeDecimals(product.oracle_price_x18),\n interestFloor: removeDecimals(product.config.interest_floor_x18),\n interestInflectionUtil: removeDecimals(\n product.config.interest_inflection_util_x18,\n ),\n interestLargeCap: removeDecimals(product.config.interest_large_cap_x18),\n interestSmallCap: removeDecimals(product.config.interest_small_cap_x18),\n minDepositRate: removeDecimals(product.config.min_deposit_rate_x18),\n longWeightInitial: removeDecimals(product.risk.long_weight_initial_x18),\n longWeightMaintenance: removeDecimals(\n product.risk.long_weight_maintenance_x18,\n ),\n shortWeightInitial: removeDecimals(product.risk.short_weight_initial_x18),\n shortWeightMaintenance: removeDecimals(\n product.risk.short_weight_maintenance_x18,\n ),\n tokenAddr: product.config.token,\n },\n };\n}\n\nexport function mapEngineServerPerpProduct(\n product: EngineServerPerpProduct,\n): PerpMarket {\n return {\n type: ProductEngineType.PERP,\n productId: product.product_id,\n minSize: toBigNumber(product.book_info.min_size),\n priceIncrement: removeDecimals(product.book_info.price_increment_x18),\n sizeIncrement: toBigNumber(product.book_info.size_increment),\n product: {\n productId: product.product_id,\n type: ProductEngineType.PERP,\n oraclePrice: removeDecimals(product.oracle_price_x18),\n longWeightInitial: removeDecimals(product.risk.long_weight_initial_x18),\n longWeightMaintenance: removeDecimals(\n product.risk.long_weight_maintenance_x18,\n ),\n shortWeightInitial: removeDecimals(product.risk.short_weight_initial_x18),\n shortWeightMaintenance: removeDecimals(\n product.risk.short_weight_maintenance_x18,\n ),\n openInterest: toBigNumber(product.state.open_interest),\n cumulativeFundingLong: removeDecimals(\n product.state.cumulative_funding_long_x18,\n ),\n cumulativeFundingShort: removeDecimals(\n product.state.cumulative_funding_short_x18,\n ),\n },\n };\n}\n\nexport function mapEngineServerBalanceHealthContributions(\n healthContributionsForBalance: string[],\n): BalanceHealthContributions {\n return {\n initial: toBigNumber(healthContributionsForBalance[0]),\n maintenance: toBigNumber(healthContributionsForBalance[1]),\n unweighted: toBigNumber(healthContributionsForBalance[2]),\n };\n}\n\nexport function mapSubaccountSummary(\n baseResponse: EngineServerSubaccountInfoResponse,\n): GetEngineSubaccountSummaryResponse {\n return {\n exists: baseResponse.exists,\n ...mapSubaccountSummaryState(\n baseResponse,\n baseResponse.spot_products,\n baseResponse.perp_products,\n ),\n preState: baseResponse.pre_state\n ? mapSubaccountSummaryState(\n baseResponse.pre_state,\n baseResponse.spot_products,\n baseResponse.perp_products,\n )\n : undefined,\n };\n}\n\nfunction mapSubaccountSummaryState(\n state: EngineServerSubaccountInfoState,\n spotProducts: EngineServerSubaccountInfoResponse['spot_products'],\n perpProducts: EngineServerSubaccountInfoResponse['perp_products'],\n): SubaccountSummaryState {\n const balances: SubaccountSummaryState['balances'] = [];\n\n state.spot_balances.forEach((spotBalance) => {\n const product = spotProducts.find(\n (product) => product.product_id === spotBalance.product_id,\n );\n if (!product) {\n throw Error(`Could not find product ${spotBalance.product_id}`);\n }\n\n balances.push({\n amount: toBigNumber(spotBalance.balance.amount),\n healthContributions: mapEngineServerBalanceHealthContributions(\n state.health_contributions[spotBalance.product_id],\n ),\n ...mapEngineServerSpotProduct(product).product,\n });\n });\n\n state.perp_balances.forEach((perpBalance) => {\n const product = perpProducts.find(\n (product) => product.product_id === perpBalance.product_id,\n );\n if (!product) {\n throw Error(`Could not find product ${perpBalance.product_id}`);\n }\n\n balances.push({\n amount: toBigNumber(perpBalance.balance.amount),\n vQuoteBalance: toBigNumber(perpBalance.balance.v_quote_balance),\n healthContributions: mapEngineServerBalanceHealthContributions(\n state.health_contributions[perpBalance.product_id],\n ),\n ...mapEngineServerPerpProduct(product).product,\n });\n });\n\n return {\n balances,\n health: {\n initial: {\n health: toBigNumber(state.healths[0].health),\n assets: toBigNumber(state.healths[0].assets),\n liabilities: toBigNumber(state.healths[0].liabilities),\n },\n maintenance: {\n health: toBigNumber(state.healths[1].health),\n assets: toBigNumber(state.healths[1].assets),\n liabilities: toBigNumber(state.healths[1].liabilities),\n },\n unweighted: {\n health: toBigNumber(state.healths[2].health),\n assets: toBigNumber(state.healths[2].assets),\n liabilities: toBigNumber(state.healths[2].liabilities),\n },\n },\n };\n}\n\nexport function mapEngineServerIsolatedPositions(\n baseResponse: EngineServerIsolatedPositionsResponse,\n): GetEngineIsolatedPositionsResponse {\n return baseResponse.isolated_positions.map((position) => {\n const perpBalance = position.base_balance;\n const quoteBalance = position.quote_balance;\n\n return {\n subaccount: subaccountFromHex(position.subaccount),\n healths: {\n initial: toBigNumber(position.healths[0].health),\n maintenance: toBigNumber(position.healths[1].health),\n unweighted: toBigNumber(position.healths[2].health),\n },\n baseBalance: {\n amount: toBigNumber(perpBalance.balance.amount),\n vQuoteBalance: toBigNumber(perpBalance.balance.v_quote_balance),\n // Health contributions === healths for an isolated position\n healthContributions: {\n initial: toBigNumber(position.base_healths[0]),\n maintenance: toBigNumber(position.base_healths[1]),\n unweighted: toBigNumber(position.base_healths[2]),\n },\n ...mapEngineServerPerpProduct(position.base_product).product,\n },\n quoteBalance: {\n amount: toBigNumber(quoteBalance.balance.amount),\n healthContributions: {\n initial: toBigNumber(position.quote_healths[0]),\n maintenance: toBigNumber(position.quote_healths[1]),\n unweighted: toBigNumber(position.quote_healths[2]),\n },\n ...mapEngineServerSpotProduct(position.quote_product).product,\n },\n };\n });\n}\n\nexport function mapEngineServerSymbols(\n baseResponse: EngineServerSymbolsResponse,\n): EngineSymbolsResponse {\n const symbols: Record<string, EngineSymbol> = mapValues(\n baseResponse.symbols,\n mapEngineServerSymbol,\n );\n\n return {\n symbols,\n };\n}\n\nexport function mapEngineServerSymbol(\n engineServerSymbol: EngineServerSymbol,\n): EngineSymbol {\n return {\n type: mapEngineServerProductType(engineServerSymbol.type),\n productId: engineServerSymbol.product_id,\n symbol: engineServerSymbol.symbol,\n priceIncrement: removeDecimals(engineServerSymbol.price_increment_x18),\n sizeIncrement: toBigNumber(engineServerSymbol.size_increment),\n minSize: toBigNumber(engineServerSymbol.min_size),\n makerFeeRate: removeDecimals(engineServerSymbol.maker_fee_rate_x18),\n takerFeeRate: removeDecimals(engineServerSymbol.taker_fee_rate_x18),\n longWeightInitial: removeDecimals(\n engineServerSymbol.long_weight_initial_x18,\n ),\n longWeightMaintenance: removeDecimals(\n engineServerSymbol.long_weight_maintenance_x18,\n ),\n maxOpenInterest: removeDecimals(engineServerSymbol.max_open_interest_x18),\n isolatedOnly: engineServerSymbol.isolated_only,\n };\n}\n\nexport function mapEngineMarketPrice(\n baseResponse: EngineServerMarketPrice,\n): EngineMarketPrice {\n return {\n ask: removeDecimals(baseResponse.ask_x18),\n bid: removeDecimals(baseResponse.bid_x18),\n productId: baseResponse.product_id,\n };\n}\n\nexport function mapEngineServerNlpLockedBalances(\n baseResponse: EngineServerNlpLockedBalancesResponse,\n): GetEngineNlpLockedBalancesResponse {\n const lockedBalances: EngineNlpLockedBalance[] =\n baseResponse.locked_balances.map((lockedBalance) => ({\n productId: lockedBalance.balance.product_id,\n balance: toBigNumber(lockedBalance.balance.balance.amount),\n unlockedAt: lockedBalance.unlocked_at,\n }));\n\n return {\n lockedBalances,\n balanceLocked: {\n productId: baseResponse.balance_locked.product_id,\n balance: toBigNumber(baseResponse.balance_locked.balance.amount),\n },\n balanceUnlocked: {\n productId: baseResponse.balance_unlocked.product_id,\n balance: toBigNumber(baseResponse.balance_unlocked.balance.amount),\n },\n };\n}\n\nexport function mapEngineServerNlpPoolInfo(\n baseResponse: EngineServerNlpPoolInfoResponse,\n): GetEngineNlpPoolInfoResponse {\n return {\n nlpPools: baseResponse.nlp_pools.map((pool) => ({\n poolId: pool.pool_id,\n subaccountHex: pool.subaccount,\n ownerAddress: pool.owner,\n balanceWeight: removeDecimals(pool.balance_weight_x18),\n subaccountInfo: mapSubaccountSummary(pool.subaccount_info),\n openOrders: pool.open_orders.map(mapEngineServerOrder),\n })),\n };\n}\n"],"mappings":";;;;;AAwCA,SAAgB,6BACd,MAC0B;CAC1B,OAAO;EACL,QAAA,GAAA,eAAA,gBAAsB,KAAK,EAAE;EAC7B,YAAA,GAAA,eAAA,aAAuB,KAAK,EAAE;CAChC;AACF;AAEA,SAAgB,qBACd,OACa;CACb,MAAM,cAAA,GAAA,eAAA,mBAA+B,MAAM,MAAM;CACjD,OAAO;EACL,QAAQ,MAAM;EACd,YAAY,OAAO,MAAM,UAAU;EACnC,OAAO,MAAM;EACb,QAAA,GAAA,eAAA,gBAAsB,MAAM,SAAS;EACrC,WAAW,MAAM;EACjB,iBAAiB,WAAW;EAC5B,gBAAgB,WAAW;EAC3B,cAAA,GAAA,eAAA,aAAyB,MAAM,MAAM;EACrC,iBAAA,GAAA,eAAA,aAA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AA,eAAA,gBAA6B,mBAAmB,kBAAkB;EAClE,eAAA,GAAA,eAAA,gBAA6B,mBAAmB,kBAAkB;EAClE,oBAAA,GAAA,eAAA,gBACE,mBAAmB,uBACrB;EACA,wBAAA,GAAA,eAAA,gBACE,mBAAmB,2BACrB;EACA,kBAAA,GAAA,eAAA,gBAAgC,mBAAmB,qBAAqB;EACxE,cAAc,mBAAmB;CACnC;AACF;AAEA,SAAgB,qBACd,cACmB;CACnB,OAAO;EACL,MAAA,GAAA,eAAA,gBAAoB,aAAa,OAAO;EACxC,MAAA,GAAA,eAAA,gBAAoB,aAAa,OAAO;EACxC,WAAW,aAAa;CAC1B;AACF;AAEA,SAAgB,iCACd,cACoC;CAQpC,OAAO;EACL,gBAPA,aAAa,gBAAgB,KAAK,mBAAmB;GACnD,WAAW,cAAc,QAAQ;GACjC,UAAA,GAAA,eAAA,aAAqB,cAAc,QAAQ,QAAQ,MAAM;GACzD,YAAY,cAAc;EAC5B,EAGa;EACb,eAAe;GACb,WAAW,aAAa,eAAe;GACvC,UAAA,GAAA,eAAA,aAAqB,aAAa,eAAe,QAAQ,MAAM;EACjE;EACA,iBAAiB;GACf,WAAW,aAAa,iBAAiB;GACzC,UAAA,GAAA,eAAA,aAAqB,aAAa,iBAAiB,QAAQ,MAAM;EACnE;CACF;AACF;AAEA,SAAgB,2BACd,cAC8B;CAC9B,OAAO,EACL,UAAU,aAAa,UAAU,KAAK,UAAU;EAC9C,QAAQ,KAAK;EACb,eAAe,KAAK;EACpB,cAAc,KAAK;EACnB,gBAAA,GAAA,eAAA,gBAA8B,KAAK,kBAAkB;EACrD,gBAAgB,qBAAqB,KAAK,eAAe;EACzD,YAAY,KAAK,YAAY,IAAI,oBAAoB;CACvD,EAAE,EACJ;AACF"}
|
|
1
|
+
{"version":3,"file":"queryDataMappers.cjs","names":["ProductEngineType","mapEngineServerProductType"],"sources":["../../src/utils/queryDataMappers.ts"],"sourcesContent":["import {\n BalanceHealthContributions,\n calcTotalBorrowed,\n calcTotalDeposited,\n mapValues,\n PerpMarket,\n ProductEngineType,\n removeDecimals,\n SpotMarket,\n subaccountFromHex,\n toBigNumber,\n unpackOrderAppendix,\n} from '@nadohq/shared';\nimport {\n EngineMarketPrice,\n EngineNlpLockedBalance,\n EngineOrder,\n EnginePriceTickLiquidity,\n EngineServerCachedBboHistoryResponse,\n EngineServerIsolatedPositionsResponse,\n EngineServerMarketPrice,\n EngineServerNlpLockedBalancesResponse,\n EngineServerNlpPoolInfoResponse,\n EngineServerOrderResponse,\n EngineServerPerpProduct,\n EngineServerPriceTickLiquidity,\n EngineServerSpotProduct,\n EngineServerSubaccountInfoResponse,\n EngineServerSubaccountInfoState,\n EngineServerSymbol,\n EngineServerSymbolsResponse,\n EngineSymbol,\n EngineSymbolsResponse,\n GetEngineCachedBboHistoryResponse,\n GetEngineIsolatedPositionsResponse,\n GetEngineNlpLockedBalancesResponse,\n GetEngineNlpPoolInfoResponse,\n GetEngineSubaccountSummaryResponse,\n SubaccountSummaryState,\n} from '../types';\nimport { mapEngineServerProductType } from './productEngineTypeMappers';\n\nexport function mapEngineServerTickLiquidity(\n tick: EngineServerPriceTickLiquidity,\n): EnginePriceTickLiquidity {\n return {\n price: removeDecimals(tick[0]),\n liquidity: toBigNumber(tick[1]),\n };\n}\n\nexport function mapEngineServerOrder(\n order: EngineServerOrderResponse,\n): EngineOrder {\n const subaccount = subaccountFromHex(order.sender);\n return {\n digest: order.digest,\n expiration: Number(order.expiration),\n nonce: order.nonce,\n price: removeDecimals(order.price_x18),\n productId: order.product_id,\n subaccountOwner: subaccount.subaccountOwner,\n subaccountName: subaccount.subaccountName,\n totalAmount: toBigNumber(order.amount),\n unfilledAmount: toBigNumber(order.unfilled_amount),\n placementTime: order.placed_at,\n appendix: unpackOrderAppendix(order.appendix),\n };\n}\n\nexport function mapEngineServerSpotProduct(\n product: EngineServerSpotProduct,\n): SpotMarket {\n return {\n type: ProductEngineType.SPOT,\n productId: product.product_id,\n minSize: toBigNumber(product.book_info.min_size),\n priceIncrement: removeDecimals(product.book_info.price_increment_x18),\n sizeIncrement: toBigNumber(product.book_info.size_increment),\n product: {\n productId: product.product_id,\n type: ProductEngineType.SPOT,\n totalBorrowed: calcTotalBorrowed(\n product.state.total_borrows_normalized,\n product.state.cumulative_borrows_multiplier_x18,\n ),\n totalDeposited: calcTotalDeposited(\n product.state.total_deposits_normalized,\n product.state.cumulative_deposits_multiplier_x18,\n ),\n oraclePrice: removeDecimals(product.oracle_price_x18),\n interestFloor: removeDecimals(product.config.interest_floor_x18),\n interestInflectionUtil: removeDecimals(\n product.config.interest_inflection_util_x18,\n ),\n interestLargeCap: removeDecimals(product.config.interest_large_cap_x18),\n interestSmallCap: removeDecimals(product.config.interest_small_cap_x18),\n minDepositRate: removeDecimals(product.config.min_deposit_rate_x18),\n longWeightInitial: removeDecimals(product.risk.long_weight_initial_x18),\n longWeightMaintenance: removeDecimals(\n product.risk.long_weight_maintenance_x18,\n ),\n shortWeightInitial: removeDecimals(product.risk.short_weight_initial_x18),\n shortWeightMaintenance: removeDecimals(\n product.risk.short_weight_maintenance_x18,\n ),\n tokenAddr: product.config.token,\n },\n };\n}\n\nexport function mapEngineServerPerpProduct(\n product: EngineServerPerpProduct,\n): PerpMarket {\n return {\n type: ProductEngineType.PERP,\n productId: product.product_id,\n minSize: toBigNumber(product.book_info.min_size),\n priceIncrement: removeDecimals(product.book_info.price_increment_x18),\n sizeIncrement: toBigNumber(product.book_info.size_increment),\n product: {\n productId: product.product_id,\n type: ProductEngineType.PERP,\n oraclePrice: removeDecimals(product.oracle_price_x18),\n longWeightInitial: removeDecimals(product.risk.long_weight_initial_x18),\n longWeightMaintenance: removeDecimals(\n product.risk.long_weight_maintenance_x18,\n ),\n shortWeightInitial: removeDecimals(product.risk.short_weight_initial_x18),\n shortWeightMaintenance: removeDecimals(\n product.risk.short_weight_maintenance_x18,\n ),\n openInterest: toBigNumber(product.state.open_interest),\n cumulativeFundingLong: removeDecimals(\n product.state.cumulative_funding_long_x18,\n ),\n cumulativeFundingShort: removeDecimals(\n product.state.cumulative_funding_short_x18,\n ),\n },\n };\n}\n\nexport function mapEngineServerBalanceHealthContributions(\n healthContributionsForBalance: string[],\n): BalanceHealthContributions {\n return {\n initial: toBigNumber(healthContributionsForBalance[0]),\n maintenance: toBigNumber(healthContributionsForBalance[1]),\n unweighted: toBigNumber(healthContributionsForBalance[2]),\n };\n}\n\nexport function mapSubaccountSummary(\n baseResponse: EngineServerSubaccountInfoResponse,\n): GetEngineSubaccountSummaryResponse {\n return {\n exists: baseResponse.exists,\n ...mapSubaccountSummaryState(\n baseResponse,\n baseResponse.spot_products,\n baseResponse.perp_products,\n ),\n preState: baseResponse.pre_state\n ? mapSubaccountSummaryState(\n baseResponse.pre_state,\n baseResponse.spot_products,\n baseResponse.perp_products,\n )\n : undefined,\n };\n}\n\nfunction mapSubaccountSummaryState(\n state: EngineServerSubaccountInfoState,\n spotProducts: EngineServerSubaccountInfoResponse['spot_products'],\n perpProducts: EngineServerSubaccountInfoResponse['perp_products'],\n): SubaccountSummaryState {\n const balances: SubaccountSummaryState['balances'] = [];\n\n state.spot_balances.forEach((spotBalance) => {\n const product = spotProducts.find(\n (product) => product.product_id === spotBalance.product_id,\n );\n if (!product) {\n throw Error(`Could not find product ${spotBalance.product_id}`);\n }\n\n balances.push({\n amount: toBigNumber(spotBalance.balance.amount),\n healthContributions: mapEngineServerBalanceHealthContributions(\n state.health_contributions[spotBalance.product_id],\n ),\n ...mapEngineServerSpotProduct(product).product,\n });\n });\n\n state.perp_balances.forEach((perpBalance) => {\n const product = perpProducts.find(\n (product) => product.product_id === perpBalance.product_id,\n );\n if (!product) {\n throw Error(`Could not find product ${perpBalance.product_id}`);\n }\n\n balances.push({\n amount: toBigNumber(perpBalance.balance.amount),\n vQuoteBalance: toBigNumber(perpBalance.balance.v_quote_balance),\n healthContributions: mapEngineServerBalanceHealthContributions(\n state.health_contributions[perpBalance.product_id],\n ),\n ...mapEngineServerPerpProduct(product).product,\n });\n });\n\n return {\n balances,\n health: {\n initial: {\n health: toBigNumber(state.healths[0].health),\n assets: toBigNumber(state.healths[0].assets),\n liabilities: toBigNumber(state.healths[0].liabilities),\n },\n maintenance: {\n health: toBigNumber(state.healths[1].health),\n assets: toBigNumber(state.healths[1].assets),\n liabilities: toBigNumber(state.healths[1].liabilities),\n },\n unweighted: {\n health: toBigNumber(state.healths[2].health),\n assets: toBigNumber(state.healths[2].assets),\n liabilities: toBigNumber(state.healths[2].liabilities),\n },\n },\n };\n}\n\nexport function mapEngineServerIsolatedPositions(\n baseResponse: EngineServerIsolatedPositionsResponse,\n): GetEngineIsolatedPositionsResponse {\n return baseResponse.isolated_positions.map((position) => {\n const perpBalance = position.base_balance;\n const quoteBalance = position.quote_balance;\n\n return {\n subaccount: subaccountFromHex(position.subaccount),\n healths: {\n initial: toBigNumber(position.healths[0].health),\n maintenance: toBigNumber(position.healths[1].health),\n unweighted: toBigNumber(position.healths[2].health),\n },\n baseBalance: {\n amount: toBigNumber(perpBalance.balance.amount),\n vQuoteBalance: toBigNumber(perpBalance.balance.v_quote_balance),\n // Health contributions === healths for an isolated position\n healthContributions: {\n initial: toBigNumber(position.base_healths[0]),\n maintenance: toBigNumber(position.base_healths[1]),\n unweighted: toBigNumber(position.base_healths[2]),\n },\n ...mapEngineServerPerpProduct(position.base_product).product,\n },\n quoteBalance: {\n amount: toBigNumber(quoteBalance.balance.amount),\n healthContributions: {\n initial: toBigNumber(position.quote_healths[0]),\n maintenance: toBigNumber(position.quote_healths[1]),\n unweighted: toBigNumber(position.quote_healths[2]),\n },\n ...mapEngineServerSpotProduct(position.quote_product).product,\n },\n };\n });\n}\n\nexport function mapEngineServerSymbols(\n baseResponse: EngineServerSymbolsResponse,\n): EngineSymbolsResponse {\n const symbols: Record<string, EngineSymbol> = mapValues(\n baseResponse.symbols,\n mapEngineServerSymbol,\n );\n\n return {\n symbols,\n };\n}\n\nexport function mapEngineServerSymbol(\n engineServerSymbol: EngineServerSymbol,\n): EngineSymbol {\n return {\n type: mapEngineServerProductType(engineServerSymbol.type),\n productId: engineServerSymbol.product_id,\n symbol: engineServerSymbol.symbol,\n priceIncrement: removeDecimals(engineServerSymbol.price_increment_x18),\n sizeIncrement: toBigNumber(engineServerSymbol.size_increment),\n minSize: toBigNumber(engineServerSymbol.min_size),\n makerFeeRate: removeDecimals(engineServerSymbol.maker_fee_rate_x18),\n takerFeeRate: removeDecimals(engineServerSymbol.taker_fee_rate_x18),\n longWeightInitial: removeDecimals(\n engineServerSymbol.long_weight_initial_x18,\n ),\n longWeightMaintenance: removeDecimals(\n engineServerSymbol.long_weight_maintenance_x18,\n ),\n maxOpenInterest: removeDecimals(engineServerSymbol.max_open_interest_x18),\n isolatedOnly: engineServerSymbol.isolated_only,\n };\n}\n\nexport function mapEngineMarketPrice(\n baseResponse: EngineServerMarketPrice,\n): EngineMarketPrice {\n return {\n ask: removeDecimals(baseResponse.ask_x18),\n bid: removeDecimals(baseResponse.bid_x18),\n productId: baseResponse.product_id,\n };\n}\n\nexport function mapEngineBboHistory(\n baseResponse: EngineServerCachedBboHistoryResponse,\n): GetEngineCachedBboHistoryResponse {\n return {\n intervalMs: baseResponse.interval_ms,\n limit: baseResponse.limit,\n history: baseResponse.history.map((item) => ({\n productId: item.product_id,\n timestamp: Number(item.timestamp),\n bid: removeDecimals(item.bid),\n ask: removeDecimals(item.ask),\n })),\n };\n}\n\nexport function mapEngineServerNlpLockedBalances(\n baseResponse: EngineServerNlpLockedBalancesResponse,\n): GetEngineNlpLockedBalancesResponse {\n const lockedBalances: EngineNlpLockedBalance[] =\n baseResponse.locked_balances.map((lockedBalance) => ({\n productId: lockedBalance.balance.product_id,\n balance: toBigNumber(lockedBalance.balance.balance.amount),\n unlockedAt: lockedBalance.unlocked_at,\n }));\n\n return {\n lockedBalances,\n balanceLocked: {\n productId: baseResponse.balance_locked.product_id,\n balance: toBigNumber(baseResponse.balance_locked.balance.amount),\n },\n balanceUnlocked: {\n productId: baseResponse.balance_unlocked.product_id,\n balance: toBigNumber(baseResponse.balance_unlocked.balance.amount),\n },\n };\n}\n\nexport function mapEngineServerNlpPoolInfo(\n baseResponse: EngineServerNlpPoolInfoResponse,\n): GetEngineNlpPoolInfoResponse {\n return {\n nlpPools: baseResponse.nlp_pools.map((pool) => ({\n poolId: pool.pool_id,\n subaccountHex: pool.subaccount,\n ownerAddress: pool.owner,\n balanceWeight: removeDecimals(pool.balance_weight_x18),\n subaccountInfo: mapSubaccountSummary(pool.subaccount_info),\n openOrders: pool.open_orders.map(mapEngineServerOrder),\n })),\n };\n}\n"],"mappings":";;;;;AA0CA,SAAgB,6BACd,MAC0B;CAC1B,OAAO;EACL,QAAA,GAAA,eAAA,gBAAsB,KAAK,EAAE;EAC7B,YAAA,GAAA,eAAA,aAAuB,KAAK,EAAE;CAChC;AACF;AAEA,SAAgB,qBACd,OACa;CACb,MAAM,cAAA,GAAA,eAAA,mBAA+B,MAAM,MAAM;CACjD,OAAO;EACL,QAAQ,MAAM;EACd,YAAY,OAAO,MAAM,UAAU;EACnC,OAAO,MAAM;EACb,QAAA,GAAA,eAAA,gBAAsB,MAAM,SAAS;EACrC,WAAW,MAAM;EACjB,iBAAiB,WAAW;EAC5B,gBAAgB,WAAW;EAC3B,cAAA,GAAA,eAAA,aAAyB,MAAM,MAAM;EACrC,iBAAA,GAAA,eAAA,aAA4B,MAAM,eAAe;EACjD,eAAe,MAAM;EACrB,WAAA,GAAA,eAAA,qBAA8B,MAAM,QAAQ;CAC9C;AACF;AAEA,SAAgB,2BACd,SACY;CACZ,OAAO;EACL,MAAMA,eAAAA,kBAAkB;EACxB,WAAW,QAAQ;EACnB,UAAA,GAAA,eAAA,aAAqB,QAAQ,UAAU,QAAQ;EAC/C,iBAAA,GAAA,eAAA,gBAA+B,QAAQ,UAAU,mBAAmB;EACpE,gBAAA,GAAA,eAAA,aAA2B,QAAQ,UAAU,cAAc;EAC3D,SAAS;GACP,WAAW,QAAQ;GACnB,MAAMA,eAAAA,kBAAkB;GACx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a;CAC1B;AACF;AAEA,SAAgB,oBACd,cACmC;CACnC,OAAO;EACL,YAAY,aAAa;EACzB,OAAO,aAAa;EACpB,SAAS,aAAa,QAAQ,KAAK,UAAU;GAC3C,WAAW,KAAK;GAChB,WAAW,OAAO,KAAK,SAAS;GAChC,MAAA,GAAA,eAAA,gBAAoB,KAAK,GAAG;GAC5B,MAAA,GAAA,eAAA,gBAAoB,KAAK,GAAG;EAC9B,EAAE;CACJ;AACF;AAEA,SAAgB,iCACd,cACoC;CAQpC,OAAO;EACL,gBAPA,aAAa,gBAAgB,KAAK,mBAAmB;GACnD,WAAW,cAAc,QAAQ;GACjC,UAAA,GAAA,eAAA,aAAqB,cAAc,QAAQ,QAAQ,MAAM;GACzD,YAAY,cAAc;EAC5B,EAGa;EACb,eAAe;GACb,WAAW,aAAa,eAAe;GACvC,UAAA,GAAA,eAAA,aAAqB,aAAa,eAAe,QAAQ,MAAM;EACjE;EACA,iBAAiB;GACf,WAAW,aAAa,iBAAiB;GACzC,UAAA,GAAA,eAAA,aAAqB,aAAa,iBAAiB,QAAQ,MAAM;EACnE;CACF;AACF;AAEA,SAAgB,2BACd,cAC8B;CAC9B,OAAO,EACL,UAAU,aAAa,UAAU,KAAK,UAAU;EAC9C,QAAQ,KAAK;EACb,eAAe,KAAK;EACpB,cAAc,KAAK;EACnB,gBAAA,GAAA,eAAA,gBAA8B,KAAK,kBAAkB;EACrD,gBAAgB,qBAAqB,KAAK,eAAe;EACzD,YAAY,KAAK,YAAY,IAAI,oBAAoB;CACvD,EAAE,EACJ;AACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
export { mapEngineMarketPrice, mapEngineServerBalanceHealthContributions, mapEngineServerIsolatedPositions, mapEngineServerNlpLockedBalances, mapEngineServerNlpPoolInfo, mapEngineServerOrder, mapEngineServerPerpProduct, mapEngineServerSpotProduct, mapEngineServerSymbol, mapEngineServerSymbols, mapEngineServerTickLiquidity, mapSubaccountSummary };
|
|
1
|
+
import { a as mapEngineServerNlpLockedBalances, c as mapEngineServerPerpProduct, d as mapEngineServerSymbols, f as mapEngineServerTickLiquidity, i as mapEngineServerIsolatedPositions, l as mapEngineServerSpotProduct, n as mapEngineMarketPrice, o as mapEngineServerNlpPoolInfo, p as mapSubaccountSummary, r as mapEngineServerBalanceHealthContributions, s as mapEngineServerOrder, t as mapEngineBboHistory, u as mapEngineServerSymbol } from "../queryDataMappers-kC-SPiHX.cjs";
|
|
2
|
+
export { mapEngineBboHistory, mapEngineMarketPrice, mapEngineServerBalanceHealthContributions, mapEngineServerIsolatedPositions, mapEngineServerNlpLockedBalances, mapEngineServerNlpPoolInfo, mapEngineServerOrder, mapEngineServerPerpProduct, mapEngineServerSpotProduct, mapEngineServerSymbol, mapEngineServerSymbols, mapEngineServerTickLiquidity, mapSubaccountSummary };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
export { mapEngineMarketPrice, mapEngineServerBalanceHealthContributions, mapEngineServerIsolatedPositions, mapEngineServerNlpLockedBalances, mapEngineServerNlpPoolInfo, mapEngineServerOrder, mapEngineServerPerpProduct, mapEngineServerSpotProduct, mapEngineServerSymbol, mapEngineServerSymbols, mapEngineServerTickLiquidity, mapSubaccountSummary };
|
|
1
|
+
import { a as mapEngineServerNlpLockedBalances, c as mapEngineServerPerpProduct, d as mapEngineServerSymbols, f as mapEngineServerTickLiquidity, i as mapEngineServerIsolatedPositions, l as mapEngineServerSpotProduct, n as mapEngineMarketPrice, o as mapEngineServerNlpPoolInfo, p as mapSubaccountSummary, r as mapEngineServerBalanceHealthContributions, s as mapEngineServerOrder, t as mapEngineBboHistory, u as mapEngineServerSymbol } from "../queryDataMappers-CyUgQn0_.js";
|
|
2
|
+
export { mapEngineBboHistory, mapEngineMarketPrice, mapEngineServerBalanceHealthContributions, mapEngineServerIsolatedPositions, mapEngineServerNlpLockedBalances, mapEngineServerNlpPoolInfo, mapEngineServerOrder, mapEngineServerPerpProduct, mapEngineServerSpotProduct, mapEngineServerSymbol, mapEngineServerSymbols, mapEngineServerTickLiquidity, mapSubaccountSummary };
|
|
@@ -185,6 +185,18 @@ function mapEngineMarketPrice(baseResponse) {
|
|
|
185
185
|
productId: baseResponse.product_id
|
|
186
186
|
};
|
|
187
187
|
}
|
|
188
|
+
function mapEngineBboHistory(baseResponse) {
|
|
189
|
+
return {
|
|
190
|
+
intervalMs: baseResponse.interval_ms,
|
|
191
|
+
limit: baseResponse.limit,
|
|
192
|
+
history: baseResponse.history.map((item) => ({
|
|
193
|
+
productId: item.product_id,
|
|
194
|
+
timestamp: Number(item.timestamp),
|
|
195
|
+
bid: removeDecimals(item.bid),
|
|
196
|
+
ask: removeDecimals(item.ask)
|
|
197
|
+
}))
|
|
198
|
+
};
|
|
199
|
+
}
|
|
188
200
|
function mapEngineServerNlpLockedBalances(baseResponse) {
|
|
189
201
|
return {
|
|
190
202
|
lockedBalances: baseResponse.locked_balances.map((lockedBalance) => ({
|
|
@@ -213,6 +225,6 @@ function mapEngineServerNlpPoolInfo(baseResponse) {
|
|
|
213
225
|
})) };
|
|
214
226
|
}
|
|
215
227
|
//#endregion
|
|
216
|
-
export { mapEngineMarketPrice, mapEngineServerBalanceHealthContributions, mapEngineServerIsolatedPositions, mapEngineServerNlpLockedBalances, mapEngineServerNlpPoolInfo, mapEngineServerOrder, mapEngineServerPerpProduct, mapEngineServerSpotProduct, mapEngineServerSymbol, mapEngineServerSymbols, mapEngineServerTickLiquidity, mapSubaccountSummary };
|
|
228
|
+
export { mapEngineBboHistory, mapEngineMarketPrice, mapEngineServerBalanceHealthContributions, mapEngineServerIsolatedPositions, mapEngineServerNlpLockedBalances, mapEngineServerNlpPoolInfo, mapEngineServerOrder, mapEngineServerPerpProduct, mapEngineServerSpotProduct, mapEngineServerSymbol, mapEngineServerSymbols, mapEngineServerTickLiquidity, mapSubaccountSummary };
|
|
217
229
|
|
|
218
230
|
//# sourceMappingURL=queryDataMappers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queryDataMappers.js","names":[],"sources":["../../src/utils/queryDataMappers.ts"],"sourcesContent":["import {\n BalanceHealthContributions,\n calcTotalBorrowed,\n calcTotalDeposited,\n mapValues,\n PerpMarket,\n ProductEngineType,\n removeDecimals,\n SpotMarket,\n subaccountFromHex,\n toBigNumber,\n unpackOrderAppendix,\n} from '@nadohq/shared';\nimport {\n EngineMarketPrice,\n EngineNlpLockedBalance,\n EngineOrder,\n EnginePriceTickLiquidity,\n EngineServerIsolatedPositionsResponse,\n EngineServerMarketPrice,\n EngineServerNlpLockedBalancesResponse,\n EngineServerNlpPoolInfoResponse,\n EngineServerOrderResponse,\n EngineServerPerpProduct,\n EngineServerPriceTickLiquidity,\n EngineServerSpotProduct,\n EngineServerSubaccountInfoResponse,\n EngineServerSubaccountInfoState,\n EngineServerSymbol,\n EngineServerSymbolsResponse,\n EngineSymbol,\n EngineSymbolsResponse,\n GetEngineIsolatedPositionsResponse,\n GetEngineNlpLockedBalancesResponse,\n GetEngineNlpPoolInfoResponse,\n GetEngineSubaccountSummaryResponse,\n SubaccountSummaryState,\n} from '../types';\nimport { mapEngineServerProductType } from './productEngineTypeMappers';\n\nexport function mapEngineServerTickLiquidity(\n tick: EngineServerPriceTickLiquidity,\n): EnginePriceTickLiquidity {\n return {\n price: removeDecimals(tick[0]),\n liquidity: toBigNumber(tick[1]),\n };\n}\n\nexport function mapEngineServerOrder(\n order: EngineServerOrderResponse,\n): EngineOrder {\n const subaccount = subaccountFromHex(order.sender);\n return {\n digest: order.digest,\n expiration: Number(order.expiration),\n nonce: order.nonce,\n price: removeDecimals(order.price_x18),\n productId: order.product_id,\n subaccountOwner: subaccount.subaccountOwner,\n subaccountName: subaccount.subaccountName,\n totalAmount: toBigNumber(order.amount),\n unfilledAmount: toBigNumber(order.unfilled_amount),\n placementTime: order.placed_at,\n appendix: unpackOrderAppendix(order.appendix),\n };\n}\n\nexport function mapEngineServerSpotProduct(\n product: EngineServerSpotProduct,\n): SpotMarket {\n return {\n type: ProductEngineType.SPOT,\n productId: product.product_id,\n minSize: toBigNumber(product.book_info.min_size),\n priceIncrement: removeDecimals(product.book_info.price_increment_x18),\n sizeIncrement: toBigNumber(product.book_info.size_increment),\n product: {\n productId: product.product_id,\n type: ProductEngineType.SPOT,\n totalBorrowed: calcTotalBorrowed(\n product.state.total_borrows_normalized,\n product.state.cumulative_borrows_multiplier_x18,\n ),\n totalDeposited: calcTotalDeposited(\n product.state.total_deposits_normalized,\n product.state.cumulative_deposits_multiplier_x18,\n ),\n oraclePrice: removeDecimals(product.oracle_price_x18),\n interestFloor: removeDecimals(product.config.interest_floor_x18),\n interestInflectionUtil: removeDecimals(\n product.config.interest_inflection_util_x18,\n ),\n interestLargeCap: removeDecimals(product.config.interest_large_cap_x18),\n interestSmallCap: removeDecimals(product.config.interest_small_cap_x18),\n minDepositRate: removeDecimals(product.config.min_deposit_rate_x18),\n longWeightInitial: removeDecimals(product.risk.long_weight_initial_x18),\n longWeightMaintenance: removeDecimals(\n product.risk.long_weight_maintenance_x18,\n ),\n shortWeightInitial: removeDecimals(product.risk.short_weight_initial_x18),\n shortWeightMaintenance: removeDecimals(\n product.risk.short_weight_maintenance_x18,\n ),\n tokenAddr: product.config.token,\n },\n };\n}\n\nexport function mapEngineServerPerpProduct(\n product: EngineServerPerpProduct,\n): PerpMarket {\n return {\n type: ProductEngineType.PERP,\n productId: product.product_id,\n minSize: toBigNumber(product.book_info.min_size),\n priceIncrement: removeDecimals(product.book_info.price_increment_x18),\n sizeIncrement: toBigNumber(product.book_info.size_increment),\n product: {\n productId: product.product_id,\n type: ProductEngineType.PERP,\n oraclePrice: removeDecimals(product.oracle_price_x18),\n longWeightInitial: removeDecimals(product.risk.long_weight_initial_x18),\n longWeightMaintenance: removeDecimals(\n product.risk.long_weight_maintenance_x18,\n ),\n shortWeightInitial: removeDecimals(product.risk.short_weight_initial_x18),\n shortWeightMaintenance: removeDecimals(\n product.risk.short_weight_maintenance_x18,\n ),\n openInterest: toBigNumber(product.state.open_interest),\n cumulativeFundingLong: removeDecimals(\n product.state.cumulative_funding_long_x18,\n ),\n cumulativeFundingShort: removeDecimals(\n product.state.cumulative_funding_short_x18,\n ),\n },\n };\n}\n\nexport function mapEngineServerBalanceHealthContributions(\n healthContributionsForBalance: string[],\n): BalanceHealthContributions {\n return {\n initial: toBigNumber(healthContributionsForBalance[0]),\n maintenance: toBigNumber(healthContributionsForBalance[1]),\n unweighted: toBigNumber(healthContributionsForBalance[2]),\n };\n}\n\nexport function mapSubaccountSummary(\n baseResponse: EngineServerSubaccountInfoResponse,\n): GetEngineSubaccountSummaryResponse {\n return {\n exists: baseResponse.exists,\n ...mapSubaccountSummaryState(\n baseResponse,\n baseResponse.spot_products,\n baseResponse.perp_products,\n ),\n preState: baseResponse.pre_state\n ? mapSubaccountSummaryState(\n baseResponse.pre_state,\n baseResponse.spot_products,\n baseResponse.perp_products,\n )\n : undefined,\n };\n}\n\nfunction mapSubaccountSummaryState(\n state: EngineServerSubaccountInfoState,\n spotProducts: EngineServerSubaccountInfoResponse['spot_products'],\n perpProducts: EngineServerSubaccountInfoResponse['perp_products'],\n): SubaccountSummaryState {\n const balances: SubaccountSummaryState['balances'] = [];\n\n state.spot_balances.forEach((spotBalance) => {\n const product = spotProducts.find(\n (product) => product.product_id === spotBalance.product_id,\n );\n if (!product) {\n throw Error(`Could not find product ${spotBalance.product_id}`);\n }\n\n balances.push({\n amount: toBigNumber(spotBalance.balance.amount),\n healthContributions: mapEngineServerBalanceHealthContributions(\n state.health_contributions[spotBalance.product_id],\n ),\n ...mapEngineServerSpotProduct(product).product,\n });\n });\n\n state.perp_balances.forEach((perpBalance) => {\n const product = perpProducts.find(\n (product) => product.product_id === perpBalance.product_id,\n );\n if (!product) {\n throw Error(`Could not find product ${perpBalance.product_id}`);\n }\n\n balances.push({\n amount: toBigNumber(perpBalance.balance.amount),\n vQuoteBalance: toBigNumber(perpBalance.balance.v_quote_balance),\n healthContributions: mapEngineServerBalanceHealthContributions(\n state.health_contributions[perpBalance.product_id],\n ),\n ...mapEngineServerPerpProduct(product).product,\n });\n });\n\n return {\n balances,\n health: {\n initial: {\n health: toBigNumber(state.healths[0].health),\n assets: toBigNumber(state.healths[0].assets),\n liabilities: toBigNumber(state.healths[0].liabilities),\n },\n maintenance: {\n health: toBigNumber(state.healths[1].health),\n assets: toBigNumber(state.healths[1].assets),\n liabilities: toBigNumber(state.healths[1].liabilities),\n },\n unweighted: {\n health: toBigNumber(state.healths[2].health),\n assets: toBigNumber(state.healths[2].assets),\n liabilities: toBigNumber(state.healths[2].liabilities),\n },\n },\n };\n}\n\nexport function mapEngineServerIsolatedPositions(\n baseResponse: EngineServerIsolatedPositionsResponse,\n): GetEngineIsolatedPositionsResponse {\n return baseResponse.isolated_positions.map((position) => {\n const perpBalance = position.base_balance;\n const quoteBalance = position.quote_balance;\n\n return {\n subaccount: subaccountFromHex(position.subaccount),\n healths: {\n initial: toBigNumber(position.healths[0].health),\n maintenance: toBigNumber(position.healths[1].health),\n unweighted: toBigNumber(position.healths[2].health),\n },\n baseBalance: {\n amount: toBigNumber(perpBalance.balance.amount),\n vQuoteBalance: toBigNumber(perpBalance.balance.v_quote_balance),\n // Health contributions === healths for an isolated position\n healthContributions: {\n initial: toBigNumber(position.base_healths[0]),\n maintenance: toBigNumber(position.base_healths[1]),\n unweighted: toBigNumber(position.base_healths[2]),\n },\n ...mapEngineServerPerpProduct(position.base_product).product,\n },\n quoteBalance: {\n amount: toBigNumber(quoteBalance.balance.amount),\n healthContributions: {\n initial: toBigNumber(position.quote_healths[0]),\n maintenance: toBigNumber(position.quote_healths[1]),\n unweighted: toBigNumber(position.quote_healths[2]),\n },\n ...mapEngineServerSpotProduct(position.quote_product).product,\n },\n };\n });\n}\n\nexport function mapEngineServerSymbols(\n baseResponse: EngineServerSymbolsResponse,\n): EngineSymbolsResponse {\n const symbols: Record<string, EngineSymbol> = mapValues(\n baseResponse.symbols,\n mapEngineServerSymbol,\n );\n\n return {\n symbols,\n };\n}\n\nexport function mapEngineServerSymbol(\n engineServerSymbol: EngineServerSymbol,\n): EngineSymbol {\n return {\n type: mapEngineServerProductType(engineServerSymbol.type),\n productId: engineServerSymbol.product_id,\n symbol: engineServerSymbol.symbol,\n priceIncrement: removeDecimals(engineServerSymbol.price_increment_x18),\n sizeIncrement: toBigNumber(engineServerSymbol.size_increment),\n minSize: toBigNumber(engineServerSymbol.min_size),\n makerFeeRate: removeDecimals(engineServerSymbol.maker_fee_rate_x18),\n takerFeeRate: removeDecimals(engineServerSymbol.taker_fee_rate_x18),\n longWeightInitial: removeDecimals(\n engineServerSymbol.long_weight_initial_x18,\n ),\n longWeightMaintenance: removeDecimals(\n engineServerSymbol.long_weight_maintenance_x18,\n ),\n maxOpenInterest: removeDecimals(engineServerSymbol.max_open_interest_x18),\n isolatedOnly: engineServerSymbol.isolated_only,\n };\n}\n\nexport function mapEngineMarketPrice(\n baseResponse: EngineServerMarketPrice,\n): EngineMarketPrice {\n return {\n ask: removeDecimals(baseResponse.ask_x18),\n bid: removeDecimals(baseResponse.bid_x18),\n productId: baseResponse.product_id,\n };\n}\n\nexport function mapEngineServerNlpLockedBalances(\n baseResponse: EngineServerNlpLockedBalancesResponse,\n): GetEngineNlpLockedBalancesResponse {\n const lockedBalances: EngineNlpLockedBalance[] =\n baseResponse.locked_balances.map((lockedBalance) => ({\n productId: lockedBalance.balance.product_id,\n balance: toBigNumber(lockedBalance.balance.balance.amount),\n unlockedAt: lockedBalance.unlocked_at,\n }));\n\n return {\n lockedBalances,\n balanceLocked: {\n productId: baseResponse.balance_locked.product_id,\n balance: toBigNumber(baseResponse.balance_locked.balance.amount),\n },\n balanceUnlocked: {\n productId: baseResponse.balance_unlocked.product_id,\n balance: toBigNumber(baseResponse.balance_unlocked.balance.amount),\n },\n };\n}\n\nexport function mapEngineServerNlpPoolInfo(\n baseResponse: EngineServerNlpPoolInfoResponse,\n): GetEngineNlpPoolInfoResponse {\n return {\n nlpPools: baseResponse.nlp_pools.map((pool) => ({\n poolId: pool.pool_id,\n subaccountHex: pool.subaccount,\n ownerAddress: pool.owner,\n balanceWeight: removeDecimals(pool.balance_weight_x18),\n subaccountInfo: mapSubaccountSummary(pool.subaccount_info),\n openOrders: pool.open_orders.map(mapEngineServerOrder),\n })),\n };\n}\n"],"mappings":";;;AAwCA,SAAgB,6BACd,MAC0B;CAC1B,OAAO;EACL,OAAO,eAAe,KAAK,EAAE;EAC7B,WAAW,YAAY,KAAK,EAAE;CAChC;AACF;AAEA,SAAgB,qBACd,OACa;CACb,MAAM,aAAa,kBAAkB,MAAM,MAAM;CACjD,OAAO;EACL,QAAQ,MAAM;EACd,YAAY,OAAO,MAAM,UAAU;EACnC,OAAO,MAAM;EACb,OAAO,eAAe,MAAM,SAAS;EACrC,WAAW,MAAM;EACjB,iBAAiB,WAAW;EAC5B,gBAAgB,WAAW;EAC3B,aAAa,YAAY,MAAM,MAAM;EACrC,gBAAgB,YAAY,MAAM,eAAe;EACjD,eAAe,MAAM;EACrB,UAAU,oBAAoB,MAAM,QAAQ;CAC9C;AACF;AAEA,SAAgB,2BACd,SACY;CACZ,OAAO;EACL,MAAM,kBAAkB;EACxB,WAAW,QAAQ;EACnB,SAAS,YAAY,QAAQ,UAAU,QAAQ;EAC/C,gBAAgB,eAAe,QAAQ,UAAU,mBAAmB;EACpE,eAAe,YAAY,QAAQ,UAAU,cAAc;EAC3D,SAAS;GACP,WAAW,QAAQ;GACnB,MAAM,kBAAkB;GACxB,eAAe,kBACb,QAAQ,MAAM,0BACd,QAAQ,MAAM,iCAChB;GACA,gBAAgB,mBACd,QAAQ,MAAM,2BACd,QAAQ,MAAM,kCAChB;GACA,aAAa,eAAe,QAAQ,gBAAgB;GACpD,eAAe,eAAe,QAAQ,OAAO,kBAAkB;GAC/D,wBAAwB,eACtB,QAAQ,OAAO,4BACjB;GACA,kBAAkB,eAAe,QAAQ,OAAO,sBAAsB;GACtE,kBAAkB,eAAe,QAAQ,OAAO,sBAAsB;GAC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,qBACd,cACmB;CACnB,OAAO;EACL,KAAK,eAAe,aAAa,OAAO;EACxC,KAAK,eAAe,aAAa,OAAO;EACxC,WAAW,aAAa;CAC1B;AACF;AAEA,SAAgB,iCACd,cACoC;CAQpC,OAAO;EACL,gBAPA,aAAa,gBAAgB,KAAK,mBAAmB;GACnD,WAAW,cAAc,QAAQ;GACjC,SAAS,YAAY,cAAc,QAAQ,QAAQ,MAAM;GACzD,YAAY,cAAc;EAC5B,EAGa;EACb,eAAe;GACb,WAAW,aAAa,eAAe;GACvC,SAAS,YAAY,aAAa,eAAe,QAAQ,MAAM;EACjE;EACA,iBAAiB;GACf,WAAW,aAAa,iBAAiB;GACzC,SAAS,YAAY,aAAa,iBAAiB,QAAQ,MAAM;EACnE;CACF;AACF;AAEA,SAAgB,2BACd,cAC8B;CAC9B,OAAO,EACL,UAAU,aAAa,UAAU,KAAK,UAAU;EAC9C,QAAQ,KAAK;EACb,eAAe,KAAK;EACpB,cAAc,KAAK;EACnB,eAAe,eAAe,KAAK,kBAAkB;EACrD,gBAAgB,qBAAqB,KAAK,eAAe;EACzD,YAAY,KAAK,YAAY,IAAI,oBAAoB;CACvD,EAAE,EACJ;AACF"}
|
|
1
|
+
{"version":3,"file":"queryDataMappers.js","names":[],"sources":["../../src/utils/queryDataMappers.ts"],"sourcesContent":["import {\n BalanceHealthContributions,\n calcTotalBorrowed,\n calcTotalDeposited,\n mapValues,\n PerpMarket,\n ProductEngineType,\n removeDecimals,\n SpotMarket,\n subaccountFromHex,\n toBigNumber,\n unpackOrderAppendix,\n} from '@nadohq/shared';\nimport {\n EngineMarketPrice,\n EngineNlpLockedBalance,\n EngineOrder,\n EnginePriceTickLiquidity,\n EngineServerCachedBboHistoryResponse,\n EngineServerIsolatedPositionsResponse,\n EngineServerMarketPrice,\n EngineServerNlpLockedBalancesResponse,\n EngineServerNlpPoolInfoResponse,\n EngineServerOrderResponse,\n EngineServerPerpProduct,\n EngineServerPriceTickLiquidity,\n EngineServerSpotProduct,\n EngineServerSubaccountInfoResponse,\n EngineServerSubaccountInfoState,\n EngineServerSymbol,\n EngineServerSymbolsResponse,\n EngineSymbol,\n EngineSymbolsResponse,\n GetEngineCachedBboHistoryResponse,\n GetEngineIsolatedPositionsResponse,\n GetEngineNlpLockedBalancesResponse,\n GetEngineNlpPoolInfoResponse,\n GetEngineSubaccountSummaryResponse,\n SubaccountSummaryState,\n} from '../types';\nimport { mapEngineServerProductType } from './productEngineTypeMappers';\n\nexport function mapEngineServerTickLiquidity(\n tick: EngineServerPriceTickLiquidity,\n): EnginePriceTickLiquidity {\n return {\n price: removeDecimals(tick[0]),\n liquidity: toBigNumber(tick[1]),\n };\n}\n\nexport function mapEngineServerOrder(\n order: EngineServerOrderResponse,\n): EngineOrder {\n const subaccount = subaccountFromHex(order.sender);\n return {\n digest: order.digest,\n expiration: Number(order.expiration),\n nonce: order.nonce,\n price: removeDecimals(order.price_x18),\n productId: order.product_id,\n subaccountOwner: subaccount.subaccountOwner,\n subaccountName: subaccount.subaccountName,\n totalAmount: toBigNumber(order.amount),\n unfilledAmount: toBigNumber(order.unfilled_amount),\n placementTime: order.placed_at,\n appendix: unpackOrderAppendix(order.appendix),\n };\n}\n\nexport function mapEngineServerSpotProduct(\n product: EngineServerSpotProduct,\n): SpotMarket {\n return {\n type: ProductEngineType.SPOT,\n productId: product.product_id,\n minSize: toBigNumber(product.book_info.min_size),\n priceIncrement: removeDecimals(product.book_info.price_increment_x18),\n sizeIncrement: toBigNumber(product.book_info.size_increment),\n product: {\n productId: product.product_id,\n type: ProductEngineType.SPOT,\n totalBorrowed: calcTotalBorrowed(\n product.state.total_borrows_normalized,\n product.state.cumulative_borrows_multiplier_x18,\n ),\n totalDeposited: calcTotalDeposited(\n product.state.total_deposits_normalized,\n product.state.cumulative_deposits_multiplier_x18,\n ),\n oraclePrice: removeDecimals(product.oracle_price_x18),\n interestFloor: removeDecimals(product.config.interest_floor_x18),\n interestInflectionUtil: removeDecimals(\n product.config.interest_inflection_util_x18,\n ),\n interestLargeCap: removeDecimals(product.config.interest_large_cap_x18),\n interestSmallCap: removeDecimals(product.config.interest_small_cap_x18),\n minDepositRate: removeDecimals(product.config.min_deposit_rate_x18),\n longWeightInitial: removeDecimals(product.risk.long_weight_initial_x18),\n longWeightMaintenance: removeDecimals(\n product.risk.long_weight_maintenance_x18,\n ),\n shortWeightInitial: removeDecimals(product.risk.short_weight_initial_x18),\n shortWeightMaintenance: removeDecimals(\n product.risk.short_weight_maintenance_x18,\n ),\n tokenAddr: product.config.token,\n },\n };\n}\n\nexport function mapEngineServerPerpProduct(\n product: EngineServerPerpProduct,\n): PerpMarket {\n return {\n type: ProductEngineType.PERP,\n productId: product.product_id,\n minSize: toBigNumber(product.book_info.min_size),\n priceIncrement: removeDecimals(product.book_info.price_increment_x18),\n sizeIncrement: toBigNumber(product.book_info.size_increment),\n product: {\n productId: product.product_id,\n type: ProductEngineType.PERP,\n oraclePrice: removeDecimals(product.oracle_price_x18),\n longWeightInitial: removeDecimals(product.risk.long_weight_initial_x18),\n longWeightMaintenance: removeDecimals(\n product.risk.long_weight_maintenance_x18,\n ),\n shortWeightInitial: removeDecimals(product.risk.short_weight_initial_x18),\n shortWeightMaintenance: removeDecimals(\n product.risk.short_weight_maintenance_x18,\n ),\n openInterest: toBigNumber(product.state.open_interest),\n cumulativeFundingLong: removeDecimals(\n product.state.cumulative_funding_long_x18,\n ),\n cumulativeFundingShort: removeDecimals(\n product.state.cumulative_funding_short_x18,\n ),\n },\n };\n}\n\nexport function mapEngineServerBalanceHealthContributions(\n healthContributionsForBalance: string[],\n): BalanceHealthContributions {\n return {\n initial: toBigNumber(healthContributionsForBalance[0]),\n maintenance: toBigNumber(healthContributionsForBalance[1]),\n unweighted: toBigNumber(healthContributionsForBalance[2]),\n };\n}\n\nexport function mapSubaccountSummary(\n baseResponse: EngineServerSubaccountInfoResponse,\n): GetEngineSubaccountSummaryResponse {\n return {\n exists: baseResponse.exists,\n ...mapSubaccountSummaryState(\n baseResponse,\n baseResponse.spot_products,\n baseResponse.perp_products,\n ),\n preState: baseResponse.pre_state\n ? mapSubaccountSummaryState(\n baseResponse.pre_state,\n baseResponse.spot_products,\n baseResponse.perp_products,\n )\n : undefined,\n };\n}\n\nfunction mapSubaccountSummaryState(\n state: EngineServerSubaccountInfoState,\n spotProducts: EngineServerSubaccountInfoResponse['spot_products'],\n perpProducts: EngineServerSubaccountInfoResponse['perp_products'],\n): SubaccountSummaryState {\n const balances: SubaccountSummaryState['balances'] = [];\n\n state.spot_balances.forEach((spotBalance) => {\n const product = spotProducts.find(\n (product) => product.product_id === spotBalance.product_id,\n );\n if (!product) {\n throw Error(`Could not find product ${spotBalance.product_id}`);\n }\n\n balances.push({\n amount: toBigNumber(spotBalance.balance.amount),\n healthContributions: mapEngineServerBalanceHealthContributions(\n state.health_contributions[spotBalance.product_id],\n ),\n ...mapEngineServerSpotProduct(product).product,\n });\n });\n\n state.perp_balances.forEach((perpBalance) => {\n const product = perpProducts.find(\n (product) => product.product_id === perpBalance.product_id,\n );\n if (!product) {\n throw Error(`Could not find product ${perpBalance.product_id}`);\n }\n\n balances.push({\n amount: toBigNumber(perpBalance.balance.amount),\n vQuoteBalance: toBigNumber(perpBalance.balance.v_quote_balance),\n healthContributions: mapEngineServerBalanceHealthContributions(\n state.health_contributions[perpBalance.product_id],\n ),\n ...mapEngineServerPerpProduct(product).product,\n });\n });\n\n return {\n balances,\n health: {\n initial: {\n health: toBigNumber(state.healths[0].health),\n assets: toBigNumber(state.healths[0].assets),\n liabilities: toBigNumber(state.healths[0].liabilities),\n },\n maintenance: {\n health: toBigNumber(state.healths[1].health),\n assets: toBigNumber(state.healths[1].assets),\n liabilities: toBigNumber(state.healths[1].liabilities),\n },\n unweighted: {\n health: toBigNumber(state.healths[2].health),\n assets: toBigNumber(state.healths[2].assets),\n liabilities: toBigNumber(state.healths[2].liabilities),\n },\n },\n };\n}\n\nexport function mapEngineServerIsolatedPositions(\n baseResponse: EngineServerIsolatedPositionsResponse,\n): GetEngineIsolatedPositionsResponse {\n return baseResponse.isolated_positions.map((position) => {\n const perpBalance = position.base_balance;\n const quoteBalance = position.quote_balance;\n\n return {\n subaccount: subaccountFromHex(position.subaccount),\n healths: {\n initial: toBigNumber(position.healths[0].health),\n maintenance: toBigNumber(position.healths[1].health),\n unweighted: toBigNumber(position.healths[2].health),\n },\n baseBalance: {\n amount: toBigNumber(perpBalance.balance.amount),\n vQuoteBalance: toBigNumber(perpBalance.balance.v_quote_balance),\n // Health contributions === healths for an isolated position\n healthContributions: {\n initial: toBigNumber(position.base_healths[0]),\n maintenance: toBigNumber(position.base_healths[1]),\n unweighted: toBigNumber(position.base_healths[2]),\n },\n ...mapEngineServerPerpProduct(position.base_product).product,\n },\n quoteBalance: {\n amount: toBigNumber(quoteBalance.balance.amount),\n healthContributions: {\n initial: toBigNumber(position.quote_healths[0]),\n maintenance: toBigNumber(position.quote_healths[1]),\n unweighted: toBigNumber(position.quote_healths[2]),\n },\n ...mapEngineServerSpotProduct(position.quote_product).product,\n },\n };\n });\n}\n\nexport function mapEngineServerSymbols(\n baseResponse: EngineServerSymbolsResponse,\n): EngineSymbolsResponse {\n const symbols: Record<string, EngineSymbol> = mapValues(\n baseResponse.symbols,\n mapEngineServerSymbol,\n );\n\n return {\n symbols,\n };\n}\n\nexport function mapEngineServerSymbol(\n engineServerSymbol: EngineServerSymbol,\n): EngineSymbol {\n return {\n type: mapEngineServerProductType(engineServerSymbol.type),\n productId: engineServerSymbol.product_id,\n symbol: engineServerSymbol.symbol,\n priceIncrement: removeDecimals(engineServerSymbol.price_increment_x18),\n sizeIncrement: toBigNumber(engineServerSymbol.size_increment),\n minSize: toBigNumber(engineServerSymbol.min_size),\n makerFeeRate: removeDecimals(engineServerSymbol.maker_fee_rate_x18),\n takerFeeRate: removeDecimals(engineServerSymbol.taker_fee_rate_x18),\n longWeightInitial: removeDecimals(\n engineServerSymbol.long_weight_initial_x18,\n ),\n longWeightMaintenance: removeDecimals(\n engineServerSymbol.long_weight_maintenance_x18,\n ),\n maxOpenInterest: removeDecimals(engineServerSymbol.max_open_interest_x18),\n isolatedOnly: engineServerSymbol.isolated_only,\n };\n}\n\nexport function mapEngineMarketPrice(\n baseResponse: EngineServerMarketPrice,\n): EngineMarketPrice {\n return {\n ask: removeDecimals(baseResponse.ask_x18),\n bid: removeDecimals(baseResponse.bid_x18),\n productId: baseResponse.product_id,\n };\n}\n\nexport function mapEngineBboHistory(\n baseResponse: EngineServerCachedBboHistoryResponse,\n): GetEngineCachedBboHistoryResponse {\n return {\n intervalMs: baseResponse.interval_ms,\n limit: baseResponse.limit,\n history: baseResponse.history.map((item) => ({\n productId: item.product_id,\n timestamp: Number(item.timestamp),\n bid: removeDecimals(item.bid),\n ask: removeDecimals(item.ask),\n })),\n };\n}\n\nexport function mapEngineServerNlpLockedBalances(\n baseResponse: EngineServerNlpLockedBalancesResponse,\n): GetEngineNlpLockedBalancesResponse {\n const lockedBalances: EngineNlpLockedBalance[] =\n baseResponse.locked_balances.map((lockedBalance) => ({\n productId: lockedBalance.balance.product_id,\n balance: toBigNumber(lockedBalance.balance.balance.amount),\n unlockedAt: lockedBalance.unlocked_at,\n }));\n\n return {\n lockedBalances,\n balanceLocked: {\n productId: baseResponse.balance_locked.product_id,\n balance: toBigNumber(baseResponse.balance_locked.balance.amount),\n },\n balanceUnlocked: {\n productId: baseResponse.balance_unlocked.product_id,\n balance: toBigNumber(baseResponse.balance_unlocked.balance.amount),\n },\n };\n}\n\nexport function mapEngineServerNlpPoolInfo(\n baseResponse: EngineServerNlpPoolInfoResponse,\n): GetEngineNlpPoolInfoResponse {\n return {\n nlpPools: baseResponse.nlp_pools.map((pool) => ({\n poolId: pool.pool_id,\n subaccountHex: pool.subaccount,\n ownerAddress: pool.owner,\n balanceWeight: removeDecimals(pool.balance_weight_x18),\n subaccountInfo: mapSubaccountSummary(pool.subaccount_info),\n openOrders: pool.open_orders.map(mapEngineServerOrder),\n })),\n };\n}\n"],"mappings":";;;AA0CA,SAAgB,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UAAU;GAC3C,WAAW,KAAK;GAChB,WAAW,OAAO,KAAK,SAAS;GAChC,KAAK,eAAe,KAAK,GAAG;GAC5B,KAAK,eAAe,KAAK,GAAG;EAC9B,EAAE;CACJ;AACF;AAEA,SAAgB,iCACd,cACoC;CAQpC,OAAO;EACL,gBAPA,aAAa,gBAAgB,KAAK,mBAAmB;GACnD,WAAW,cAAc,QAAQ;GACjC,SAAS,YAAY,cAAc,QAAQ,QAAQ,MAAM;GACzD,YAAY,cAAc;EAC5B,EAGa;EACb,eAAe;GACb,WAAW,aAAa,eAAe;GACvC,SAAS,YAAY,aAAa,eAAe,QAAQ,MAAM;EACjE;EACA,iBAAiB;GACf,WAAW,aAAa,iBAAiB;GACzC,SAAS,YAAY,aAAa,iBAAiB,QAAQ,MAAM;EACnE;CACF;AACF;AAEA,SAAgB,2BACd,cAC8B;CAC9B,OAAO,EACL,UAAU,aAAa,UAAU,KAAK,UAAU;EAC9C,QAAQ,KAAK;EACb,eAAe,KAAK;EACpB,cAAc,KAAK;EACnB,eAAe,eAAe,KAAK,kBAAkB;EACrD,gBAAgB,qBAAqB,KAAK,eAAe;EACzD,YAAY,KAAK,YAAY,IAAI,oBAAoB;CACvD,EAAE,EACJ;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nadohq/engine-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "HTTP client for the Nado off-chain matching engine",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"module": "./dist/index.js",
|
|
42
42
|
"types": "./dist/index.d.ts",
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@nadohq/shared": "^0.
|
|
44
|
+
"@nadohq/shared": "^0.21.0",
|
|
45
45
|
"axios": "1.16.1",
|
|
46
46
|
"ts-mixer": "6.0.4"
|
|
47
47
|
},
|
|
@@ -53,5 +53,5 @@
|
|
|
53
53
|
"bignumber.js": "11.1.1",
|
|
54
54
|
"viem": "2.50.4"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "dc5abad6e9ca4c9a08de51655fc8f4950a9946e2"
|
|
57
57
|
}
|
package/src/EngineBaseClient.ts
CHANGED
|
@@ -7,11 +7,18 @@ import {
|
|
|
7
7
|
} from '@nadohq/shared';
|
|
8
8
|
import axios, { AxiosInstance, AxiosResponse } from 'axios';
|
|
9
9
|
import {
|
|
10
|
+
EngineServerCachedQueryRequestByType,
|
|
11
|
+
EngineServerCachedQueryRequestType,
|
|
12
|
+
EngineServerCachedQueryResponse,
|
|
13
|
+
EngineServerCachedQueryResponseByType,
|
|
14
|
+
EngineServerCachedQuerySuccessResponse,
|
|
15
|
+
EngineServerEdgeControlRequestByType,
|
|
16
|
+
EngineServerEdgeControlRequestType,
|
|
17
|
+
EngineServerEdgeControlResponseByType,
|
|
10
18
|
EngineServerExecuteRequestByType,
|
|
11
19
|
EngineServerExecuteRequestType,
|
|
12
20
|
EngineServerExecuteResult,
|
|
13
21
|
EngineServerExecuteSuccessResult,
|
|
14
|
-
EngineServerQueryRequest,
|
|
15
22
|
EngineServerQueryRequestByType,
|
|
16
23
|
EngineServerQueryRequestType,
|
|
17
24
|
EngineServerQueryResponse,
|
|
@@ -38,6 +45,11 @@ type EngineQueryRequestResponse<
|
|
|
38
45
|
T extends EngineServerQueryRequestType = EngineServerQueryRequestType,
|
|
39
46
|
> = EngineServerQueryResponse<T>;
|
|
40
47
|
|
|
48
|
+
type EngineCachedQueryRequestResponse<
|
|
49
|
+
T extends EngineServerCachedQueryRequestType =
|
|
50
|
+
EngineServerCachedQueryRequestType,
|
|
51
|
+
> = EngineServerCachedQueryResponse<T>;
|
|
52
|
+
|
|
41
53
|
/**
|
|
42
54
|
* Base client for all engine requests
|
|
43
55
|
*/
|
|
@@ -119,19 +131,62 @@ export class EngineBaseClient {
|
|
|
119
131
|
}
|
|
120
132
|
|
|
121
133
|
/**
|
|
122
|
-
*
|
|
134
|
+
* Queries the gateway's in-memory cache via the `/edge/query` endpoint. Lower latency than
|
|
135
|
+
* {@link query} at the cost of eventual consistency — do not use for order, margin, or
|
|
136
|
+
* settlement decisions.
|
|
123
137
|
*
|
|
124
138
|
* @param requestType
|
|
125
139
|
* @param params
|
|
140
|
+
* @public
|
|
126
141
|
*/
|
|
127
|
-
public
|
|
142
|
+
public async edgeQuery<
|
|
143
|
+
TRequestType extends EngineServerCachedQueryRequestType,
|
|
144
|
+
>(
|
|
128
145
|
requestType: TRequestType,
|
|
129
|
-
params:
|
|
130
|
-
):
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
146
|
+
params: EngineServerCachedQueryRequestByType[TRequestType],
|
|
147
|
+
): Promise<EngineServerCachedQueryResponseByType[TRequestType]> {
|
|
148
|
+
const request = this.getQueryRequest(requestType, params);
|
|
149
|
+
const response =
|
|
150
|
+
await this.axiosInstance.post<EngineCachedQueryRequestResponse>(
|
|
151
|
+
`${this.opts.url}/edge/query`,
|
|
152
|
+
request,
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
this.checkResponseStatus(response);
|
|
156
|
+
this.checkServerStatus(response);
|
|
157
|
+
|
|
158
|
+
// checkServerStatus throws on failure responses so the cast to the success response is acceptable here
|
|
159
|
+
const successResponse = response as AxiosResponse<
|
|
160
|
+
EngineServerCachedQuerySuccessResponse<TRequestType>
|
|
161
|
+
>;
|
|
162
|
+
|
|
163
|
+
return successResponse.data.data;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Sends an edge control message (`ping` / `time`) via the `/edge/query` endpoint. These
|
|
168
|
+
* return immediately from the server clock and use a different envelope than the cached data
|
|
169
|
+
* queries (no `data` field), so the whole response body is returned.
|
|
170
|
+
*
|
|
171
|
+
* @param requestType
|
|
172
|
+
* @param params
|
|
173
|
+
* @public
|
|
174
|
+
*/
|
|
175
|
+
public async edgeControlQuery<
|
|
176
|
+
TRequestType extends EngineServerEdgeControlRequestType,
|
|
177
|
+
>(
|
|
178
|
+
requestType: TRequestType,
|
|
179
|
+
params: EngineServerEdgeControlRequestByType[TRequestType],
|
|
180
|
+
): Promise<EngineServerEdgeControlResponseByType[TRequestType]> {
|
|
181
|
+
const request = this.getQueryRequest(requestType, params);
|
|
182
|
+
const response = await this.axiosInstance.post<
|
|
183
|
+
EngineServerEdgeControlResponseByType[TRequestType]
|
|
184
|
+
>(`${this.opts.url}/edge/query`, request);
|
|
185
|
+
|
|
186
|
+
this.checkResponseStatus(response);
|
|
187
|
+
this.checkServerStatus(response);
|
|
188
|
+
|
|
189
|
+
return response.data;
|
|
135
190
|
}
|
|
136
191
|
|
|
137
192
|
/**
|
|
@@ -158,6 +213,24 @@ export class EngineBaseClient {
|
|
|
158
213
|
return response.data as EngineServerExecuteSuccessResult<TRequestType>;
|
|
159
214
|
}
|
|
160
215
|
|
|
216
|
+
/**
|
|
217
|
+
* A simple, typechecked fn for constructing a `type`-tagged request body in the format expected
|
|
218
|
+
* by the server. Generic over the request type / params pair so it can build live query, edge
|
|
219
|
+
* (cached) query, and edge control request bodies alike.
|
|
220
|
+
*
|
|
221
|
+
* @param requestType
|
|
222
|
+
* @param params
|
|
223
|
+
*/
|
|
224
|
+
public getQueryRequest<TRequestType extends string, TParams extends object>(
|
|
225
|
+
requestType: TRequestType,
|
|
226
|
+
params: TParams,
|
|
227
|
+
): { type: TRequestType } & TParams {
|
|
228
|
+
return {
|
|
229
|
+
type: requestType,
|
|
230
|
+
...params,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
161
234
|
/**
|
|
162
235
|
* A simple, typechecked fn for constructing an execute request in the format expected by the server.
|
|
163
236
|
*
|
|
@@ -215,7 +288,10 @@ export class EngineBaseClient {
|
|
|
215
288
|
|
|
216
289
|
private checkServerStatus(
|
|
217
290
|
response: AxiosResponse<
|
|
218
|
-
|
|
291
|
+
| EngineServerExecuteResult
|
|
292
|
+
| EngineQueryRequestResponse
|
|
293
|
+
| EngineCachedQueryRequestResponse
|
|
294
|
+
| EngineServerEdgeControlResponseByType[EngineServerEdgeControlRequestType]
|
|
219
295
|
>,
|
|
220
296
|
) {
|
|
221
297
|
if (response.data.status !== 'success') {
|
package/src/EngineClient.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { Mixin } from 'ts-mixer';
|
|
2
|
+
import { EngineEdgeQueryClient } from './EngineEdgeQueryClient';
|
|
2
3
|
import { EngineExecuteClient } from './EngineExecuteClient';
|
|
3
4
|
import { EngineQueryClient } from './EngineQueryClient';
|
|
4
5
|
import { EngineWebClient } from './EngineWebClient';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
|
-
* Combined Engine client providing query, execution, and WebSocket functionality for off-chain matching engine communication
|
|
8
|
+
* Combined Engine client providing query, edge (cached) query, execution, and WebSocket functionality for off-chain matching engine communication
|
|
8
9
|
*/
|
|
9
10
|
export class EngineClient extends Mixin(
|
|
10
11
|
EngineQueryClient,
|
|
12
|
+
EngineEdgeQueryClient,
|
|
11
13
|
EngineExecuteClient,
|
|
12
14
|
EngineWebClient,
|
|
13
15
|
) {}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { mapValues, MarketWithProduct } from '@nadohq/shared';
|
|
2
|
+
import { EngineBaseClient } from './EngineBaseClient';
|
|
3
|
+
import {
|
|
4
|
+
EngineCachedTimeParams,
|
|
5
|
+
EnginePingParams,
|
|
6
|
+
EnginePingResponse,
|
|
7
|
+
EngineServerStatusResponse,
|
|
8
|
+
EngineSymbolsResponse,
|
|
9
|
+
GetEngineAllMarketsResponse,
|
|
10
|
+
GetEngineCachedBboHistoryParams,
|
|
11
|
+
GetEngineCachedBboHistoryResponse,
|
|
12
|
+
GetEngineCachedMarketPricesParams,
|
|
13
|
+
GetEngineContractsResponse,
|
|
14
|
+
GetEngineHealthGroupsResponse,
|
|
15
|
+
GetEngineMarketPricesResponse,
|
|
16
|
+
GetEngineNlpPoolInfoResponse,
|
|
17
|
+
GetEngineSymbolsParams,
|
|
18
|
+
} from './types';
|
|
19
|
+
import { mapProductEngineType } from './utils/productEngineTypeMappers';
|
|
20
|
+
import {
|
|
21
|
+
mapEngineBboHistory,
|
|
22
|
+
mapEngineMarketPrice,
|
|
23
|
+
mapEngineServerNlpPoolInfo,
|
|
24
|
+
mapEngineServerPerpProduct,
|
|
25
|
+
mapEngineServerSpotProduct,
|
|
26
|
+
mapEngineServerSymbols,
|
|
27
|
+
} from './utils/queryDataMappers';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* "Edge" queries hit the gateway's in-memory cache via `/edge/query`. They are lower latency
|
|
31
|
+
* than the live {@link EngineQueryClient} queries but eventually consistent — do not use them
|
|
32
|
+
* for order, margin, or settlement decisions.
|
|
33
|
+
*
|
|
34
|
+
* Each `getCached*` method mirrors the data shape of its live counterpart; only the request
|
|
35
|
+
* type and refresh semantics differ.
|
|
36
|
+
*
|
|
37
|
+
* @see https://docs.nado.xyz/developer-resources/api/gateway/edge
|
|
38
|
+
*/
|
|
39
|
+
export class EngineEdgeQueryClient extends EngineBaseClient {
|
|
40
|
+
/**
|
|
41
|
+
* Cached highest bid / lowest ask per product. Products with no cached best-bid/offer yet are
|
|
42
|
+
* silently omitted, so the returned array may be shorter than `productIds`.
|
|
43
|
+
*
|
|
44
|
+
* @param params Omit `productIds` to return every product currently in the cache
|
|
45
|
+
*/
|
|
46
|
+
async getCachedMarketPrices(
|
|
47
|
+
params: GetEngineCachedMarketPricesParams = {},
|
|
48
|
+
): Promise<GetEngineMarketPricesResponse> {
|
|
49
|
+
const baseResponse = await this.edgeQuery('cached_prices', {
|
|
50
|
+
product_ids: params.productIds,
|
|
51
|
+
});
|
|
52
|
+
return {
|
|
53
|
+
marketPrices: baseResponse.market_prices.map(mapEngineMarketPrice),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Cached product metadata and trading config (increments, fees, weights, trading status).
|
|
59
|
+
*
|
|
60
|
+
* @param params Optional `productIds` / `productType` filters
|
|
61
|
+
*/
|
|
62
|
+
async getCachedSymbols(
|
|
63
|
+
params: GetEngineSymbolsParams = {},
|
|
64
|
+
): Promise<EngineSymbolsResponse> {
|
|
65
|
+
const baseResponse = await this.edgeQuery('cached_symbols', {
|
|
66
|
+
product_ids: params.productIds,
|
|
67
|
+
product_type:
|
|
68
|
+
params.productType != null
|
|
69
|
+
? mapProductEngineType(params.productType)
|
|
70
|
+
: undefined,
|
|
71
|
+
});
|
|
72
|
+
return mapEngineServerSymbols(baseResponse);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Cached chain ID and on-chain contract addresses for the current chain. Always available: if
|
|
77
|
+
* the cache is cold the gateway falls back to its live contract config.
|
|
78
|
+
*/
|
|
79
|
+
async getCachedContracts(): Promise<GetEngineContractsResponse> {
|
|
80
|
+
const baseResponse = await this.edgeQuery('cached_contracts', {});
|
|
81
|
+
return {
|
|
82
|
+
chainId: Number(baseResponse.chain_id),
|
|
83
|
+
endpointAddr: baseResponse.endpoint_addr,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Cached engine status (e.g. `active`, `syncing`). Refreshed every 500ms.
|
|
89
|
+
*/
|
|
90
|
+
async getCachedStatus(): Promise<EngineServerStatusResponse> {
|
|
91
|
+
return this.edgeQuery('cached_status', {});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Cached full config, risk, and on-chain state for every spot and perp product.
|
|
96
|
+
*/
|
|
97
|
+
async getCachedAllMarkets(): Promise<GetEngineAllMarketsResponse> {
|
|
98
|
+
const markets: MarketWithProduct[] = [];
|
|
99
|
+
|
|
100
|
+
const baseResponse = await this.edgeQuery('cached_all_products', {});
|
|
101
|
+
baseResponse.spot_products.forEach((spotProduct) => {
|
|
102
|
+
markets.push(mapEngineServerSpotProduct(spotProduct));
|
|
103
|
+
});
|
|
104
|
+
baseResponse.perp_products.forEach((perpProduct) => {
|
|
105
|
+
markets.push(mapEngineServerPerpProduct(perpProduct));
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
return markets;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Cached all products grouped per edge market, keyed by chain ID.
|
|
113
|
+
*/
|
|
114
|
+
async getCachedEdgeAllMarkets(): Promise<
|
|
115
|
+
Record<number, MarketWithProduct[]>
|
|
116
|
+
> {
|
|
117
|
+
const baseResponse = await this.edgeQuery('cached_edge_all_products', {});
|
|
118
|
+
|
|
119
|
+
return mapValues(baseResponse.edge_all_products, (allProducts) => {
|
|
120
|
+
const markets: MarketWithProduct[] = [];
|
|
121
|
+
|
|
122
|
+
allProducts.spot_products.forEach((spotProduct) => {
|
|
123
|
+
markets.push(mapEngineServerSpotProduct(spotProduct));
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
allProducts.perp_products.forEach((perpProduct) => {
|
|
127
|
+
markets.push(mapEngineServerPerpProduct(perpProduct));
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
return markets;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Cached spot/perp product pairings used for health grouping.
|
|
136
|
+
*/
|
|
137
|
+
async getCachedHealthGroups(): Promise<GetEngineHealthGroupsResponse> {
|
|
138
|
+
const baseResponse = await this.edgeQuery('cached_health_groups', {});
|
|
139
|
+
|
|
140
|
+
return {
|
|
141
|
+
healthGroups: baseResponse.health_groups.map(
|
|
142
|
+
([spotProductId, perpProductId]) => ({
|
|
143
|
+
spotProductId,
|
|
144
|
+
perpProductId,
|
|
145
|
+
}),
|
|
146
|
+
),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Cached NLP pool information. Refreshed every 5s.
|
|
152
|
+
*/
|
|
153
|
+
async getCachedNlpPoolInfo(): Promise<GetEngineNlpPoolInfoResponse> {
|
|
154
|
+
const baseResponse = await this.edgeQuery('cached_nlp_pool_info', {});
|
|
155
|
+
return mapEngineServerNlpPoolInfo(baseResponse);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* A rolling, down-samplable time series of best-bid/offer snapshots per product. The gateway
|
|
160
|
+
* samples the live price cache every 500ms and retains up to 30 minutes of history per product.
|
|
161
|
+
*
|
|
162
|
+
* @param params
|
|
163
|
+
*/
|
|
164
|
+
async getCachedBboHistory(
|
|
165
|
+
params: GetEngineCachedBboHistoryParams = {},
|
|
166
|
+
): Promise<GetEngineCachedBboHistoryResponse> {
|
|
167
|
+
const baseResponse = await this.edgeQuery('cached_bbo_history', {
|
|
168
|
+
product_ids: params.productIds,
|
|
169
|
+
interval_ms: params.intervalMs,
|
|
170
|
+
max_time_ms: params.maxTimeMs,
|
|
171
|
+
limit: params.limit,
|
|
172
|
+
});
|
|
173
|
+
return mapEngineBboHistory(baseResponse);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Latency / clock-sync helper. Echoes `clientTime` back alongside the server time so the caller
|
|
178
|
+
* can measure round-trip latency and clock offset in one call.
|
|
179
|
+
*
|
|
180
|
+
* @param params
|
|
181
|
+
*/
|
|
182
|
+
async ping(params: EnginePingParams = {}): Promise<EnginePingResponse> {
|
|
183
|
+
const baseResponse = await this.edgeControlQuery('ping', {
|
|
184
|
+
id: params.id,
|
|
185
|
+
client_time:
|
|
186
|
+
params.clientTime != null ? String(params.clientTime) : undefined,
|
|
187
|
+
});
|
|
188
|
+
return {
|
|
189
|
+
id: baseResponse.id,
|
|
190
|
+
serverTime: Number(baseResponse.server_time),
|
|
191
|
+
clientTime:
|
|
192
|
+
baseResponse.client_time != null
|
|
193
|
+
? Number(baseResponse.client_time)
|
|
194
|
+
: undefined,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Returns the current server time (epoch ms) from the gateway clock.
|
|
200
|
+
*
|
|
201
|
+
* @param params
|
|
202
|
+
*/
|
|
203
|
+
async getEdgeControlTime(
|
|
204
|
+
params: EngineCachedTimeParams = {},
|
|
205
|
+
): Promise<number> {
|
|
206
|
+
const baseResponse = await this.edgeControlQuery('time', {
|
|
207
|
+
id: params.id,
|
|
208
|
+
});
|
|
209
|
+
return Number(baseResponse.server_time);
|
|
210
|
+
}
|
|
211
|
+
}
|