|
| 1 | +import { |
| 2 | + UnspecifiedErrorCode, |
| 3 | + GrpcUnaryRequestException, |
| 4 | +} from '@injectivelabs/exceptions' |
| 5 | +import { InjectiveReferralRpc } from '@injectivelabs/indexer-proto-ts' |
| 6 | +import BaseGrpcConsumer from '../../base/BaseIndexerGrpcConsumer.js' |
| 7 | +import { IndexerGrpcReferralTransformer } from '../transformers/index.js' |
| 8 | +import { IndexerModule } from '../types/index.js' |
| 9 | + |
| 10 | +/** |
| 11 | + * @category Indexer Grpc API |
| 12 | + */ |
| 13 | +export class IndexerGrpcReferralApi extends BaseGrpcConsumer { |
| 14 | + protected module: string = IndexerModule.Referral |
| 15 | + |
| 16 | + protected client: InjectiveReferralRpc.InjectiveReferralRPCClientImpl |
| 17 | + |
| 18 | + constructor(endpoint: string) { |
| 19 | + super(endpoint) |
| 20 | + |
| 21 | + this.client = new InjectiveReferralRpc.InjectiveReferralRPCClientImpl( |
| 22 | + this.getGrpcWebImpl(endpoint), |
| 23 | + ) |
| 24 | + } |
| 25 | + |
| 26 | + async fetchReferrerDetails(address: string) { |
| 27 | + const request = InjectiveReferralRpc.GetReferrerDetailsRequest.create() |
| 28 | + request.referrerAddress = address |
| 29 | + |
| 30 | + try { |
| 31 | + const response = |
| 32 | + await this.retry<InjectiveReferralRpc.GetReferrerDetailsResponse>(() => |
| 33 | + this.client.GetReferrerDetails(request), |
| 34 | + ) |
| 35 | + |
| 36 | + return IndexerGrpcReferralTransformer.referrerDetailsResponseToReferrerDetails( |
| 37 | + address, |
| 38 | + response, |
| 39 | + ) |
| 40 | + } catch (e: unknown) { |
| 41 | + if (e instanceof InjectiveReferralRpc.GrpcWebError) { |
| 42 | + throw new GrpcUnaryRequestException(new Error(e.toString()), { |
| 43 | + code: e.code, |
| 44 | + context: 'Referral', |
| 45 | + contextModule: this.module, |
| 46 | + }) |
| 47 | + } |
| 48 | + |
| 49 | + throw new GrpcUnaryRequestException(e as Error, { |
| 50 | + code: UnspecifiedErrorCode, |
| 51 | + context: 'Referral', |
| 52 | + contextModule: this.module, |
| 53 | + }) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + async fetchInviteeDetails(address: string) { |
| 58 | + const request = InjectiveReferralRpc.GetInviteeDetailsRequest.create() |
| 59 | + request.inviteeAddress = address |
| 60 | + |
| 61 | + try { |
| 62 | + const response = |
| 63 | + await this.retry<InjectiveReferralRpc.GetInviteeDetailsResponse>(() => |
| 64 | + this.client.GetInviteeDetails(request), |
| 65 | + ) |
| 66 | + |
| 67 | + return IndexerGrpcReferralTransformer.inviteeDetailsResponseToInviteeDetails( |
| 68 | + response, |
| 69 | + ) |
| 70 | + } catch (e: unknown) { |
| 71 | + if (e instanceof InjectiveReferralRpc.GrpcWebError) { |
| 72 | + throw new GrpcUnaryRequestException(new Error(e.toString()), { |
| 73 | + code: e.code, |
| 74 | + context: 'Referral', |
| 75 | + contextModule: this.module, |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + throw new GrpcUnaryRequestException(e as Error, { |
| 80 | + code: UnspecifiedErrorCode, |
| 81 | + context: 'Referral', |
| 82 | + contextModule: this.module, |
| 83 | + }) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + async fetchReferrerByCode(address: string) { |
| 88 | + const request = InjectiveReferralRpc.GetReferrerByCodeRequest.create() |
| 89 | + request.referralCode = address |
| 90 | + |
| 91 | + try { |
| 92 | + const response = |
| 93 | + await this.retry<InjectiveReferralRpc.GetReferrerByCodeResponse>(() => |
| 94 | + this.client.GetReferrerByCode(request), |
| 95 | + ) |
| 96 | + |
| 97 | + return IndexerGrpcReferralTransformer.referrerByCodeResponseToReferrerByCode( |
| 98 | + response, |
| 99 | + ) |
| 100 | + } catch (e: unknown) { |
| 101 | + if (e instanceof InjectiveReferralRpc.GrpcWebError) { |
| 102 | + throw new GrpcUnaryRequestException(new Error(e.toString()), { |
| 103 | + code: e.code, |
| 104 | + context: 'Referral', |
| 105 | + contextModule: this.module, |
| 106 | + }) |
| 107 | + } |
| 108 | + |
| 109 | + throw new GrpcUnaryRequestException(e as Error, { |
| 110 | + code: UnspecifiedErrorCode, |
| 111 | + context: 'Referral', |
| 112 | + contextModule: this.module, |
| 113 | + }) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments