From cf5279bbc9afa52bae57cccb2961c945d64c9ad4 Mon Sep 17 00:00:00 2001 From: j-yw <32629001+j-yw@users.noreply.github.com> Date: Sat, 14 Sep 2024 15:38:42 +0800 Subject: [PATCH] feat: add conditional logic to change UTILS.decimals value --- packages/telescope/__tests__/misc.test.ts | 21 +++++++- packages/telescope/src/builder.ts | 53 ++++++++++++------- .../src/generators/create-helpers.ts | 8 +-- .../src/generators/customize-utils.ts | 13 +++++ packages/telescope/src/utils/index.ts | 2 +- packages/types/src/telescope.ts | 2 +- 6 files changed, 70 insertions(+), 29 deletions(-) create mode 100644 packages/telescope/src/generators/customize-utils.ts diff --git a/packages/telescope/__tests__/misc.test.ts b/packages/telescope/__tests__/misc.test.ts index 28895ff42..789582baa 100644 --- a/packages/telescope/__tests__/misc.test.ts +++ b/packages/telescope/__tests__/misc.test.ts @@ -48,7 +48,6 @@ const options: TelescopeOptions = { typingsFormat: { customTypes: { useCosmosSDKDec: true, - useAgoricDecimal: undefined, }, num64: 'bigint', useDeepPartial: true, @@ -434,4 +433,24 @@ describe('misc', () => { await telescope.build(); }); + + it('generates with useAgoricDecimals', async () => { + const testFolder = '/output-decimals/agoric'; + + const telescope = new TelescopeBuilder({ + outPath: __dirname + '/../../../__fixtures__/misc' + testFolder, + protoDirs: [__dirname + '/../../../__fixtures__/misc/proto'], + options: deepmerge(options, { + prototypes: { + typingsFormat: { + customTypes: { + useAgoricDecimal: true, + }, + }, + }, + }), + }); + + await telescope.build(); + }); }); diff --git a/packages/telescope/src/builder.ts b/packages/telescope/src/builder.ts index 7e92f5ff6..1c5d7e199 100644 --- a/packages/telescope/src/builder.ts +++ b/packages/telescope/src/builder.ts @@ -25,20 +25,23 @@ import { plugin as createBundle } from './generators/create-bundle'; import { plugin as createIndex } from './generators/create-index'; import { plugin as createHelpers } from './generators/create-helpers'; import { plugin as createCosmWasmBundle } from './generators/create-cosmwasm-bundle'; -import { plugin as createPiniaStore } from './generators/create-pinia-store' -import { plugin as createPiniaStoreBundle } from './generators/create-pinia-store-bundle' -import { plugin as createRpcOpsBundle } from './generators/create-rpc-ops-bundle' +import { plugin as createPiniaStore } from './generators/create-pinia-store'; +import { plugin as createPiniaStoreBundle } from './generators/create-pinia-store-bundle'; +import { plugin as createRpcOpsBundle } from './generators/create-rpc-ops-bundle'; +import { plugin as customizeUtils } from './generators/customize-utils'; const sanitizeOptions = (options: TelescopeOptions): TelescopeOptions => { // If an element at the same key is present for both x and y, the value from y will appear in the result. options = deepmerge(defaultTelescopeOptions, options ?? {}); // correct the path for windows - if(options.cosmwasm){ - options.cosmwasm.outPath = toPosixPath(options.cosmwasm.outPath) - options.cosmwasm.contracts = options.cosmwasm.contracts.map((item:{name:string, dir:string})=>{ - item.dir = toPosixPath(item.dir) - return item - }) + if (options.cosmwasm) { + options.cosmwasm.outPath = toPosixPath(options.cosmwasm.outPath); + options.cosmwasm.contracts = options.cosmwasm.contracts.map( + (item: { name: string; dir: string }) => { + item.dir = toPosixPath(item.dir); + return item; + } + ); } // strip off leading slashes options.tsDisable.files = options.tsDisable.files.map((file) => @@ -49,7 +52,7 @@ const sanitizeOptions = (options: TelescopeOptions): TelescopeOptions => { ); // uniq bc of deepmerge options.rpcClients.enabledServices = [ - ...new Set([...options.rpcClients.enabledServices]) + ...new Set([...options.rpcClients.enabledServices]), ]; return options; }; @@ -73,12 +76,12 @@ export class TelescopeBuilder { protoDirs, outPath, store, - options + options, }: TelescopeInput & { store?: ProtoStore }) { - const fixedDirs = protoDirs.map((directory)=>{ - return toPosixPath(directory) + const fixedDirs = protoDirs.map((directory) => { + return toPosixPath(directory); }); - this.protoDirs = fixedDirs + this.protoDirs = fixedDirs; this.outPath = resolve(toPosixPath(outPath)); this.options = sanitizeOptions(options); this.store = store ?? new ProtoStore(fixedDirs, this.options); @@ -123,14 +126,24 @@ export class TelescopeBuilder { async build() { // check warnings - if(!this.options.aminoEncoding?.enabled && (this.options.prototypes?.methods?.fromAmino || this.options.prototypes?.methods?.toAmino)){ - console.warn("There could be compilation errors in generated code, because 'aminoEncoding.enabled: false' means amino types wouldn't be created, but 'toAmino' or 'fromAmino' need amino types."); + if ( + !this.options.aminoEncoding?.enabled && + (this.options.prototypes?.methods?.fromAmino || + this.options.prototypes?.methods?.toAmino) + ) { + console.warn( + "There could be compilation errors in generated code, because 'aminoEncoding.enabled: false' means amino types wouldn't be created, but 'toAmino' or 'fromAmino' need amino types." + ); } - if(!this.options.prototypes.methods.fromPartial){ - console.warn("The 'fromPartial' option will be deprecated in a future version. Encoder objects need fromPartial to be a creator function to create instance of the type. So it should always be left on, otherwise there could be compilation errors in generated code."); + if (!this.options.prototypes.methods.fromPartial) { + console.warn( + "The 'fromPartial' option will be deprecated in a future version. Encoder objects need fromPartial to be a creator function to create instance of the type. So it should always be left on, otherwise there could be compilation errors in generated code." + ); } + customizeUtils(this); + // [x] get bundle of all packages const bundles = bundlePackages(this.store).map((bundle) => { // store bundleFile in filesToInclude @@ -150,7 +163,7 @@ export class TelescopeBuilder { createRPCQueryClients(this, bundler); createRPCMsgClients(this, bundler); - createPiniaStore(this, bundler) + createPiniaStore(this, bundler); // [x] write out one client for each base package, referencing the last two steps createStargateClients(this, bundler); @@ -174,7 +187,7 @@ export class TelescopeBuilder { await createCosmWasmBundle(this); createHelpers(this); - createPiniaStoreBundle(this) + createPiniaStoreBundle(this); // finally, write one index file with all files, exported createIndex(this); diff --git a/packages/telescope/src/generators/create-helpers.ts b/packages/telescope/src/generators/create-helpers.ts index b919fc2c7..37bfe1941 100644 --- a/packages/telescope/src/generators/create-helpers.ts +++ b/packages/telescope/src/generators/create-helpers.ts @@ -95,13 +95,9 @@ export const plugin = (builder: TelescopeBuilder) => { write(builder, 'grpc-web.ts', grpcWeb); } - const useAgoric = - builder.options.prototypes.typingsFormat.customTypes.useAgoricDecimal; - - if (useAgoric === 'default' || useAgoric === undefined) { + if (builder.options.prototypes.typingsFormat.customTypes.useAgoricDecimal) { builder.files.push('decimals.ts'); - const content = useAgoric === 'default' ? decimal : cosmjsDecimal; - write(builder, 'decimals.ts', content); + write(builder, 'decimals.ts', decimal); } if ( diff --git a/packages/telescope/src/generators/customize-utils.ts b/packages/telescope/src/generators/customize-utils.ts new file mode 100644 index 000000000..85b0e0a94 --- /dev/null +++ b/packages/telescope/src/generators/customize-utils.ts @@ -0,0 +1,13 @@ +import { TelescopeBuilder } from '../builder'; +import { UTILS } from '../utils'; + +export const plugin = (builder: TelescopeBuilder) => { + if ( + builder.options.prototypes.typingsFormat.customTypes.useAgoricDecimal === + true + ) { + UTILS.Decimal = '__decimals__'; + } else { + UTILS.Decimal = '@cosmjs/math'; + } +}; diff --git a/packages/telescope/src/utils/index.ts b/packages/telescope/src/utils/index.ts index 9a207324b..7bca9c65c 100644 --- a/packages/telescope/src/utils/index.ts +++ b/packages/telescope/src/utils/index.ts @@ -21,7 +21,7 @@ export const UTILS = { bytesFromBase64: '__helpers__', BrowserHeaders: 'browser-headers', connectComet: '@cosmjs/tendermint-rpc', - Decimal: '__decimals__', + Decimal: '@cosmjs/math', padDecimal: '__helpers__', createProtobufRpcClient: '@cosmjs/stargate', Pubkey: '@cosmjs/amino', diff --git a/packages/types/src/telescope.ts b/packages/types/src/telescope.ts index 796c68337..abf3f29d7 100644 --- a/packages/types/src/telescope.ts +++ b/packages/types/src/telescope.ts @@ -87,7 +87,7 @@ interface TelescopeOpts { typingsFormat?: { customTypes?: { useCosmosSDKDec?: boolean; - useAgoricDecimal?: 'default' | undefined; + useAgoricDecimal?: boolean; }; num64?: 'long' | 'bigint';