Skip to content

Commit

Permalink
feat: add conditional logic to change UTILS.decimals value
Browse files Browse the repository at this point in the history
  • Loading branch information
j-yw committed Sep 14, 2024
1 parent 6ad6734 commit cf5279b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 29 deletions.
21 changes: 20 additions & 1 deletion packages/telescope/__tests__/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const options: TelescopeOptions = {
typingsFormat: {
customTypes: {
useCosmosSDKDec: true,
useAgoricDecimal: undefined,
},
num64: 'bigint',
useDeepPartial: true,
Expand Down Expand Up @@ -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();
});
});
53 changes: 33 additions & 20 deletions packages/telescope/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand All @@ -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;
};
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions packages/telescope/src/generators/create-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
13 changes: 13 additions & 0 deletions packages/telescope/src/generators/customize-utils.ts
Original file line number Diff line number Diff line change
@@ -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';
}
};
2 changes: 1 addition & 1 deletion packages/telescope/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
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?: 'default' | undefined;
useAgoricDecimal?: boolean;
};

num64?: 'long' | 'bigint';
Expand Down

0 comments on commit cf5279b

Please sign in to comment.