From e4a57895cbe9fc9c3842a97ecbd272b48cf262ae Mon Sep 17 00:00:00 2001 From: shane-moore Date: Mon, 29 Jul 2024 12:40:50 -0700 Subject: [PATCH] chore: campaignv2 --- packages/sdk-ts/package.json | 2 +- .../grpc/IndexerGrpcCampaignApi.spec.ts | 24 +++++++++++ .../indexer/grpc/IndexerGrpcCampaignApi.ts | 42 +++++++++++++++++++ .../IndexerCampaignTransformer.ts | 36 ++++++++++++++++ .../src/client/indexer/types/campaign.ts | 21 ++++++++++ yarn.lock | 8 ++-- 6 files changed, 128 insertions(+), 5 deletions(-) diff --git a/packages/sdk-ts/package.json b/packages/sdk-ts/package.json index 79abfbe45..eca760bdb 100644 --- a/packages/sdk-ts/package.json +++ b/packages/sdk-ts/package.json @@ -43,7 +43,7 @@ "@injectivelabs/grpc-web": "^0.0.1", "@injectivelabs/grpc-web-node-http-transport": "^0.0.2", "@injectivelabs/grpc-web-react-native-transport": "^0.0.2", - "@injectivelabs/indexer-proto-ts": "1.11.44", + "@injectivelabs/indexer-proto-ts": "1.11.46", "@injectivelabs/mito-proto-ts": "1.0.65", "@injectivelabs/networks": "^1.14.13-dev.3", "@injectivelabs/test-utils": "^1.14.13-beta.0", diff --git a/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcCampaignApi.spec.ts b/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcCampaignApi.spec.ts index 3ea6b968f..3095f4619 100644 --- a/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcCampaignApi.spec.ts +++ b/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcCampaignApi.spec.ts @@ -98,4 +98,28 @@ describe.skip('IndexerGrpcCampaignApi', () => { ) } }) + + test('fetchCampaigns', async () => { + try { + const response = await indexerGrpcCampaignApi.fetchCampaigns({ + type: 'reward', + active: true, + limit: 10, + cursor: '', + }) + + expect(response).toBeDefined() + expect(response).toEqual( + expect.objectContaining< + ReturnType< + typeof IndexerCampaignTransformer.CampaignsV2ResponseToCampaigns + > + >(response), + ) + } catch (e) { + console.error( + 'IndexerGrpcCampaignApi.fetchCampaigns => ' + (e as any).message, + ) + } + }) }) diff --git a/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcCampaignApi.ts b/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcCampaignApi.ts index 76b31a04d..5ebdb8b1a 100644 --- a/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcCampaignApi.ts +++ b/packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcCampaignApi.ts @@ -85,6 +85,48 @@ export class IndexerGrpcCampaignApi extends BaseGrpcConsumer { } } + async fetchCampaigns({ + type, + active, + limit, + cursor, + }: { + type: string + active: boolean + limit: number + cursor: string + }) { + const request = InjectiveCampaignRpc.CampaignsV2Request.create() + + request.type = type + request.active = active + request.limit = limit + request.cursor = cursor + console.log({ request, type, active, limit, cursor }) + try { + const response = + await this.retry(() => + this.client.CampaignsV2(request), + ) + + return IndexerCampaignTransformer.CampaignsV2ResponseToCampaigns(response) + } catch (e: unknown) { + if (e instanceof InjectiveMetaRpc.GrpcWebError) { + throw new GrpcUnaryRequestException(new Error(e.toString()), { + code: e.code, + context: 'FetchCampaigns', + contextModule: this.module, + }) + } + + throw new GrpcUnaryRequestException(e as Error, { + code: UnspecifiedErrorCode, + context: 'FetchCampaigns', + contextModule: this.module, + }) + } + } + async fetchRound({ roundId, toRoundId, diff --git a/packages/sdk-ts/src/client/indexer/transformers/IndexerCampaignTransformer.ts b/packages/sdk-ts/src/client/indexer/transformers/IndexerCampaignTransformer.ts index 503ad7cae..c9f602027 100644 --- a/packages/sdk-ts/src/client/indexer/transformers/IndexerCampaignTransformer.ts +++ b/packages/sdk-ts/src/client/indexer/transformers/IndexerCampaignTransformer.ts @@ -4,6 +4,7 @@ import { grpcPagingToPaging } from '../../..//utils/pagination' import { Guild, Campaign, + CampaignV2, GuildMember, CampaignUser, GuildCampaignSummary, @@ -177,4 +178,39 @@ export class IndexerCampaignTransformer { : undefined, } } + + static GrpcCampaignV2ToCampaignV2( + campaign: InjectiveCampaignRpc.CampaignV2, + ): CampaignV2 { + return { + campaignId: campaign.campaignId, + marketId: campaign.marketId, + totalScore: campaign.totalScore, + createdAt: campaign.createdAt, + updatedAt: campaign.updatedAt, + startDate: campaign.startDate, + endDate: campaign.endDate, + isClaimable: campaign.isClaimable, + roundId: campaign.roundId, + managerContract: campaign.managerContract, + rewards: campaign.rewards.map(IndexerCampaignTransformer.GrpcCoinToCoin), + subaccountIdSuffix: campaign.subaccountIdSuffix, + rewardContract: campaign.rewardContract, + type: campaign.type, + version: campaign.version, + name: campaign.name, + description: campaign.description, + } + } + + static CampaignsV2ResponseToCampaigns( + response: InjectiveCampaignRpc.CampaignsV2Response, + ) { + return { + campaigns: response.campaigns.map( + IndexerCampaignTransformer.GrpcCampaignV2ToCampaignV2, + ), + cursor: response.cursor, + } + } } diff --git a/packages/sdk-ts/src/client/indexer/types/campaign.ts b/packages/sdk-ts/src/client/indexer/types/campaign.ts index f36e1cbe5..97e30ad94 100644 --- a/packages/sdk-ts/src/client/indexer/types/campaign.ts +++ b/packages/sdk-ts/src/client/indexer/types/campaign.ts @@ -74,7 +74,28 @@ export interface GuildCampaignSummary { endTime: number } +export interface CampaignV2 { + campaignId: string + marketId: string + totalScore: string + createdAt: string + updatedAt: string + startDate: string + endDate: string + isClaimable: boolean + roundId: number + managerContract: string + rewards: Coin[] + subaccountIdSuffix: string + rewardContract: string + type: string + version: string + name: string + description: string +} + export type GrpcGuild = InjectiveCampaignRpc.Guild export type GrpcCampaign = InjectiveCampaignRpc.Campaign export type GrpcGuildMember = InjectiveCampaignRpc.GuildMember export type GrpcCampaignUser = InjectiveCampaignRpc.CampaignUser +export type GrpcCampaignV2 = InjectiveCampaignRpc.CampaignV2 diff --git a/yarn.lock b/yarn.lock index bd3d157a7..bb69920dc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2222,10 +2222,10 @@ dependencies: browser-headers "^0.4.1" -"@injectivelabs/indexer-proto-ts@1.11.44": - version "1.11.44" - resolved "https://registry.yarnpkg.com/@injectivelabs/indexer-proto-ts/-/indexer-proto-ts-1.11.44.tgz#f3d48c421d835d9362d4cc604215295cb14164d7" - integrity sha512-D8/y6BE9h2mvMPlPz0GIwEr/0Ge0LdqBnYP9iwvI+4B3XUoE6sCFliE+Q0+4moj7kodsS3lpZ5qm0Q3s9Nun4g== +"@injectivelabs/indexer-proto-ts@1.11.46": + version "1.11.46" + resolved "https://registry.yarnpkg.com/@injectivelabs/indexer-proto-ts/-/indexer-proto-ts-1.11.46.tgz#f65b6bcf2391605dded471988e915215bdfb40be" + integrity sha512-HpZrwJR7OreyhUSbOyPRGZHbNeEQXGwZkrfYFfN0/tT3JOBtCs9DIF9B4oA744NUlq4V+SB5A20tm4kq2GfQkA== dependencies: "@injectivelabs/grpc-web" "^0.0.1" google-protobuf "^3.14.0"