From af0e362d3944f4c2d3bc423b38f0f7c228197abc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B8b=C4=97rt=C3=B8?= <106074508+EchoDex@users.noreply.github.com> Date: Thu, 29 Aug 2024 18:02:57 +0400 Subject: [PATCH] feat: update orderbook initialization --- package.json | 2 +- src/IndexerApi.ts | 30 ------------------------------ src/SparkOrderbook.ts | 34 +++++++++++++++++++++++----------- src/interface.ts | 3 +-- 4 files changed, 25 insertions(+), 44 deletions(-) diff --git a/package.json b/package.json index 05958cd..1046c75 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@compolabs/spark-orderbook-ts-sdk", - "version": "1.6.4", + "version": "1.6.5", "type": "module", "main": "./dist/index.сjs", "module": "./dist/index.js", diff --git a/src/IndexerApi.ts b/src/IndexerApi.ts index c17b8c2..94142f0 100644 --- a/src/IndexerApi.ts +++ b/src/IndexerApi.ts @@ -20,36 +20,6 @@ import { import { getActiveOrdersQuery, getOrdersQuery } from "./query"; export class IndexerApi extends GraphClient { - // TODO: NOT IMPLEMENTED FOR NEW VERSION - // getMarketCreateEvents = async (): Promise => { - // const query = ` - // query SpotMarketCreateEventQuery { - // SpotMarketCreateEvent { - // id, - // asset_id, - // asset_decimals, - // timestamp, - // } - // } - // `; - // const response = await this.post< - // IndexerResponse - // >({ - // query, - // }); - - // // return response.SpotMarketCreateEvent; - - // return [{ - // id: 1, - // asset_id: '', - // asset_decimals: '', - // timestamp: '', - // createdAt: '', - // updatedAt: '', - // }] - // }; - getOrders = ( params: GetOrdersParams, ): Promise> => { diff --git a/src/SparkOrderbook.ts b/src/SparkOrderbook.ts index 05ccf8c..188661a 100644 --- a/src/SparkOrderbook.ts +++ b/src/SparkOrderbook.ts @@ -21,6 +21,7 @@ import { GetActiveOrdersParams, GetOrdersParams, GetTradeOrderEventsParams, + GraphClientConfig, MarketInfo, Markets, Options, @@ -46,29 +47,38 @@ export class SparkOrderbook { private provider: Undefinable; private options: OptionsSpark; - private indexerApi: IndexerApi; + private indexerApi: Undefinable; constructor(params: SparkParams) { this.options = { - contractAddresses: params.contractAddresses, + contractAddresses: { + ...params.contractAddresses, + market: "", + }, wallet: params.wallet, gasPrice: params.gasPrice ?? DEFAULT_GAS_PRICE, gasLimitMultiplier: params.gasLimitMultiplier ?? DEFAULT_GAS_LIMIT_MULTIPLIER, }; - this.indexerApi = new IndexerApi(params.indexerConfig); - this.providerPromise = Provider.create(params.networkUrl); } + get activeIndexerApi() { + if (!this.indexerApi) { + throw new Error("Please set correct active indexer"); + } + + return this.indexerApi; + } + setActiveWallet = (wallet?: WalletLocked | WalletUnlocked) => { const newOptions = { ...this.options }; newOptions.wallet = wallet; this.options = newOptions; }; - setActiveMarketAddress = (contractAddress: string) => { + setActiveMarket = (contractAddress: string, indexer: GraphClientConfig) => { this.options = { ...this.options, contractAddresses: { @@ -76,6 +86,8 @@ export class SparkOrderbook { market: contractAddress, }, }; + + this.indexerApi = new IndexerApi(indexer); }; createOrder = async ( @@ -151,35 +163,35 @@ export class SparkOrderbook { fetchOrders = async ( params: GetOrdersParams, ): Promise> => { - return this.indexerApi.getOrders(params); + return this.activeIndexerApi.getOrders(params); }; fetchActiveOrders = async ( params: GetActiveOrdersParams, ): Promise>> => { - return this.indexerApi.getActiveOrders(params); + return this.activeIndexerApi.getActiveOrders(params); }; subscribeOrders = ( params: GetOrdersParams, ): Observable> => { - return this.indexerApi.subscribeOrders(params); + return this.activeIndexerApi.subscribeOrders(params); }; subscribeActiveOrders = ( params: GetActiveOrdersParams, ): Observable>> => { - return this.indexerApi.subscribeActiveOrders(params); + return this.activeIndexerApi.subscribeActiveOrders(params); }; subscribeTradeOrderEvents = ( params: GetTradeOrderEventsParams, ): Observable> => { - return this.indexerApi.subscribeTradeOrderEvents(params); + return this.activeIndexerApi.subscribeTradeOrderEvents(params); }; fetchVolume = async (): Promise => { - return this.indexerApi.getVolume(); + return this.activeIndexerApi.getVolume(); }; fetchOrderById = async ( diff --git a/src/interface.ts b/src/interface.ts index 6248e7c..5651d58 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -37,8 +37,7 @@ export interface GraphClientConfig { export interface SparkParams { networkUrl: string; - indexerConfig: GraphClientConfig; - contractAddresses: OrderbookContracts; + contractAddresses: Omit; wallet?: WalletLocked | WalletUnlocked; gasPrice?: string; gasLimitMultiplier?: string;