Skip to content

Commit

Permalink
change the flag name and add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetazzz committed Sep 14, 2024
1 parent 40b4130 commit 8535757
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
1 change: 1 addition & 0 deletions packages/telescope/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
4 changes: 2 additions & 2 deletions packages/telescope/__tests__/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -444,7 +444,7 @@ describe('misc', () => {
prototypes: {
typingsFormat: {
customTypes: {
useAgoricDecimal: true,
usePatchedDecimal: true,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/telescope/src/generators/create-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/telescope/src/generators/customize-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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__';
Expand Down
1 change: 0 additions & 1 deletion packages/telescope/types/helpers/decimals.d.ts
Original file line number Diff line number Diff line change
@@ -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";
2 changes: 1 addition & 1 deletion packages/types/src/telescope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ interface TelescopeOpts {
typingsFormat?: {
customTypes?: {
useCosmosSDKDec?: boolean;
useAgoricDecimal?: boolean;
usePatchedDecimal?: boolean;
};

num64?: 'long' | 'bigint';
Expand Down
2 changes: 1 addition & 1 deletion packages/types/types/telescope.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ interface TelescopeOpts {
typingsFormat?: {
customTypes?: {
useCosmosSDKDec?: boolean;
useAgoricDecimal?: boolean;
usePatchedDecimal?: boolean;
};
num64?: 'long' | 'bigint';
useDeepPartial?: boolean;
Expand Down

0 comments on commit 8535757

Please sign in to comment.