-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1898fd0
commit 1a2c561
Showing
11 changed files
with
210 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcCampaignApi.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
79
packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcCampaignApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
47 changes: 47 additions & 0 deletions
47
packages/sdk-ts/src/client/indexer/transformers/IndexerCampaignTransformer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters