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

fix(pactjs-cli): fixed incorrect network by extracting data from api field #1337

Merged
merged 2 commits into from
Dec 19, 2023
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
5 changes: 5 additions & 0 deletions .changeset/proud-chairs-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@kadena/pactjs-cli': patch
---

Extract networkId and chainId from the api
13 changes: 11 additions & 2 deletions packages/tools/pactjs-cli/src/contract-generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Option } from 'commander';
import { z } from 'zod';
import type { networkMap } from '../utils/networkMap';
import { generate } from './generate';
import { extractNetworkAndChain } from './utils';

export interface IContractGenerateOptions {
clean?: boolean;
Expand Down Expand Up @@ -33,7 +34,7 @@ const Options = z
api: z.string().optional(),
chain: z.number().optional(),
namespace: z.string().optional(),
network: z.enum(['mainnet', 'testnet']),
network: z.string(),
parseTreePath: z.string().optional(),
})
.refine(({ file, contract }) => {
Expand Down Expand Up @@ -101,7 +102,15 @@ export function contractGenerateCommand(
)
.action((args: IContractGenerateOptions) => {
try {
Options.parse(args);
const { api, ...rest } = args;
const { network, chain } = extractNetworkAndChain(api);
const updatedArgs = {
...rest,
api,
network: rest.network ?? network,
chain: rest.chain ?? chain,
};
Options.parse(updatedArgs);
} catch (e) {
program.error(
`${(e as z.ZodError).errors
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, it } from 'vitest';
import { extractNetworkAndChain } from '../utils';

describe('extractNetworkAndChain', () => {
it('should return an empty object when api is undefined', () => {
const result = extractNetworkAndChain(undefined);
expect(result).toEqual({});
});

it('should extract the network and chain from the api string', () => {
const api = '/chainweb/0.0/mainnet01/';
const result = extractNetworkAndChain(api);
expect(result).toEqual({ network: 'mainnet01', chain: undefined });
});

it('should return undefined for network and chain when api string does not match the pattern', () => {
const api = '/chainweb/0.0/';
const result = extractNetworkAndChain(api);
expect(result).toEqual({ network: undefined, chain: undefined });
});

it('should parse the chain number correctly when api string contains a different chain', () => {
const api = 'http://api.com/chainweb/0.0/testnet04/chain/2/pact';
const result = extractNetworkAndChain(api);
expect(result).toEqual({ network: 'testnet04', chain: 2 });
});
});
12 changes: 12 additions & 0 deletions packages/tools/pactjs-cli/src/contract-generate/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function extractNetworkAndChain(api?: string): {
network?: string;
chain?: number;
} {
if (api === undefined) return {};
const network = api.match(/\/chainweb\/\d+\.\d+\/(\w+)\//)?.[1];
const chain = api.match(/\/chain\/(\d+)\//)?.[1];
return {
network,
chain: chain !== undefined ? parseInt(chain, 10) : undefined,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ export async function retrieveContractFromChain(
module: string,
apiHost: string,
chain: number | string,
network: keyof typeof networkMap,
network: string,
): Promise<string> {
const command = Pact.builder
.execution(`(describe-module "${module}")`)
.setNetworkId(networkMap[network].network)
.setNetworkId(
network === 'testnet' || network === 'mainnet'
? networkMap[network].network
: network,
)
.setMeta({ chainId: chain.toString() as ChainId })
.createTransaction();

Expand Down
Loading