Skip to content

Commit

Permalink
fix: add tokenNameToString and boolToBoolean methods in utils
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Mar 11, 2020
1 parent 3ee8d21 commit d6ecef1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/api/entities/SecurityToken/index.ts
@@ -1,7 +1,7 @@
import { Identity } from '~/api/entities/Identity';
import { Entity } from '~/base';
import { Context } from '~/context';
import { balanceToBigNumber } from '~/utils';
import { balanceToBigNumber, boolToBoolean, identityIdToString, tokenNameToString } from '~/utils';

import { SecurityTokenDetails } from './types';

Expand Down Expand Up @@ -46,7 +46,7 @@ export class SecurityToken extends Entity<UniqueIdentifiers> {
}

/**
* Retrieve the security token's name, total supply, whether is divisible or not and the identity owner
* Retrieve the Security Token's name, total supply, whether it is divisible or not and the identity of the owner
*/
public async details(): Promise<SecurityTokenDetails> {
const {
Expand All @@ -63,10 +63,10 @@ export class SecurityToken extends Entity<UniqueIdentifiers> {
const { name, total_supply, divisible, owner_did } = await asset.tokens(ticker);

return {
name: name.toString(),
name: tokenNameToString(name),
totalSupply: balanceToBigNumber(total_supply),
isDivisible: divisible.valueOf(),
owner: new Identity({ did: owner_did.toString() }, context),
isDivisible: boolToBoolean(divisible),
owner: new Identity({ did: identityIdToString(owner_did) }, context),
};
/* eslint-enable @typescript-eslint/camelcase */
}
Expand Down
35 changes: 34 additions & 1 deletion src/utils/__tests__/index.ts
@@ -1,5 +1,12 @@
import { bool } from '@polymathnetwork/polkadot/types';
import * as createTypeModule from '@polymathnetwork/polkadot/types/create/createType';
import { Balance, IdentityId, Moment, Ticker } from '@polymathnetwork/polkadot/types/interfaces';
import {
Balance,
IdentityId,
Moment,
Ticker,
TokenName,
} from '@polymathnetwork/polkadot/types/interfaces';
import { ISubmittableResult } from '@polymathnetwork/polkadot/types/types';
import BigNumber from 'bignumber.js';
import sinon, { SinonStub } from 'sinon';
Expand All @@ -10,6 +17,7 @@ import { PolkadotMockFactory } from '~/testUtils/mocks';

import {
balanceToBigNumber,
boolToBoolean,
dateToMoment,
delay,
findEventRecord,
Expand All @@ -20,6 +28,7 @@ import {
stringToIdentityId,
stringToTicker,
tickerToString,
tokenNameToString,
unserialize,
unwrapValues,
} from '../';
Expand Down Expand Up @@ -210,6 +219,30 @@ describe('stringToTicker and tickerToString', () => {
});
});

describe('tokenNameToString', () => {
test('tokenNameToString should convert a TokenName object to a string', () => {
const fakeResult = 'someTokenName';
const tokenName = ({
toString: sinon.stub().returns(fakeResult),
} as unknown) as TokenName;

const result = tokenNameToString(tokenName);
expect(result).toEqual(fakeResult);
});
});

describe('boolToBoolean', () => {
test('boolToBoolean should convert a bool object to a boolean', () => {
const fakeResult = true;
const bool = ({
valueOf: sinon.stub().returns(fakeResult),
} as unknown) as bool;

const result = boolToBoolean(bool);
expect(result).toEqual(fakeResult);
});
});

describe('dateToMoment and momentToDate', () => {
const polkadotMockFactory = new PolkadotMockFactory();
polkadotMockFactory.initMocks({ mockContext: true });
Expand Down
16 changes: 16 additions & 0 deletions src/utils/index.ts
@@ -1,10 +1,12 @@
import { bool } from '@polymathnetwork/polkadot/types';
import { createType } from '@polymathnetwork/polkadot/types/create/createType';
import {
Balance,
EventRecord,
IdentityId,
Moment,
Ticker,
TokenName,
} from '@polymathnetwork/polkadot/types/interfaces';
import { ISubmittableResult } from '@polymathnetwork/polkadot/types/types';
import BigNumber from 'bignumber.js';
Expand Down Expand Up @@ -65,6 +67,20 @@ export function unserialize<UniqueIdentifiers extends object>(id: string): Uniqu
}
}

/**
* @hidden
*/
export function tokenNameToString(name: TokenName): string {
return name.toString();
}

/**
* @hidden
*/
export function boolToBoolean(value: bool): boolean {
return value.valueOf();
}

/**
* @hidden
*/
Expand Down

0 comments on commit d6ecef1

Please sign in to comment.