Skip to content

Commit

Permalink
Merge pull request #22 from compolabs/feat/1210-trades
Browse files Browse the repository at this point in the history
[1210] Correct trades events
  • Loading branch information
EchoDex authored Jun 25, 2024
2 parents 6e46df9 + 2b345af commit dda3583
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 54 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.3.4",
"version": "1.3.5",
"type": "module",
"main": "./dist/index.сjs",
"module": "./dist/index.js",
Expand Down
68 changes: 23 additions & 45 deletions src/IndexerApi.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import BN from "./utils/BN";
import { Fetch } from "./utils/Fetch";
import {
GetMatchOrderEventsParams,
GetOrdersParams,
MatchOrderEvent,
GetTradeOrderEventsParams,
Order,
TradeOrderEvent,
Volume,
} from "./interface";

Expand Down Expand Up @@ -92,45 +92,25 @@ export class IndexerApi extends Fetch {
return response.Order;
};

getMatchOrderEvents = async (
params: GetMatchOrderEventsParams,
): Promise<MatchOrderEvent[]> => {
const whereFilterParts: string[] = [];

// if (params.user) {
// whereFilterParts.push(`
// _or: [
// { owner: { _eq: "${params.user}" } },
// { counterparty: { _eq: "${params.user}" } }
// ]
// `);
// }

if (params.asset) {
whereFilterParts.push(`asset: { _eq: "${params.asset}" }`);
}

const whereFilter = whereFilterParts.join(", ");

const query = `query MatchOrderEventQuery {
MatchOrderEvent(limit: ${params.limit}, where: {${whereFilter}}, order_by: { timestamp: desc }) {
getTradeOrderEvents = async (
params: GetTradeOrderEventsParams,
): Promise<TradeOrderEvent[]> => {
const query = `query TradeOrderEventQuery {
TradeOrderEvent(limit: ${params.limit}, order_by: { timestamp: desc }) {
id
owner
counterparty
asset
match_size
match_price
trade_price
trade_size
timestamp
}
}`;

const response = await this.post<
IndexerResponse<MatchOrderEvent[], "MatchOrderEvent">
IndexerResponse<TradeOrderEvent[], "TradeOrderEvent">
>({
query,
});

return response.MatchOrderEvent;
return response.TradeOrderEvent;
};

getVolume = async (): Promise<Volume> => {
Expand All @@ -140,38 +120,36 @@ export class IndexerApi extends Fetch {

const yesterdayISO = yesterday.toISOString();

const query = `query MatchOrderEventQuery {
MatchOrderEvent(where: {timestamp: {_gte: "${yesterdayISO}"}}) {
id
match_size
match_price
timestamp
const query = `query TradeOrderEventQuery {
TradeOrderEvent(where: {timestamp: {_gte: "${yesterdayISO}"}}) {
trade_size
trade_price
}
}`;

type MatchOrderEventPartial = Pick<
MatchOrderEvent,
"id" | "match_size" | "match_price"
type TradeOrderEventPartial = Pick<
TradeOrderEvent,
"trade_size" | "trade_price"
>;

const response = await this.post<
IndexerResponse<MatchOrderEventPartial[], "MatchOrderEvent">
IndexerResponse<TradeOrderEventPartial[], "TradeOrderEvent">
>({
query,
});

if (!response.MatchOrderEvent.length) {
if (!response.TradeOrderEvent.length) {
return {
volume24h: BN.ZERO.toString(),
high24h: BN.ZERO.toString(),
low24h: BN.ZERO.toString(),
};
}

const data = response.MatchOrderEvent.reduce(
const data = response.TradeOrderEvent.reduce(
(prev, currentData) => {
const price = BigInt(currentData.match_price);
const size = BigInt(currentData.match_size);
const price = BigInt(currentData.trade_price);
const size = BigInt(currentData.trade_size);
prev.volume24h += size;

if (prev.high24h < price) {
Expand Down
12 changes: 6 additions & 6 deletions src/SparkOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import {
Asset,
CreateOrderParams,
DepositParams,
GetMatchOrderEventsParams,
GetOrdersParams,
GetTradeOrderEventsParams,
MarketCreateEvent,
MatchOrderEvent,
Options,
OptionsSpark,
Order,
SparkParams,
SpotOrderWithoutTimestamp,
TradeOrderEvent,
Volume,
WithdrawParams,
WriteTransactionResponse,
Expand Down Expand Up @@ -126,10 +126,10 @@ export class SparkOrderbook {
return this.indexerApi.getOrders(params);
};

getMatchOrderEvents = async (
params: GetMatchOrderEventsParams,
): Promise<MatchOrderEvent[]> => {
return this.indexerApi.getMatchOrderEvents(params);
getTradeOrderEvents = async (
params: GetTradeOrderEventsParams,
): Promise<TradeOrderEvent[]> => {
return this.indexerApi.getTradeOrderEvents(params);
};

fetchVolume = async (): Promise<Volume> => {
Expand Down
10 changes: 8 additions & 2 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ export interface Order {
timestamp: string;
}

export interface GetMatchOrderEventsParams {
asset: string;
export interface GetTradeOrderEventsParams {
limit: number;
}

Expand All @@ -139,6 +138,13 @@ export interface MatchOrderEvent {
timestamp: string;
}

export interface TradeOrderEvent {
id: string;
timestamp: string;
trade_price: string;
trade_size: string;
}

export type Volume = {
volume24h: string;
high24h: string;
Expand Down

0 comments on commit dda3583

Please sign in to comment.