-
Notifications
You must be signed in to change notification settings - Fork 310
/
aminomessages.ts
332 lines (316 loc) · 11.1 KB
/
aminomessages.ts
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* eslint-disable @typescript-eslint/naming-convention */
import { AminoMsg, Coin, Pubkey } from "@cosmjs/amino";
import { Decimal } from "@cosmjs/math";
import { anyToSinglePubkey, encodePubkey } from "@cosmjs/proto-signing";
import { assertDefinedAndNotNull } from "@cosmjs/utils";
import {
MsgBeginRedelegate,
MsgCreateValidator,
MsgDelegate,
MsgEditValidator,
MsgUndelegate,
} from "cosmjs-types/cosmos/staking/v1beta1/tx";
import { AminoConverter } from "../..";
/** The initial commission rates to be used for creating a validator */
interface CommissionRates {
readonly rate: string;
readonly max_rate: string;
readonly max_change_rate: string;
}
/** A validator description. */
interface Description {
readonly moniker: string;
readonly identity: string;
readonly website: string;
readonly security_contact: string;
readonly details: string;
}
function protoDecimalToJson(decimal: string): string {
const parsed = Decimal.fromAtomics(decimal, 18);
const [whole, fractional] = parsed.toString().split(".");
return `${whole}.${fractional.padEnd(18, "0")}`;
}
function jsonDecimalToProto(decimal: string): string {
const parsed = Decimal.fromUserInput(decimal, 18);
return parsed.atomics;
}
/** Creates a new validator. */
export interface AminoMsgCreateValidator extends AminoMsg {
readonly type: "cosmos-sdk/MsgCreateValidator";
readonly value: {
readonly description: Description;
readonly commission: CommissionRates;
readonly min_self_delegation: string;
/** Bech32 encoded delegator address */
readonly delegator_address: string;
/** Bech32 encoded validator address */
readonly validator_address: string;
/** Public key */
readonly pubkey: Pubkey;
readonly value: Coin;
};
}
export function isAminoMsgCreateValidator(msg: AminoMsg): msg is AminoMsgCreateValidator {
return msg.type === "cosmos-sdk/MsgCreateValidator";
}
/** Edits an existing validator. */
export interface AminoMsgEditValidator extends AminoMsg {
readonly type: "cosmos-sdk/MsgEditValidator";
readonly value: {
readonly description: Description;
/** Bech32 encoded validator address */
readonly validator_address: string;
/**
* The new value for the comission rate.
*
* An empty string in the protobuf document means "do not change".
* In Amino JSON this empty string becomes undefined (omitempty)
*/
readonly commission_rate: string | undefined;
/**
* The new value for the comission rate.
*
* An empty string in the protobuf document means "do not change".
* In Amino JSON this empty string becomes undefined (omitempty)
*/
readonly min_self_delegation: string | undefined;
};
}
export function isAminoMsgEditValidator(msg: AminoMsg): msg is AminoMsgEditValidator {
return msg.type === "cosmos-sdk/MsgEditValidator";
}
/**
* Performs a delegation from a delegate to a validator.
*
* @see https://docs.cosmos.network/master/modules/staking/03_messages.html#msgdelegate
*/
export interface AminoMsgDelegate extends AminoMsg {
readonly type: "cosmos-sdk/MsgDelegate";
readonly value: {
/** Bech32 encoded delegator address */
readonly delegator_address: string;
/** Bech32 encoded validator address */
readonly validator_address: string;
readonly amount: Coin;
};
}
export function isAminoMsgDelegate(msg: AminoMsg): msg is AminoMsgDelegate {
return msg.type === "cosmos-sdk/MsgDelegate";
}
/** Performs a redelegation from a delegate and source validator to a destination validator */
export interface AminoMsgBeginRedelegate extends AminoMsg {
readonly type: "cosmos-sdk/MsgBeginRedelegate";
readonly value: {
/** Bech32 encoded delegator address */
readonly delegator_address: string;
/** Bech32 encoded source validator address */
readonly validator_src_address: string;
/** Bech32 encoded destination validator address */
readonly validator_dst_address: string;
readonly amount: Coin;
};
}
export function isAminoMsgBeginRedelegate(msg: AminoMsg): msg is AminoMsgBeginRedelegate {
return msg.type === "cosmos-sdk/MsgBeginRedelegate";
}
/** Performs an undelegation from a delegate and a validator */
export interface AminoMsgUndelegate extends AminoMsg {
readonly type: "cosmos-sdk/MsgUndelegate";
readonly value: {
/** Bech32 encoded delegator address */
readonly delegator_address: string;
/** Bech32 encoded validator address */
readonly validator_address: string;
readonly amount: Coin;
};
}
export function isAminoMsgUndelegate(msg: AminoMsg): msg is AminoMsgUndelegate {
return msg.type === "cosmos-sdk/MsgUndelegate";
}
export function createStakingAminoConverters(
_prefix: string,
): Record<string, AminoConverter | "not_supported_by_chain"> {
return {
"/cosmos.staking.v1beta1.MsgBeginRedelegate": {
aminoType: "cosmos-sdk/MsgBeginRedelegate",
toAmino: ({
delegatorAddress,
validatorSrcAddress,
validatorDstAddress,
amount,
}: MsgBeginRedelegate): AminoMsgBeginRedelegate["value"] => {
assertDefinedAndNotNull(amount, "missing amount");
return {
delegator_address: delegatorAddress,
validator_src_address: validatorSrcAddress,
validator_dst_address: validatorDstAddress,
amount: amount,
};
},
fromAmino: ({
delegator_address,
validator_src_address,
validator_dst_address,
amount,
}: AminoMsgBeginRedelegate["value"]): MsgBeginRedelegate => ({
delegatorAddress: delegator_address,
validatorSrcAddress: validator_src_address,
validatorDstAddress: validator_dst_address,
amount: amount,
}),
},
"/cosmos.staking.v1beta1.MsgCreateValidator": {
aminoType: "cosmos-sdk/MsgCreateValidator",
toAmino: ({
description,
commission,
minSelfDelegation,
delegatorAddress,
validatorAddress,
pubkey,
value,
}: MsgCreateValidator): AminoMsgCreateValidator["value"] => {
assertDefinedAndNotNull(description, "missing description");
assertDefinedAndNotNull(commission, "missing commission");
assertDefinedAndNotNull(pubkey, "missing pubkey");
assertDefinedAndNotNull(value, "missing value");
return {
description: {
moniker: description.moniker,
identity: description.identity,
website: description.website,
security_contact: description.securityContact,
details: description.details,
},
commission: {
rate: protoDecimalToJson(commission.rate),
max_rate: protoDecimalToJson(commission.maxRate),
max_change_rate: protoDecimalToJson(commission.maxChangeRate),
},
min_self_delegation: minSelfDelegation,
delegator_address: delegatorAddress,
validator_address: validatorAddress,
pubkey: anyToSinglePubkey(pubkey),
value: value,
};
},
fromAmino: ({
description,
commission,
min_self_delegation,
delegator_address,
validator_address,
pubkey,
value,
}: AminoMsgCreateValidator["value"]): MsgCreateValidator => {
return {
description: {
moniker: description.moniker,
identity: description.identity,
website: description.website,
securityContact: description.security_contact,
details: description.details,
},
commission: {
rate: jsonDecimalToProto(commission.rate),
maxRate: jsonDecimalToProto(commission.max_rate),
maxChangeRate: jsonDecimalToProto(commission.max_change_rate),
},
minSelfDelegation: min_self_delegation,
delegatorAddress: delegator_address,
validatorAddress: validator_address,
pubkey: encodePubkey(pubkey),
value: value,
};
},
},
"/cosmos.staking.v1beta1.MsgDelegate": {
aminoType: "cosmos-sdk/MsgDelegate",
toAmino: ({ delegatorAddress, validatorAddress, amount }: MsgDelegate): AminoMsgDelegate["value"] => {
assertDefinedAndNotNull(amount, "missing amount");
return {
delegator_address: delegatorAddress,
validator_address: validatorAddress,
amount: amount,
};
},
fromAmino: ({
delegator_address,
validator_address,
amount,
}: AminoMsgDelegate["value"]): MsgDelegate => ({
delegatorAddress: delegator_address,
validatorAddress: validator_address,
amount: amount,
}),
},
"/cosmos.staking.v1beta1.MsgEditValidator": {
aminoType: "cosmos-sdk/MsgEditValidator",
toAmino: ({
description,
commissionRate,
minSelfDelegation,
validatorAddress,
}: MsgEditValidator): AminoMsgEditValidator["value"] => {
assertDefinedAndNotNull(description, "missing description");
return {
description: {
moniker: description.moniker,
identity: description.identity,
website: description.website,
security_contact: description.securityContact,
details: description.details,
},
// empty string in the protobuf document means "do not change"
commission_rate: commissionRate ? protoDecimalToJson(commissionRate) : undefined,
// empty string in the protobuf document means "do not change"
min_self_delegation: minSelfDelegation ? minSelfDelegation : undefined,
validator_address: validatorAddress,
};
},
fromAmino: ({
description,
commission_rate,
min_self_delegation,
validator_address,
}: AminoMsgEditValidator["value"]): MsgEditValidator => ({
description: {
moniker: description.moniker,
identity: description.identity,
website: description.website,
securityContact: description.security_contact,
details: description.details,
},
// empty string in the protobuf document means "do not change"
commissionRate: commission_rate ? jsonDecimalToProto(commission_rate) : "",
// empty string in the protobuf document means "do not change"
minSelfDelegation: min_self_delegation ?? "",
validatorAddress: validator_address,
}),
},
"/cosmos.staking.v1beta1.MsgUndelegate": {
aminoType: "cosmos-sdk/MsgUndelegate",
toAmino: ({
delegatorAddress,
validatorAddress,
amount,
}: MsgUndelegate): AminoMsgUndelegate["value"] => {
assertDefinedAndNotNull(amount, "missing amount");
return {
delegator_address: delegatorAddress,
validator_address: validatorAddress,
amount: amount,
};
},
fromAmino: ({
delegator_address,
validator_address,
amount,
}: AminoMsgUndelegate["value"]): MsgUndelegate => ({
delegatorAddress: delegator_address,
validatorAddress: validator_address,
amount: amount,
}),
},
};
}