|
| 1 | +import { |
| 2 | + IndexerErrorModule, |
| 3 | + UnspecifiedErrorCode, |
| 4 | + GrpcUnaryRequestException, |
| 5 | +} from '@injectivelabs/exceptions' |
| 6 | +import { InjectiveAbacusRpc } from '@injectivelabs/abacus-proto-ts' |
| 7 | +import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js' |
| 8 | +import { AbacusGrpcTransformer } from './transformers/index.js' |
| 9 | + |
| 10 | +export class AbacusGrpcApi extends BaseGrpcConsumer { |
| 11 | + protected module: string = IndexerErrorModule.Abacus |
| 12 | + |
| 13 | + protected client: InjectiveAbacusRpc.PointsSvcClientImpl |
| 14 | + |
| 15 | + constructor(endpoint: string) { |
| 16 | + super(endpoint) |
| 17 | + |
| 18 | + this.client = new InjectiveAbacusRpc.PointsSvcClientImpl( |
| 19 | + this.getGrpcWebImpl(endpoint), |
| 20 | + ) |
| 21 | + } |
| 22 | + |
| 23 | + async fetchAccountLatestPoints(address: string) { |
| 24 | + const request = InjectiveAbacusRpc.PointsLatestForAccountRequest.create() |
| 25 | + |
| 26 | + request.accountAddress = address |
| 27 | + |
| 28 | + try { |
| 29 | + const response = |
| 30 | + await this.retry<InjectiveAbacusRpc.PointsLatestForAccountResponse>( |
| 31 | + () => this.client.PointsLatestForAccount(request), |
| 32 | + ) |
| 33 | + |
| 34 | + return AbacusGrpcTransformer.grpcPointsLatestToPointsLatest(response) |
| 35 | + } catch (e: unknown) { |
| 36 | + if (e instanceof InjectiveAbacusRpc.GrpcWebError) { |
| 37 | + throw new GrpcUnaryRequestException(new Error(e.toString()), { |
| 38 | + code: e.code, |
| 39 | + context: 'PointsStatsLatestForAccount', |
| 40 | + contextModule: this.module, |
| 41 | + }) |
| 42 | + } |
| 43 | + |
| 44 | + throw new GrpcUnaryRequestException(e as Error, { |
| 45 | + code: UnspecifiedErrorCode, |
| 46 | + context: 'PointsStatsLatestForAccount', |
| 47 | + contextModule: this.module, |
| 48 | + }) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + async fetchAccountDailyPoints(address: string, daysLimit?: number) { |
| 53 | + const request = |
| 54 | + InjectiveAbacusRpc.PointsStatsDailyForAccountRequest.create() |
| 55 | + |
| 56 | + request.accountAddress = address |
| 57 | + |
| 58 | + if (daysLimit) { |
| 59 | + request.daysLimit = daysLimit |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + const response = |
| 64 | + await this.retry<InjectiveAbacusRpc.PointsStatsDailyForAccountResponse>( |
| 65 | + () => this.client.PointsStatsDailyForAccount(request), |
| 66 | + ) |
| 67 | + |
| 68 | + return AbacusGrpcTransformer.grpcPointsStatsDailyToPointsStatsDaily( |
| 69 | + response, |
| 70 | + ) |
| 71 | + } catch (e: unknown) { |
| 72 | + if (e instanceof InjectiveAbacusRpc.GrpcWebError) { |
| 73 | + throw new GrpcUnaryRequestException(new Error(e.toString()), { |
| 74 | + code: e.code, |
| 75 | + context: 'PointsStatsDailyForAccount', |
| 76 | + contextModule: this.module, |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + throw new GrpcUnaryRequestException(e as Error, { |
| 81 | + code: UnspecifiedErrorCode, |
| 82 | + context: 'PointsStatsDailyForAccount', |
| 83 | + contextModule: this.module, |
| 84 | + }) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + async fetchAccountWeeklyPoints(address: string, weeksLimit?: number) { |
| 89 | + const request = |
| 90 | + InjectiveAbacusRpc.PointsStatsWeeklyForAccountRequest.create() |
| 91 | + |
| 92 | + request.accountAddress = address |
| 93 | + |
| 94 | + if (weeksLimit) { |
| 95 | + request.weeksLimit = weeksLimit |
| 96 | + } |
| 97 | + |
| 98 | + try { |
| 99 | + const response = |
| 100 | + await this.retry<InjectiveAbacusRpc.PointsStatsWeeklyForAccountResponse>( |
| 101 | + () => this.client.PointsStatsWeeklyForAccount(request), |
| 102 | + ) |
| 103 | + |
| 104 | + return AbacusGrpcTransformer.grpcPointsStatsWeeklyToPointsStatsWeekly( |
| 105 | + response, |
| 106 | + ) |
| 107 | + } catch (e: unknown) { |
| 108 | + if (e instanceof InjectiveAbacusRpc.GrpcWebError) { |
| 109 | + throw new GrpcUnaryRequestException(new Error(e.toString()), { |
| 110 | + code: e.code, |
| 111 | + context: 'PointsStatsWeeklyForAccount', |
| 112 | + contextModule: this.module, |
| 113 | + }) |
| 114 | + } |
| 115 | + |
| 116 | + throw new GrpcUnaryRequestException(e as Error, { |
| 117 | + code: UnspecifiedErrorCode, |
| 118 | + context: 'PointsStatsWeeklyForAccount', |
| 119 | + contextModule: this.module, |
| 120 | + }) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments