Skip to content

Commit

Permalink
Merge pull request #35 from PolymathNetwork/feat/MSDK-79-use-original…
Browse files Browse the repository at this point in the history
…-polkadot

Use native polkadot typegen
  • Loading branch information
monitz87 authored Mar 13, 2020
2 parents 8c9965f + 4cbd362 commit a23790e
Show file tree
Hide file tree
Showing 48 changed files with 10,211 additions and 1,626 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
"sourceType": "module",
"project": "./tsconfig.json"
},
"extends": ["standard", "semistandard", "plugin:@typescript-eslint/recommended", "prettier"]
"extends": ["standard", "semistandard", "plugin:@typescript-eslint/recommended", "prettier"],
"ignorePatterns": ["src/polkadot/"]
}
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ module.exports = {
testPathIgnorePatterns: ['dist'],
moduleNameMapper: {
'~/(.*)': '<rootDir>/src/$1',
'polymesh-types/(.*)': '<rootDir>/src/polkadot/$1',
},
};
16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"license": "ISC",
"scripts": {
"start": "cross-env webpack-dev-server --config=webpack.config.dev.js",
"generate:polkadot-types": "yarn fetch-schema && yarn generate:defs && yarn generate:meta && yarn generate:tags",
"fetch-schema": "node scripts/fetchSchema.js",
"generate:defs": "ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --package polymesh-types --input ./src/polkadot",
"generate:meta": "ts-node --skip-project node_modules/.bin/polkadot-types-from-chain --package polymesh-types --endpoint wss://pme.polymath.network --output ./src/polkadot --strict",
"generate:tags": "node scripts/generateTxTags.js",
"test": "jest --coverage",
"build:ts": "ttsc -b",
"build:docs": "typedoc src",
Expand All @@ -20,11 +25,13 @@
"devDependencies": {
"@commitlint/cli": "^7.6.1",
"@commitlint/config-conventional": "^7.6.0",
"@polkadot/typegen": "1.6.0-beta.10",
"@types/jest": "^23.3.10",
"@types/json-stable-stringify": "^1.0.32",
"@types/lodash": "^4.14.149",
"@types/node": "^13.1.8",
"@types/sinon": "^7.5.1",
"@types/websocket": "^1.0.0",
"@typescript-eslint/eslint-plugin": "^2.17.0",
"@typescript-eslint/parser": "^2.17.0",
"@zerollup/ts-transform-paths": "^1.7.11",
Expand All @@ -51,16 +58,16 @@
"semantic-release": "^16.0.3",
"sinon": "^8.1.1",
"ts-jest": "^25.2.1",
"ts-mock-imports": "^1.2.6",
"tsconfig-paths": "^3.9.0",
"tsconfig-paths-webpack-plugin": "^3.2.0",
"ttypescript": "^1.5.10",
"typedoc": "^0.16.8",
"typescript": "3.7.5",
"typescript": "3.8.2",
"webpack": "^4.41.6",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3",
"webpack-merge": "^4.2.2"
"webpack-merge": "^4.2.2",
"websocket": "^1.0.31"
},
"config": {
"commitizen": {
Expand All @@ -71,7 +78,8 @@
"access": "public"
},
"dependencies": {
"@polymathnetwork/polkadot": "0.101.0-beta.18",
"@polkadot/api": "1.6.0-beta.10",
"@polkadot/util": "2.6.2",
"@types/bignumber.js": "^5.0.0",
"bignumber.js": "^9.0.0",
"json-stable-stringify": "^1.0.1",
Expand Down
29 changes: 29 additions & 0 deletions scripts/fetchSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable */
const https = require('https');
const path = require('path');
const fs = require('fs');
const rimraf = require('rimraf');
const util = require('util');

const dirName = path.resolve('src', 'polkadot', 'polymesh');
rimraf.sync(dirName);
fs.mkdirSync(dirName);

https.get('https://pme.polymath.network/code/polymesh_schema.json', res => {
const chunks = [];
res.on('data', chunk => {
chunks.push(chunk);
});

res.on('end', () => {
const schema = Buffer.concat(chunks).toString();
const content = JSON.parse(`{ "types": ${schema} }`);
fs.writeFileSync(
path.resolve(dirName, 'definitions.ts'),
`/* eslint-disable @typescript-eslint/camelcase */\nexport default ${util.inspect(content, {
compact: false,
depth: null,
})}`
);
});
});
54 changes: 54 additions & 0 deletions scripts/generateTxTags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* eslint-disable */
const Metadata = require('@polkadot/metadata/Metadata').default;
const { w3cwebsocket } = require('websocket');
const { TypeRegistry } = require('@polkadot/types/create');
const { stringCamelCase, stringLowerFirst, stringUpperFirst } = require('@polkadot/util');
const fs = require('fs');
const path = require('path');

const websocket = new w3cwebsocket('wss://pme.polymath.network');
websocket.onopen = () => {
websocket.send('{"id":"1","jsonrpc":"2.0","method":"state_getMetadata","params":[]}');
};
websocket.onmessage = message => {
let namespaces = '';
let txTag = 'export type TxTag =';
let txTags = 'export const TxTags = {\n';

const registry = new TypeRegistry();

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

modules.forEach(({ name, calls }, index) => {
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(` ${stringLowerFirst(moduleName)}: ${moduleNamePascal}Tx,\n`);

namespaces = namespaces.concat(`export enum ${moduleNamePascal}Tx {\n`);
allCalls.forEach(({ name }) => {
const nameCamelCase = stringCamelCase(name.toString());
const namePascal = stringUpperFirst(nameCamelCase);
namespaces = namespaces.concat(
` ${namePascal} = '${moduleNameCamelCase}.${nameCamelCase}',\n`
);
});
namespaces = namespaces.concat('}\n\n');
}
});

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

fs.appendFileSync(
path.resolve('src', 'polkadot', 'types.ts'),
'\n'.concat(namespaces).concat(`${txTag}\n\n${txTags}\n`)
);

websocket.close();
};
24 changes: 14 additions & 10 deletions src/Polymesh.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ApiPromise, Keyring, WsProvider } from '@polkadot/api';
import { Option } from '@polkadot/types';
import { u8aToString } from '@polkadot/util';
import { ApiPromise, Keyring, WsProvider } from '@polymathnetwork/polkadot/api';
import { Option } from '@polymathnetwork/polkadot/types';
import { Link } from '@polymathnetwork/polkadot/types/interfaces';
import { BigNumber } from 'bignumber.js';
import { polymesh } from 'polymesh-types/definitions';
import { Link } from 'polymesh-types/types';

import { TickerReservation } from '~/api/entities';
import { reserveTicker, ReserveTickerParams } from '~/api/procedures';
Expand Down Expand Up @@ -46,6 +47,7 @@ export class Polymesh {
try {
polymeshApi = await ApiPromise.create({
provider: new WsProvider(nodeUrl),
types: polymesh.types,
});

let context: Context;
Expand Down Expand Up @@ -83,27 +85,29 @@ export class Polymesh {
/**
* Get the POLY balance of the current account
*/
public getIdentityBalance = async (): Promise<BigNumber> => {
public async getIdentityBalance(): Promise<BigNumber> {
const { currentIdentity } = this.context;
if (currentIdentity) {
const balance = await currentIdentity.getIdentityBalance();
const balance = await currentIdentity.getPolyXBalance();
return balance;
} else {
throw new PolymeshError({
code: ErrorCode.FatalError,
message: 'The current account does not have an associated identity',
});
}
};
}

/**
* Get the free POLY balance of the current account
* Get the free POLY balance of an account
*
* @param args.accountId - defaults to the current account
*/
public getAccountBalance = (accountId?: string): Promise<BigNumber> => {
public getAccountBalance(args?: { accountId: string }): Promise<BigNumber> {
const { context } = this;

return context.accountBalance(accountId);
};
return context.accountBalance(args?.accountId);
}

/**
* Reserve a ticker symbol to later use in the creation of a Security Token.
Expand Down
Loading

0 comments on commit a23790e

Please sign in to comment.