Skip to content

Commit

Permalink
feat: update logic related to filters
Browse files Browse the repository at this point in the history
  • Loading branch information
EchoDex committed Oct 21, 2024
1 parent 33c5e34 commit 08bcee2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 103 deletions.
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
3 changes: 2 additions & 1 deletion 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 Down Expand Up @@ -166,6 +166,7 @@ export interface Order {
status: Status;
user: string;
timestamp: string;
market: string;
}

export interface GetTradeOrderEventsParams {
Expand Down
87 changes: 14 additions & 73 deletions src/query/index.ts
Original file line number Diff line number Diff line change
@@ -1,55 +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.market?.length) {
if (params.market.length > 1) {
where._or = params.market.map((market: string) => ({
market: { _eq: market },
}));
} else {
where.market = { _eq: params.market[0] };
}
}

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 @@ -68,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 @@ -87,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 @@ -130,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 08bcee2

Please sign in to comment.