Skip to content

Commit

Permalink
Merge pull request #1337 from kadena-community/fix/incorrect-networkid
Browse files Browse the repository at this point in the history
fix(pactjs-cli): fixed incorrect network by extracting data from api field
  • Loading branch information
javadkh2 authored Dec 19, 2023
2 parents 6a711ce + d57ade4 commit da87410
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
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

0 comments on commit da87410

Please sign in to comment.