Skip to content

Commit

Permalink
Merge pull request #21 from PolymathNetwork/feat/MSDK-29-type-convers…
Browse files Browse the repository at this point in the history
…ion-utils

feat: type conversion utils
  • Loading branch information
monitz87 authored Feb 25, 2020
2 parents 4cefac0 + a2102ba commit adf9a12
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 7 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"commit": "npx git-cz",
"semantic-release": "semantic-release",
"lint": "eslint src --ext .js,.jsx,.ts,.tsx",
"format": "cross-env prettier-eslint $PWD\"/src/**/*.{ts,tsx,js,jsx}\" --write"
"format": "cross-env prettier-eslint $PWD\"/src/**/*.{ts,tsx,js,jsx}\" --write",
"ts-node": "ts-node -r tsconfig-paths/register"
},
"devDependencies": {
"@commitlint/cli": "^7.6.1",
Expand Down Expand Up @@ -48,6 +49,7 @@
"sinon": "^8.1.1",
"ts-jest": "^24.3.0",
"ts-mock-imports": "^1.2.6",
"tsconfig-paths": "^3.9.0",
"ttypescript": "^1.5.10",
"typedoc": "^0.16.8",
"typescript": "3.7.5"
Expand All @@ -61,7 +63,8 @@
"access": "public"
},
"dependencies": {
"@polymathnetwork/polkadot": "0.101.0-beta.14",
"@polymathnetwork/polkadot": "0.101.0-beta.15",
"@types/bn.js": "^4.11.6",
"@types/bignumber.js": "^5.0.0",
"bignumber.js": "^9.0.0",
"json-stable-stringify": "^1.0.1",
Expand Down
106 changes: 105 additions & 1 deletion src/utils/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import * as createTypeModule from '@polymathnetwork/polkadot/types/create/createType';
import { Balance, IdentityId } from '@polymathnetwork/polkadot/types/interfaces';
import { ISubmittableResult } from '@polymathnetwork/polkadot/types/types';
import BigNumber from 'bignumber.js';
import sinon, { SinonStub } from 'sinon';
import { ImportMock } from 'ts-mock-imports';

import { PostTransactionValue } from '~/base';
import { delay, serialize, unserialize, unwrapValues } from '~/utils';
import { PolkadotMockFactory } from '~/testUtils/mocks';
import {
balanceToBigNumber,
delay,
identityIdToString,
numberToBalance,
serialize,
stringToIdentityId,
unserialize,
unwrapValues,
} from '~/utils';

describe('delay', () => {
jest.useFakeTimers();
Expand Down Expand Up @@ -58,6 +73,95 @@ describe('serialize and unserialize', () => {
});
});

describe('stringToIdentityId and identityIdToString', () => {
const polkadotMockFactory = new PolkadotMockFactory();
polkadotMockFactory.initMocks({ mockContext: true });

let mockCreateType: SinonStub;

beforeEach(() => {
mockCreateType = ImportMock.mockFunction(createTypeModule, 'createType', 'type');
});

afterEach(() => {
polkadotMockFactory.reset();
mockCreateType.restore();
});

afterAll(() => {
polkadotMockFactory.cleanup();
});

test('stringToIdentityId should convert a did string into an IdentityId', () => {
const identity = 'IdentityObject';
const fakeResult = ('type' as unknown) as IdentityId;
const context = polkadotMockFactory.getContextInstance();

mockCreateType
.withArgs(context.polymeshApi.registry, 'IdentityId', identity)
.returns(fakeResult);

const result = stringToIdentityId(identity, context);

expect(result).toBe(fakeResult);
});

test('identityIdToString should convert an IdentityId to a did string', () => {
const fakeResult = 'IdentityString';
const toStringStub = sinon.stub().returns(fakeResult);
const identityId = ({
toString: toStringStub,
} as unknown) as IdentityId;

const result = identityIdToString(identityId);
expect(result).toBe(fakeResult);
});
});

describe('numberToBalance and balanceToBigNumber', () => {
const polkadotMockFactory = new PolkadotMockFactory();
polkadotMockFactory.initMocks({ mockContext: true });

let mockCreateType: SinonStub;

beforeEach(() => {
mockCreateType = ImportMock.mockFunction(createTypeModule, 'createType', 'type');
});

afterEach(() => {
polkadotMockFactory.reset();
mockCreateType.restore();
});

afterAll(() => {
polkadotMockFactory.cleanup();
});

test('numberToBalance should convert a number to a polkadot Balance object', () => {
const value = new BigNumber(100);
const fakeResult = ('100' as unknown) as Balance;
const context = polkadotMockFactory.getContextInstance();

mockCreateType
.withArgs(context.polymeshApi.registry, 'Balance', value.pow(Math.pow(10, 6)))
.returns(fakeResult);

const result = numberToBalance(value, context);

expect(result).toBe(fakeResult);
});

test('balanceToBigNumber should convert a polkadot Balance object to a BigNumber', () => {
const fakeResult = new BigNumber(100);
const balance = ({
toString: sinon.stub().returns(fakeResult.toString()),
} as unknown) as Balance;

const result = balanceToBigNumber(balance);
expect(result).toEqual(fakeResult.div(Math.pow(10, 6)));
});
});

describe('unwrapValues', () => {
test('should unwrap all Post Transaction Values in the array', async () => {
const values = [1, 2, 3, 4, 5];
Expand Down
36 changes: 36 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { createType } from '@polymathnetwork/polkadot/types/create/createType';
import { Balance, IdentityId } from '@polymathnetwork/polkadot/types/interfaces';
import BigNumber from 'bignumber.js';
import stringify from 'json-stable-stringify';

import { PostTransactionValue } from '~/base';
import { Context } from '~/base/Context';
import { MapMaybePostTransactionValue, MaybePostTransactionValue } from '~/types/internal';

/**
Expand Down Expand Up @@ -46,6 +50,38 @@ export function unserialize(id: string): Record<string, unknown> {
}
}

/**
* Convert an string to an IdentityId representation
*/
export function stringToIdentityId(identityId: string, context: Context): IdentityId {
return createType<'IdentityId'>(context.polymeshApi.registry, 'IdentityId', identityId);
}

/**
* Convert an IdentityId representation to an string
*/
export function identityIdToString(identityId: IdentityId): string {
return identityId.toString();
}

/**
* Convert an human readable number to a Balance representation
*/
export function numberToBalance(value: number | BigNumber, context: Context): Balance {
return createType<'Balance'>(
context.polymeshApi.registry,
'Balance',
new BigNumber(value).pow(Math.pow(10, 6))
);
}

/**
* Convert a Balance representation to a BigNumber
*/
export function balanceToBigNumber(balance: Balance): BigNumber {
return new BigNumber(balance.toString()).div(Math.pow(10, 6));
}

/**
* Unwrap Post Transaction Value
*/
Expand Down
30 changes: 26 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -670,10 +670,10 @@
resolved "https://registry.yarnpkg.com/@polkadot/wasm-crypto/-/wasm-crypto-1.0.1.tgz#80a2a6e8d63a7decdd1163cc49b263ccef4ad36d"
integrity sha512-l6izEdPXl4V8SBU9m3ct5PkHcdKZVDJMb10CFPNuNXT5OXMRaDs+Rlt/EHKjh8wbv8s9EOqy42BSHO8Qqrcg/g==

"@polymathnetwork/polkadot@0.101.0-beta.14":
version "0.101.0-beta.14"
resolved "https://registry.yarnpkg.com/@polymathnetwork/polkadot/-/polkadot-0.101.0-beta.14.tgz#1905bd3842e93772598d508b59edceaa9dbfeda4"
integrity sha512-/Y+HnwlFbEKfGvr3kIhEp83kUtAQ60ub4ZToPPbVgpwzLBURQW5mRlPVXm4c4mnD4/EyaOfUqmcJISwNNCMtEA==
"@polymathnetwork/polkadot@0.101.0-beta.15":
version "0.101.0-beta.15"
resolved "https://registry.yarnpkg.com/@polymathnetwork/polkadot/-/polkadot-0.101.0-beta.15.tgz#f224130e60b93163adfd555bbd643e5272be51a6"
integrity sha512-a9cKRLphBYtKhyiXkt1r0LGttCTXh71sUCC8SEmPGevXEakH/i1YnJlcfl6+UN1G2kvBymesd9IhOvNVUBWYqA==
dependencies:
"@babel/core" "7.8.4"
"@babel/register" "7.8.3"
Expand Down Expand Up @@ -907,6 +907,11 @@
resolved "https://registry.yarnpkg.com/@types/json-stable-stringify/-/json-stable-stringify-1.0.32.tgz#121f6917c4389db3923640b2e68de5fa64dda88e"
integrity sha512-q9Q6+eUEGwQkv4Sbst3J4PNgDOvpuVuKj79Hl/qnmBMEIPzB5QoFRUtjcgcg2xNUZyYUGXBk5wYIBKHt0A+Mxw==

"@types/json5@^0.0.29":
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=

"@types/lodash@^4.14.149":
version "4.14.149"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.149.tgz#1342d63d948c6062838fbf961012f74d4e638440"
Expand Down Expand Up @@ -5141,6 +5146,13 @@ json5@2.x, json5@^2.1.0:
dependencies:
minimist "^1.2.0"

json5@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/json5/-/json5-1.0.1.tgz#779fb0018604fa854eacbf6252180d83543e3dbe"
integrity sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==
dependencies:
minimist "^1.2.0"

jsonfile@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
Expand Down Expand Up @@ -8760,6 +8772,16 @@ ts-mock-imports@^1.2.6:
resolved "https://registry.yarnpkg.com/ts-mock-imports/-/ts-mock-imports-1.2.6.tgz#5a98a398c3eadb7f75b6904984bb0ba5f3fbb912"
integrity sha512-rZjsIEBWx9a3RGUo4Rhj/hzEGB4GPWJx46fls9EJf4UBsf5SxS2qiozf6dQp0Ym/9LC5MArlXZbZ+93wJzAmjA==

tsconfig-paths@^3.9.0:
version "3.9.0"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.9.0.tgz#098547a6c4448807e8fcb8eae081064ee9a3c90b"
integrity sha512-dRcuzokWhajtZWkQsDVKbWyY+jgcLC5sqJhg2PSgf4ZkH2aHPvaOY8YWGhmjb68b5qqTfasSsDO9k7RUiEmZAw==
dependencies:
"@types/json5" "^0.0.29"
json5 "^1.0.1"
minimist "^1.2.0"
strip-bom "^3.0.0"

tslib@^1.8.1, tslib@^1.9.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"
Expand Down

0 comments on commit adf9a12

Please sign in to comment.