Skip to content

Commit

Permalink
feat: implement access to token details
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Mar 10, 2020
1 parent e5f18c9 commit 8725506
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 1 deletion.
127 changes: 126 additions & 1 deletion src/api/entities/SecurityToken/__tests__/index.ts
@@ -1,5 +1,17 @@
import { Balance } from '@polymathnetwork/polkadot/types/interfaces';

import { Entity } from '~/base';
import { PolkadotMockFactory } from '~/testUtils/mocks';
import {
createMockAssetType,
createMockBalance,
createMockBool,
createMockIdentityId,
createMockSecurityToken,
createMockTokenName,
createMockU64,
PolkadotMockFactory,
} from '~/testUtils/mocks';
import { balanceToBigNumber } from '~/utils';

import { SecurityToken } from '../';

Expand Down Expand Up @@ -37,4 +49,117 @@ describe('SecurityToken class', () => {
expect(SecurityToken.isUniqueIdentifiers({ ticker: 3 })).toBe(false);
});
});

describe('method: name', () => {
test('should return the name of the Security Token', async () => {
const ticker = 'test';
const context = polkadotMockFactory.getContextInstance();
const securityToken = new SecurityToken({ ticker }, context);

polkadotMockFactory.createQueryStub(
'asset',
'tokens',
createMockSecurityToken({
/* eslint-disable @typescript-eslint/camelcase */
owner_did: createMockIdentityId('did'),
name: createMockTokenName(ticker),
asset_type: createMockAssetType('equity'),
divisible: createMockBool(true),
link_id: createMockU64(3),
total_supply: createMockBalance(1000),
/* eslint-enable @typescript-eslint/camelcase */
})
);

const result = await securityToken.name();

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

describe('method: totalSupply', () => {
test('should return the total supply of the Security Token', async () => {
const ticker = 'test';
const totalSupply = 1000;
const context = polkadotMockFactory.getContextInstance();
const securityToken = new SecurityToken({ ticker }, context);

polkadotMockFactory.createQueryStub(
'asset',
'tokens',
createMockSecurityToken({
/* eslint-disable @typescript-eslint/camelcase */
owner_did: createMockIdentityId('did'),
name: createMockTokenName(ticker),
asset_type: createMockAssetType('equity'),
divisible: createMockBool(true),
link_id: createMockU64(3),
total_supply: createMockBalance(totalSupply),
/* eslint-enable @typescript-eslint/camelcase */
})
);

const result = await securityToken.totalSupply();

expect(result.toNumber()).toBe(
balanceToBigNumber((totalSupply as unknown) as Balance).toNumber()
);
});
});

describe('method: isDivisible', () => {
test('should return whether or not the Security Token is divisible', async () => {
const ticker = 'test';
const divisible = true;
const context = polkadotMockFactory.getContextInstance();
const securityToken = new SecurityToken({ ticker }, context);

polkadotMockFactory.createQueryStub(
'asset',
'tokens',
createMockSecurityToken({
/* eslint-disable @typescript-eslint/camelcase */
owner_did: createMockIdentityId('did'),
name: createMockTokenName(ticker),
asset_type: createMockAssetType('equity'),
divisible: createMockBool(divisible),
link_id: createMockU64(3),
total_supply: createMockBalance(1000),
/* eslint-enable @typescript-eslint/camelcase */
})
);

const result = await securityToken.isDivisible();

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

describe('method: owner', () => {
test('should return the identity owner of the Security Token', async () => {
const ticker = 'test';
const did = '0x123';
const context = polkadotMockFactory.getContextInstance();
const securityToken = new SecurityToken({ ticker }, context);

polkadotMockFactory.createQueryStub(
'asset',
'tokens',
createMockSecurityToken({
/* eslint-disable @typescript-eslint/camelcase */
owner_did: createMockIdentityId(did),
name: createMockTokenName(ticker),
asset_type: createMockAssetType('equity'),
divisible: createMockBool(true),
link_id: createMockU64(3),
total_supply: createMockBalance(1000),
/* eslint-enable @typescript-eslint/camelcase */
})
);

const result = await securityToken.owner();

expect(result.did).toBe(did);
});
});
});
80 changes: 80 additions & 0 deletions src/api/entities/SecurityToken/index.ts
@@ -1,5 +1,9 @@
import BigNumber from 'bignumber.js';

import { Identity } from '~/api/entities/Identity';
import { Entity } from '~/base';
import { Context } from '~/context';
import { balanceToBigNumber } from '~/utils';

/**
* Properties that uniquely identify a Security Token
Expand Down Expand Up @@ -40,4 +44,80 @@ export class SecurityToken extends Entity<UniqueIdentifiers> {

this.ticker = ticker;
}

/**
* Retrieve the name of the Security Token
*/
public async name(): Promise<string> {
const {
context: {
polymeshApi: {
query: { asset },
},
},
ticker,
} = this;

const { name } = await asset.tokens(ticker);

return name.toString();
}

/**
* Retrieve the total supply of the Security Token
*/
public async totalSupply(): Promise<BigNumber> {
const {
context: {
polymeshApi: {
query: { asset },
},
},
ticker,
} = this;

// eslint-disable-next-line @typescript-eslint/camelcase
const { total_supply } = await asset.tokens(ticker);

return balanceToBigNumber(total_supply);
}

/**
* Retrieve whether or not the Security Token is divisible
*/
public async isDivisible(): Promise<boolean> {
const {
context: {
polymeshApi: {
query: { asset },
},
},
ticker,
} = this;

const { divisible } = await asset.tokens(ticker);

return divisible.valueOf();
}

/**
* Retrieve the identity owner of the Security Token
*/
public async owner(): Promise<Identity> {
const {
context: {
polymeshApi: {
query: { asset },
},
},
ticker,
context,
} = this;

/* eslint-disable @typescript-eslint/camelcase */
const { owner_did } = await asset.tokens(ticker);

return new Identity({ did: owner_did.toString() }, context);
/* eslint-enable @typescript-eslint/camelcase */
}
}
1 change: 1 addition & 0 deletions src/testUtils/mocks/PolkadotMockFactory.ts
Expand Up @@ -664,6 +664,7 @@ export const createMockBool = (value?: boolean): bool =>
{
isTrue: () => value,
isFalse: () => !value,
valueOf: () => value,
},
!value
) as bool;
Expand Down

0 comments on commit 8725506

Please sign in to comment.