@keplr-wallet/stores 0.13.38 → 0.13.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/common/query/query.d.ts +1 -0
- package/build/common/query/query.js +23 -16
- package/build/common/query/query.js.map +1 -1
- package/build/query/cosmos/staking/delegations.d.ts +4 -0
- package/build/query/cosmos/staking/delegations.js +35 -3
- package/build/query/cosmos/staking/delegations.js.map +1 -1
- package/build/query/cosmos/staking/initia-delegations.d.ts +4 -0
- package/build/query/cosmos/staking/initia-delegations.js +42 -4
- package/build/query/cosmos/staking/initia-delegations.js.map +1 -1
- package/build/query/cosmos/staking/initia-unbonding-delegations.d.ts +4 -0
- package/build/query/cosmos/staking/initia-unbonding-delegations.js +31 -3
- package/build/query/cosmos/staking/initia-unbonding-delegations.js.map +1 -1
- package/build/query/cosmos/staking/response-guards.spec.d.ts +1 -0
- package/build/query/cosmos/staking/response-guards.spec.js +318 -0
- package/build/query/cosmos/staking/response-guards.spec.js.map +1 -0
- package/build/query/cosmos/staking/rewards.d.ts +4 -0
- package/build/query/cosmos/staking/rewards.js +48 -10
- package/build/query/cosmos/staking/rewards.js.map +1 -1
- package/build/query/cosmos/staking/types.d.ts +13 -2
- package/build/query/cosmos/staking/types.js +162 -1
- package/build/query/cosmos/staking/types.js.map +1 -1
- package/build/query/cosmos/staking/unbonding-delegations.d.ts +4 -0
- package/build/query/cosmos/staking/unbonding-delegations.js +30 -2
- package/build/query/cosmos/staking/unbonding-delegations.js.map +1 -1
- package/package.json +12 -11
- package/src/common/query/query.ts +32 -21
- package/src/query/cosmos/staking/delegations.ts +35 -4
- package/src/query/cosmos/staking/initia-delegations.ts +51 -4
- package/src/query/cosmos/staking/initia-unbonding-delegations.ts +30 -3
- package/src/query/cosmos/staking/response-guards.spec.ts +414 -0
- package/src/query/cosmos/staking/rewards.ts +45 -10
- package/src/query/cosmos/staking/types.ts +210 -4
- package/src/query/cosmos/staking/unbonding-delegations.ts +29 -3
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { Coin } from "@keplr-wallet/types";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
CoinPrimitive,
|
|
4
|
+
isSuspectedResponseDataWithInvalidValue,
|
|
5
|
+
} from "../../../common";
|
|
6
|
+
import Joi from "joi";
|
|
3
7
|
|
|
4
8
|
export type Rewards = {
|
|
5
9
|
rewards?: DelegatorReward[] | null;
|
|
@@ -37,8 +41,7 @@ export type InitiaDelegation = {
|
|
|
37
41
|
delegation: {
|
|
38
42
|
delegator_address: string;
|
|
39
43
|
validator_address: string;
|
|
40
|
-
|
|
41
|
-
shares: string;
|
|
44
|
+
shares: Coin[];
|
|
42
45
|
};
|
|
43
46
|
balance: {
|
|
44
47
|
denom: string;
|
|
@@ -72,11 +75,214 @@ export type InitiaUnbondingDelegation = {
|
|
|
72
75
|
entries: {
|
|
73
76
|
creation_height: string;
|
|
74
77
|
completion_time: string;
|
|
75
|
-
initial_balance:
|
|
78
|
+
initial_balance: Coin[];
|
|
76
79
|
balance: Coin[];
|
|
77
80
|
}[];
|
|
78
81
|
};
|
|
79
82
|
|
|
83
|
+
const validationOptions: Joi.ValidationOptions = {
|
|
84
|
+
convert: false,
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const IntegerStringSchema = Joi.string()
|
|
88
|
+
.pattern(/^[0-9]+$/)
|
|
89
|
+
.required();
|
|
90
|
+
const DecimalStringSchema = Joi.string()
|
|
91
|
+
.pattern(/^[0-9]+(\.[0-9]+)?$/)
|
|
92
|
+
.required();
|
|
93
|
+
|
|
94
|
+
const CoinPrimitiveSchema = Joi.object<CoinPrimitive>({
|
|
95
|
+
denom: Joi.string().required(),
|
|
96
|
+
amount: IntegerStringSchema,
|
|
97
|
+
}).unknown(true);
|
|
98
|
+
|
|
99
|
+
const RewardCoinSchema = Joi.object<CoinPrimitive>({
|
|
100
|
+
denom: Joi.string().required(),
|
|
101
|
+
amount: DecimalStringSchema,
|
|
102
|
+
}).unknown(true);
|
|
103
|
+
|
|
104
|
+
const DelegationSchema = Joi.object<Delegation>({
|
|
105
|
+
delegation: Joi.object({
|
|
106
|
+
delegator_address: Joi.string().required(),
|
|
107
|
+
validator_address: Joi.string().required(),
|
|
108
|
+
shares: Joi.string().required(),
|
|
109
|
+
})
|
|
110
|
+
.unknown(true)
|
|
111
|
+
.required(),
|
|
112
|
+
balance: Joi.object({
|
|
113
|
+
denom: Joi.string().required(),
|
|
114
|
+
amount: IntegerStringSchema,
|
|
115
|
+
})
|
|
116
|
+
.unknown(true)
|
|
117
|
+
.required(),
|
|
118
|
+
}).unknown(true);
|
|
119
|
+
|
|
120
|
+
const InitiaDelegationSchema = Joi.object<InitiaDelegation>({
|
|
121
|
+
delegation: Joi.object({
|
|
122
|
+
delegator_address: Joi.string().required(),
|
|
123
|
+
validator_address: Joi.string().required(),
|
|
124
|
+
shares: Joi.array().items(RewardCoinSchema).required(),
|
|
125
|
+
})
|
|
126
|
+
.unknown(true)
|
|
127
|
+
.required(),
|
|
128
|
+
balance: Joi.array().items(CoinPrimitiveSchema).required(),
|
|
129
|
+
}).unknown(true);
|
|
130
|
+
|
|
131
|
+
const UnbondingEntrySchema = Joi.object<UnbondingDelegation["entries"][number]>(
|
|
132
|
+
{
|
|
133
|
+
creation_height: Joi.string().required(),
|
|
134
|
+
completion_time: Joi.string().required(),
|
|
135
|
+
initial_balance: IntegerStringSchema,
|
|
136
|
+
balance: IntegerStringSchema,
|
|
137
|
+
}
|
|
138
|
+
).unknown(true);
|
|
139
|
+
|
|
140
|
+
const UnbondingDelegationSchema = Joi.object<UnbondingDelegation>({
|
|
141
|
+
delegator_address: Joi.string().required(),
|
|
142
|
+
validator_address: Joi.string().required(),
|
|
143
|
+
entries: Joi.array().items(UnbondingEntrySchema).required(),
|
|
144
|
+
}).unknown(true);
|
|
145
|
+
|
|
146
|
+
const InitiaUnbondingEntrySchema = Joi.object<
|
|
147
|
+
InitiaUnbondingDelegation["entries"][number]
|
|
148
|
+
>({
|
|
149
|
+
creation_height: Joi.string().required(),
|
|
150
|
+
completion_time: Joi.string().required(),
|
|
151
|
+
initial_balance: Joi.array().items(CoinPrimitiveSchema).required(),
|
|
152
|
+
balance: Joi.array().items(CoinPrimitiveSchema).required(),
|
|
153
|
+
}).unknown(true);
|
|
154
|
+
|
|
155
|
+
const InitiaUnbondingDelegationSchema = Joi.object<InitiaUnbondingDelegation>({
|
|
156
|
+
delegator_address: Joi.string().required(),
|
|
157
|
+
validator_address: Joi.string().required(),
|
|
158
|
+
entries: Joi.array().items(InitiaUnbondingEntrySchema).required(),
|
|
159
|
+
}).unknown(true);
|
|
160
|
+
|
|
161
|
+
const DelegatorRewardSchema = Joi.object<DelegatorReward>({
|
|
162
|
+
validator_address: Joi.string().required(),
|
|
163
|
+
reward: Joi.array().items(RewardCoinSchema).allow(null).optional(),
|
|
164
|
+
}).unknown(true);
|
|
165
|
+
|
|
166
|
+
const DelegationsResponseSchema = Joi.object<Delegations>({
|
|
167
|
+
delegation_responses: Joi.array().items(DelegationSchema).required(),
|
|
168
|
+
}).unknown(true);
|
|
169
|
+
|
|
170
|
+
const InitiaDelegationsResponseSchema = Joi.object<InitiaDelegations>({
|
|
171
|
+
delegation_responses: Joi.array().items(InitiaDelegationSchema).required(),
|
|
172
|
+
}).unknown(true);
|
|
173
|
+
|
|
174
|
+
const UnbondingDelegationsResponseSchema = Joi.object<UnbondingDelegations>({
|
|
175
|
+
unbonding_responses: Joi.array().items(UnbondingDelegationSchema).required(),
|
|
176
|
+
}).unknown(true);
|
|
177
|
+
|
|
178
|
+
const InitiaUnbondingDelegationsResponseSchema =
|
|
179
|
+
Joi.object<InitiaUnbondingDelegations>({
|
|
180
|
+
unbonding_responses: Joi.array()
|
|
181
|
+
.items(InitiaUnbondingDelegationSchema)
|
|
182
|
+
.required(),
|
|
183
|
+
}).unknown(true);
|
|
184
|
+
|
|
185
|
+
const RewardsResponseSchema = Joi.object<Rewards>({
|
|
186
|
+
rewards: Joi.array().items(DelegatorRewardSchema).allow(null).optional(),
|
|
187
|
+
total: Joi.array().items(RewardCoinSchema).optional(),
|
|
188
|
+
})
|
|
189
|
+
.or("rewards", "total")
|
|
190
|
+
.unknown(true);
|
|
191
|
+
|
|
192
|
+
function getValidatedResponse<T>(
|
|
193
|
+
schema: Joi.ObjectSchema<T>,
|
|
194
|
+
data: unknown
|
|
195
|
+
): T | undefined {
|
|
196
|
+
const validated = schema.validate(data, validationOptions);
|
|
197
|
+
if (validated.error) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return validated.value;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function assertStakingResponseData(
|
|
205
|
+
headers: any,
|
|
206
|
+
data: unknown,
|
|
207
|
+
assertResponse: (data: unknown) => void
|
|
208
|
+
): void {
|
|
209
|
+
if (isSuspectedResponseDataWithInvalidValue(headers, data)) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
assertResponse(data);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function getDelegationResponses(
|
|
217
|
+
data: unknown
|
|
218
|
+
): Delegation[] | undefined {
|
|
219
|
+
return getValidatedResponse(DelegationsResponseSchema, data)
|
|
220
|
+
?.delegation_responses;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function assertDelegationsResponse(
|
|
224
|
+
data: unknown
|
|
225
|
+
): asserts data is Delegations {
|
|
226
|
+
if (!getDelegationResponses(data)) {
|
|
227
|
+
throw new Error("Invalid Cosmos staking delegations response");
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export function getInitiaDelegationResponses(
|
|
232
|
+
data: unknown
|
|
233
|
+
): InitiaDelegation[] | undefined {
|
|
234
|
+
return getValidatedResponse(InitiaDelegationsResponseSchema, data)
|
|
235
|
+
?.delegation_responses;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function assertInitiaDelegationsResponse(
|
|
239
|
+
data: unknown
|
|
240
|
+
): asserts data is InitiaDelegations {
|
|
241
|
+
if (!getInitiaDelegationResponses(data)) {
|
|
242
|
+
throw new Error("Invalid Initia staking delegations response");
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function getUnbondingResponses(
|
|
247
|
+
data: unknown
|
|
248
|
+
): UnbondingDelegation[] | undefined {
|
|
249
|
+
return getValidatedResponse(UnbondingDelegationsResponseSchema, data)
|
|
250
|
+
?.unbonding_responses;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export function assertUnbondingDelegationsResponse(
|
|
254
|
+
data: unknown
|
|
255
|
+
): asserts data is UnbondingDelegations {
|
|
256
|
+
if (!getUnbondingResponses(data)) {
|
|
257
|
+
throw new Error("Invalid Cosmos staking unbonding delegations response");
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function getInitiaUnbondingResponses(
|
|
262
|
+
data: unknown
|
|
263
|
+
): InitiaUnbondingDelegation[] | undefined {
|
|
264
|
+
return getValidatedResponse(InitiaUnbondingDelegationsResponseSchema, data)
|
|
265
|
+
?.unbonding_responses;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export function assertInitiaUnbondingDelegationsResponse(
|
|
269
|
+
data: unknown
|
|
270
|
+
): asserts data is InitiaUnbondingDelegations {
|
|
271
|
+
if (!getInitiaUnbondingResponses(data)) {
|
|
272
|
+
throw new Error("Invalid Initia staking unbonding delegations response");
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export function getRewardsResponse(data: unknown): Rewards | undefined {
|
|
277
|
+
return getValidatedResponse(RewardsResponseSchema, data);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function assertRewardsResponse(data: unknown): asserts data is Rewards {
|
|
281
|
+
if (!getRewardsResponse(data)) {
|
|
282
|
+
throw new Error("Invalid Cosmos distribution rewards response");
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
80
286
|
export type Validator = {
|
|
81
287
|
operator_address: string;
|
|
82
288
|
consensus_pubkey: {
|
|
@@ -2,7 +2,13 @@ import {
|
|
|
2
2
|
ObservableChainQuery,
|
|
3
3
|
ObservableChainQueryMap,
|
|
4
4
|
} from "../../chain-query";
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
assertStakingResponseData,
|
|
7
|
+
assertUnbondingDelegationsResponse,
|
|
8
|
+
getUnbondingResponses,
|
|
9
|
+
UnbondingDelegation,
|
|
10
|
+
UnbondingDelegations,
|
|
11
|
+
} from "./types";
|
|
6
12
|
import { ChainGetter, requireCosmosInfo } from "../../../chain";
|
|
7
13
|
import { CoinPretty, Int, Dec } from "@keplr-wallet/unit";
|
|
8
14
|
import { computed, makeObservable } from "mobx";
|
|
@@ -39,6 +45,16 @@ export class ObservableQueryUnbondingDelegationsInner extends ObservableChainQue
|
|
|
39
45
|
return this.bech32Address.length > 0;
|
|
40
46
|
}
|
|
41
47
|
|
|
48
|
+
protected override async fetchResponse(abortController: AbortController) {
|
|
49
|
+
const response = await super.fetchResponse(abortController);
|
|
50
|
+
assertStakingResponseData(
|
|
51
|
+
response.headers,
|
|
52
|
+
response.data,
|
|
53
|
+
assertUnbondingDelegationsResponse
|
|
54
|
+
);
|
|
55
|
+
return response;
|
|
56
|
+
}
|
|
57
|
+
|
|
42
58
|
@computed
|
|
43
59
|
get total(): CoinPretty | undefined {
|
|
44
60
|
const cosmosInfo = requireCosmosInfo(
|
|
@@ -54,8 +70,13 @@ export class ObservableQueryUnbondingDelegationsInner extends ObservableChainQue
|
|
|
54
70
|
return new CoinPretty(stakeCurrency, new Int(0)).ready(false);
|
|
55
71
|
}
|
|
56
72
|
|
|
73
|
+
const unbondingResponses = getUnbondingResponses(this.response.data);
|
|
74
|
+
if (!unbondingResponses) {
|
|
75
|
+
return new CoinPretty(stakeCurrency, new Int(0)).ready(false);
|
|
76
|
+
}
|
|
77
|
+
|
|
57
78
|
let totalBalance = new Int(0);
|
|
58
|
-
for (const unbondingDelegation of
|
|
79
|
+
for (const unbondingDelegation of unbondingResponses) {
|
|
59
80
|
for (const entry of unbondingDelegation.entries) {
|
|
60
81
|
const amount = new Int(entry.balance);
|
|
61
82
|
if (amount.gt(new Int(0))) {
|
|
@@ -120,7 +141,12 @@ export class ObservableQueryUnbondingDelegationsInner extends ObservableChainQue
|
|
|
120
141
|
|
|
121
142
|
const res: UnbondingDelegation[] = [];
|
|
122
143
|
|
|
123
|
-
|
|
144
|
+
const unbondingResponses = getUnbondingResponses(this.response.data);
|
|
145
|
+
if (!unbondingResponses) {
|
|
146
|
+
return [];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
for (const unbonding of unbondingResponses) {
|
|
124
150
|
const u = {
|
|
125
151
|
...unbonding,
|
|
126
152
|
};
|