Skip to content

Commit

Permalink
feat: grpc indexer campaign api
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasRalee committed Oct 29, 2023
1 parent 1898fd0 commit 1a2c561
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/stable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
node-version: '16.18.1'

- name: 'Setup lerna@6.6.1'
run: yarn global add lerna@6.6.1 --ignore-engines
run: yarn global add lerna@6.6.1 --ignore-engines

- name: Set up Git user
run: |
Expand All @@ -32,7 +32,7 @@ jobs:
- name: Build dependencies
run: |
node etc/bootstrapEnv
yarn install --ignore-engines
yarn install --ignore-engines
yarn build
- name: 'Setup npm'
Expand Down
1 change: 1 addition & 0 deletions packages/exceptions/src/types/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export enum IndexerErrorModule {
ChronosDerivative = 'indexer-chronos-derivative',
ChronosSpot = 'indexer-chronos-spot',
ChronosMarkets = 'indexer-chronos-markets',
Campaign = 'indexer-campaign',
}

export enum WalletErrorActionModule {
Expand Down
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.10",
"@injectivelabs/indexer-proto-ts": "1.11.11",
"@injectivelabs/mito-proto-ts": "1.0.50",
"@injectivelabs/networks": "^1.14.1",
"@injectivelabs/test-utils": "^1.14.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'
import { IndexerCampaignTransformer } from '../transformers'
import { IndexerGrpcCampaignApi } from './IndexerGrpcCampaignApi'

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

describe('IndexerGrpcCampaignApi', () => {
test('fetchCampaign', async () => {
try {
const response = await indexerGrpcCampaignApi.fetchCampaign({
marketId: MARKET_ID,
campaignId: CAMPAIGN_ID,
})

expect(response).toBeDefined()
expect(response).toEqual(
expect.objectContaining<
ReturnType<
typeof IndexerCampaignTransformer.CampaignResponseToCampaign
>
>(response),
)
} catch (e) {
console.error(
'IndexerGrpcCampaignApi.fetchCampaign => ' + (e as any).message,
)
}
})
})
79 changes: 79 additions & 0 deletions packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcCampaignApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
UnspecifiedErrorCode,
GrpcUnaryRequestException,
} from '@injectivelabs/exceptions'
import { InjectiveMetaRpc } from '@injectivelabs/indexer-proto-ts'
import { InjectiveCampaignRpc } from '@injectivelabs/indexer-proto-ts'
import BaseGrpcConsumer from '../../BaseGrpcConsumer'
import { IndexerCampaignTransformer } from '../transformers'
import { IndexerModule } from '../types'

/**
* @category Indexer Grpc API
*/
export class IndexerGrpcCampaignApi extends BaseGrpcConsumer {
protected module: string = IndexerModule.Campaign

protected client: InjectiveCampaignRpc.InjectiveCampaignRPCClientImpl

constructor(endpoint: string) {
super(endpoint)

this.client = new InjectiveCampaignRpc.InjectiveCampaignRPCClientImpl(
this.getGrpcWebImpl(endpoint),
)
}

async fetchCampaign({
skip,
limit,
marketId,
campaignId,
accountAddress,
}: {
skip?: string
limit?: number
marketId: string
campaignId: string
accountAddress?: string
}) {
const request = InjectiveCampaignRpc.RankingRequest.create()

request.marketId = marketId
request.campaignId = campaignId

if (skip) {
request.skip = skip
}

if (limit) {
request.limit = limit
}

if (accountAddress) {
request.accountAddress = accountAddress
}

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

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

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'FetchCampaign',
contextModule: this.module,
})
}
}
}
15 changes: 8 additions & 7 deletions packages/sdk-ts/src/client/indexer/grpc/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export { IndexerGrpcAccountApi } from './IndexerGrpcAccountApi'
export { IndexerGrpcAccountPortfolioApi } from './IndexerGrpcPortfolioApi'
export { IndexerGrpcAuctionApi } from './IndexerGrpcAuctionApi'
export { IndexerGrpcExplorerApi } from './IndexerGrpcExplorerApi'
export { IndexerGrpcMitoApi } from './IndexerGrpcMitoApi'
export { IndexerGrpcMetaApi } from './IndexerGrpcMetaApi'
export { IndexerGrpcSpotApi } from './IndexerGrpcSpotApi'
export { IndexerGrpcOracleApi } from './IndexerGrpcOracleApi'
export { IndexerGrpcInsuranceFundApi } from './IndexerGrpcInsuranceFundApi'
export { IndexerGrpcAccountApi } from './IndexerGrpcAccountApi'
export { IndexerGrpcAuctionApi } from './IndexerGrpcAuctionApi'
export { IndexerGrpcTradingApi } from './IndexerGrpcTradingApi'
export { IndexerGrpcExplorerApi } from './IndexerGrpcExplorerApi'
export { IndexerGrpcCampaignApi } from './IndexerGrpcCampaignApi'
export { IndexerGrpcDerivativesApi } from './IndexerGrpcDerivativesApi'
export { IndexerGrpcSpotApi } from './IndexerGrpcSpotApi'
export { IndexerGrpcTransactionApi } from './IndexerGrpcTransactionApi'
export { IndexerGrpcTradingApi } from './IndexerGrpcTradingApi'
export { IndexerGrpcAccountPortfolioApi } from './IndexerGrpcPortfolioApi'
export { IndexerGrpcInsuranceFundApi } from './IndexerGrpcInsuranceFundApi'
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { InjectiveCampaignRpc } from '@injectivelabs/indexer-proto-ts'
import { grpcPagingToPaging } from '../../..//utils/pagination'
import { Campaign, CampaignUser } from '../types/campaign'

export class IndexerCampaignTransformer {
static GrpcCampaignUserToCampaignUser(
campaignUser: InjectiveCampaignRpc.CampaignUser,
): CampaignUser {
return {
campaignId: campaignUser.campaignId,
marketId: campaignUser.marketId,
accountAddress: campaignUser.accountAddress,
score: campaignUser.score,
contractUpdated: campaignUser.contractUpdated,
blockHeight: campaignUser.blockHeight,
blockTime: parseInt(campaignUser.blockTime, 10),
}
}

static GrpcCampaignToCampaign(
campaign: InjectiveCampaignRpc.Campaign,
): Campaign {
return {
campaignId: campaign.campaignId,
marketId: campaign.marketId,
totalScore: campaign.totalScore,
lastUpdated: parseInt(campaign.lastUpdated, 10),
startDate: parseInt(campaign.startDate, 10),
endDate: parseInt(campaign.endDate, 10),
isClaimable: campaign.isClaimable,
}
}

static CampaignResponseToCampaign(
response: InjectiveCampaignRpc.RankingResponse,
) {
return {
campaign: response.campaign
? IndexerCampaignTransformer.GrpcCampaignToCampaign(response.campaign)
: undefined,
uses: response.users.map(
IndexerCampaignTransformer.GrpcCampaignUserToCampaignUser,
),
paging: grpcPagingToPaging(response.paging),
}
}
}
1 change: 1 addition & 0 deletions packages/sdk-ts/src/client/indexer/transformers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './IndexerGrpcMitoTransformer'
export * from './IndexerGrpcSpotTransformer'
export * from './IndexerCampaignTransformer'
export * from './IndexerGrpcOracleTransformer'
export * from './IndexerSpotStreamTransformer'
export * from './IndexerGrpcAccountTransformer'
Expand Down
24 changes: 24 additions & 0 deletions packages/sdk-ts/src/client/indexer/types/campaign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { InjectiveCampaignRpc } from '@injectivelabs/indexer-proto-ts'

export interface Campaign {
campaignId: string
marketId: string
totalScore: string
lastUpdated: number
startDate: number
endDate: number
isClaimable: boolean
}

export interface CampaignUser {
campaignId: string
marketId: string
accountAddress: string
score: string
contractUpdated: boolean
blockHeight: string
blockTime: number
}

export type GrpcCampaign = InjectiveCampaignRpc.Campaign
export type GrpcCampaignUser = InjectiveCampaignRpc.CampaignUser
19 changes: 10 additions & 9 deletions packages/sdk-ts/src/client/indexer/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { IndexerErrorModule } from '@injectivelabs/exceptions'

export * from './mito'
export * from './swap'
export * from './spot'
export * from './oracle'
export * from './account'
export * from './account-portfolio'
export * from './auction'
export * from './derivatives-rest'
export * from './derivatives'
export * from './trading'
export * from './exchange'
export * from './explorer'
export * from './campaign'
export * from './spot-rest'
export * from './derivatives'
export * from './explorer-rest'
export * from './insurance-funds'
export * from './derivatives-rest'
export * from './leaderboard-rest'
export * from './account-portfolio'
export * from './markets-history-rest'
export * from './mito'
export * from './swap'
export * from './oracle'
export * from './spot-rest'
export * from './spot'
export * from './trading'

export interface StreamStatusResponse {
details: string
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2166,10 +2166,10 @@
dependencies:
browser-headers "^0.4.1"

"@injectivelabs/indexer-proto-ts@1.11.10":
version "1.11.10"
resolved "https://registry.yarnpkg.com/@injectivelabs/indexer-proto-ts/-/indexer-proto-ts-1.11.10.tgz#91649b2634d31a003ba2e928eba4f2de821dc9a3"
integrity sha512-KpGrXYePUhIZfr/ApD6dw0l3bv222yTfVoqqbAWGtmoefeT18Lu490ots9qMsJWfokTsJeWBb1M9ce5JvaSA3Q==
"@injectivelabs/indexer-proto-ts@1.11.11":
version "1.11.11"
resolved "https://registry.yarnpkg.com/@injectivelabs/indexer-proto-ts/-/indexer-proto-ts-1.11.11.tgz#faee76f81f0bc5566a295fa4a069312f17810804"
integrity sha512-1zDIhF83fld/FNrQ43rX7fLFoY7zXneqqufXWMKh2ekFOAnXdyyPc832keNG7hczCbxG4trw/Tgpos0C9J7ndg==
dependencies:
"@injectivelabs/grpc-web" "^0.0.1"
google-protobuf "^3.14.0"
Expand Down

0 comments on commit 1a2c561

Please sign in to comment.