Skip to content

Commit

Permalink
chore: mito api mission
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasRalee committed Aug 2, 2023
1 parent d26211e commit 8ddf0c1
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 20 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-node-http-transport": "^0.0.2",
"@injectivelabs/grpc-web-react-native-transport": "^0.0.2",
"@injectivelabs/indexer-proto-ts": "1.11.4",
"@injectivelabs/mito-proto-ts": "1.0.25",
"@injectivelabs/mito-proto-ts": "1.0.27",
"@injectivelabs/networks": "^1.12.0-beta.14",
"@injectivelabs/test-utils": "^1.12.0-beta.1",
"@injectivelabs/token-metadata": "^1.12.0-beta.32",
Expand Down
73 changes: 73 additions & 0 deletions packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcMitoApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,77 @@ describe('IndexerGrpcMitoApi', () => {
)
}
})

test('fetchLeaderboardEpochs', async () => {
try {
const response = await indexerGrpcMitoApi.fetchLeaderboardEpochs({})

if (response.epochs.length === 0) {
console.warn('fetchLeaderboardEpochs.responseIsEmptyArray')
}

expect(response).toBeDefined()
expect(response).toEqual(
expect.objectContaining<
ReturnType<
typeof IndexerGrpcMitoTransformer.leaderboardEpochsResponseToLeaderboardEpochs
>
>(response),
)
} catch (e) {
console.error(
'IndexerGrpcMitoApi.fetchLeaderboardEpochs vaults => ' +
(e as any).message,
)
}
})

test('fetchMissions', async () => {
try {
const response = await indexerGrpcMitoApi.fetchMissions({
accountAddress: injectiveAddress,
})

if (response.length === 0) {
console.warn('fetchMissions.responseIsEmptyArray')
}

expect(response).toBeDefined()
expect(response).toEqual(
expect.objectContaining<
ReturnType<
typeof IndexerGrpcMitoTransformer.mitoMissionsResponseMissions
>
>(response),
)
} catch (e) {
console.error(
'IndexerGrpcMitoApi.fetchMissions missions => ' + (e as any).message,
)
}
})

test('fetchMissionLeaderboard', async () => {
try {
const response = await indexerGrpcMitoApi.fetchMissionLeaderboard()

if (response.data.length === 0) {
console.warn('fetchMissionLeaderboard.responseIsEmptyArray')
}

expect(response).toBeDefined()
expect(response).toEqual(
expect.objectContaining<
ReturnType<
typeof IndexerGrpcMitoTransformer.mitoMissionLeaderboardResponseToMissionLeaderboard
>
>(response),
)
} catch (e) {
console.error(
'IndexerGrpcMitoApi.fetchMissionLeaderboard missionLeaderboard => ' +
(e as any).message,
)
}
})
})
60 changes: 58 additions & 2 deletions packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcMitoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,70 @@ export class IndexerGrpcMitoApi extends BaseGrpcConsumer {
if (e instanceof InjectiveMetaRpc.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'StakingHistory',
context: 'StakingReward',
contextModule: this.module,
})
}

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

async fetchMissions({ accountAddress }: { accountAddress: string }) {
const request = MitoApi.MissionsRequest.create()

request.accountAddress = accountAddress

try {
const response = await this.retry<MitoApi.MissionsResponse>(() =>
this.client.Missions(request),
)

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

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

async fetchMissionLeaderboard() {
const request = MitoApi.MissionLeaderboardRequest.create()

try {
const response = await this.retry<MitoApi.MissionLeaderboardResponse>(
() => this.client.MissionLeaderboard(request),
)

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

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'MissionLeaderboard',
contextModule: this.module,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import {
MitoVault,
MitoHolders,
MitoChanges,
MitoMission,
MitoTransfer,
MitoPortfolio,
MitoPagination,
MitoLeaderboard,
MitoDenomBalance,
MitoSubscription,
MitoPriceSnapshot,
MitoSubaccountBalance,
MitoLeaderboardEpoch,
MitoSubaccountBalance,
MitoMissionLeaderboard,
MitoMissionLeaderboardEntry,
} from '../types/mito'
import { GrpcCoin } from '../../../types'

Expand Down Expand Up @@ -257,6 +260,37 @@ export class IndexerGrpcMitoTransformer {
}
}

static mitoLpHolderToLPHolder(holder: MitoApi.Holders): MitoHolders {
return {
holderAddress: holder.holderAddress,
vaultAddress: holder.vaultAddress,
amount: holder.amount,
updatedAt: parseInt(holder.updatedAt, 10),
lpAmountPercentage: holder.lpAmountPercentage,
redemptionLockTime: holder.redemptionLockTime,
stakedAmount: holder.stakedAmount,
}
}

static mitoMissionToMission(mission: MitoApi.Mission): MitoMission {
return {
id: mission.id,
points: mission.points,
completed: mission.completed,
accruedPoints: mission.accruedPoints,
updatedAt: parseInt(mission.updatedAt, 10),
}
}

static mitoMissionLeaderboardEntryToMissionLeaderboardEntry(
entry: MitoApi.MissionLeaderboardEntry,
): MitoMissionLeaderboardEntry {
return {
address: entry.address,
accruedPoints: entry.accruedPoints,
}
}

static vaultResponseToVault(response: MitoApi.GetVaultResponse): MitoVault {
const [vault] = response.vault

Expand Down Expand Up @@ -294,15 +328,9 @@ export class IndexerGrpcMitoTransformer {
static lpHoldersResponseToLPHolders(
response: MitoApi.LPHoldersResponse,
): MitoHolders[] {
return response.holders.map((holder) => ({
holderAddress: holder.holderAddress,
vaultAddress: holder.vaultAddress,
amount: holder.amount,
updatedAt: parseInt(holder.updatedAt, 10),
lpAmountPercentage: holder.lpAmountPercentage,
redemptionLockTime: holder.redemptionLockTime,
stakedAmount: holder.stakedAmount,
}))
return response.holders.map(
IndexerGrpcMitoTransformer.mitoLpHolderToLPHolder,
)
}

static transferHistoryResponseToTransfer(
Expand Down Expand Up @@ -369,4 +397,19 @@ export class IndexerGrpcMitoTransformer {
),
}
}

static mitoMissionsResponseMissions(response: MitoApi.MissionsResponse) {
return response.data.map(IndexerGrpcMitoTransformer.mitoMissionToMission)
}

static mitoMissionLeaderboardResponseToMissionLeaderboard(
response: MitoApi.MissionLeaderboardResponse,
): MitoMissionLeaderboard {
return {
entries: response.data.map(
IndexerGrpcMitoTransformer.mitoMissionLeaderboardEntryToMissionLeaderboardEntry,
),
updatedAt: parseInt(response.updatedAt, 10),
}
}
}
26 changes: 23 additions & 3 deletions packages/sdk-ts/src/client/indexer/types/mito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,37 @@ export interface MitoStakingActivity {
numberByAccount: number
}

export interface MitoMission {
id: string
points: string
completed: boolean
accruedPoints: string
updatedAt: number
}

export interface MitoMissionLeaderboardEntry {
address: string
accruedPoints: string
}

export interface MitoMissionLeaderboard {
entries: MitoMissionLeaderboardEntry[]
updatedAt: number
}

export type GrpcMitoVault = MitoApi.Vault
export type GrpcMitoMission = MitoApi.Mission
export type GrpcMitoChanges = MitoApi.Changes
export type GrpcMitoHolders = MitoApi.Holders
export type GrpcMitoStakingGauge = MitoApi.Gauge
export type GrpcMitoPagination = MitoApi.Pagination
export type GrpcMitoStakingPool = MitoApi.StakingPool
export type GrpcMitoDenomBalance = MitoApi.DenomBalance
export type GrpcMitoSubscription = MitoApi.Subscription
export type GrpcMitoPriceSnapshot = MitoApi.PriceSnapshot
export type GrpcMitoLeaderboardEntry = MitoApi.LeaderboardEntry
export type GrpcMitoLeaderboardEpoch = MitoApi.LeaderboardEpoch
export type GrpcMitoSubaccountBalance = MitoApi.SubaccountBalance
export type GrpcMitoStakingGauge = MitoApi.Gauge
export type GrpcMitoStakingPool = MitoApi.StakingPool
export type GrpcMitoStakingStakingReward = MitoApi.StakingReward
export type GrpcMitoSubaccountBalance = MitoApi.SubaccountBalance
export type GrpcMitoStakingStakingActivity = MitoApi.StakingActivity
export type GrpcMitoMissionLeaderboardEntry = MitoApi.MissionLeaderboardEntry
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2166,10 +2166,10 @@
protobufjs "^7.0.0"
rxjs "^7.4.0"

"@injectivelabs/[email protected].25":
version "1.0.25"
resolved "https://registry.yarnpkg.com/@injectivelabs/mito-proto-ts/-/mito-proto-ts-1.0.25.tgz#63828e2b8b48db6a7480caf9cc1ab5e0e502d6e2"
integrity sha512-sEt2WZsnltRnZn/xxnrgQ8f4B9TBCS4QapxjOCjha/gmiFHtTWIdGqdS90Bk6KDTiEmtgtKK2GUBhdrWT45CRw==
"@injectivelabs/[email protected].27":
version "1.0.27"
resolved "https://registry.yarnpkg.com/@injectivelabs/mito-proto-ts/-/mito-proto-ts-1.0.27.tgz#a06e4012f4f81829f99e343e083098fa8bd5633d"
integrity sha512-21aTRw0sBuyNCusO9ad4a4brSQuyFwga9LS14LkmThnymV4AkTHejoEv+0lPH9DPEzi6KhBca8KAO087/wHBnA==
dependencies:
"@injectivelabs/grpc-web" "^0.0.1"
google-protobuf "^3.14.0"
Expand Down

0 comments on commit 8ddf0c1

Please sign in to comment.