Skip to content

Commit

Permalink
chore: campaignv2
Browse files Browse the repository at this point in the history
  • Loading branch information
shane-moore committed Jul 29, 2024
1 parent 80546db commit e4a5789
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/sdk-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
})
})
42 changes: 42 additions & 0 deletions packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcCampaignApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<InjectiveCampaignRpc.CampaignsV2Response>(() =>
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { grpcPagingToPaging } from '../../..//utils/pagination'
import {
Guild,
Campaign,
CampaignV2,
GuildMember,
CampaignUser,
GuildCampaignSummary,
Expand Down Expand Up @@ -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,
}
}
}
21 changes: 21 additions & 0 deletions packages/sdk-ts/src/client/indexer/types/campaign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit e4a5789

Please sign in to comment.