From 08dfc494c78fa3fa4bc8feda19c32ff442c63895 Mon Sep 17 00:00:00 2001 From: Bojan Angjelkoski Date: Fri, 6 Dec 2024 21:13:05 +0100 Subject: [PATCH] feat: abacus abstractions --- .../src/exceptions/types/modules.ts | 3 +- packages/sdk-ts/package.json | 1 + .../src/client/abacus/grpc/AbacusGrpcApi.ts | 123 ++++++++++++++++++ .../sdk-ts/src/client/abacus/grpc/index.ts | 3 + .../client/abacus/grpc/transformers/index.ts | 21 +++ packages/sdk-ts/src/client/abacus/index.ts | 1 + packages/sdk-ts/src/client/index.ts | 1 + yarn.lock | 10 ++ 8 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 packages/sdk-ts/src/client/abacus/grpc/AbacusGrpcApi.ts create mode 100644 packages/sdk-ts/src/client/abacus/grpc/index.ts create mode 100644 packages/sdk-ts/src/client/abacus/grpc/transformers/index.ts create mode 100644 packages/sdk-ts/src/client/abacus/index.ts diff --git a/packages/exceptions/src/exceptions/types/modules.ts b/packages/exceptions/src/exceptions/types/modules.ts index 648b32ef5..9674c5e94 100644 --- a/packages/exceptions/src/exceptions/types/modules.ts +++ b/packages/exceptions/src/exceptions/types/modules.ts @@ -44,7 +44,8 @@ export enum IndexerErrorModule { ChronosSpot = 'indexer-chronos-spot', ChronosMarkets = 'indexer-chronos-markets', Campaign = 'indexer-campaign', - Web3Gw = 'web3-gateway' + Web3Gw = 'web3-gateway', + Abacus = 'abacus' } export enum WalletErrorActionModule { diff --git a/packages/sdk-ts/package.json b/packages/sdk-ts/package.json index 1cbccf5c9..3172a4b6b 100644 --- a/packages/sdk-ts/package.json +++ b/packages/sdk-ts/package.json @@ -128,6 +128,7 @@ "@injectivelabs/grpc-web-react-native-transport": "^0.0.2", "@injectivelabs/indexer-proto-ts": "1.13.3", "@injectivelabs/mito-proto-ts": "1.13.2", + "@injectivelabs/abacus-proto-ts": "1.13.0", "@injectivelabs/networks": "^1.14.33", "@injectivelabs/olp-proto-ts": "1.13.1", "@injectivelabs/test-utils": "^1.14.34-beta.2", diff --git a/packages/sdk-ts/src/client/abacus/grpc/AbacusGrpcApi.ts b/packages/sdk-ts/src/client/abacus/grpc/AbacusGrpcApi.ts new file mode 100644 index 000000000..db77a140e --- /dev/null +++ b/packages/sdk-ts/src/client/abacus/grpc/AbacusGrpcApi.ts @@ -0,0 +1,123 @@ +import { + IndexerErrorModule, + UnspecifiedErrorCode, + GrpcUnaryRequestException, +} from '@injectivelabs/exceptions' +import { InjectiveAbacusRpc } from '@injectivelabs/abacus-proto-ts' +import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js' +import { AbacusGrpcTransformer } from './transformers/index.js' + +export class AbacusGrpcApi extends BaseGrpcConsumer { + protected module: string = IndexerErrorModule.Abacus + + protected client: InjectiveAbacusRpc.PointsSvcClientImpl + + constructor(endpoint: string) { + super(endpoint) + + this.client = new InjectiveAbacusRpc.PointsSvcClientImpl( + this.getGrpcWebImpl(endpoint), + ) + } + + async fetchAccountLatestPoints(address: string) { + const request = InjectiveAbacusRpc.PointsLatestForAccountRequest.create() + + request.accountAddress = address + + try { + const response = + await this.retry( + () => this.client.PointsLatestForAccount(request), + ) + + return AbacusGrpcTransformer.grpcPointsLatestToPointsLatest(response) + } catch (e: unknown) { + if (e instanceof InjectiveAbacusRpc.GrpcWebError) { + throw new GrpcUnaryRequestException(new Error(e.toString()), { + code: e.code, + context: 'PointsStatsLatestForAccount', + contextModule: this.module, + }) + } + + throw new GrpcUnaryRequestException(e as Error, { + code: UnspecifiedErrorCode, + context: 'PointsStatsLatestForAccount', + contextModule: this.module, + }) + } + } + + async fetchAccountDailyPoints(address: string, daysLimit?: number) { + const request = + InjectiveAbacusRpc.PointsStatsDailyForAccountRequest.create() + + request.accountAddress = address + + if (daysLimit) { + request.daysLimit = daysLimit + } + + try { + const response = + await this.retry( + () => this.client.PointsStatsDailyForAccount(request), + ) + + return AbacusGrpcTransformer.grpcPointsStatsDailyToPointsStatsDaily( + response, + ) + } catch (e: unknown) { + if (e instanceof InjectiveAbacusRpc.GrpcWebError) { + throw new GrpcUnaryRequestException(new Error(e.toString()), { + code: e.code, + context: 'PointsStatsDailyForAccount', + contextModule: this.module, + }) + } + + throw new GrpcUnaryRequestException(e as Error, { + code: UnspecifiedErrorCode, + context: 'PointsStatsDailyForAccount', + contextModule: this.module, + }) + } + } + + async fetchAccountWeeklyPoints(address: string, weeksLimit?: number) { + const request = + InjectiveAbacusRpc.PointsStatsWeeklyForAccountRequest.create() + + request.accountAddress = address + + if (weeksLimit) { + request.weeksLimit = weeksLimit + } + + try { + const response = + await this.retry( + () => this.client.PointsStatsWeeklyForAccount(request), + ) + + return AbacusGrpcTransformer.grpcPointsStatsWeeklyToPointsStatsWeekly( + response, + ) + } catch (e: unknown) { + if (e instanceof InjectiveAbacusRpc.GrpcWebError) { + throw new GrpcUnaryRequestException(new Error(e.toString()), { + code: e.code, + context: 'PointsStatsWeeklyForAccount', + contextModule: this.module, + }) + } + + throw new GrpcUnaryRequestException(e as Error, { + code: UnspecifiedErrorCode, + context: 'PointsStatsWeeklyForAccount', + contextModule: this.module, + }) + } + } +} diff --git a/packages/sdk-ts/src/client/abacus/grpc/index.ts b/packages/sdk-ts/src/client/abacus/grpc/index.ts new file mode 100644 index 000000000..25984aec7 --- /dev/null +++ b/packages/sdk-ts/src/client/abacus/grpc/index.ts @@ -0,0 +1,3 @@ +import { AbacusGrpcApi } from './AbacusGrpcApi.js' + +export { AbacusGrpcApi } diff --git a/packages/sdk-ts/src/client/abacus/grpc/transformers/index.ts b/packages/sdk-ts/src/client/abacus/grpc/transformers/index.ts new file mode 100644 index 000000000..6ed1fda90 --- /dev/null +++ b/packages/sdk-ts/src/client/abacus/grpc/transformers/index.ts @@ -0,0 +1,21 @@ +import { InjectiveAbacusRpc } from '@injectivelabs/abacus-proto-ts' + +export class AbacusGrpcTransformer { + static grpcPointsStatsDailyToPointsStatsDaily( + response: InjectiveAbacusRpc.PointsStatsDailyForAccountResponse, + ) { + return response.field + } + + static grpcPointsStatsWeeklyToPointsStatsWeekly( + response: InjectiveAbacusRpc.PointsStatsWeeklyForAccountResponse, + ) { + return response.field + } + + static grpcPointsLatestToPointsLatest( + response: InjectiveAbacusRpc.PointsLatestForAccountResponse, + ) { + return response + } +} diff --git a/packages/sdk-ts/src/client/abacus/index.ts b/packages/sdk-ts/src/client/abacus/index.ts new file mode 100644 index 000000000..42fd8cb49 --- /dev/null +++ b/packages/sdk-ts/src/client/abacus/index.ts @@ -0,0 +1 @@ +export * from './grpc/index.js' diff --git a/packages/sdk-ts/src/client/index.ts b/packages/sdk-ts/src/client/index.ts index 8a88a9388..51efe98d7 100644 --- a/packages/sdk-ts/src/client/index.ts +++ b/packages/sdk-ts/src/client/index.ts @@ -2,3 +2,4 @@ export * from './chain/index.js' export * from './indexer/index.js' export * from './wasm/index.js' export * from './olp/index.js' +export * from './abacus/index.js' diff --git a/yarn.lock b/yarn.lock index d18b5dbc6..42943d8e8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2183,6 +2183,16 @@ resolved "https://registry.npmjs.org/@hutson/parse-repository-url/-/parse-repository-url-3.0.2.tgz#98c23c950a3d9b6c8f0daed06da6c3af06981340" integrity sha512-H9XAx3hc0BQHY6l+IFSWHDySypcXsvsuLhgYLUGywmJ5pswRVQJUHpOsobnLYp2ZUaUlKiKDrgWWhosOwAEM8Q== +"@injectivelabs/abacus-proto-ts@1.13.0": + version "1.13.0" + resolved "https://registry.npmjs.org/@injectivelabs/abacus-proto-ts/-/abacus-proto-ts-1.13.0.tgz#cf5b0e683d9c51e01377f3137a2b8af65e798a13" + integrity sha512-yfMbTux7ljzDcmkKAZTHh3PuwjtlNfQ5PxFhEPO51gfQdRab0t3CqZ31rN+Q7LEaRMxZH/OxW2+ULLAUF07fsQ== + dependencies: + "@injectivelabs/grpc-web" "^0.0.1" + google-protobuf "^3.14.0" + protobufjs "^7.0.0" + rxjs "^7.4.0" + "@injectivelabs/core-proto-ts@1.13.4": version "1.13.4" resolved "https://registry.npmjs.org/@injectivelabs/core-proto-ts/-/core-proto-ts-1.13.4.tgz#c33568e471b8031ed66b1b954ac752f965e7f2bf"