Skip to content

Commit

Permalink
feat: method getIdentifiers and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Mar 25, 2020
1 parent 669361a commit 81da255
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 25 deletions.
9 changes: 1 addition & 8 deletions src/Polymesh.ts
Expand Up @@ -5,7 +5,7 @@ import { BigNumber } from 'bignumber.js';
import { polymesh } from 'polymesh-types/definitions';
import { Link } from 'polymesh-types/types';

import { SecurityToken, TickerReservation } from '~/api/entities';
import { TickerReservation } from '~/api/entities';
import { reserveTicker, ReserveTickerParams } from '~/api/procedures';
import { PolymeshError, TransactionQueue } from '~/base';
import { Context } from '~/context';
Expand Down Expand Up @@ -182,11 +182,4 @@ export class Polymesh {
message: `There is no reservation for ${ticker} ticker`,
});
}

/**
*
*/
public getToken(): SecurityToken {
return new SecurityToken({ ticker: 'MAIA' }, this.context);
}
}
70 changes: 67 additions & 3 deletions src/api/entities/SecurityToken/__tests__/index.ts
@@ -1,10 +1,12 @@
import { Balance } from '@polkadot/types/interfaces';
import { AssetIdentifier, IdentifierType } from 'polymesh-types/types';
import sinon from 'sinon';

import { modifyToken } from '~/api/procedures';
import { Entity, TransactionQueue } from '~/base';
import { polkadotMockUtils } from '~/testUtils/mocks';
import { balanceToBigNumber, tickerToDid } from '~/utils';
import { TokenIdentifierType } from '~/types';
import * as utilsModule from '~/utils';

import { SecurityToken } from '../';

Expand Down Expand Up @@ -32,7 +34,7 @@ describe('SecurityToken class', () => {
const securityToken = new SecurityToken({ ticker }, context);

expect(securityToken.ticker).toBe(ticker);
expect(securityToken.did).toBe(tickerToDid(ticker));
expect(securityToken.did).toBe(utilsModule.tickerToDid(ticker));
});
});

Expand Down Expand Up @@ -71,7 +73,9 @@ describe('SecurityToken class', () => {
const details = await securityToken.details();

expect(details.name).toBe(ticker);
expect(details.totalSupply).toEqual(balanceToBigNumber((totalSupply as unknown) as Balance));
expect(details.totalSupply).toEqual(
utilsModule.balanceToBigNumber((totalSupply as unknown) as Balance)
);
expect(details.isDivisible).toBe(isDivisible);
expect(details.owner.did).toBe(owner);
expect(details.assetType).toBe(assetType);
Expand Down Expand Up @@ -118,4 +122,64 @@ describe('SecurityToken class', () => {
expect(result).toBe(fundingRound);
});
});

describe('method: getIdentifiers', () => {
test('should return the list of token identifiers for a security token', async () => {
const ticker = 'TEST';
const isinValue = 'FAKE ISIN';
const cusipValue = 'FAKE CUSIP';
const isinMock = polkadotMockUtils.createMockAssetIdentifier(isinValue);
const cusipMock = polkadotMockUtils.createMockAssetIdentifier(cusipValue);
const tokenIdentifiers = [
{
type: TokenIdentifierType.Isin,
value: 'Fake Isin value',
},
{
type: TokenIdentifierType.Cusip,
value: 'Fake Cusip value',
},
];

const rawIdentifiers: [IdentifierType, AssetIdentifier][] = tokenIdentifiers.map(
({ type, value }) => {
return [
polkadotMockUtils.createMockIdentifierType(type as TokenIdentifierType),
polkadotMockUtils.createMockAssetIdentifier(value),
];
}
);

const context = polkadotMockUtils.getContextInstance();

const tokenIdentifierTypeToIdentifierTypeStub = sinon.stub(
utilsModule,
'tokenIdentifierTypeToIdentifierType'
);

tokenIdentifierTypeToIdentifierTypeStub
.withArgs(tokenIdentifiers[0].type, context)
.returns(rawIdentifiers[0][0]);

tokenIdentifierTypeToIdentifierTypeStub
.withArgs(tokenIdentifiers[1].type, context)
.returns(rawIdentifiers[1][0]);

polkadotMockUtils.createQueryStub('asset', 'identifiers', {
multi: [isinMock, cusipMock],
});

const assetIdentifierToStringStub = sinon.stub(utilsModule, 'assetIdentifierToString');

assetIdentifierToStringStub.withArgs(isinMock).returns(isinValue);
assetIdentifierToStringStub.withArgs(cusipMock).returns(cusipValue);

const securityToken = new SecurityToken({ ticker }, context);

const result = await securityToken.getIdentifiers();

expect(result[0].value).toBe(isinValue);
expect(result[1].value).toBe(cusipValue);
});
});
});
27 changes: 16 additions & 11 deletions src/api/entities/SecurityToken/index.ts
@@ -1,15 +1,19 @@
import { AssetIdentifier } from 'polymesh-types/types';

import { Identity } from '~/api/entities/Identity';
import { modifyToken, ModifyTokenParams } from '~/api/procedures';
import { Entity, TransactionQueue } from '~/base';
import { Context } from '~/context';
import { TokenIdentifier, TokenIdentifierType } from '~/types';
import {
assetIdentifierToString,
assetTypeToString,
balanceToBigNumber,
boolToBoolean,
fundingRoundNameToString,
identityIdToString,
tickerToDid,
tokenIdentifierTypeToIdentifierType,
tokenNameToString,
} from '~/utils';

Expand Down Expand Up @@ -127,21 +131,22 @@ export class SecurityToken extends Entity<UniqueIdentifiers> {
},
},
ticker,
context,
} = this;

const identifierTypes = Object.keys(TokenIdentifierType);
console.log(identifierTypes);
const tokenIdentifierTypes = Object.values(TokenIdentifierType);
const identifierTypes = tokenIdentifierTypes.map(type => [
ticker,
tokenIdentifierTypeToIdentifierType(type, context),
]);

// Object.keys(TokenIdentifierType).map(type => )
const assetIdentifiers = await asset.identifiers.multi<AssetIdentifier>(identifierTypes);

/*
const assetIdentifiers = await asset.identifiers.multi([
[ticker, ''],
[ticker, ''],
]);
*/
const tokenIdentifiers = tokenIdentifierTypes.map((type, i) => ({
type,
value: assetIdentifierToString(assetIdentifiers[i]),
}));

const result = [] as TokenIdentifier[];
return result;
return tokenIdentifiers;
}
}
2 changes: 1 addition & 1 deletion src/polkadot/augment-api-tx.ts
Expand Up @@ -26,7 +26,7 @@ import {
import { Keys } from '@polkadot/types/interfaces/session';
import { EraIndex, RewardDestination, ValidatorPrefs } from '@polkadot/types/interfaces/staking';
import { Key } from '@polkadot/types/interfaces/system';
import { bool, Bytes, u16, u32, u64,u128 } from '@polkadot/types/primitive';
import { bool, Bytes, u16, u32, u64, u128 } from '@polkadot/types/primitive';
import { AnyNumber, ITuple } from '@polkadot/types/types';
import {
AccountKey,
Expand Down
2 changes: 1 addition & 1 deletion src/polkadot/polymesh/types.ts
Expand Up @@ -4,7 +4,7 @@
import { Enum, Option, Struct, U8aFixed, Vec } from '@polkadot/types/codec';
import { Signature } from '@polkadot/types/interfaces/extrinsics';
import { Balance, Call, H256, H512, Hash, Moment } from '@polkadot/types/interfaces/runtime';
import { bool, Bytes, Text, u8,u16, u32, u64, u128 } from '@polkadot/types/primitive';
import { bool, Bytes, Text, u8, u16, u32, u64, u128 } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';

/** @name AccountKey */
Expand Down
6 changes: 5 additions & 1 deletion src/testUtils/mocks/polkadot.ts
Expand Up @@ -510,6 +510,7 @@ export function createQueryStub<
opts?: {
returnValue?: unknown;
entries?: unknown[];
multi?: unknown;
}
): Queries[ModuleName][QueryName] & SinonStub<ArgsType<Queries[ModuleName][QueryName]>> {
let runtimeModule = queryModule[mod];
Expand All @@ -519,14 +520,17 @@ export function createQueryStub<
queryModule[mod] = runtimeModule;
}

/* eslint-disable @typescript-eslint/no-explicit-any */
if (!runtimeModule[query]) {
runtimeModule[query] = (sinon.stub() as unknown) as Queries[ModuleName][QueryName];
if (opts?.entries) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(runtimeModule[query] as any).entries = sinon
.stub()
.resolves(opts.entries.map(entry => ['someKey', entry]));
} else if (opts?.multi) {
(runtimeModule[query] as any).multi = sinon.stub().resolves(opts.multi);
}
/* eslint-enable @typescript-eslint/no-explicit-any */

updateQuery();
}
Expand Down

0 comments on commit 81da255

Please sign in to comment.