Skip to content

Commit

Permalink
feat: add sentio method from leaderboard (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-rock authored Dec 17, 2024
1 parent 15419b9 commit 5a71b6a
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 2 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.2",
"version": "1.14.3",
"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 @@ -20,6 +20,7 @@ import {
FulfillOrderManyParams,
FulfillOrderManyWithDepositParams,
GetActiveOrdersParams,
GetLeaderBoardQueryParams,
GetOrdersParams,
GetTradeEventQueryParams,
GetTradeOrderEventsParams,
Expand Down Expand Up @@ -358,6 +359,10 @@ export class SparkOrderbook {
return this.activeSentioApi?.getTradeEvent(params);
}

async getLeaderBoard(params: GetLeaderBoardQueryParams) {
return this.activeSentioApi?.getLeaderBoard(params);
}

/**
* @experimental
* Returns the current instance to allow method chaining.
Expand Down
15 changes: 15 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ export interface GetTradeEventQueryParams {
fromTimestamp: number;
}

export interface GetLeaderBoardQueryParams {
page: number;
search?: string;
currentTimestamp: number;
interval: number;
}

export interface SentioApiParams {
url: string;
apiKey: string;
Expand Down Expand Up @@ -273,6 +280,14 @@ export interface RowTradeEvent {
volume: number;
}

export interface TraderVolumeResponse {
walletId: string;
traderVolume: number;
id: number;
totalCount: number;
isYour: boolean;
}

interface ColumnTypes {
block_date: string;
total_value_locked_score: string;
Expand Down
88 changes: 87 additions & 1 deletion src/query/sentioQuery.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {
GetLeaderBoardQueryParams,
GetSentioResponse,
GetTradeEventQueryParams,
GetUserScoreSnapshotParams,
RowSnapshot,
RowTradeEvent,
SentioApiParams,
TraderVolumeResponse,
} from "src/interface";
import { Fetch } from "src/utils/Fetch";

Expand All @@ -16,13 +18,78 @@ interface sqlQueryParams {
}

export class SentioQuery extends Fetch {
private apiKey: string;
private readonly apiKey: string;

constructor({ url, apiKey }: SentioApiParams) {
super(url);
this.apiKey = apiKey;
}

async getLeaderBoardQuery({
page,
search = "",
currentTimestamp,
interval,
}: GetLeaderBoardQueryParams): Promise<
GetSentioResponse<TraderVolumeResponse>
> {
const limit = 10;
const offset = page * limit;
const sqlQuery: sqlQueryParams = {
sqlQuery: {
sql: `WITH Combined AS (
SELECT
seller AS walletId,
volume,
timestamp
FROM TradeEvent
WHERE timestamp BETWEEN ${currentTimestamp} - ${interval} AND ${currentTimestamp}
UNION ALL
SELECT
buyer AS walletId,
volume,
timestamp
FROM TradeEvent
WHERE timestamp BETWEEN ${currentTimestamp} - ${interval} AND ${currentTimestamp}
),
Ranked AS (
SELECT
walletId,
SUM(volume) AS traderVolume,
ROW_NUMBER() OVER (ORDER BY SUM(volume) DESC) AS id
FROM Combined
GROUP BY walletId
),
Filtered AS (
SELECT
id,
walletId,
traderVolume,
COUNT(*) OVER () AS totalCount
FROM Ranked
WHERE walletId LIKE '%${search}%'
)
SELECT
id,
walletId,
traderVolume,
totalCount
FROM Filtered
ORDER BY traderVolume DESC
LIMIT ${limit} OFFSET ${offset};`,
size: 20,
},
};
const headers: Record<string, string> = {
"api-key": this.apiKey,
};
return await this.post<GetSentioResponse<TraderVolumeResponse>>(
sqlQuery,
"same-origin",
headers,
);
}

async getUserScoreSnapshotQuery({
userAddress,
blockDate,
Expand Down Expand Up @@ -102,3 +169,22 @@ export const getTradeEventQuery = async ({
toTimestamp,
});
};

export const getLeaderBoardQuery = async ({
page,
search,
url,
apiKey,
currentTimestamp,
interval,
}: GetLeaderBoardQueryParams & SentioApiParams): Promise<
GetSentioResponse<TraderVolumeResponse>
> => {
const sentioQuery = new SentioQuery({ url, apiKey });
return await sentioQuery.getLeaderBoardQuery({
page,
search,
currentTimestamp,
interval,
});
};
13 changes: 13 additions & 0 deletions src/sentioApi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
getLeaderBoardQuery,
getTradeEventQuery,
getUserScoreSnapshotQuery,
} from "./query/sentioQuery";
import {
GetLeaderBoardQueryParams,
GetTradeEventQueryParams,
GetUserScoreSnapshotParams,
SentioApiParams,
Expand Down Expand Up @@ -35,4 +37,15 @@ export class SentioApi {
apiKey: this.apiKey,
});
};

getLeaderBoard = (params: GetLeaderBoardQueryParams) => {
return getLeaderBoardQuery({
page: params.page,
search: params.search,
currentTimestamp: params.currentTimestamp,
interval: params.interval,
url: this.url,
apiKey: this.apiKey,
});
};
}

0 comments on commit 5a71b6a

Please sign in to comment.