Skip to content

Commit 48e4944

Browse files
committed
handle decimal in to amino json
1 parent 0fe791c commit 48e4944

File tree

32 files changed

+44
-73
lines changed

32 files changed

+44
-73
lines changed

__fixtures__/misc/enforce-null-check-true/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output-base64/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output-decimals/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output-impl-interfaces-gen/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output-proto-amino/amino-interface-proto-optionality/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output-proto-amino/amino-interface/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output-proto-amino/amino-legacy/helpers.ts

-7
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
55
*/
66

7-
import { Dec } from "@keplr-wallet/unit";
87

9-
108
declare var self: any | undefined;
119
declare var window: any | undefined;
1210
declare var global: any | undefined;
@@ -258,8 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
258256
function numberToLong(number: number) {
259257
return BigInt(Math.trunc(number));
260258
}
261-
262-
export function padDecimal(decStr: string): string{
263-
return decStr ? new Dec(decStr).toString() : decStr;
264-
}
265-

__fixtures__/misc/output-proto-amino/amino-legacy/misc/tx.amino.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { EncodingTestForDontOmit, EncodingTestForDontOmitSDKType, EncodingTestFo
22
import { AminoMsg, Pubkey } from "@cosmjs/amino";
33
import { fromUtf8, toBase64, toUtf8, fromBase64 } from "@cosmjs/encoding";
44
import { decodePubkey, encodePubkey } from "@cosmjs/proto-signing";
5-
import { padDecimal } from "../helpers";
5+
import { Decimal } from "@cosmjs/math";
66
import { AccessConfig, AccessConfigSDKType, voteOptionFromJSON } from "./eval_request";
77
import { Any, AnySDKType } from "../google/protobuf/any";
88
import { Duration, DurationSDKType } from "../google/protobuf/duration";
@@ -255,10 +255,10 @@ export const AminoConverter = {
255255
type_url: el0.typeUrl,
256256
value: el0.value
257257
})),
258-
dec: padDecimal(dOTests.dec),
259-
d_o_dec: padDecimal(dOTests.dODec),
260-
decs: dOTests.decs.map(el0 => padDecimal(el0)),
261-
d_o_decs: dOTests.dODecs.map(el0 => padDecimal(el0))
258+
dec: Decimal.fromUserInput(dOTests.dec, 18).atomics,
259+
d_o_dec: Decimal.fromUserInput(dOTests.dODec, 18).atomics,
260+
decs: dOTests.decs.map(el0 => Decimal.fromUserInput(el0, 18).atomics),
261+
d_o_decs: dOTests.dODecs.map(el0 => Decimal.fromUserInput(el0, 18).atomics)
262262
},
263263
o_tests: {
264264
str: oTests.str,
@@ -331,10 +331,10 @@ export const AminoConverter = {
331331
type_url: el0.typeUrl,
332332
value: el0.value
333333
})),
334-
dec: padDecimal(oTests.dec),
335-
o_dec: padDecimal(oTests.oDec),
336-
decs: oTests.decs.map(el0 => padDecimal(el0)),
337-
o_decs: oTests.oDecs.map(el0 => padDecimal(el0))
334+
dec: Decimal.fromUserInput(oTests.dec, 18).atomics,
335+
o_dec: Decimal.fromUserInput(oTests.oDec, 18).atomics,
336+
decs: oTests.decs.map(el0 => Decimal.fromUserInput(el0, 18).atomics),
337+
o_decs: oTests.oDecs.map(el0 => Decimal.fromUserInput(el0, 18).atomics)
338338
}
339339
};
340340
},

__fixtures__/misc/output-proto-amino/amino-msg-no-func/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output-proto-amino/amino-msg-with-func/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output-proto-amino/amino-no-msg-with-func/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output-proto-amino/no-amino-legacy-no-proto/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output-proto-amino/no-amino-legacy/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output-proto-amino/no-amino-no-proto/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output-proto-amino/no-amino/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output-proto-amino/proto-only-legacy/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output-proto-amino/proto-only/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output-scalar-default-nullable-true/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/misc/output/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/v-next/outputhelperfunc/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/v-next/outputicjs/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/v-next/outputinstantrpc/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/v-next/outputosmojs/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/v-next/outputsign/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/v-next/outputv2/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/v-next/outputv3/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

__fixtures__/v-next/outputv4/helpers.ts

-1
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
256256
function numberToLong(number: number) {
257257
return BigInt(Math.trunc(number));
258258
}
259-

packages/ast/src/encoding/amino/converter/__snapshots__/amino-converter.spec.ts.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,9 @@ exports[`cosmos/staking/v1beta1/tx AminoConverter useCosmosSDKDec 1`] = `
232232
details: description.details
233233
},
234234
commission: {
235-
rate: padDecimal(commission.rate),
236-
max_rate: padDecimal(commission.maxRate),
237-
max_change_rate: padDecimal(commission.maxChangeRate)
235+
rate: Decimal.fromUserInput(commission.rate, 18).atomics,
236+
max_rate: Decimal.fromUserInput(commission.maxRate, 18).atomics,
237+
max_change_rate: Decimal.fromUserInput(commission.maxChangeRate, 18).atomics
238238
},
239239
min_self_delegation: minSelfDelegation,
240240
delegator_address: delegatorAddress,
@@ -296,7 +296,7 @@ exports[`cosmos/staking/v1beta1/tx AminoConverter useCosmosSDKDec 1`] = `
296296
details: description.details
297297
},
298298
validator_address: validatorAddress,
299-
commission_rate: padDecimal(commissionRate),
299+
commission_rate: Decimal.fromUserInput(commissionRate, 18).atomics,
300300
min_self_delegation: minSelfDelegation
301301
};
302302
},

packages/ast/src/encoding/amino/to-amino-json/utils.ts

+28-8
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,22 @@ export const toAmino = {
8181
"cosmossdk.io/math.LegacyDec";
8282

8383
if (isCosmosSDKDec) {
84-
args.context.addUtil("padDecimal");
84+
args.context.addUtil("Decimal");
8585
return t.objectProperty(
8686
t.identifier(args.context.aminoCaseField(args.field)),
87-
t.callExpression(t.identifier("padDecimal"), [
88-
memberExpressionOrIdentifier(args.scope),
89-
])
87+
t.memberExpression(
88+
t.callExpression(
89+
t.memberExpression(
90+
t.identifier("Decimal"),
91+
t.identifier("fromUserInput")
92+
),
93+
[
94+
memberExpressionOrIdentifier(args.scope),
95+
t.numericLiteral(18),
96+
]
97+
),
98+
t.identifier("atomics")
99+
)
90100
);
91101
}
92102
}
@@ -495,9 +505,19 @@ export const arrayTypes = {
495505
},
496506

497507
stringDec(varname: string, args: ToAminoParseField) {
498-
args.context.addUtil("padDecimal");
499-
return t.callExpression(t.identifier("padDecimal"), [
500-
memberExpressionOrIdentifier([varname]),
501-
]);
508+
args.context.addUtil("Decimal");
509+
return t.memberExpression(
510+
t.callExpression(
511+
t.memberExpression(
512+
t.identifier("Decimal"),
513+
t.identifier("fromUserInput")
514+
),
515+
[
516+
memberExpressionOrIdentifier([varname]),
517+
t.numericLiteral(18),
518+
]
519+
),
520+
t.identifier("atomics")
521+
);
502522
},
503523
};

packages/ast/types/encoding/amino/to-amino-json/utils.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ export declare const toAmino: {
2121
};
2222
export declare const arrayTypes: {
2323
long(varname: string): t.CallExpression;
24-
stringDec(varname: string, args: ToAminoParseField): t.CallExpression;
24+
stringDec(varname: string, args: ToAminoParseField): t.MemberExpression;
2525
};

packages/telescope/src/helpers/internal-for-bigint.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { TelescopeOptions } from "@cosmology/types";
22

33
export const getHelperForBigint = (options: TelescopeOptions) => {
4-
return `${options.aminoEncoding?.customTypes?.useCosmosSDKDec ? `import { Dec } from "@keplr-wallet/unit";
5-
6-
`: ""}
4+
return `
75
declare var self: any | undefined;
86
declare var window: any | undefined;
97
declare var global: any | undefined;
@@ -255,13 +253,5 @@ export function fromJsonTimestamp(o: any): Timestamp {
255253
function numberToLong(number: number) {
256254
return BigInt(Math.trunc(number));
257255
}
258-
${
259-
options.aminoEncoding?.customTypes?.useCosmosSDKDec ?
260-
`
261-
export function padDecimal(decStr: string): string{
262-
return decStr ? new Dec(decStr).toString() : decStr;
263-
}
264-
` : ""
265-
}
266256
`;
267257
};

packages/telescope/src/helpers/internal.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { TelescopeOptions } from "@cosmology/types";
22

33
export const getHelper = (options: TelescopeOptions) => {
44
return `import * as _m0 from "protobufjs/minimal";
5-
import Long from 'long';${options.aminoEncoding?.customTypes?.useCosmosSDKDec ? `
6-
import { Dec } from "@keplr-wallet/unit";
7-
`: ""}
5+
import Long from 'long';
86
97
// @ts-ignore
108
if (_m0.util.Long !== Long) {
@@ -235,11 +233,6 @@ export function fromJsonTimestamp(o: any): Timestamp {
235233
236234
function numberToLong(number: number) {
237235
return Long.fromNumber(number);
238-
}${options.aminoEncoding?.customTypes?.useCosmosSDKDec ? `
239-
240-
export function padDecimal(decStr: string): string{
241-
return decStr ? new Dec(decStr).toString() : decStr;
242236
}
243-
`: ""}
244237
`;
245238
}

0 commit comments

Comments
 (0)