Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle decimal in to amino json #736

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion __fixtures__/misc/enforce-null-check-true/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

1 change: 0 additions & 1 deletion __fixtures__/misc/output-base64/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

1 change: 0 additions & 1 deletion __fixtures__/misc/output-decimals/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

1 change: 0 additions & 1 deletion __fixtures__/misc/output-impl-interfaces-gen/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

7 changes: 0 additions & 7 deletions __fixtures__/misc/output-proto-amino/amino-legacy/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
* and run the transpile command or npm scripts command that is used to regenerate this bundle.
*/

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


declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
Expand Down Expand Up @@ -258,8 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

export function padDecimal(decStr: string): string{
return decStr ? new Dec(decStr).toString() : decStr;
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EncodingTestForDontOmit, EncodingTestForDontOmitSDKType, EncodingTestFo
import { AminoMsg, Pubkey } from "@cosmjs/amino";
import { fromUtf8, toBase64, toUtf8, fromBase64 } from "@cosmjs/encoding";
import { decodePubkey, encodePubkey } from "@cosmjs/proto-signing";
import { padDecimal } from "../helpers";
import { Decimal } from "@cosmjs/math";
import { AccessConfig, AccessConfigSDKType, voteOptionFromJSON } from "./eval_request";
import { Any, AnySDKType } from "../google/protobuf/any";
import { Duration, DurationSDKType } from "../google/protobuf/duration";
Expand Down Expand Up @@ -255,10 +255,10 @@ export const AminoConverter = {
type_url: el0.typeUrl,
value: el0.value
})),
dec: padDecimal(dOTests.dec),
d_o_dec: padDecimal(dOTests.dODec),
decs: dOTests.decs.map(el0 => padDecimal(el0)),
d_o_decs: dOTests.dODecs.map(el0 => padDecimal(el0))
dec: Decimal.fromUserInput(dOTests.dec, 18).atomics,
d_o_dec: Decimal.fromUserInput(dOTests.dODec, 18).atomics,
decs: dOTests.decs.map(el0 => Decimal.fromUserInput(el0, 18).atomics),
d_o_decs: dOTests.dODecs.map(el0 => Decimal.fromUserInput(el0, 18).atomics)
},
o_tests: {
str: oTests.str,
Expand Down Expand Up @@ -331,10 +331,10 @@ export const AminoConverter = {
type_url: el0.typeUrl,
value: el0.value
})),
dec: padDecimal(oTests.dec),
o_dec: padDecimal(oTests.oDec),
decs: oTests.decs.map(el0 => padDecimal(el0)),
o_decs: oTests.oDecs.map(el0 => padDecimal(el0))
dec: Decimal.fromUserInput(oTests.dec, 18).atomics,
o_dec: Decimal.fromUserInput(oTests.oDec, 18).atomics,
decs: oTests.decs.map(el0 => Decimal.fromUserInput(el0, 18).atomics),
o_decs: oTests.oDecs.map(el0 => Decimal.fromUserInput(el0, 18).atomics)
}
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

1 change: 0 additions & 1 deletion __fixtures__/misc/output-proto-amino/no-amino/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

1 change: 0 additions & 1 deletion __fixtures__/misc/output-proto-amino/proto-only/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

1 change: 0 additions & 1 deletion __fixtures__/misc/output/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

1 change: 0 additions & 1 deletion __fixtures__/v-next/outputhelperfunc/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

1 change: 0 additions & 1 deletion __fixtures__/v-next/outputicjs/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

1 change: 0 additions & 1 deletion __fixtures__/v-next/outputinstantrpc/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

1 change: 0 additions & 1 deletion __fixtures__/v-next/outputosmojs/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

1 change: 0 additions & 1 deletion __fixtures__/v-next/outputsign/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

1 change: 0 additions & 1 deletion __fixtures__/v-next/outputv2/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

1 change: 0 additions & 1 deletion __fixtures__/v-next/outputv3/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

1 change: 0 additions & 1 deletion __fixtures__/v-next/outputv4/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,3 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ exports[`cosmos/staking/v1beta1/tx AminoConverter useCosmosSDKDec 1`] = `
details: description.details
},
commission: {
rate: padDecimal(commission.rate),
max_rate: padDecimal(commission.maxRate),
max_change_rate: padDecimal(commission.maxChangeRate)
rate: Decimal.fromUserInput(commission.rate, 18).atomics,
max_rate: Decimal.fromUserInput(commission.maxRate, 18).atomics,
max_change_rate: Decimal.fromUserInput(commission.maxChangeRate, 18).atomics
},
min_self_delegation: minSelfDelegation,
delegator_address: delegatorAddress,
Expand Down Expand Up @@ -296,7 +296,7 @@ exports[`cosmos/staking/v1beta1/tx AminoConverter useCosmosSDKDec 1`] = `
details: description.details
},
validator_address: validatorAddress,
commission_rate: padDecimal(commissionRate),
commission_rate: Decimal.fromUserInput(commissionRate, 18).atomics,
min_self_delegation: minSelfDelegation
};
},
Expand Down
36 changes: 28 additions & 8 deletions packages/ast/src/encoding/amino/to-amino-json/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,22 @@ export const toAmino = {
"cosmossdk.io/math.LegacyDec";

if (isCosmosSDKDec) {
args.context.addUtil("padDecimal");
args.context.addUtil("Decimal");
return t.objectProperty(
t.identifier(args.context.aminoCaseField(args.field)),
t.callExpression(t.identifier("padDecimal"), [
memberExpressionOrIdentifier(args.scope),
])
t.memberExpression(
t.callExpression(
t.memberExpression(
t.identifier("Decimal"),
t.identifier("fromUserInput")
),
[
memberExpressionOrIdentifier(args.scope),
t.numericLiteral(18),
]
),
t.identifier("atomics")
)
);
}
}
Expand Down Expand Up @@ -495,9 +505,19 @@ export const arrayTypes = {
},

stringDec(varname: string, args: ToAminoParseField) {
args.context.addUtil("padDecimal");
return t.callExpression(t.identifier("padDecimal"), [
memberExpressionOrIdentifier([varname]),
]);
args.context.addUtil("Decimal");
return t.memberExpression(
t.callExpression(
t.memberExpression(
t.identifier("Decimal"),
t.identifier("fromUserInput")
),
[
memberExpressionOrIdentifier([varname]),
t.numericLiteral(18),
]
),
t.identifier("atomics")
);
},
};
2 changes: 1 addition & 1 deletion packages/ast/types/encoding/amino/to-amino-json/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export declare const toAmino: {
};
export declare const arrayTypes: {
long(varname: string): t.CallExpression;
stringDec(varname: string, args: ToAminoParseField): t.CallExpression;
stringDec(varname: string, args: ToAminoParseField): t.MemberExpression;
};
12 changes: 1 addition & 11 deletions packages/telescope/src/helpers/internal-for-bigint.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { TelescopeOptions } from "@cosmology/types";

export const getHelperForBigint = (options: TelescopeOptions) => {
return `${options.aminoEncoding?.customTypes?.useCosmosSDKDec ? `import { Dec } from "@keplr-wallet/unit";

`: ""}
return `
declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
Expand Down Expand Up @@ -255,13 +253,5 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}
${
options.aminoEncoding?.customTypes?.useCosmosSDKDec ?
`
export function padDecimal(decStr: string): string{
return decStr ? new Dec(decStr).toString() : decStr;
}
` : ""
}
`;
};
9 changes: 1 addition & 8 deletions packages/telescope/src/helpers/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { TelescopeOptions } from "@cosmology/types";

export const getHelper = (options: TelescopeOptions) => {
return `import * as _m0 from "protobufjs/minimal";
import Long from 'long';${options.aminoEncoding?.customTypes?.useCosmosSDKDec ? `
import { Dec } from "@keplr-wallet/unit";
`: ""}
import Long from 'long';

// @ts-ignore
if (_m0.util.Long !== Long) {
Expand Down Expand Up @@ -235,11 +233,6 @@ export function fromJsonTimestamp(o: any): Timestamp {

function numberToLong(number: number) {
return Long.fromNumber(number);
}${options.aminoEncoding?.customTypes?.useCosmosSDKDec ? `

export function padDecimal(decStr: string): string{
return decStr ? new Dec(decStr).toString() : decStr;
}
`: ""}
`;
}
Loading