Skip to content

Commit

Permalink
feat: update orderbook initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
EchoDex committed Aug 29, 2024
1 parent a87eff7 commit af0e362
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 44 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
30 changes: 0 additions & 30 deletions src/IndexerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,6 @@ import {
import { getActiveOrdersQuery, getOrdersQuery } from "./query";

export class IndexerApi extends GraphClient {
// TODO: NOT IMPLEMENTED FOR NEW VERSION
// getMarketCreateEvents = async (): Promise<SpotMarketCreateEvent[]> => {
// const query = `
// query SpotMarketCreateEventQuery {
// SpotMarketCreateEvent {
// id,
// asset_id,
// asset_decimals,
// timestamp,
// }
// }
// `;
// const response = await this.post<
// IndexerResponse<SpotMarketCreateEvent[], "SpotMarketCreateEvent">
// >({
// query,
// });

// // return response.SpotMarketCreateEvent;

// return [{
// id: 1,
// asset_id: '',
// asset_decimals: '',
// timestamp: '',
// createdAt: '',
// updatedAt: '',
// }]
// };

getOrders = (
params: GetOrdersParams,
): Promise<ApolloQueryResult<{ Order: Order[] }>> => {
Expand Down
34 changes: 23 additions & 11 deletions src/SparkOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
GetActiveOrdersParams,
GetOrdersParams,
GetTradeOrderEventsParams,
GraphClientConfig,
MarketInfo,
Markets,
Options,
Expand All @@ -46,36 +47,47 @@ export class SparkOrderbook {
private provider: Undefinable<Provider>;
private options: OptionsSpark;

private indexerApi: IndexerApi;
private indexerApi: Undefinable<IndexerApi>;

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: {
...this.options.contractAddresses,
market: contractAddress,
},
};

this.indexerApi = new IndexerApi(indexer);
};

createOrder = async (
Expand Down Expand Up @@ -151,35 +163,35 @@ export class SparkOrderbook {
fetchOrders = async (
params: GetOrdersParams,
): Promise<ApolloQueryResult<{ Order: Order[] }>> => {
return this.indexerApi.getOrders(params);
return this.activeIndexerApi.getOrders(params);
};

fetchActiveOrders = async <T extends OrderType>(
params: GetActiveOrdersParams,
): Promise<ApolloQueryResult<ActiveOrderReturn<T>>> => {
return this.indexerApi.getActiveOrders<T>(params);
return this.activeIndexerApi.getActiveOrders<T>(params);
};

subscribeOrders = (
params: GetOrdersParams,
): Observable<FetchResult<{ Order: Order[] }>> => {
return this.indexerApi.subscribeOrders(params);
return this.activeIndexerApi.subscribeOrders(params);
};

subscribeActiveOrders = <T extends OrderType>(
params: GetActiveOrdersParams,
): Observable<FetchResult<ActiveOrderReturn<T>>> => {
return this.indexerApi.subscribeActiveOrders<T>(params);
return this.activeIndexerApi.subscribeActiveOrders<T>(params);
};

subscribeTradeOrderEvents = (
params: GetTradeOrderEventsParams,
): Observable<FetchResult<{ TradeOrderEvent: TradeOrderEvent[] }>> => {
return this.indexerApi.subscribeTradeOrderEvents(params);
return this.activeIndexerApi.subscribeTradeOrderEvents(params);
};

fetchVolume = async (): Promise<Volume> => {
return this.indexerApi.getVolume();
return this.activeIndexerApi.getVolume();
};

fetchOrderById = async (
Expand Down
3 changes: 1 addition & 2 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ export interface GraphClientConfig {

export interface SparkParams {
networkUrl: string;
indexerConfig: GraphClientConfig;
contractAddresses: OrderbookContracts;
contractAddresses: Omit<OrderbookContracts, "market">;
wallet?: WalletLocked | WalletUnlocked;
gasPrice?: string;
gasLimitMultiplier?: string;
Expand Down

0 comments on commit af0e362

Please sign in to comment.