Skip to content

Commit fa811d4

Browse files
authored
Merge pull request #92 from hyperweb-io/cosmos-sdk-dec
apply useCosmosSDKDec and useEnhancedDecimal
2 parents 240f7ec + 363b330 commit fa811d4

File tree

90 files changed

+2811
-1938
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+2811
-1938
lines changed

.telescope.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"useDeepPartial": true,
4141
"num64": "bigint",
4242
"customTypes": {
43-
"useCosmosSDKDec": false
43+
"useCosmosSDKDec": true
4444
},
4545
"useTelescopeGeneratedType": true,
4646
"autoFixUndefinedEnumDefault": true

libs/cosmos-types/src/cosmos/base/v1beta1/coin.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BinaryReader, BinaryWriter } from "../../../binary";
22
import { DeepPartial } from "../../../helpers";
3+
import { Decimal } from "../../../decimals";
34
/**
45
* Coin defines a token with a denomination and an amount.
56
*
@@ -93,7 +94,7 @@ export const DecCoin = {
9394
writer.uint32(10).string(message.denom);
9495
}
9596
if (message.amount !== "") {
96-
writer.uint32(18).string(message.amount);
97+
writer.uint32(18).string(Decimal.fromUserInput(message.amount, 18).atomics);
9798
}
9899
return writer;
99100
},
@@ -108,7 +109,7 @@ export const DecCoin = {
108109
message.denom = reader.string();
109110
break;
110111
case 2:
111-
message.amount = reader.string();
112+
message.amount = Decimal.fromAtomics(reader.string(), 18).toString();
112113
break;
113114
default:
114115
reader.skipType(tag & 7);
@@ -171,7 +172,7 @@ export const DecProto = {
171172
aminoType: "cosmos-sdk/DecProto",
172173
encode(message: DecProto, writer: BinaryWriter = BinaryWriter.create()): BinaryWriter {
173174
if (message.dec !== "") {
174-
writer.uint32(10).string(message.dec);
175+
writer.uint32(10).string(Decimal.fromUserInput(message.dec, 18).atomics);
175176
}
176177
return writer;
177178
},
@@ -183,7 +184,7 @@ export const DecProto = {
183184
const tag = reader.uint32();
184185
switch (tag >>> 3) {
185186
case 1:
186-
message.dec = reader.string();
187+
message.dec = Decimal.fromAtomics(reader.string(), 18).toString();
187188
break;
188189
default:
189190
reader.skipType(tag & 7);

libs/cosmos-types/src/cosmos/gov/v1beta1/gov.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Any } from "../../../google/protobuf/any";
33
import { Timestamp } from "../../../google/protobuf/timestamp";
44
import { Duration } from "../../../google/protobuf/duration";
55
import { BinaryReader, BinaryWriter } from "../../../binary";
6+
import { Decimal } from "../../../decimals";
67
import { DeepPartial, toTimestamp, fromTimestamp } from "../../../helpers";
78
/** VoteOption enumerates the valid vote options for a given governance proposal. */
89
export enum VoteOption {
@@ -270,7 +271,7 @@ export const WeightedVoteOption = {
270271
writer.uint32(8).int32(message.option);
271272
}
272273
if (message.weight !== "") {
273-
writer.uint32(18).string(message.weight);
274+
writer.uint32(18).string(Decimal.fromUserInput(message.weight, 18).atomics);
274275
}
275276
return writer;
276277
},
@@ -285,7 +286,7 @@ export const WeightedVoteOption = {
285286
message.option = reader.int32() as any;
286287
break;
287288
case 2:
288-
message.weight = reader.string();
289+
message.weight = Decimal.fromAtomics(reader.string(), 18).toString();
289290
break;
290291
default:
291292
reader.skipType(tag & 7);

libs/cosmos-types/src/cosmos/staking/v1beta1/staking.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Coin } from "../../base/v1beta1/coin";
66
import { ValidatorUpdate } from "../../../tendermint/abci/types";
77
import { BinaryReader, BinaryWriter } from "../../../binary";
88
import { DeepPartial, toTimestamp, fromTimestamp } from "../../../helpers";
9+
import { Decimal } from "../../../decimals";
910
/** BondStatus is the status of a validator. */
1011
export enum BondStatus {
1112
/** BOND_STATUS_UNSPECIFIED - UNSPECIFIED defines an invalid validator status. */
@@ -391,13 +392,13 @@ export const CommissionRates = {
391392
aminoType: "cosmos-sdk/CommissionRates",
392393
encode(message: CommissionRates, writer: BinaryWriter = BinaryWriter.create()): BinaryWriter {
393394
if (message.rate !== "") {
394-
writer.uint32(10).string(message.rate);
395+
writer.uint32(10).string(Decimal.fromUserInput(message.rate, 18).atomics);
395396
}
396397
if (message.maxRate !== "") {
397-
writer.uint32(18).string(message.maxRate);
398+
writer.uint32(18).string(Decimal.fromUserInput(message.maxRate, 18).atomics);
398399
}
399400
if (message.maxChangeRate !== "") {
400-
writer.uint32(26).string(message.maxChangeRate);
401+
writer.uint32(26).string(Decimal.fromUserInput(message.maxChangeRate, 18).atomics);
401402
}
402403
return writer;
403404
},
@@ -409,13 +410,13 @@ export const CommissionRates = {
409410
const tag = reader.uint32();
410411
switch (tag >>> 3) {
411412
case 1:
412-
message.rate = reader.string();
413+
message.rate = Decimal.fromAtomics(reader.string(), 18).toString();
413414
break;
414415
case 2:
415-
message.maxRate = reader.string();
416+
message.maxRate = Decimal.fromAtomics(reader.string(), 18).toString();
416417
break;
417418
case 3:
418-
message.maxChangeRate = reader.string();
419+
message.maxChangeRate = Decimal.fromAtomics(reader.string(), 18).toString();
419420
break;
420421
default:
421422
reader.skipType(tag & 7);
@@ -583,7 +584,7 @@ export const Validator = {
583584
writer.uint32(42).string(message.tokens);
584585
}
585586
if (message.delegatorShares !== "") {
586-
writer.uint32(50).string(message.delegatorShares);
587+
writer.uint32(50).string(Decimal.fromUserInput(message.delegatorShares, 18).atomics);
587588
}
588589
if (message.description !== undefined) {
589590
Description.encode(message.description, writer.uint32(58).fork()).ldelim();
@@ -633,7 +634,7 @@ export const Validator = {
633634
message.tokens = reader.string();
634635
break;
635636
case 6:
636-
message.delegatorShares = reader.string();
637+
message.delegatorShares = Decimal.fromAtomics(reader.string(), 18).toString();
637638
break;
638639
case 7:
639640
message.description = Description.decode(reader, reader.uint32());
@@ -915,7 +916,7 @@ export const Delegation = {
915916
writer.uint32(18).string(message.validatorAddress);
916917
}
917918
if (message.shares !== "") {
918-
writer.uint32(26).string(message.shares);
919+
writer.uint32(26).string(Decimal.fromUserInput(message.shares, 18).atomics);
919920
}
920921
return writer;
921922
},
@@ -933,7 +934,7 @@ export const Delegation = {
933934
message.validatorAddress = reader.string();
934935
break;
935936
case 3:
936-
message.shares = reader.string();
937+
message.shares = Decimal.fromAtomics(reader.string(), 18).toString();
937938
break;
938939
default:
939940
reader.skipType(tag & 7);
@@ -1104,7 +1105,7 @@ export const RedelegationEntry = {
11041105
writer.uint32(26).string(message.initialBalance);
11051106
}
11061107
if (message.sharesDst !== "") {
1107-
writer.uint32(34).string(message.sharesDst);
1108+
writer.uint32(34).string(Decimal.fromUserInput(message.sharesDst, 18).atomics);
11081109
}
11091110
if (message.unbondingId !== BigInt(0)) {
11101111
writer.uint32(40).uint64(message.unbondingId);
@@ -1131,7 +1132,7 @@ export const RedelegationEntry = {
11311132
message.initialBalance = reader.string();
11321133
break;
11331134
case 4:
1134-
message.sharesDst = reader.string();
1135+
message.sharesDst = Decimal.fromAtomics(reader.string(), 18).toString();
11351136
break;
11361137
case 5:
11371138
message.unbondingId = reader.uint64();
@@ -1248,7 +1249,7 @@ export const Params = {
12481249
writer.uint32(42).string(message.bondDenom);
12491250
}
12501251
if (message.minCommissionRate !== "") {
1251-
writer.uint32(50).string(message.minCommissionRate);
1252+
writer.uint32(50).string(Decimal.fromUserInput(message.minCommissionRate, 18).atomics);
12521253
}
12531254
return writer;
12541255
},
@@ -1275,7 +1276,7 @@ export const Params = {
12751276
message.bondDenom = reader.string();
12761277
break;
12771278
case 6:
1278-
message.minCommissionRate = reader.string();
1279+
message.minCommissionRate = Decimal.fromAtomics(reader.string(), 18).toString();
12791280
break;
12801281
default:
12811282
reader.skipType(tag & 7);

libs/cosmos-types/src/decimals.ts

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* This file and any referenced files were automatically generated by @cosmology/[email protected]
3+
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
4+
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
5+
*/
6+
7+
8+
// The largest value we need is 18 (Ether).
9+
const maxFractionalDigits = 30;
10+
/**
11+
* A type for arbitrary precision, non-negative decimals.
12+
*
13+
* Instances of this class are immutable.
14+
*/
15+
export class Decimal {
16+
public static fromUserInput(
17+
input: string,
18+
fractionalDigits: number
19+
): Decimal {
20+
Decimal.verifyFractionalDigits(fractionalDigits);
21+
const badCharacter = input.match(/[^0-9.]/);
22+
if (badCharacter) {
23+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
24+
throw new Error(
25+
`Invalid character at position ${badCharacter.index! + 1}`
26+
);
27+
}
28+
let whole: string;
29+
let fractional: string;
30+
if (input === "") {
31+
whole = "0";
32+
fractional = "";
33+
} else if (input.search(/\./) === -1) {
34+
// integer format, no separator
35+
whole = input;
36+
fractional = "";
37+
} else {
38+
const parts = input.split(".");
39+
switch (parts.length) {
40+
case 0:
41+
case 1:
42+
throw new Error(
43+
"Fewer than two elements in split result. This must not happen here."
44+
);
45+
case 2:
46+
if (!parts[1]) throw new Error("Fractional part missing");
47+
whole = parts[0];
48+
fractional = parts[1].replace(/0+$/, "");
49+
break;
50+
default:
51+
throw new Error("More than one separator found");
52+
}
53+
}
54+
if (fractional.length > fractionalDigits) {
55+
throw new Error("Got more fractional digits than supported");
56+
}
57+
const quantity = `${whole}${fractional.padEnd(fractionalDigits, "0")}`;
58+
return new Decimal(quantity, fractionalDigits);
59+
}
60+
public static fromAtomics(
61+
atomics: string,
62+
fractionalDigits: number
63+
): Decimal {
64+
Decimal.verifyFractionalDigits(fractionalDigits);
65+
return new Decimal(atomics, fractionalDigits);
66+
}
67+
private static verifyFractionalDigits(fractionalDigits: number): void {
68+
if (!Number.isInteger(fractionalDigits))
69+
throw new Error("Fractional digits is not an integer");
70+
if (fractionalDigits < 0)
71+
throw new Error("Fractional digits must not be negative");
72+
if (fractionalDigits > maxFractionalDigits) {
73+
throw new Error(
74+
`Fractional digits must not exceed ${maxFractionalDigits}`
75+
);
76+
}
77+
}
78+
public get atomics(): string {
79+
return this.data.atomics.toString();
80+
}
81+
public get fractionalDigits(): number {
82+
return this.data.fractionalDigits;
83+
}
84+
private readonly data: {
85+
readonly atomics: bigint;
86+
readonly fractionalDigits: number;
87+
};
88+
private constructor(atomics: string, fractionalDigits: number) {
89+
if (!atomics.match(/^[0-9]+$/)) {
90+
throw new Error(
91+
"Invalid string format. Only non-negative integers in decimal representation supported."
92+
);
93+
}
94+
this.data = {
95+
atomics: BigInt(atomics),
96+
fractionalDigits: fractionalDigits,
97+
};
98+
}
99+
public toString(): string {
100+
const factor = BigInt(10) ** BigInt(this.data.fractionalDigits);
101+
const whole = this.data.atomics / factor;
102+
const fractional = this.data.atomics % factor;
103+
if (fractional === 0n) {
104+
return whole.toString();
105+
} else {
106+
const fullFractionalPart = fractional
107+
.toString()
108+
.padStart(this.data.fractionalDigits, "0");
109+
const trimmedFractionalPart = fullFractionalPart.replace(/0+$/, "");
110+
return `${whole.toString()}.${trimmedFractionalPart}`;
111+
}
112+
}
113+
}

libs/injective-react/src/cosmos/base/v1beta1/coin.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BinaryReader, BinaryWriter } from "../../../binary";
22
import { DeepPartial } from "../../../helpers";
3+
import { Decimal } from "../../../decimals";
34
/**
45
* Coin defines a token with a denomination and an amount.
56
*
@@ -209,7 +210,7 @@ export const DecCoin = {
209210
writer.uint32(10).string(message.denom);
210211
}
211212
if (message.amount !== "") {
212-
writer.uint32(18).string(message.amount);
213+
writer.uint32(18).string(Decimal.fromUserInput(message.amount, 18).atomics);
213214
}
214215
return writer;
215216
},
@@ -224,7 +225,7 @@ export const DecCoin = {
224225
message.denom = reader.string();
225226
break;
226227
case 2:
227-
message.amount = reader.string();
228+
message.amount = Decimal.fromAtomics(reader.string(), 18).toString();
228229
break;
229230
default:
230231
reader.skipType(tag & 7);
@@ -371,7 +372,7 @@ export const DecProto = {
371372
},
372373
encode(message: DecProto, writer: BinaryWriter = BinaryWriter.create()): BinaryWriter {
373374
if (message.dec !== "") {
374-
writer.uint32(10).string(message.dec);
375+
writer.uint32(10).string(Decimal.fromUserInput(message.dec, 18).atomics);
375376
}
376377
return writer;
377378
},
@@ -383,7 +384,7 @@ export const DecProto = {
383384
const tag = reader.uint32();
384385
switch (tag >>> 3) {
385386
case 1:
386-
message.dec = reader.string();
387+
message.dec = Decimal.fromAtomics(reader.string(), 18).toString();
387388
break;
388389
default:
389390
reader.skipType(tag & 7);

0 commit comments

Comments
 (0)