Skip to content

Commit

Permalink
feat: 🎸 update chain types to latest
Browse files Browse the repository at this point in the history
update chain types to 6.3.x. Updates typgen code to use runtime types
and update snake case params to camel case
  • Loading branch information
polymath-eric committed Sep 17, 2024
1 parent dbd52d2 commit edc5a23
Show file tree
Hide file tree
Showing 31 changed files with 1,743 additions and 685 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@polkadot/dev-ts": "^0.79.3",
"@polkadot/typegen": "11.2.1",
"@polymeshassociation/local-signing-manager": "^3.0.1",
"@polymeshassociation/polymesh-types": "^5.11.1",
"@polymeshassociation/polymesh-types": "^5.12.1",
"@polymeshassociation/signing-manager-types": "^3.0.0",
"@polymeshassociation/typedoc-theme": "^1.2.0",
"@semantic-release/changelog": "^6.0.3",
Expand Down
56 changes: 24 additions & 32 deletions scripts/fetchDefinitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ const util = require('util');
const { forEach, camelCase, mapKeys } = require('lodash');

const { typesBundle } = require('@polymeshassociation/polymesh-types');
const types = require('@polymeshassociation/polymesh-types/types/6.1.x.json');
const types = require('@polymeshassociation/polymesh-types/types/6.3.x.json');
const versionedRpc = require('@polymeshassociation/polymesh-types/rpc/6.x').default;

const definitionsDir = path.resolve('src', 'polkadot');
const typesDir = path.resolve(definitionsDir, 'polymesh');
const generatedDir = path.resolve('src', 'generated');

rimraf.sync(definitionsDir);
fs.mkdirSync(definitionsDir);

rimraf.sync(typesDir);
fs.mkdirSync(typesDir);

Expand All @@ -21,39 +25,27 @@ fs.mkdirSync(generatedDir);
/**
* @hidden
* transforms the schema so RPC types are compatible with other methods from the polkadot api.
* @note imports are added into the generated files in the postProcessTypes script
*/
function transformSchema(schemaObj) {
let {
rpc: { identity, asset, settlement },
runtime,
} = schemaObj;

camelCaseParamNames(identity.getFilteredAuthorizations);
identity.getFilteredAuthorizations.type = 'Vec<PolymeshPrimitivesAuthorization>';
runtime.IdentityApi[0].methods.get_filtered_authorizations.params[0].type =
'PolymeshPrimitivesSecondaryKeySignatory';
runtime.IdentityApi[0].methods.get_filtered_authorizations.type =
'Vec<PolymeshPrimitivesAuthorization>';

camelCaseKeys(schemaObj, 'types', 'ComplianceRequirementResult');

camelCaseKeys(schemaObj, 'types', 'Condition');
// Convert type keys to camelCase (excluding "_enum")
schemaObj.types.Condition.issuers = 'Vec<PolymeshPrimitivesConditionTrustedIssuer>';
Object.entries(types).forEach(([key, value]) => {
if (typeof value === 'object' && !value._enum) {
camelCaseKeys(schemaObj, 'types', key);
}
camelCaseParamNames(asset.canTransferGranular);
});

camelCaseKeys(schemaObj, 'types', 'TrustedIssuer');

camelCaseParamNames(asset.canTransferGranular);
asset.canTransferGranular.params[0].type = 'Option<PolymeshPrimitivesIdentityId>';
asset.canTransferGranular.params[2].type = 'Option<PolymeshPrimitivesIdentityId>';

runtime.AssetApi[0].methods.can_transfer_granular.params[0].type =
'Option<PolymeshPrimitivesIdentityId>';
runtime.AssetApi[0].methods.can_transfer_granular.params[2].type =
'Option<PolymeshPrimitivesIdentityId>';

camelCaseParamNames(settlement.getExecuteInstructionInfo);
camelCaseKeys(schemaObj, 'types', 'ExecuteInstructionInfo');
Object.values(runtime).forEach(apiMethodsArray =>
apiMethodsArray.forEach(apiMethodsObj =>
Object.values(apiMethodsObj.methods).forEach(camelCaseParamNames)
)
);
}

function camelCaseKeys(schemaObj, section, field) {
Expand All @@ -69,18 +61,18 @@ function camelCaseParamNames(field) {
}

function writeDefinitions(schemaObj) {
const { types, rpc: rpcModules } = schemaObj;
const { types, rpc: rpcModules, runtime } = schemaObj;

fs.writeFileSync(
path.resolve(typesDir, 'definitions.ts'),
`/* eslint-disable @typescript-eslint/naming-convention */\nexport default ${util.inspect(
{ rpc: {}, types },
{ rpc: {}, runtime, types },
{
compact: false,
depth: null,
maxArrayLength: null,
}
)}`
)};`
);

fs.writeFileSync(
Expand All @@ -96,7 +88,7 @@ function writeDefinitions(schemaObj) {
);

let defExports =
"/* istanbul ignore file */\n\nexport { default as polymesh } from './polymesh/definitions';\n";
"/* istanbul ignore file */\n\nexport { default as polymesh } from './polymesh/definitions';\n";

forEach(rpcModules, (rpc, moduleName) => {
const moduleDir = path.resolve(definitionsDir, moduleName);
Expand All @@ -113,7 +105,7 @@ function writeDefinitions(schemaObj) {
depth: null,
maxArrayLength: null,
}
)}`
)};`
);

defExports = `${defExports}export { default as ${moduleName} } from './${moduleName}/definitions';\n`;
Expand All @@ -123,10 +115,10 @@ function writeDefinitions(schemaObj) {
}

(() => {
const { rpc, runtime, signedExtensions } = typesBundle.spec.polymesh_dev;
const { runtime, signedExtensions } = typesBundle.spec.polymesh_dev;
const schema = {
types,
rpc,
rpc: versionedRpc,
runtime,
signedExtensions,
};
Expand Down
8 changes: 1 addition & 7 deletions src/api/client/Claims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,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
3 changes: 2 additions & 1 deletion src/api/client/Polymesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
NormalizedCacheObject,
} from '@apollo/client/core';
import { ApiPromise, HttpProvider, WsProvider } from '@polkadot/api';
import { DefinitionsCall } from '@polkadot/types/types';
import { SigningManager } from '@polymeshassociation/signing-manager-types';
import fetch from 'cross-fetch';
import schema from 'polymesh-types/schema';
Expand Down Expand Up @@ -166,7 +167,7 @@ export class Polymesh {
provider,
types,
rpc,
runtime,
runtime: runtime as unknown as DefinitionsCall,
signedExtensions,
metadata,
noInitWarn,
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 @@ -92,7 +92,6 @@ describe('Claims Class', () => {
getIdentityClaimsFromMiddleware,
},
});

let result = await claims.getIssuedClaims();
expect(result).toEqual(getIdentityClaimsFromMiddleware);

Expand Down Expand Up @@ -448,11 +447,10 @@ 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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ describe('Distributions class', () => {
treatment: 'Exclude',
},
/* eslint-disable @typescript-eslint/naming-convention */
decl_date: new BigNumber(0),
record_date: null,
default_withholding_tax: new BigNumber(3),
withholding_tax: [],
declDate: new BigNumber(0),
recordDate: null,
defaultWithholdingTax: new BigNumber(3),
withholdingTax: [],
/* eslint-enable @typescript-eslint/naming-convention */
})
),
Expand Down Expand Up @@ -157,10 +157,10 @@ describe('Distributions class', () => {
treatment: 'Exclude',
},
/* eslint-disable @typescript-eslint/naming-convention */
decl_date: new BigNumber(0),
record_date: null,
default_withholding_tax: new BigNumber(3),
withholding_tax: [],
declDate: new BigNumber(0),
recordDate: null,
defaultWithholdingTax: new BigNumber(3),
withholdingTax: [],
/* eslint-enable @typescript-eslint/naming-convention */
})
),
Expand Down
24 changes: 12 additions & 12 deletions src/api/entities/CorporateActionBase/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ describe('CorporateAction class', () => {
dsMockUtils.createMockCorporateAction({
kind,
/* eslint-disable @typescript-eslint/naming-convention */
decl_date: new BigNumber(declarationDate.getTime()),
record_date: dsMockUtils.createMockRecordDate({
declDate: new BigNumber(declarationDate.getTime()),
recordDate: dsMockUtils.createMockRecordDate({
date: new BigNumber(new Date('10/14/2019').getTime()),
checkpoint: {
Scheduled: [
Expand All @@ -98,8 +98,8 @@ describe('CorporateAction class', () => {
identities: [],
treatment: TargetTreatment.Exclude,
},
default_withholding_tax: new BigNumber(100000),
withholding_tax: [],
defaultWithholdingTax: new BigNumber(100000),
withholdingTax: [],
/* eslint-enable @typescript-eslint/naming-convention */
})
),
Expand Down Expand Up @@ -239,14 +239,14 @@ describe('CorporateAction class', () => {
dsMockUtils.createMockCorporateAction({
kind,
/* eslint-disable @typescript-eslint/naming-convention */
decl_date: new BigNumber(declarationDate.getTime()),
record_date: dsMockUtils.createMockOption(),
declDate: new BigNumber(declarationDate.getTime()),
recordDate: dsMockUtils.createMockOption(),
targets: {
identities: [],
treatment: TargetTreatment.Exclude,
},
default_withholding_tax: new BigNumber(100000),
withholding_tax: [],
defaultWithholdingTax: new BigNumber(100000),
withholdingTax: [],
/* eslint-enable @typescript-eslint/naming-convention */
})
)
Expand All @@ -271,8 +271,8 @@ describe('CorporateAction class', () => {
dsMockUtils.createMockCorporateAction({
kind,
/* eslint-disable @typescript-eslint/naming-convention */
decl_date: new BigNumber(declarationDate.getTime()),
record_date: dsMockUtils.createMockOption(
declDate: new BigNumber(declarationDate.getTime()),
recordDate: dsMockUtils.createMockOption(
dsMockUtils.createMockRecordDate({
date: createMockU64(new BigNumber(new Date('10/14/1987').getTime())),
checkpoint: { Existing: dsMockUtils.createMockU64(new BigNumber(1)) },
Expand All @@ -282,8 +282,8 @@ describe('CorporateAction class', () => {
identities: [],
treatment: TargetTreatment.Exclude,
},
default_withholding_tax: new BigNumber(100000),
withholding_tax: [],
defaultWithholdingTax: new BigNumber(100000),
withholdingTax: [],
/* eslint-enable @typescript-eslint/naming-convention */
})
)
Expand Down
8 changes: 4 additions & 4 deletions src/api/procedures/__tests__/configureDividendDistribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,8 @@ describe('configureDividendDistribution procedure', () => {
/* eslint-disable @typescript-eslint/naming-convention */
rawCorporateAction = dsMockUtils.createMockCorporateAction({
kind: 'UnpredictableBenefit',
decl_date: new BigNumber(declarationDate.getTime()),
record_date: dsMockUtils.createMockRecordDate({
declDate: new BigNumber(declarationDate.getTime()),
recordDate: dsMockUtils.createMockRecordDate({
date: new BigNumber(new Date('10/14/2021').getTime()),
checkpoint: {
Scheduled: [
Expand All @@ -628,8 +628,8 @@ describe('configureDividendDistribution procedure', () => {
},
}),
targets,
default_withholding_tax: defaultTaxWithholding.shiftedBy(4),
withholding_tax: taxWithholdings.map(({ identity, percentage }) =>
defaultWithholdingTax: defaultTaxWithholding.shiftedBy(4),
withholdingTax: taxWithholdings.map(({ identity, percentage }) =>
tuple(identity, percentage.shiftedBy(4))
),
});
Expand Down
24 changes: 12 additions & 12 deletions src/base/__tests__/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1828,46 +1828,46 @@ describe('Context class', () => {
dsMockUtils.createMockOption(
dsMockUtils.createMockCorporateAction({
kind: CorporateActionKind.PredictableBenefit,
decl_date: new BigNumber(new Date('10/14/1987').getTime()),
record_date: dsMockUtils.createMockRecordDate({
declDate: new BigNumber(new Date('10/14/1987').getTime()),
recordDate: dsMockUtils.createMockRecordDate({
date: new BigNumber(new Date('10/14/2019').getTime()),
checkpoint: { Existing: dsMockUtils.createMockU64(new BigNumber(2)) },
}),
targets: {
identities: ['someDid'],
treatment: TargetTreatment.Exclude,
},
default_withholding_tax: new BigNumber(100000),
withholding_tax: [tuple('someDid', new BigNumber(300000))],
defaultWithholdingTax: new BigNumber(100000),
withholdingTax: [tuple('someDid', new BigNumber(300000))],
})
),
dsMockUtils.createMockOption(
dsMockUtils.createMockCorporateAction({
kind: CorporateActionKind.Reorganization,
decl_date: new BigNumber(new Date('10/14/1987').getTime()),
record_date: null,
declDate: new BigNumber(new Date('10/14/1987').getTime()),
recordDate: null,
targets: {
identities: [],
treatment: TargetTreatment.Exclude,
},
default_withholding_tax: new BigNumber(0),
withholding_tax: [],
defaultWithholdingTax: new BigNumber(0),
withholdingTax: [],
})
),
dsMockUtils.createMockOption(
dsMockUtils.createMockCorporateAction({
kind: CorporateActionKind.UnpredictableBenefit,
decl_date: new BigNumber(new Date('11/26/1989').getTime()),
record_date: dsMockUtils.createMockRecordDate({
declDate: new BigNumber(new Date('11/26/1989').getTime()),
recordDate: dsMockUtils.createMockRecordDate({
date: new BigNumber(new Date('11/26/2019').getTime()),
checkpoint: { Existing: dsMockUtils.createMockU64(new BigNumber(5)) },
}),
targets: {
identities: [],
treatment: TargetTreatment.Exclude,
},
default_withholding_tax: new BigNumber(150000),
withholding_tax: [tuple('someDid', new BigNumber(200000))],
defaultWithholdingTax: new BigNumber(150000),
withholdingTax: [tuple('someDid', new BigNumber(200000))],
})
),
];
Expand Down
4 changes: 2 additions & 2 deletions src/polkadot/asset/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default {
params: [
{
name: 'fromCustodian',
type: 'Option<PolymeshPrimitivesIdentityId>',
type: 'Option<IdentityId>',
isOptional: false,
},
{
Expand All @@ -17,7 +17,7 @@ export default {
},
{
name: 'toCustodian',
type: 'Option<PolymeshPrimitivesIdentityId>',
type: 'Option<IdentityId>',
isOptional: false,
},
{
Expand Down
Loading

0 comments on commit edc5a23

Please sign in to comment.