Skip to content

Commit

Permalink
Merge pull request #249 from PolymathNetwork/chore/preserve-historic-…
Browse files Browse the repository at this point in the history
…transaction-tags

chore(types): preserve historic transaction tags on chain upgrade
  • Loading branch information
VictorVicente committed Oct 27, 2020
2 parents 62217e6 + 42c7ef9 commit 70f20ad
Show file tree
Hide file tree
Showing 13 changed files with 1,448 additions and 1,661 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@commitlint/config-conventional": "^7.6.0",
"@graphql-codegen/cli": "1.17.7",
"@graphql-codegen/typescript": "1.17.7",
"@polkadot/typegen": "1.29.1",
"@polkadot/typegen": "2.3.1",
"@semantic-release/changelog": "^3.0.4",
"@types/bluebird": "^3.5.30",
"@types/jest": "^23.3.10",
Expand Down Expand Up @@ -84,9 +84,9 @@
"access": "public"
},
"dependencies": {
"@polkadot/api": "1.29.1",
"@polkadot/util": "3.2.1",
"@polkadot/util-crypto": "3.2.1",
"@polkadot/api": "2.3.1",
"@polkadot/util": "3.6.1",
"@polkadot/util-crypto": "3.6.1",
"@types/bignumber.js": "^5.0.0",
"apollo-cache-inmemory": "^1.6.6",
"apollo-client": "^2.6.10",
Expand All @@ -97,6 +97,7 @@
"apollo-link-state": "^0.4.2",
"bignumber.js": "^9.0.0",
"bluebird": "^3.7.2",
"cross-fetch": "^3.0.6",
"graphql": "15.0.0",
"graphql-tag": "^2.10.3",
"iso-7064": "^1.0.2",
Expand Down
2 changes: 1 addition & 1 deletion scripts/fetchDefinitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function meshCountryCodeToCountryCode(meshCountryCode: MeshCountryCode):
const pascalCaseCode = upperFirst(toLower(code));

countryCodeEnum = `${countryCodeEnum}\n ${pascalCaseCode} = '${pascalCaseCode}',${
isLast ? '\n}\n' : ''
isLast ? '\n}' : ''
}`;

const returnStatement = `return CountryCode.${pascalCaseCode}`;
Expand Down
51 changes: 37 additions & 14 deletions scripts/generateTxTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ const Metadata = require('@polkadot/metadata/Metadata').default;
const { w3cwebsocket } = require('websocket');
const { TypeRegistry } = require('@polkadot/types/create');
const { stringCamelCase, stringUpperFirst } = require('@polkadot/util');
const { forEach } = require('lodash');
const fs = require('fs');
const path = require('path');
const rimraf = require('rimraf');

const websocket = new w3cwebsocket('wss://pme.polymath.network');
websocket.onopen = () => {
Expand All @@ -15,33 +17,51 @@ websocket.onmessage = message => {
let txTag = 'export type TxTag =';
let txTags = 'export const TxTags = {\n';

const jsonPath = path.resolve('scripts', 'transactions.json');

const transactionData = JSON.parse(fs.readFileSync(jsonPath));

const registry = new TypeRegistry();

const metadata = new Metadata(registry, JSON.parse(message.data).result);
const modules = metadata.asLatest.modules;

modules.forEach(({ name, calls }, index) => {
// add new calls to historic ones
modules.forEach(({ name, calls }) => {
const allCalls = calls.unwrapOr(null);
const moduleName = name.toString();

if (allCalls && allCalls.length) {
const moduleNameCamelCase = stringCamelCase(moduleName);
const moduleNamePascal = stringUpperFirst(moduleNameCamelCase);
txTag = txTag.concat(`\n | ${moduleNamePascal}Tx`);
txTags = txTags.concat(` ${stringCamelCase(moduleName)}: ${moduleNamePascal}Tx,\n`);

namespaces = namespaces.concat(`export enum ${moduleNamePascal}Tx {\n`);
allCalls.forEach(({ name: callName }) => {
const nameCamelCase = stringCamelCase(callName.toString());
const namePascal = stringUpperFirst(nameCamelCase);
namespaces = namespaces.concat(
` ${namePascal} = '${moduleNameCamelCase}.${nameCamelCase}',\n`
);
const moduleCalls = (transactionData[moduleName] = transactionData[moduleName] || {});

allCalls.forEach(({ name: cName }) => {
const callName = cName.toString();
moduleCalls[callName] = callName;
});
namespaces = namespaces.concat('}\n\n');
}
});

forEach(transactionData, (calls, moduleName) => {
const moduleNameCamelCase = stringCamelCase(moduleName);
const moduleNamePascal = stringUpperFirst(moduleNameCamelCase);

txTag = txTag.concat(`\n | ${moduleNamePascal}Tx`);
txTags = txTags.concat(` ${stringCamelCase(moduleName)}: ${moduleNamePascal}Tx,\n`);

namespaces = namespaces.concat(`export enum ${moduleNamePascal}Tx {\n`);

forEach(calls, callName => {
const nameCamelCase = stringCamelCase(callName);
const namePascal = stringUpperFirst(nameCamelCase);

namespaces = namespaces.concat(
` ${namePascal} = '${moduleNameCamelCase}.${nameCamelCase}',\n`
);
});

namespaces = namespaces.concat('}\n\n');
});

txTag = txTag.concat(';');
txTags = txTags.concat('};');

Expand All @@ -50,5 +70,8 @@ websocket.onmessage = message => {
'\n'.concat(namespaces).concat(`${txTag}\n\n${txTags}\n`)
);

rimraf.sync(jsonPath);
fs.writeFileSync(jsonPath, `${JSON.stringify(transactionData, null, 2)}\n`);

websocket.close();
};

0 comments on commit 70f20ad

Please sign in to comment.