From 8ddf0c10a9e89d6643c752b7a0359192e9e63dee Mon Sep 17 00:00:00 2001 From: thomasRalee Date: Wed, 2 Aug 2023 19:10:47 +0800 Subject: [PATCH] chore: mito api mission --- packages/sdk-ts/package.json | 2 +- .../indexer/grpc/IndexerGrpcMitoApi.spec.ts | 73 +++++++++++++++++++ .../client/indexer/grpc/IndexerGrpcMitoApi.ts | 60 ++++++++++++++- .../IndexerGrpcMitoTransformer.ts | 63 +++++++++++++--- .../sdk-ts/src/client/indexer/types/mito.ts | 26 ++++++- yarn.lock | 8 +- 6 files changed, 212 insertions(+), 20 deletions(-) diff --git a/packages/sdk-ts/package.json b/packages/sdk-ts/package.json index 24a5b6953..66bc06644 100644 --- a/packages/sdk-ts/package.json +++ b/packages/sdk-ts/package.json @@ -43,7 +43,7 @@ "@injectivelabs/grpc-web-node-http-transport": "^0.0.2", "@injectivelabs/grpc-web-react-native-transport": "^0.0.2", "@injectivelabs/indexer-proto-ts": "1.11.4", - "@injectivelabs/mito-proto-ts": "1.0.25", + "@injectivelabs/mito-proto-ts": "1.0.27", "@injectivelabs/networks": "^1.12.0-beta.14", "@injectivelabs/test-utils": "^1.12.0-beta.1", "@injectivelabs/token-metadata": "^1.12.0-beta.32", diff --git a/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcMitoApi.spec.ts b/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcMitoApi.spec.ts index 4e5638d46..c88fe6e55 100644 --- a/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcMitoApi.spec.ts +++ b/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcMitoApi.spec.ts @@ -244,4 +244,77 @@ describe('IndexerGrpcMitoApi', () => { ) } }) + + test('fetchLeaderboardEpochs', async () => { + try { + const response = await indexerGrpcMitoApi.fetchLeaderboardEpochs({}) + + if (response.epochs.length === 0) { + console.warn('fetchLeaderboardEpochs.responseIsEmptyArray') + } + + expect(response).toBeDefined() + expect(response).toEqual( + expect.objectContaining< + ReturnType< + typeof IndexerGrpcMitoTransformer.leaderboardEpochsResponseToLeaderboardEpochs + > + >(response), + ) + } catch (e) { + console.error( + 'IndexerGrpcMitoApi.fetchLeaderboardEpochs vaults => ' + + (e as any).message, + ) + } + }) + + test('fetchMissions', async () => { + try { + const response = await indexerGrpcMitoApi.fetchMissions({ + accountAddress: injectiveAddress, + }) + + if (response.length === 0) { + console.warn('fetchMissions.responseIsEmptyArray') + } + + expect(response).toBeDefined() + expect(response).toEqual( + expect.objectContaining< + ReturnType< + typeof IndexerGrpcMitoTransformer.mitoMissionsResponseMissions + > + >(response), + ) + } catch (e) { + console.error( + 'IndexerGrpcMitoApi.fetchMissions missions => ' + (e as any).message, + ) + } + }) + + test('fetchMissionLeaderboard', async () => { + try { + const response = await indexerGrpcMitoApi.fetchMissionLeaderboard() + + if (response.data.length === 0) { + console.warn('fetchMissionLeaderboard.responseIsEmptyArray') + } + + expect(response).toBeDefined() + expect(response).toEqual( + expect.objectContaining< + ReturnType< + typeof IndexerGrpcMitoTransformer.mitoMissionLeaderboardResponseToMissionLeaderboard + > + >(response), + ) + } catch (e) { + console.error( + 'IndexerGrpcMitoApi.fetchMissionLeaderboard missionLeaderboard => ' + + (e as any).message, + ) + } + }) }) diff --git a/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcMitoApi.ts b/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcMitoApi.ts index 0382bf9e6..d5d6ac7cb 100644 --- a/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcMitoApi.ts +++ b/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcMitoApi.ts @@ -585,14 +585,70 @@ export class IndexerGrpcMitoApi extends BaseGrpcConsumer { if (e instanceof InjectiveMetaRpc.GrpcWebError) { throw new GrpcUnaryRequestException(new Error(e.toString()), { code: e.code, - context: 'StakingHistory', + context: 'StakingReward', contextModule: this.module, }) } throw new GrpcUnaryRequestException(e as Error, { code: UnspecifiedErrorCode, - context: 'StakingHistory', + context: 'StakingReward', + contextModule: this.module, + }) + } + } + + async fetchMissions({ accountAddress }: { accountAddress: string }) { + const request = MitoApi.MissionsRequest.create() + + request.accountAddress = accountAddress + + try { + const response = await this.retry(() => + this.client.Missions(request), + ) + + return IndexerGrpcMitoTransformer.mitoMissionsResponseMissions(response) + } catch (e: unknown) { + if (e instanceof InjectiveMetaRpc.GrpcWebError) { + throw new GrpcUnaryRequestException(new Error(e.toString()), { + code: e.code, + context: 'Missions', + contextModule: this.module, + }) + } + + throw new GrpcUnaryRequestException(e as Error, { + code: UnspecifiedErrorCode, + context: 'Missions', + contextModule: this.module, + }) + } + } + + async fetchMissionLeaderboard() { + const request = MitoApi.MissionLeaderboardRequest.create() + + try { + const response = await this.retry( + () => this.client.MissionLeaderboard(request), + ) + + return IndexerGrpcMitoTransformer.mitoMissionLeaderboardResponseToMissionLeaderboard( + response, + ) + } catch (e: unknown) { + if (e instanceof InjectiveMetaRpc.GrpcWebError) { + throw new GrpcUnaryRequestException(new Error(e.toString()), { + code: e.code, + context: 'MissionLeaderboard', + contextModule: this.module, + }) + } + + throw new GrpcUnaryRequestException(e as Error, { + code: UnspecifiedErrorCode, + context: 'MissionLeaderboard', contextModule: this.module, }) } diff --git a/packages/sdk-ts/src/client/indexer/transformers/IndexerGrpcMitoTransformer.ts b/packages/sdk-ts/src/client/indexer/transformers/IndexerGrpcMitoTransformer.ts index 85ff137bb..f375121f9 100644 --- a/packages/sdk-ts/src/client/indexer/transformers/IndexerGrpcMitoTransformer.ts +++ b/packages/sdk-ts/src/client/indexer/transformers/IndexerGrpcMitoTransformer.ts @@ -4,6 +4,7 @@ import { MitoVault, MitoHolders, MitoChanges, + MitoMission, MitoTransfer, MitoPortfolio, MitoPagination, @@ -11,8 +12,10 @@ import { MitoDenomBalance, MitoSubscription, MitoPriceSnapshot, - MitoSubaccountBalance, MitoLeaderboardEpoch, + MitoSubaccountBalance, + MitoMissionLeaderboard, + MitoMissionLeaderboardEntry, } from '../types/mito' import { GrpcCoin } from '../../../types' @@ -257,6 +260,37 @@ export class IndexerGrpcMitoTransformer { } } + static mitoLpHolderToLPHolder(holder: MitoApi.Holders): MitoHolders { + return { + holderAddress: holder.holderAddress, + vaultAddress: holder.vaultAddress, + amount: holder.amount, + updatedAt: parseInt(holder.updatedAt, 10), + lpAmountPercentage: holder.lpAmountPercentage, + redemptionLockTime: holder.redemptionLockTime, + stakedAmount: holder.stakedAmount, + } + } + + static mitoMissionToMission(mission: MitoApi.Mission): MitoMission { + return { + id: mission.id, + points: mission.points, + completed: mission.completed, + accruedPoints: mission.accruedPoints, + updatedAt: parseInt(mission.updatedAt, 10), + } + } + + static mitoMissionLeaderboardEntryToMissionLeaderboardEntry( + entry: MitoApi.MissionLeaderboardEntry, + ): MitoMissionLeaderboardEntry { + return { + address: entry.address, + accruedPoints: entry.accruedPoints, + } + } + static vaultResponseToVault(response: MitoApi.GetVaultResponse): MitoVault { const [vault] = response.vault @@ -294,15 +328,9 @@ export class IndexerGrpcMitoTransformer { static lpHoldersResponseToLPHolders( response: MitoApi.LPHoldersResponse, ): MitoHolders[] { - return response.holders.map((holder) => ({ - holderAddress: holder.holderAddress, - vaultAddress: holder.vaultAddress, - amount: holder.amount, - updatedAt: parseInt(holder.updatedAt, 10), - lpAmountPercentage: holder.lpAmountPercentage, - redemptionLockTime: holder.redemptionLockTime, - stakedAmount: holder.stakedAmount, - })) + return response.holders.map( + IndexerGrpcMitoTransformer.mitoLpHolderToLPHolder, + ) } static transferHistoryResponseToTransfer( @@ -369,4 +397,19 @@ export class IndexerGrpcMitoTransformer { ), } } + + static mitoMissionsResponseMissions(response: MitoApi.MissionsResponse) { + return response.data.map(IndexerGrpcMitoTransformer.mitoMissionToMission) + } + + static mitoMissionLeaderboardResponseToMissionLeaderboard( + response: MitoApi.MissionLeaderboardResponse, + ): MitoMissionLeaderboard { + return { + entries: response.data.map( + IndexerGrpcMitoTransformer.mitoMissionLeaderboardEntryToMissionLeaderboardEntry, + ), + updatedAt: parseInt(response.updatedAt, 10), + } + } } diff --git a/packages/sdk-ts/src/client/indexer/types/mito.ts b/packages/sdk-ts/src/client/indexer/types/mito.ts index 89df90e4b..f43a746ab 100644 --- a/packages/sdk-ts/src/client/indexer/types/mito.ts +++ b/packages/sdk-ts/src/client/indexer/types/mito.ts @@ -148,17 +148,37 @@ export interface MitoStakingActivity { numberByAccount: number } +export interface MitoMission { + id: string + points: string + completed: boolean + accruedPoints: string + updatedAt: number +} + +export interface MitoMissionLeaderboardEntry { + address: string + accruedPoints: string +} + +export interface MitoMissionLeaderboard { + entries: MitoMissionLeaderboardEntry[] + updatedAt: number +} + export type GrpcMitoVault = MitoApi.Vault +export type GrpcMitoMission = MitoApi.Mission export type GrpcMitoChanges = MitoApi.Changes export type GrpcMitoHolders = MitoApi.Holders +export type GrpcMitoStakingGauge = MitoApi.Gauge export type GrpcMitoPagination = MitoApi.Pagination +export type GrpcMitoStakingPool = MitoApi.StakingPool export type GrpcMitoDenomBalance = MitoApi.DenomBalance export type GrpcMitoSubscription = MitoApi.Subscription export type GrpcMitoPriceSnapshot = MitoApi.PriceSnapshot export type GrpcMitoLeaderboardEntry = MitoApi.LeaderboardEntry export type GrpcMitoLeaderboardEpoch = MitoApi.LeaderboardEpoch -export type GrpcMitoSubaccountBalance = MitoApi.SubaccountBalance -export type GrpcMitoStakingGauge = MitoApi.Gauge -export type GrpcMitoStakingPool = MitoApi.StakingPool export type GrpcMitoStakingStakingReward = MitoApi.StakingReward +export type GrpcMitoSubaccountBalance = MitoApi.SubaccountBalance export type GrpcMitoStakingStakingActivity = MitoApi.StakingActivity +export type GrpcMitoMissionLeaderboardEntry = MitoApi.MissionLeaderboardEntry diff --git a/yarn.lock b/yarn.lock index e88858f8b..d3e2918c8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2166,10 +2166,10 @@ protobufjs "^7.0.0" rxjs "^7.4.0" -"@injectivelabs/mito-proto-ts@1.0.25": - version "1.0.25" - resolved "https://registry.yarnpkg.com/@injectivelabs/mito-proto-ts/-/mito-proto-ts-1.0.25.tgz#63828e2b8b48db6a7480caf9cc1ab5e0e502d6e2" - integrity sha512-sEt2WZsnltRnZn/xxnrgQ8f4B9TBCS4QapxjOCjha/gmiFHtTWIdGqdS90Bk6KDTiEmtgtKK2GUBhdrWT45CRw== +"@injectivelabs/mito-proto-ts@1.0.27": + version "1.0.27" + resolved "https://registry.yarnpkg.com/@injectivelabs/mito-proto-ts/-/mito-proto-ts-1.0.27.tgz#a06e4012f4f81829f99e343e083098fa8bd5633d" + integrity sha512-21aTRw0sBuyNCusO9ad4a4brSQuyFwga9LS14LkmThnymV4AkTHejoEv+0lPH9DPEzi6KhBca8KAO087/wHBnA== dependencies: "@injectivelabs/grpc-web" "^0.0.1" google-protobuf "^3.14.0"