From 85357578177867fb2ad9b567a4e2808098ac6047 Mon Sep 17 00:00:00 2001 From: Zetazzz Date: Sat, 14 Sep 2024 20:58:21 +0800 Subject: [PATCH] change the flag name and add docs --- README.md | 1 + packages/telescope/README.md | 1 + packages/telescope/__tests__/misc.test.ts | 4 ++-- packages/telescope/src/generators/create-helpers.ts | 2 +- packages/telescope/src/generators/customize-utils.ts | 2 +- packages/telescope/types/helpers/decimals.d.ts | 1 - packages/types/src/telescope.ts | 2 +- packages/types/types/telescope.d.ts | 2 +- 8 files changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ce1de97cb..23fa39dcc 100644 --- a/README.md +++ b/README.md @@ -414,6 +414,7 @@ See [RPC Clients](#rpc-clients) for more info. | option | description | defaults | | ----------------------------------------- | -------------------------------------------------------------- | --------- | | `prototypes.typingsFormat.customTypes.useCosmosSDKDec` | enable handling "prototypes.typingsFormat.customTypes.useCosmosSDKDec" proto custom type. Used to show decimal fields with the custom type correctly. Highly recommend set to true. | `true` | +| `prototypes.typingsFormat.customTypes.usePatchedDecimal` | To use patched decimal other then decimal from @cosmjs/math | `false` | | `prototypes.typingsFormat.num64` | 'long' or 'bigint', the way of generating int64 proto types, set to 'bigint' to enable using more stable built-in type | `bigint` | | `prototypes.typingsFormat.useTelescopeGeneratedType` | Discard GeneratedType from cosmjs, use TelescopeGeneratedType instead inside *.registry.ts files | `false` | | `prototypes.typingsFormat.useDeepPartial` | defaults to true, but if disabled uses the `Partial` TS type | `false` | diff --git a/packages/telescope/README.md b/packages/telescope/README.md index ce1de97cb..23fa39dcc 100644 --- a/packages/telescope/README.md +++ b/packages/telescope/README.md @@ -414,6 +414,7 @@ See [RPC Clients](#rpc-clients) for more info. | option | description | defaults | | ----------------------------------------- | -------------------------------------------------------------- | --------- | | `prototypes.typingsFormat.customTypes.useCosmosSDKDec` | enable handling "prototypes.typingsFormat.customTypes.useCosmosSDKDec" proto custom type. Used to show decimal fields with the custom type correctly. Highly recommend set to true. | `true` | +| `prototypes.typingsFormat.customTypes.usePatchedDecimal` | To use patched decimal other then decimal from @cosmjs/math | `false` | | `prototypes.typingsFormat.num64` | 'long' or 'bigint', the way of generating int64 proto types, set to 'bigint' to enable using more stable built-in type | `bigint` | | `prototypes.typingsFormat.useTelescopeGeneratedType` | Discard GeneratedType from cosmjs, use TelescopeGeneratedType instead inside *.registry.ts files | `false` | | `prototypes.typingsFormat.useDeepPartial` | defaults to true, but if disabled uses the `Partial` TS type | `false` | diff --git a/packages/telescope/__tests__/misc.test.ts b/packages/telescope/__tests__/misc.test.ts index 789582baa..a461f0c0a 100644 --- a/packages/telescope/__tests__/misc.test.ts +++ b/packages/telescope/__tests__/misc.test.ts @@ -434,7 +434,7 @@ describe('misc', () => { await telescope.build(); }); - it('generates with useAgoricDecimals', async () => { + it('generates with usePatchedDecimal', async () => { const testFolder = '/output-decimals/agoric'; const telescope = new TelescopeBuilder({ @@ -444,7 +444,7 @@ describe('misc', () => { prototypes: { typingsFormat: { customTypes: { - useAgoricDecimal: true, + usePatchedDecimal: true, }, }, }, diff --git a/packages/telescope/src/generators/create-helpers.ts b/packages/telescope/src/generators/create-helpers.ts index 015daa8b5..0522311c0 100644 --- a/packages/telescope/src/generators/create-helpers.ts +++ b/packages/telescope/src/generators/create-helpers.ts @@ -94,7 +94,7 @@ export const plugin = (builder: TelescopeBuilder) => { write(builder, 'grpc-web.ts', grpcWeb); } - if (builder.options.prototypes.typingsFormat.customTypes.useAgoricDecimal) { + if (builder.options.prototypes.typingsFormat.customTypes.usePatchedDecimal) { builder.files.push('decimals.ts'); write(builder, 'decimals.ts', decimal); } diff --git a/packages/telescope/src/generators/customize-utils.ts b/packages/telescope/src/generators/customize-utils.ts index 85b0e0a94..03b1e9e4d 100644 --- a/packages/telescope/src/generators/customize-utils.ts +++ b/packages/telescope/src/generators/customize-utils.ts @@ -3,7 +3,7 @@ import { UTILS } from '../utils'; export const plugin = (builder: TelescopeBuilder) => { if ( - builder.options.prototypes.typingsFormat.customTypes.useAgoricDecimal === + builder.options.prototypes.typingsFormat.customTypes.usePatchedDecimal === true ) { UTILS.Decimal = '__decimals__'; diff --git a/packages/telescope/types/helpers/decimals.d.ts b/packages/telescope/types/helpers/decimals.d.ts index 1f6c756e7..2f54c9084 100644 --- a/packages/telescope/types/helpers/decimals.d.ts +++ b/packages/telescope/types/helpers/decimals.d.ts @@ -1,2 +1 @@ -export declare const cosmjsDecimal = "\nexport { Decimal } from \"@cosmjs/math\";\n"; export declare const decimal = "\n// START agoric-sdk patch\n// The largest value we need is 18 (Ether).\nconst maxFractionalDigits = 30;\n/**\n * A type for arbitrary precision, non-negative decimals.\n *\n * Instances of this class are immutable.\n */\nexport class Decimal {\n public static fromUserInput(\n input: string,\n fractionalDigits: number\n ): Decimal {\n Decimal.verifyFractionalDigits(fractionalDigits);\n const badCharacter = input.match(/[^0-9.]/);\n if (badCharacter) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n throw new Error(\n `Invalid character at position ${badCharacter.index! + 1}`\n );\n }\n let whole: string;\n let fractional: string;\n if (input === \"\") {\n whole = \"0\";\n fractional = \"\";\n } else if (input.search(/\\./) === -1) {\n // integer format, no separator\n whole = input;\n fractional = \"\";\n } else {\n const parts = input.split(\".\");\n switch (parts.length) {\n case 0:\n case 1:\n throw new Error(\n \"Fewer than two elements in split result. This must not happen here.\"\n );\n case 2:\n if (!parts[1]) throw new Error(\"Fractional part missing\");\n whole = parts[0];\n fractional = parts[1].replace(/0+$/, \"\");\n break;\n default:\n throw new Error(\"More than one separator found\");\n }\n }\n if (fractional.length > fractionalDigits) {\n throw new Error(\"Got more fractional digits than supported\");\n }\n const quantity = `${whole}${fractional.padEnd(fractionalDigits, \"0\")}`;\n return new Decimal(quantity, fractionalDigits);\n }\n public static fromAtomics(\n atomics: string,\n fractionalDigits: number\n ): Decimal {\n Decimal.verifyFractionalDigits(fractionalDigits);\n return new Decimal(atomics, fractionalDigits);\n }\n private static verifyFractionalDigits(fractionalDigits: number): void {\n if (!Number.isInteger(fractionalDigits))\n throw new Error(\"Fractional digits is not an integer\");\n if (fractionalDigits < 0)\n throw new Error(\"Fractional digits must not be negative\");\n if (fractionalDigits > maxFractionalDigits) {\n throw new Error(\n `Fractional digits must not exceed ${maxFractionalDigits}`\n );\n }\n }\n public get atomics(): string {\n return this.data.atomics.toString();\n }\n public get fractionalDigits(): number {\n return this.data.fractionalDigits;\n }\n private readonly data: {\n readonly atomics: bigint;\n readonly fractionalDigits: number;\n };\n private constructor(atomics: string, fractionalDigits: number) {\n if (!atomics.match(/^[0-9]+$/)) {\n throw new Error(\n \"Invalid string format. Only non-negative integers in decimal representation supported.\"\n );\n }\n this.data = {\n atomics: BigInt(atomics),\n fractionalDigits: fractionalDigits,\n };\n }\n public toString(): string {\n const factor = BigInt(10) ** BigInt(this.data.fractionalDigits);\n const whole = this.data.atomics / factor;\n const fractional = this.data.atomics % factor;\n if (fractional === 0n) {\n return whole.toString();\n } else {\n const fullFractionalPart = fractional\n .toString()\n .padStart(this.data.fractionalDigits, \"0\");\n const trimmedFractionalPart = fullFractionalPart.replace(/0+$/, \"\");\n return `${whole.toString()}.${trimmedFractionalPart}`;\n }\n }\n}\n// END agoric-sdk patch\n"; diff --git a/packages/types/src/telescope.ts b/packages/types/src/telescope.ts index abf3f29d7..5342aa029 100644 --- a/packages/types/src/telescope.ts +++ b/packages/types/src/telescope.ts @@ -87,7 +87,7 @@ interface TelescopeOpts { typingsFormat?: { customTypes?: { useCosmosSDKDec?: boolean; - useAgoricDecimal?: boolean; + usePatchedDecimal?: boolean; }; num64?: 'long' | 'bigint'; diff --git a/packages/types/types/telescope.d.ts b/packages/types/types/telescope.d.ts index 511a0d1d1..ecfd05c9d 100644 --- a/packages/types/types/telescope.d.ts +++ b/packages/types/types/telescope.d.ts @@ -74,7 +74,7 @@ interface TelescopeOpts { typingsFormat?: { customTypes?: { useCosmosSDKDec?: boolean; - useAgoricDecimal?: boolean; + usePatchedDecimal?: boolean; }; num64?: 'long' | 'bigint'; useDeepPartial?: boolean;