Skip to content

Commit

Permalink
[2036] Add get trade events sentio (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-rock authored Dec 3, 2024
1 parent 76b802f commit 2eb4c01
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 13 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.14.1",
"version": "1.14.2",
"type": "module",
"main": "./dist/index.сjs",
"module": "./dist/index.js",
Expand Down
5 changes: 5 additions & 0 deletions src/SparkOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
FulfillOrderManyWithDepositParams,
GetActiveOrdersParams,
GetOrdersParams,
GetTradeEventQueryParams,
GetTradeOrderEventsParams,
GetUserScoreSnapshotParams,
GraphClientConfig,
Expand Down Expand Up @@ -353,6 +354,10 @@ export class SparkOrderbook {
return this.activeSentioApi?.getUserScoreSnapshot(params);
}

async getTradeEvent(params: GetTradeEventQueryParams) {
return this.activeSentioApi?.getTradeEvent(params);
}

/**
* @experimental
* Returns the current instance to allow method chaining.
Expand Down
21 changes: 16 additions & 5 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,20 @@ export interface GetUserScoreSnapshotParams {
blockDate: number;
}

export interface GetTradeEventQueryParams {
userAddress: string;
toTimestamp: number;
fromTimestamp: number;
}

export interface SentioApiParams {
url: string;
apiKey: string;
}

export interface GetUserScoreSnapshotResponse {
export interface GetSentioResponse<T> {
runtimeCost: string;
result: Result;
result: Result<T>;
computeStats: ComputeStats;
}

Expand All @@ -248,20 +254,25 @@ interface ComputeStats {
isRefreshing: boolean;
}

interface Result {
export interface Result<T> {
columns: string[];
columnTypes: ColumnTypes;
rows: Row[];
rows: T[];
generatedAt: string;
cursor: string;
}

export interface Row {
export interface RowSnapshot {
block_date: string;
total_value_locked_score: number;
tradeVolume: number;
}

export interface RowTradeEvent {
timestamp: string;
volume: number;
}

interface ColumnTypes {
block_date: string;
total_value_locked_score: string;
Expand Down
58 changes: 53 additions & 5 deletions src/query/sentioQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {
GetSentioResponse,
GetTradeEventQueryParams,
GetUserScoreSnapshotParams,
GetUserScoreSnapshotResponse,
RowSnapshot,
RowTradeEvent,
SentioApiParams,
} from "src/interface";
import { Fetch } from "src/utils/Fetch";
Expand All @@ -23,7 +26,7 @@ export class SentioQuery extends Fetch {
async getUserScoreSnapshotQuery({
userAddress,
blockDate,
}: GetUserScoreSnapshotParams): Promise<GetUserScoreSnapshotResponse> {
}: GetUserScoreSnapshotParams): Promise<GetSentioResponse<RowSnapshot>> {
const sqlQuery: sqlQueryParams = {
sqlQuery: {
sql: `SELECT total_value_locked_score, tradeVolume, block_date FROM UserScoreSnapshot_raw WHERE user_address = '${userAddress}' AND timestamp > '${blockDate}' ORDER BY timestamp;`,
Expand All @@ -33,7 +36,34 @@ export class SentioQuery extends Fetch {
const headers: Record<string, string> = {
"api-key": this.apiKey,
};
return await this.post<GetUserScoreSnapshotResponse>(
return await this.post<GetSentioResponse<RowSnapshot>>(
sqlQuery,
"same-origin",
headers,
);
}

async getTradeEventQuery({
userAddress,
fromTimestamp,
toTimestamp,
}: GetTradeEventQueryParams): Promise<GetSentioResponse<RowTradeEvent>> {
const sqlQuery: sqlQueryParams = {
sqlQuery: {
sql: `SELECT *
FROM TradeEvent
WHERE (seller = '${userAddress}'
OR buyer = '${userAddress}')
AND timestamp >= '${fromTimestamp}'
AND timestamp <= '${toTimestamp}';
`,
size: 1000,
},
};
const headers: Record<string, string> = {
"api-key": this.apiKey,
};
return await this.post<GetSentioResponse<RowTradeEvent>>(
sqlQuery,
"same-origin",
headers,
Expand All @@ -46,11 +76,29 @@ export const getUserScoreSnapshotQuery = async ({
blockDate,
url,
apiKey,
}: GetUserScoreSnapshotParams &
SentioApiParams): Promise<GetUserScoreSnapshotResponse> => {
}: GetUserScoreSnapshotParams & SentioApiParams): Promise<
GetSentioResponse<RowSnapshot>
> => {
const sentioQuery = new SentioQuery({ url, apiKey });
return await sentioQuery.getUserScoreSnapshotQuery({
userAddress,
blockDate,
});
};

export const getTradeEventQuery = async ({
userAddress,
fromTimestamp,
toTimestamp,
url,
apiKey,
}: GetTradeEventQueryParams & SentioApiParams): Promise<
GetSentioResponse<RowTradeEvent>
> => {
const sentioQuery = new SentioQuery({ url, apiKey });
return await sentioQuery.getTradeEventQuery({
userAddress,
fromTimestamp,
toTimestamp,
});
};
21 changes: 19 additions & 2 deletions src/sentioApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { getUserScoreSnapshotQuery } from "./query/sentioQuery";
import { GetUserScoreSnapshotParams, SentioApiParams } from "./interface";
import {
getTradeEventQuery,
getUserScoreSnapshotQuery,
} from "./query/sentioQuery";
import {
GetTradeEventQueryParams,
GetUserScoreSnapshotParams,
SentioApiParams,
} from "./interface";

export class SentioApi {
private url: string;
Expand All @@ -18,4 +25,14 @@ export class SentioApi {
apiKey: this.apiKey,
});
};

getTradeEvent = (params: GetTradeEventQueryParams) => {
return getTradeEventQuery({
userAddress: params.userAddress,
fromTimestamp: params.fromTimestamp,
toTimestamp: params.toTimestamp,
url: this.url,
apiKey: this.apiKey,
});
};
}

0 comments on commit 2eb4c01

Please sign in to comment.