Skip to content

Commit

Permalink
Merge pull request #46 from compolabs/feat/interact-with-any-market
Browse files Browse the repository at this point in the history
[1861] Interact with any market
  • Loading branch information
EchoDex authored Oct 21, 2024
2 parents 5525f4c + 08bcee2 commit b937463
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 96 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.11.1",
"version": "1.11.2",
"type": "module",
"main": "./dist/index.сjs",
"module": "./dist/index.js",
Expand Down
35 changes: 6 additions & 29 deletions src/IndexerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@apollo/client";

import BN from "./utils/BN";
import { generateWhereFilter } from "./utils/generateWhereFilter";
import { GraphClient } from "./utils/GraphClient";
import {
ActiveOrderReturn,
Expand Down Expand Up @@ -55,15 +56,7 @@ export class IndexerApi extends GraphClient {
subscribeTradeOrderEvents = (
params: GetTradeOrderEventsParams,
): Observable<FetchResult<{ TradeOrderEvent: TradeOrderEvent[] }>> => {
const generateWhereFilter = (params: GetTradeOrderEventsParams) => {
const where: any = {};

if (params.market) {
where.market = { _eq: params.market };
}

return where;
};
const { limit, ...restParams } = params;

const query = gql`
subscription (
Expand Down Expand Up @@ -99,31 +92,15 @@ export class IndexerApi extends GraphClient {
}>({
query,
variables: {
limit: params.limit,
limit,
orderBy: "desc",
where: generateWhereFilter(params),
where: generateWhereFilter(restParams),
},
});
};

getVolume = async (params: GetTradeOrderEventsParams): Promise<Volume> => {
const generateWhereFilter = (
params: GetTradeOrderEventsParams & {
timestamp: string;
},
) => {
const where: any = {};

if (params.market) {
where.market = { _eq: params.market };
}

if (params.timestamp) {
where.timestamp = { _gte: params.timestamp };
}

return where;
};
const { limit, ...restParams } = params;
const now = new Date();
const dayMilliseconds = 24 * 60 * 60 * 1000;
const yesterday = new Date(now.getTime() - dayMilliseconds);
Expand All @@ -150,7 +127,7 @@ export class IndexerApi extends GraphClient {
query,
variables: {
where: generateWhereFilter({
...params,
...restParams,
timestamp,
}),
},
Expand Down
27 changes: 27 additions & 0 deletions src/SparkOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,31 @@ export class SparkOrderbook {
const read = await this.getRead();
return read.fetchMinOrderSize();
}

/**
* @experimental
* Returns the current instance to allow method chaining.
* @returns {this} The current instance of the object.
*/
chain(): this {
return this;
}

/**
* @experimental
* Returns the current instance to allow method chaining.
* @returns {this} The current instance of the object.
*/
writeWithMarket(marketAddress: string): SparkOrderbook {
const params = {
...this.options,
contractAddresses: {
...this.options.contractAddresses,
market: marketAddress,
},
networkUrl: this.provider!.url,
};

return new SparkOrderbook(params);
}
}
7 changes: 4 additions & 3 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export interface WithdrawAllType {

export interface GetOrdersParams {
limit: number;
market: string;
market?: string[];
orderType?: OrderType;
status?: Status[];
user?: string;
Expand All @@ -105,7 +105,7 @@ export interface GetOrdersParams {

export interface GetActiveOrdersParams {
limit: number;
market: string;
market: string[];
orderType: OrderType;
user?: string;
asset?: string;
Expand Down Expand Up @@ -166,11 +166,12 @@ export interface Order {
status: Status;
user: string;
timestamp: string;
market: string;
}

export interface GetTradeOrderEventsParams {
limit: number;
market: string;
market: string[];
}

export interface TradeOrderEvent {
Expand Down
77 changes: 14 additions & 63 deletions src/query/index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,15 @@
import { gql, QueryOptions } from "@apollo/client";
import { generateWhereFilter } from "src/utils/generateWhereFilter";

import { GetActiveOrdersParams, GetOrdersParams } from "..";

export const getOrdersQuery = (
type: "query" | "subscription",
params: GetOrdersParams,
): QueryOptions => {
const generateWhereFilter = (params: GetOrdersParams) => {
const where: any = {};

if (params.orderType) {
where.orderType = { _eq: params.orderType };
}

if (params.market) {
where.market = { _eq: params.market };
}

if (params.status?.length) {
if (params.status.length > 1) {
where._or = params.status.map((status: string) => ({
status: { _eq: status },
}));
} else {
where.status = { _eq: params.status[0] };
}
}

if (params.user) {
where.user = { _eq: params.user };
}

if (params.asset) {
where.asset = { _eq: params.asset };
}

return where;
};

const priceOrder = params.orderType === "Buy" ? "desc" : "asc";
const offsetInRange = params.offset ?? 0;
const { limit, orderType, offset, ...restParams } = params;
const priceOrder = orderType === "Buy" ? "desc" : "asc";
const offsetInRange = offset ?? 0;

const query = gql`
${type} OrderQuery(
Expand All @@ -58,16 +28,17 @@ export const getOrdersQuery = (
status
user
timestamp
market
}
}
`;

return {
query,
variables: {
limit: params.limit,
limit,
offset: offsetInRange,
where: generateWhereFilter(params),
where: generateWhereFilter(restParams),
priceOrder,
},
};
Expand All @@ -77,31 +48,10 @@ export const getActiveOrdersQuery = (
type: "query" | "subscription",
params: GetActiveOrdersParams,
): QueryOptions => {
const generateWhereFilter = (params: GetActiveOrdersParams) => {
const where: any = {};

if (params.orderType) {
where.orderType = { _eq: params.orderType };
}

if (params.market) {
where.market = { _eq: params.market };
}

if (params.user) {
where.user = { _eq: params.user };
}

if (params.asset) {
where.asset = { _eq: params.asset };
}

return where;
};

const priceOrder = params.orderType === "Buy" ? "desc" : "asc";
const queryObject = `Active${params.orderType}Order`;
const offsetInRange = params.offset ?? 0;
const { limit, orderType, offset, ...restParams } = params;
const priceOrder = orderType === "Buy" ? "desc" : "asc";
const queryObject = `Active${orderType}Order`;
const offsetInRange = offset ?? 0;

const query = gql`
${type} ${queryObject}Query(
Expand All @@ -120,16 +70,17 @@ export const getActiveOrdersQuery = (
status
user
timestamp
market
}
}
`;

return {
query,
variables: {
limit: params.limit,
limit,
offset: offsetInRange,
where: generateWhereFilter(params),
where: generateWhereFilter(restParams),
priceOrder,
},
};
Expand Down
42 changes: 42 additions & 0 deletions src/utils/generateWhereFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
type ComparisonOperator = {
_eq?: any;
};

type Condition = {
[key: string]: ComparisonOperator;
};

interface WhereFilter {
[key: string]: ComparisonOperator | Condition[] | undefined;
_or?: Condition[];
}

export const generateWhereFilter = (
params: Record<string, any>,
): WhereFilter => {
const where: WhereFilter = {};

for (const key in params) {
const value = params[key];

if (Array.isArray(value) && value.length) {
if (value.length === 1) {
where[key] = { _eq: value[0] };
} else {
const orConditions = value.map((v) => ({
[key]: { _eq: v },
}));

if (where._or) {
where._or = where._or.concat(orConditions);
} else {
where._or = orConditions;
}
}
} else if (value) {
where[key] = { _eq: value };
}
}

return where;
};

0 comments on commit b937463

Please sign in to comment.