Skip to content

refactor: 💡 use polkadot types from polymesh-types #1484

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

Merged
merged 12 commits into from
Jun 30, 2025
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
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ module.exports = {
modulePathIgnorePatterns: ['dist'],
moduleNameMapper: {
'~/(.*)': '<rootDir>/src/$1',
'polymesh-types/(.*)': '<rootDir>/src/polkadot/$1',
},
};
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"update-readme": "cross-env node scripts/updateReadme.js",
"generate:defs": "node --loader @polkadot/dev-ts node_modules/.bin/polkadot-types-from-defs --endpoint metadata.json --package polymesh-types --input ./src/polkadot/",
"generate:meta": "node --loader @polkadot/dev-ts node_modules/.bin/polkadot-types-from-chain --endpoint metadata.json --package polymesh-types --output ./src/polkadot --strict",
"generate:consts": "cross-env node scripts/generateConsts.js",
"generate:consts": "ts-node scripts/generateConsts.ts",
"generate:post-process": "cross-env node scripts/postProcessTypes.js",
"test": "POLKADOTJS_DISABLE_ESM_CJS_WARNING=1 jest --coverage",
"test:integration": "./scripts/integration-test.sh",
Expand All @@ -45,7 +45,8 @@
"lint": "eslint src --ext .js,.jsx,.ts,.tsx",
"format": "cross-env prettier-eslint $PWD\"/src/**/*.{ts,tsx,js,jsx}\" --write",
"ts-node": "yarn build:ts && ts-node --",
"prepare": "is-ci || husky install"
"prepare": "is-ci || husky install",
"postinstall": "yarn generate:consts"
},
"devDependencies": {
"@babel/core": "^7.22.11",
Expand All @@ -58,7 +59,7 @@
"@polkadot/dev-ts": "^0.79.3",
"@polkadot/typegen": "11.2.1",
"@polymeshassociation/local-signing-manager": "^3.0.1",
"@polymeshassociation/polymesh-types": "^5.15.0",
"@polymeshassociation/polymesh-types": "^6.0.0",
"@polymeshassociation/signing-manager-types": "^3.0.0",
"@polymeshassociation/typedoc-theme": "^1.2.0",
"@semantic-release/changelog": "^6.0.3",
Expand Down
169 changes: 0 additions & 169 deletions scripts/generateConsts.js

This file was deleted.

62 changes: 62 additions & 0 deletions scripts/generateConsts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* This script generates the following :
* - the `TxTags`, `TxTag` and `ModuleName` consts
* - the `CountryCode` enum and the related conversion utility functions
* (`countryCodeToMeshCountryCode` and `meshCountryCodeToCountryCode`) from the chain metadata.
*
* NOTE: it is assumed that the metadata has been previously fetched by the `fetchMetadata.js` script, and stored
* in `metadata.json`
*/

/* eslint-disable */
import { CountryCode } from '@polymeshassociation/polymesh-types/generated/types';
import fs from 'fs';
import path from 'path';

function assembleCountryCodes() {
const countryCodes = Object.values(CountryCode);

let countryCodeFunctions = `/**
* @hidden
*/
export function countryCodeToMeshCountryCode(countryCode: CountryCode, context: Context): PolymeshPrimitivesJurisdictionCountryCode {
return context.createType('PolymeshPrimitivesJurisdictionCountryCode', countryCode);
}

/**
* @hidden
*/
export function meshCountryCodeToCountryCode(meshCountryCode: PolymeshPrimitivesJurisdictionCountryCode): CountryCode {`;

countryCodes.forEach((code, index) => {
const isLast = index === countryCodes.length - 1;

const returnStatement = `return CountryCode.${code}`;
if (isLast) {
countryCodeFunctions = `${countryCodeFunctions}\n ${returnStatement};\n}`;
} else {
countryCodeFunctions = `${countryCodeFunctions}\n if (meshCountryCode.is${code}) {\n ${returnStatement};\n }\n`;
}
});

return {
countryCodeFunctions,
};
}

const { countryCodeFunctions } = assembleCountryCodes();

const istanbulIgnore = '/* istanbul ignore file */';

const utilsFile = `${istanbulIgnore}

import { PolymeshPrimitivesJurisdictionCountryCode } from '@polkadot/types/lookup';
import { CountryCode } from '@polymeshassociation/polymesh-types/generated/types';

import { Context } from '~/internal';

${countryCodeFunctions}`;

const generatedDir = path.resolve('src', 'generated');

fs.writeFileSync(path.resolve(generatedDir, 'utils.ts'), utilsFile);
10 changes: 2 additions & 8 deletions src/api/client/Claims.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Vec } from '@polkadot/types';
import { IdentityClaim } from '@polymeshassociation/polymesh-types/polkadot/polymesh';
import BigNumber from 'bignumber.js';
import { filter, flatten, isEqual, uniqBy, uniqWith } from 'lodash';

Expand All @@ -15,7 +16,6 @@ import {
customClaimTypeQuery,
} from '~/middleware/queries/claims';
import { ClaimsOrderBy, ClaimTypeEnum, Query } from '~/middleware/types';
import { IdentityClaim } from '~/polkadot/polymesh';
import {
CddClaim,
ClaimData,
Expand Down Expand Up @@ -431,13 +431,7 @@ export class Claims {
const data: ClaimData<CddClaim>[] = [];

result.forEach(optClaim => {
const {
claim_issuer: claimIssuer,
issuance_date: issuanceDate,
last_update_date: lastUpdateDate,
expiry: rawExpiry,
claim,
} = optClaim;
const { claimIssuer, issuanceDate, lastUpdateDate, expiry: rawExpiry, claim } = optClaim;

const expiry = !rawExpiry.isEmpty ? momentToDate(rawExpiry.unwrap()) : null;

Expand Down
2 changes: 1 addition & 1 deletion src/api/client/Polymesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
} from '@apollo/client/core';
import { ApiPromise, HttpProvider, WsProvider } from '@polkadot/api';
import { DefinitionsCall } from '@polkadot/types/types';
import schema from '@polymeshassociation/polymesh-types/polkadot/schema';
import { SigningManager } from '@polymeshassociation/signing-manager-types';
import fetch from 'cross-fetch';
import schema from 'polymesh-types/schema';

import { Staking } from '~/api/client/Staking';
import { Account, Context, createTransactionBatch, Identity, PolymeshError } from '~/internal';
Expand Down
8 changes: 3 additions & 5 deletions src/api/client/__tests__/Claims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,17 +452,15 @@ describe('Claims Class', () => {
id: 'someCddId',
};

/* eslint-disable @typescript-eslint/naming-convention */
const rawIdentityClaim = {
claim_issuer: dsMockUtils.createMockIdentityId(claimIssuer),
issuance_date: dsMockUtils.createMockMoment(new BigNumber(issuanceDate.getTime())),
last_update_date: dsMockUtils.createMockMoment(new BigNumber(lastUpdateDate.getTime())),
claimIssuer: dsMockUtils.createMockIdentityId(claimIssuer),
issuanceDate: dsMockUtils.createMockMoment(new BigNumber(issuanceDate.getTime())),
lastUpdateDate: dsMockUtils.createMockMoment(new BigNumber(lastUpdateDate.getTime())),
expiry: dsMockUtils.createMockOption(),
claim: dsMockUtils.createMockClaim({
CustomerDueDiligence: dsMockUtils.createMockCddId(claim.id),
}),
};
/* eslint-enable @typescript-eslint/naming-convention */

jest.spyOn(utilsConversionModule, 'identityIdToString').mockReturnValue(claimIssuer);
dsMockUtils.createCallMock('identityApi', 'validCddClaims', {
Expand Down
5 changes: 4 additions & 1 deletion src/api/entities/Asset/Base/Settlements/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { DispatchError } from '@polkadot/types/interfaces/system';
import { Result, Vec } from '@polkadot/types-codec';
import {
ComplianceReport,
TransferCondition,
} from '@polymeshassociation/polymesh-types/polkadot/polymesh';
import BigNumber from 'bignumber.js';

import { assertPortfolioExists } from '~/api/procedures/utils';
Expand All @@ -11,7 +15,6 @@ import {
Nft,
toggleAssetPreApproval,
} from '~/internal';
import { ComplianceReport, TransferCondition } from '~/polkadot/polymesh';
import { NftCollection, NoArgsProcedureMethod, PortfolioLike, TransferBreakdown } from '~/types';
import { isFungibleAsset } from '~/utils';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PolymeshMoment } from '@polymeshassociation/polymesh-types/polkadot/polymesh';
import BigNumber from 'bignumber.js';
import { when } from 'jest-when';

Expand All @@ -9,7 +10,6 @@ import {
Namespace,
PolymeshTransaction,
} from '~/internal';
import { Moment } from '~/polkadot';
import { dsMockUtils, entityMockUtils, procedureMockUtils } from '~/testUtils/mocks';
import { tuple } from '~/types/utils';
import * as utilsConversionModule from '~/utils/conversion';
Expand Down Expand Up @@ -171,7 +171,7 @@ describe('Schedules class', () => {
[dsMockUtils.createMockAssetId(rawAssetId), rawId],
dsMockUtils.createMockOption(
dsMockUtils.createMockCheckpointSchedule({
pending: dsMockUtils.createMockBTreeSet<Moment>([
pending: dsMockUtils.createMockBtreeSet<PolymeshMoment>([
dsMockUtils.createMockMoment(new BigNumber(start.getTime())),
dsMockUtils.createMockMoment(new BigNumber(nextCheckpointDate.getTime())),
]),
Expand Down
Loading
Loading