-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathChainGrpcBankApi.ts
More file actions
164 lines (125 loc) · 5.74 KB
/
ChainGrpcBankApi.ts
File metadata and controls
164 lines (125 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import * as CosmosBankV1Beta1QueryPb from '@injectivelabs/core-proto-ts-v2/generated/cosmos/bank/v1beta1/query_pb'
import { QueryClient as CosmosBankV1BetaQueryClient } from '@injectivelabs/core-proto-ts-v2/generated/cosmos/bank/v1beta1/query_pb.client'
import { ChainModule } from '../types/index.js'
import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js'
import { ChainGrpcBankTransformer } from '../transformers/index.js'
import { fetchAllWithPagination } from '../../../utils/pagination.js'
import { ChainGrpcCommonTransformer } from '../transformers/ChainGrpcCommonTransformer.js'
import type { PaginationOption } from '../../../types/pagination.js'
import type { GrpcWebTransportAdditionalOptions } from '../../../utils/grpc.js'
const MAX_LIMIT_FOR_SUPPLY = 10000
/**
* @category Chain Grpc API
*/
export class ChainGrpcBankApi extends BaseGrpcConsumer {
protected module: string = ChainModule.Bank
private client: CosmosBankV1BetaQueryClient
constructor(endpoint: string, options?: GrpcWebTransportAdditionalOptions) {
super(endpoint, options)
this.client = new CosmosBankV1BetaQueryClient(this.transport)
}
async fetchModuleParams() {
const request = CosmosBankV1Beta1QueryPb.QueryParamsRequest.create()
const response = await this.executeGrpcCall<
CosmosBankV1Beta1QueryPb.QueryParamsRequest,
CosmosBankV1Beta1QueryPb.QueryParamsResponse
>(request, this.client.params.bind(this.client))
return ChainGrpcBankTransformer.moduleParamsResponseToModuleParams(response)
}
async fetchBalance({
accountAddress,
denom,
}: {
accountAddress: string
denom: string
}) {
const request = CosmosBankV1Beta1QueryPb.QueryBalanceRequest.create()
request.address = accountAddress
request.denom = denom
const response = await this.executeGrpcCall<
CosmosBankV1Beta1QueryPb.QueryBalanceRequest,
CosmosBankV1Beta1QueryPb.QueryBalanceResponse
>(request, this.client.balance.bind(this.client))
return ChainGrpcBankTransformer.balanceResponseToBalance(response)
}
async fetchBalances(address: string, pagination?: PaginationOption) {
const request = CosmosBankV1Beta1QueryPb.QueryAllBalancesRequest.create()
request.address = address
const paginationForRequest =
ChainGrpcCommonTransformer.pageRequestToGrpcPageRequestV2(pagination)
if (paginationForRequest) {
request.pagination = paginationForRequest
}
const response = await this.executeGrpcCall<
CosmosBankV1Beta1QueryPb.QueryAllBalancesRequest,
CosmosBankV1Beta1QueryPb.QueryAllBalancesResponse
>(request, this.client.allBalances.bind(this.client))
return ChainGrpcBankTransformer.balancesResponseToBalances(response)
}
async fetchTotalSupply(pagination?: PaginationOption) {
const request = CosmosBankV1Beta1QueryPb.QueryTotalSupplyRequest.create()
const paginationForRequest =
ChainGrpcCommonTransformer.pageRequestToGrpcPageRequestV2(pagination)
if (paginationForRequest) {
request.pagination = paginationForRequest
}
const response = await this.executeGrpcCall<
CosmosBankV1Beta1QueryPb.QueryTotalSupplyRequest,
CosmosBankV1Beta1QueryPb.QueryTotalSupplyResponse
>(request, this.client.totalSupply.bind(this.client))
return ChainGrpcBankTransformer.totalSupplyResponseToTotalSupply(response)
}
/** a way to ensure all total supply is fully fetched */
async fetchAllTotalSupply(
pagination: PaginationOption = { limit: MAX_LIMIT_FOR_SUPPLY },
) {
return fetchAllWithPagination(pagination, this.fetchTotalSupply.bind(this))
}
async fetchSupplyOf(denom: string) {
const request = CosmosBankV1Beta1QueryPb.QuerySupplyOfRequest.create()
request.denom = denom
const response = await this.executeGrpcCall<
CosmosBankV1Beta1QueryPb.QuerySupplyOfRequest,
CosmosBankV1Beta1QueryPb.QuerySupplyOfResponse
>(request, this.client.supplyOf.bind(this.client))
return ChainGrpcCommonTransformer.grpcCoinToCoin(response.amount!)
}
async fetchDenomsMetadata(pagination?: PaginationOption) {
const request = CosmosBankV1Beta1QueryPb.QueryDenomsMetadataRequest.create()
const paginationForRequest =
ChainGrpcCommonTransformer.pageRequestToGrpcPageRequestV2(pagination)
if (paginationForRequest) {
request.pagination = paginationForRequest
}
const response = await this.executeGrpcCall<
CosmosBankV1Beta1QueryPb.QueryDenomsMetadataRequest,
CosmosBankV1Beta1QueryPb.QueryDenomsMetadataResponse
>(request, this.client.denomsMetadata.bind(this.client))
return ChainGrpcBankTransformer.denomsMetadataResponseToDenomsMetadata(
response,
)
}
async fetchDenomMetadata(denom: string) {
const request = CosmosBankV1Beta1QueryPb.QueryDenomMetadataRequest.create()
request.denom = denom
const response = await this.executeGrpcCall<
CosmosBankV1Beta1QueryPb.QueryDenomMetadataRequest,
CosmosBankV1Beta1QueryPb.QueryDenomMetadataResponse
>(request, this.client.denomMetadata.bind(this.client))
return ChainGrpcBankTransformer.metadataToMetadata(response.metadata!)
}
async fetchDenomOwners(denom: string, pagination?: PaginationOption) {
const request = CosmosBankV1Beta1QueryPb.QueryDenomOwnersRequest.create()
request.denom = denom
const paginationForRequest =
ChainGrpcCommonTransformer.pageRequestToGrpcPageRequestV2(pagination)
if (paginationForRequest) {
request.pagination = paginationForRequest
}
const response = await this.executeGrpcCall<
CosmosBankV1Beta1QueryPb.QueryDenomOwnersRequest,
CosmosBankV1Beta1QueryPb.QueryDenomOwnersResponse
>(request, this.client.denomOwners.bind(this.client))
return ChainGrpcBankTransformer.denomOwnersResponseToDenomOwners(response)
}
}