Skip to content

Commit

Permalink
[2036] Add Sentio routers (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-rock authored Nov 20, 2024
1 parent c951b6f commit dc4612b
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 11 deletions.
3 changes: 2 additions & 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.0",
"version": "1.14.1",
"type": "module",
"main": "./dist/index.сjs",
"module": "./dist/index.js",
Expand Down Expand Up @@ -43,6 +43,7 @@
},
"dependencies": {
"@apollo/client": "^3.10.8",
"axios": "^1.7.7",
"bignumber.js": "^9.1.2",
"graphql-ws": "^5.16.0",
"tsdef": "^0.0.14"
Expand Down
40 changes: 40 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/IndexerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Observable,
} from "@apollo/client";

import { getActiveOrdersQuery, getOrdersQuery } from "./query/indexerQuery";
import BN from "./utils/BN";
import { generateWhereFilter } from "./utils/generateWhereFilter";
import { GraphClient } from "./utils/GraphClient";
Expand All @@ -20,7 +21,6 @@ import {
UserInfoParams,
Volume,
} from "./interface";
import { getActiveOrdersQuery, getOrdersQuery } from "./query";

export class IndexerApi extends GraphClient {
getOrders = (
Expand Down
19 changes: 19 additions & 0 deletions src/SparkOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
GetActiveOrdersParams,
GetOrdersParams,
GetTradeOrderEventsParams,
GetUserScoreSnapshotParams,
GraphClientConfig,
MarketInfo,
Markets,
Expand All @@ -30,6 +31,7 @@ import {
Order,
OrderType,
ProtocolFee,
SentioApiParams,
SparkParams,
SpotOrderWithoutTimestamp,
TradeOrderEvent,
Expand All @@ -42,13 +44,15 @@ import {
WriteTransactionResponse,
} from "./interface";
import { ReadActions } from "./ReadActions";
import { SentioApi } from "./sentioApi";
import { WriteActions } from "./WriteActions";

export class SparkOrderbook {
private providerPromise: Promise<Provider>;
private provider?: Provider;
private options: OptionsSpark;
private indexerApi?: IndexerApi;
private sentioApi?: SentioApi;

constructor(params: SparkParams) {
this.options = {
Expand All @@ -72,6 +76,13 @@ export class SparkOrderbook {
return this.indexerApi;
}

private get activeSentioApi(): SentioApi {
if (!this.sentioApi) {
throw new Error("Please set the correct active indexer.");
}
return this.sentioApi;
}

async getProvider(): Promise<Provider> {
if (!this.provider) {
this.provider = await this.providerPromise;
Expand Down Expand Up @@ -122,6 +133,10 @@ export class SparkOrderbook {
this.indexerApi = new IndexerApi(indexer);
}

setSentioConfig(params: SentioApiParams): void {
this.sentioApi = new SentioApi(params);
}

async createOrder(
order: CreateOrderParams,
): Promise<WriteTransactionResponse> {
Expand Down Expand Up @@ -334,6 +349,10 @@ export class SparkOrderbook {
return read.fetchMinOrderPrice();
}

async getUserScoreSnapshot(params: GetUserScoreSnapshotParams) {
return this.activeSentioApi?.getUserScoreSnapshot(params);
}

/**
* @experimental
* Returns the current instance to allow method chaining.
Expand Down
47 changes: 46 additions & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export type WriteTransactionResponse = {
};

export interface GraphQLResponse<T> {
data: T;
result: T;
errors?: { message: string }[];
}

Expand Down Expand Up @@ -222,3 +222,48 @@ export interface UserInfo {
closed: number;
timestamp: string;
}

export interface GetUserScoreSnapshotParams {
userAddress: string;
blockDate: number;
}

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

export interface GetUserScoreSnapshotResponse {
runtimeCost: string;
result: Result;
computeStats: ComputeStats;
}

interface ComputeStats {
computedAt: string;
computeCostMs: string;
binaryVersionHash: string;
computedBy: string;
isCached: boolean;
isRefreshing: boolean;
}

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

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

interface ColumnTypes {
block_date: string;
total_value_locked_score: string;
tradeVolume: string;
}
File renamed without changes.
56 changes: 56 additions & 0 deletions src/query/sentioQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
GetUserScoreSnapshotParams,
GetUserScoreSnapshotResponse,
SentioApiParams,
} from "src/interface";
import { Fetch } from "src/utils/Fetch";

interface sqlQueryParams {
sqlQuery: {
sql: string;
size: number;
};
}

export class SentioQuery extends Fetch {
private apiKey: string;

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

async getUserScoreSnapshotQuery({
userAddress,
blockDate,
}: GetUserScoreSnapshotParams): Promise<GetUserScoreSnapshotResponse> {
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;`,
size: 1000,
},
};
const headers: Record<string, string> = {
"api-key": this.apiKey,
};
return await this.post<GetUserScoreSnapshotResponse>(
sqlQuery,
"same-origin",
headers,
);
}
}

export const getUserScoreSnapshotQuery = async ({
userAddress,
blockDate,
url,
apiKey,
}: GetUserScoreSnapshotParams &
SentioApiParams): Promise<GetUserScoreSnapshotResponse> => {
const sentioQuery = new SentioQuery({ url, apiKey });
return await sentioQuery.getUserScoreSnapshotQuery({
userAddress,
blockDate,
});
};
21 changes: 21 additions & 0 deletions src/sentioApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getUserScoreSnapshotQuery } from "./query/sentioQuery";
import { GetUserScoreSnapshotParams, SentioApiParams } from "./interface";

export class SentioApi {
private url: string;
private apiKey: string;

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

getUserScoreSnapshot = (params: GetUserScoreSnapshotParams) => {
return getUserScoreSnapshotQuery({
userAddress: params.userAddress,
blockDate: params.blockDate,
url: this.url,
apiKey: this.apiKey,
});
};
}
11 changes: 4 additions & 7 deletions src/utils/Fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { GraphQLResponse } from "src/interface";
import { Nilable } from "tsdef";

export class Fetch {
Expand All @@ -14,25 +13,23 @@ export class Fetch {
): Promise<T> => {
return fetch(endpoint, data)
.then((response) => response.json())
.then((data: GraphQLResponse<T>) => {
if (data.errors) {
throw new Error(data.errors[0].message);
}

return data.data;
.then((data: T) => {
return data;
});
};

protected post = <T>(
body: Record<string, any>,
credentials: RequestCredentials = "same-origin",
headers: Record<string, string>,
) => {
return this.request<T>(this.url, {
method: "POST",
body: JSON.stringify(body),
credentials,
headers: {
"Content-Type": "application/json",
...headers,
},
});
};
Expand Down
5 changes: 4 additions & 1 deletion tests/indexerApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import {
OrderType,
TradeOrderEvent,
} from "../src/interface";
import { getActiveOrdersQuery, getOrdersQuery } from "../src/query";
import {
getActiveOrdersQuery,
getOrdersQuery,
} from "../src/query/indexerQuery";

const USER_ADDRESS = "0x0000";
const ASSET_ADDRESS = "0x0001";
Expand Down

0 comments on commit dc4612b

Please sign in to comment.