Skip to content

Commit

Permalink
chore: package bump
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasRalee committed Nov 8, 2023
1 parent 020ad66 commit a858eee
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/sdk-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,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.11",
"@injectivelabs/indexer-proto-ts": "1.11.16",
"@injectivelabs/mito-proto-ts": "1.0.50",
"@injectivelabs/networks": "^1.14.2",
"@injectivelabs/test-utils": "^1.14.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { mockFactory } from '@injectivelabs/test-utils'
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'
import { IndexerCampaignTransformer } from '../transformers'
import { IndexerGrpcCampaignApi } from './IndexerGrpcCampaignApi'

const injectiveAddress = mockFactory.injectiveAddress
const CAMPAIGN_ID = 'spot-grid-inj-usdt-test'
const MARKET_ID =
'0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0'
const CAMPAIGN_CONTRACT = 'inj1yum4v0v5l92jkxn8xpn9mjg7wuldk784ysdn0w'
const endpoints = getNetworkEndpoints(Network.MainnetSentry)
const indexerGrpcCampaignApi = new IndexerGrpcCampaignApi(endpoints.indexer)

Expand All @@ -30,4 +33,69 @@ describe('IndexerGrpcCampaignApi', () => {
)
}
})

test('fetchGuilds', async () => {
try {
const response = await indexerGrpcCampaignApi.fetchGuilds({
sortBy: 'rank',
campaignContract: CAMPAIGN_CONTRACT,
})

expect(response).toBeDefined()
expect(response).toEqual(
expect.objectContaining<
ReturnType<typeof IndexerCampaignTransformer.GuildsResponseToGuilds>
>(response),
)
} catch (e) {
console.error(
'IndexerGrpcCampaignApi.fetchGuilds => ' + (e as any).message,
)
}
})

test('fetchGuildMember', async () => {
try {
const response = await indexerGrpcCampaignApi.fetchGuildMember({
address: injectiveAddress,
campaignContract: CAMPAIGN_CONTRACT,
})

expect(response).toBeDefined()
expect(response).toEqual(
expect.objectContaining<
ReturnType<
typeof IndexerCampaignTransformer.GuildMemberResponseToGuildMember
>
>(response),
)
} catch (e) {
console.error(
'IndexerGrpcCampaignApi.fetchGuildMember => ' + (e as any).message,
)
}
})

test('fetchGuildMembers', async () => {
try {
const response = await indexerGrpcCampaignApi.fetchGuildMembers({
guildId: '1',
includeGuildInfo: false,
campaignContract: CAMPAIGN_CONTRACT,
})

expect(response).toBeDefined()
expect(response).toEqual(
expect.objectContaining<
ReturnType<
typeof IndexerCampaignTransformer.GuildMembersResponseToGuildMembers
>
>(response),
)
} catch (e) {
console.error(
'IndexerGrpcCampaignApi.fetchGuildMembers => ' + (e as any).message,
)
}
})
})
139 changes: 139 additions & 0 deletions packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcCampaignApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,143 @@ export class IndexerGrpcCampaignApi extends BaseGrpcConsumer {
})
}
}

async fetchGuilds({
skip,
limit,
sortBy,
campaignContract,
}: {
skip?: string
limit?: number
sortBy: string
campaignContract: string
}) {
const request = InjectiveCampaignRpc.ListGuildsRequest.create()

request.sortBy = sortBy
request.campaignContract = campaignContract

if (skip) {
request.skip = skip
}

if (limit) {
request.limit = limit
}

try {
const response =
await this.retry<InjectiveCampaignRpc.ListGuildsResponse>(() =>
this.client.ListGuilds(request),
)

return IndexerCampaignTransformer.GuildsResponseToGuilds(response)
} catch (e: unknown) {
if (e instanceof InjectiveMetaRpc.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'FetchGuilds',
contextModule: this.module,
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'FetchGuilds',
contextModule: this.module,
})
}
}

async fetchGuildMember({
address,
campaignContract,
}: {
address: string
campaignContract: string
}) {
const request = InjectiveCampaignRpc.GetGuildMemberRequest.create()

request.address = address
request.campaignContract = campaignContract

try {
const response =
await this.retry<InjectiveCampaignRpc.GetGuildMemberResponse>(() =>
this.client.GetGuildMember(request),
)

return IndexerCampaignTransformer.GuildMemberResponseToGuildMember(
response,
)
} catch (e: unknown) {
if (e instanceof InjectiveMetaRpc.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'FetchGuildMember',
contextModule: this.module,
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'FetchGuildMember',
contextModule: this.module,
})
}
}

async fetchGuildMembers({
skip,
limit,
guildId,
campaignContract,
includeGuildInfo,
}: {
skip?: string
limit?: number
guildId: string
campaignContract: string
includeGuildInfo: boolean
}) {
const request = InjectiveCampaignRpc.ListGuildMembersRequest.create()

request.guildId = guildId
request.campaignContract = campaignContract
request.includeGuildInfo = includeGuildInfo

if (skip) {
request.skip = skip
}

if (limit) {
request.limit = limit
}

try {
const response =
await this.retry<InjectiveCampaignRpc.ListGuildMembersResponse>(() =>
this.client.ListGuildMembers(request),
)

return IndexerCampaignTransformer.GuildMembersResponseToGuildMembers(
response,
)
} catch (e: unknown) {
if (e instanceof InjectiveMetaRpc.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'FetchGuildMembers',
contextModule: this.module,
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'FetchGuildMembers',
contextModule: this.module,
})
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { InjectiveCampaignRpc } from '@injectivelabs/indexer-proto-ts'
import { grpcPagingToPaging } from '../../..//utils/pagination'
import { Campaign, CampaignUser } from '../types/campaign'
import {
Guild,
Campaign,
GuildMember,
CampaignUser,
GuildCampaignSummary,
} from '../types/campaign'

export class IndexerCampaignTransformer {
static GrpcCampaignUserToCampaignUser(
Expand Down Expand Up @@ -31,6 +37,55 @@ export class IndexerCampaignTransformer {
}
}

static GrpcGuildToGuild(guild: InjectiveCampaignRpc.Guild): Guild {
return {
campaignContract: guild.campaignContract,
guildId: guild.guildId,
masterAddress: guild.masterAddress,
createdAt: parseInt(guild.createdAt, 10),
tvlScore: guild.tvlScore,
volumeScore: guild.volumeScore,
rankByVolume: guild.rankByVolume,
rankByTvl: guild.rankByTvl,
logo: guild.logo,
totalTvl: guild.totalTvl,
updatedAt: parseInt(guild.updatedAt, 10),
name: guild.name,
isActive: guild.isActive,
}
}

static GrpcGuildMemberToGuildMember(
member: InjectiveCampaignRpc.GuildMember,
): GuildMember {
return {
campaignContract: member.campaignContract,
guildId: member.guildId,
address: member.address,
joinedAt: parseInt(member.joinedAt, 10),
tvlScore: member.tvlScore,
volumeScore: member.volumeScore,
totalTvl: member.totalTvl,
}
}

static GrpcGuildCampaignSummaryToGuildCampaignSummary(
campaignSummary: InjectiveCampaignRpc.CampaignSummary,
): GuildCampaignSummary {
return {
campaignId: campaignSummary.campaignId,
campaignContract: campaignSummary.campaignContract,
totalGuildsCount: campaignSummary.totalGuildsCount,
totalTvl: campaignSummary.totalTvl,
totalAverageTvl: campaignSummary.totalAverageTvl,
totalVolume: campaignSummary.totalVolume,
updatedAt: parseInt(campaignSummary.updatedAt, 10),
totalMembersCount: campaignSummary.totalMembersCount,
startTime: parseInt(campaignSummary.startTime, 10),
endTime: parseInt(campaignSummary.endTime, 10),
}
}

static CampaignResponseToCampaign(
response: InjectiveCampaignRpc.RankingResponse,
) {
Expand All @@ -44,4 +99,43 @@ export class IndexerCampaignTransformer {
paging: grpcPagingToPaging(response.paging),
}
}

static GuildsResponseToGuilds(
response: InjectiveCampaignRpc.ListGuildsResponse,
) {
return {
guilds: response.guilds.map(IndexerCampaignTransformer.GrpcGuildToGuild),
paging: grpcPagingToPaging(response.pagination),
updatedAt: parseInt(response.updatedAt, 10),
summary: response.campaignSummary
? IndexerCampaignTransformer.GrpcGuildCampaignSummaryToGuildCampaignSummary(
response.campaignSummary,
)
: undefined,
}
}

static GuildMemberResponseToGuildMember(
response: InjectiveCampaignRpc.GetGuildMemberResponse,
) {
return {
info: response.info
? IndexerCampaignTransformer.GrpcGuildMemberToGuildMember(response.info)
: undefined,
}
}

static GuildMembersResponseToGuildMembers(
response: InjectiveCampaignRpc.ListGuildMembersResponse,
) {
return {
members: response.members.map(
IndexerCampaignTransformer.GrpcGuildMemberToGuildMember,
),
paging: grpcPagingToPaging(response.pagination),
guildInfo: response.guildInfo
? IndexerCampaignTransformer.GrpcGuildToGuild(response.guildInfo)
: undefined,
}
}
}
41 changes: 41 additions & 0 deletions packages/sdk-ts/src/client/indexer/types/campaign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,46 @@ export interface CampaignUser {
blockTime: number
}

export interface Guild {
campaignContract: string
guildId: string
masterAddress: string
createdAt: number
tvlScore: string
volumeScore: string
rankByVolume: number
rankByTvl: number
logo: string
totalTvl: string
updatedAt: number
name: string
isActive: boolean
}

export interface GuildMember {
campaignContract: string
guildId: string
address: string
joinedAt: number
tvlScore: string
volumeScore: string
totalTvl: string
}

export interface GuildCampaignSummary {
campaignId: string
campaignContract: string
totalGuildsCount: number
totalTvl: string
totalAverageTvl: string
totalVolume: string
updatedAt: number
totalMembersCount: number
startTime: number
endTime: number
}

export type GrpcGuild = InjectiveCampaignRpc.Guild
export type GrpcCampaign = InjectiveCampaignRpc.Campaign
export type GrpcGuildMember = InjectiveCampaignRpc.GuildMember
export type GrpcCampaignUser = InjectiveCampaignRpc.CampaignUser
Loading

0 comments on commit a858eee

Please sign in to comment.