Skip to content

Commit

Permalink
feat: 🎸 update supported polymesh private versions
Browse files Browse the repository at this point in the history
update expected spec versions to account for upgraded polymesh private
chains
  • Loading branch information
polymath-eric authored and prashantasdeveloper committed Nov 8, 2024
1 parent 7728c68 commit ddf3d72
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 21 deletions.
5 changes: 4 additions & 1 deletion src/api/client/Polymesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ export class Polymesh {
const { types, rpc, signedExtensions, runtime, runtimeV6 } = schema;

/* istanbul ignore next: this will be removed after dual version support for v6-v7 */
const runtimeApis = specVersion >= 7000000 ? runtime : runtimeV6;
const runtimeApis =
specVersion >= 7000000 || (specVersion < 3000000 && specVersion > 1000000)
? runtime
: runtimeV6;
const connectionProtocol = extractProtocol(nodeUrl)!;

const provider = connectionProtocol.startsWith('http')
Expand Down
2 changes: 0 additions & 2 deletions src/api/client/__tests__/Claims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,6 @@ describe('Claims Class', () => {

let result = await claims.getClaimScopes({ target });

console.log(result);

expect(result[0].ticker).toBeUndefined();
expect(result[0].scope).toEqual({ type: ScopeType.Identity, value: someDid });
expect(result[2].ticker).toEqual(ticker);
Expand Down
4 changes: 2 additions & 2 deletions src/api/procedures/__tests__/addInstruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ describe('addInstruction procedure', () => {
});

it('should handle NFT legs', async () => {
dsMockUtils.configureMocks({ contextOptions: { did: fromDid, specVersion: 7000003 } });
dsMockUtils.configureMocks({ contextOptions: { did: fromDid, isV6: false } });
entityMockUtils.configureMocks({
venueOptions: {
exists: true,
Expand Down Expand Up @@ -1018,7 +1018,7 @@ describe('addInstruction procedure', () => {
null,
null,
[undefined],
[rawFrom, rawTo],
new Set([rawFrom, rawTo]),
null,
rawEmptyMediatorSet,
],
Expand Down
40 changes: 39 additions & 1 deletion src/api/procedures/__tests__/modifyInstructionAffirmation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,45 @@ describe('modifyInstructionAffirmation procedure', () => {
.calledWith(rawAffirmationStatus)
.mockReturnValue(AffirmationStatus.Affirmed);

dsMockUtils.configureMocks({ contextOptions: { specVersion: 7000003 } });
const proc = procedureMockUtils.getInstance<
ModifyInstructionAffirmationParams,
Instruction,
Storage
>(mockContext, {
portfolios: [portfolio, portfolio],
portfolioParams: [],
senderLegAmount: legAmount,
totalLegAmount: legAmount,
signer,
offChainLegIndices: [],
instructionInfo: mockExecuteInfo,
});

const transaction = dsMockUtils.createTxMock('settlement', 'withdrawAffirmationWithCount');

const result = await prepareModifyInstructionAffirmation.call(proc, {
id,
operation: InstructionAffirmationOperation.Withdraw,
});

expect(result).toEqual({
transaction,
feeMultiplier: new BigNumber(2),
args: [rawInstructionId, new Set([rawPortfolioId, rawPortfolioId]), mockAffirmCount],
resolver: expect.objectContaining({ id }),
});
});

it('should return a withdraw instruction transaction spec with portfolios as an array for v6', async () => {
mockContext.isV6 = true;
const rawAffirmationStatus = dsMockUtils.createMockAffirmationStatus('Affirmed');
dsMockUtils.createQueryMock('settlement', 'userAffirmations', {
multi: [rawAffirmationStatus, rawAffirmationStatus],
});
when(meshAffirmationStatusToAffirmationStatusSpy)
.calledWith(rawAffirmationStatus)
.mockReturnValue(AffirmationStatus.Affirmed);

const proc = procedureMockUtils.getInstance<
ModifyInstructionAffirmationParams,
Instruction,
Expand Down
7 changes: 3 additions & 4 deletions src/api/procedures/addInstruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
} from '~/types';
import { BatchTransactionSpec, ProcedureAuthorization } from '~/types/internal';
import { isFungibleLegBuilder, isNftLegBuilder, isOffChainLeg } from '~/utils';
import { BTREE_SET_PORTFOLIO_ID_SPEC_VERSION, MAX_LEGS_LENGTH } from '~/utils/constants';
import { MAX_LEGS_LENGTH } from '~/utils/constants';
import {
assetToMeshAssetIdWithKey,
bigNumberToBalance,
Expand Down Expand Up @@ -481,9 +481,8 @@ async function getTxArgsAndErrors(
rawTradeDate,
rawValueDate,
rawLegs,
context.specVersion >= BTREE_SET_PORTFOLIO_ID_SPEC_VERSION
? portfolioIdsToBtreeSet(rawPortfolioIds, context)
: rawPortfolioIds,
/* istanbul ignore next: this will be removed after dual version support for v6-v7 */
context.isV6 ? rawPortfolioIds : portfolioIdsToBtreeSet(rawPortfolioIds, context),
rawInstructionMemo,
rawMediators,
]);
Expand Down
7 changes: 3 additions & 4 deletions src/api/procedures/modifyInstructionAffirmation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
} from '~/types/internal';
import { tuple } from '~/types/utils';
import { isOffChainLeg } from '~/utils';
import { BTREE_SET_PORTFOLIO_ID_SPEC_VERSION } from '~/utils/constants';
import {
assetCountToRaw,
bigNumberToU64,
Expand Down Expand Up @@ -463,10 +462,10 @@ export async function prepareModifyInstructionAffirmation(
);

let portfolioIds;
if (context.specVersion >= BTREE_SET_PORTFOLIO_ID_SPEC_VERSION) {
portfolioIds = portfolioIdsToBtreeSet(validPortfolioIds, context);
} else {
if (context.isV6) {
portfolioIds = validPortfolioIds;
} else {
portfolioIds = portfolioIdsToBtreeSet(validPortfolioIds, context);
}

if (transaction === settlementTx.affirmWithReceiptsWithCount) {
Expand Down
8 changes: 6 additions & 2 deletions src/base/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import {
delay,
getApiAtBlock,
getLatestSqVersion,
isV6Spec,
} from '~/utils/internal';

import { processType } from './utils';
Expand Down Expand Up @@ -131,6 +132,8 @@ export class Context {

public specVersion: number;

public specName: string;

private readonly unsubChainVersion: UnsubscribePromise;

/**
Expand All @@ -144,13 +147,14 @@ export class Context {
this.ss58Format = ss58Format;

this.specVersion = polymeshApi.runtimeVersion.specVersion.toNumber();
this.isV6 = this.specVersion < 7000000;
this.specName = polymeshApi.runtimeVersion.specName.toString();
this.isV6 = isV6Spec(this.specName, this.specVersion);

this.unsubChainVersion = polymeshApi.query.system.lastRuntimeUpgrade(upgrade => {
/* istanbul ignore next: this will be removed after dual version support for v6-v7 */
if (upgrade.isSome) {
this.specVersion = upgrade.unwrap().specVersion.toNumber();
this.isV6 = this.specVersion < 7000000;
this.isV6 = isV6Spec(this.specName, this.specVersion);
}
});
}
Expand Down
1 change: 1 addition & 0 deletions src/base/__tests__/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe('Context class', () => {

dsMockUtils.setRuntimeVersion({
specVersion: dsMockUtils.createMockU32(new BigNumber(7000004)),
specName: dsMockUtils.createMockText('mock_spec'),
});

dsMockUtils.setConstMock('system', 'ss58Prefix', {
Expand Down
27 changes: 27 additions & 0 deletions src/utils/__tests__/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import {
isAllowedCharacters,
isModuleOrTagMatch,
isPrintableAscii,
isV6Spec,
mergeReceipts,
neededStatTypeForRestrictionInput,
optionize,
Expand Down Expand Up @@ -3056,3 +3057,29 @@ describe('getAssetIdFromMiddleware', () => {
expect(result).toEqual(ticker);
});
});

describe('isV6Spec', () => {
it('should return true for pre v7 spec', () => {
const result = isV6Spec('some_spec', 6000000);

expect(result).toEqual(true);
});

it('should return false for post v7 spec', () => {
const result = isV6Spec('some_spec', 7000001);

expect(result).toEqual(false);
});

it('should return true for pre v2 private spec', () => {
const result = isV6Spec('polymesh_private_dev', 1000000);

expect(result).toEqual(true);
});

it('should return false for post v2 private spec', () => {
const result = isV6Spec('polymesh_private_dev', 2000000);

expect(result).toEqual(false);
});
});
7 changes: 2 additions & 5 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const SUPPORTED_NODE_VERSION_RANGE = '6.2 || 6.3 || 7.0';
/**
* The Polymesh Private RPC node version range that is compatible with this version of the SDK
*/
export const PRIVATE_SUPPORTED_NODE_VERSION_RANGE = '1.0';
export const PRIVATE_SUPPORTED_NODE_VERSION_RANGE = '1.0 || 1.1 || 2.0';

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
export const SUPPORTED_NODE_SEMVER = coerce(SUPPORTED_NODE_VERSION_RANGE)!.version;
Expand All @@ -131,7 +131,7 @@ export const SUPPORTED_SPEC_VERSION_RANGE = '6.3 || 7.0';
/**
* The Polymesh private chain spec version range that is compatible with this version of the SDK
*/
export const PRIVATE_SUPPORTED_SPEC_VERSION_RANGE = '1.0';
export const PRIVATE_SUPPORTED_SPEC_VERSION_RANGE = '1.0 || 1.1 || 2.0';

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
export const SUPPORTED_SPEC_SEMVER = coerce(SUPPORTED_SPEC_VERSION_RANGE)!.version;
Expand Down Expand Up @@ -195,6 +195,3 @@ export const GLOBAL_TOKEN_URI_NAME = 'tokenUri';
export const GLOBAL_BASE_TOKEN_URI_NAME = 'baseTokenUri';

export const ASSET_ID_PREFIX = 'modlpy/pallet_asset';

// TODO @prashantasdeveloper remove this when removing dual version support
export const BTREE_SET_PORTFOLIO_ID_SPEC_VERSION = 7000004;
13 changes: 13 additions & 0 deletions src/utils/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,7 @@ export async function getExemptedIds(
const getAllowedMajors = (range: string, supportedSpecSemver: string): string[] => {
const lowMajor = major(supportedSpecSemver).toString();
const versions = range.split('||');
/* istanbul ignore next: should get covered after dual version support is removed */
if (versions.length === 1) {
return [lowMajor];
}
Expand Down Expand Up @@ -2365,3 +2366,15 @@ export function getAssetIdFromMiddleware(

return hexToUuid(id);
}

/**
* @hidden
*
* @returns true if spec version indicates public v7 or private v2
*/
export function isV6Spec(specName: string, specVersion: number): boolean {
return (
(specVersion > 3000000 && specVersion < 7000000) ||
(specName === 'polymesh_private_dev' && specVersion < 2000000)
);
}

0 comments on commit ddf3d72

Please sign in to comment.