Skip to content

Commit

Permalink
feat: add support for historic queries
Browse files Browse the repository at this point in the history
Also added subscription to queries that didn't have it, fixed some import paths, improved comments
  • Loading branch information
monitz87 committed Jul 14, 2020
1 parent 7565662 commit 092d3af
Show file tree
Hide file tree
Showing 29 changed files with 408 additions and 125 deletions.
28 changes: 24 additions & 4 deletions src/Polymesh.ts
Expand Up @@ -71,6 +71,9 @@ export class Polymesh {
this.context = context;
}

/**
* Create the instance and connect to the Polymesh node
*/
static async connect(params: ConnectParamsBase & { accountSeed: string }): Promise<Polymesh>;

static async connect(
Expand All @@ -83,9 +86,7 @@ export class Polymesh {

static async connect(params: ConnectParamsBase): Promise<Polymesh>;

/**
* Create the instance and connect to the Polymesh node
*/
// eslint-disable-next-line require-jsdoc
static async connect(
params: ConnectParamsBase & {
accountSeed?: string;
Expand Down Expand Up @@ -260,9 +261,28 @@ export class Polymesh {

/**
* Check if a ticker hasn't been reserved
*
* @note can be subscribed to
*/
public async isTickerAvailable(args: { ticker: string }): Promise<boolean> {
public isTickerAvailable(args: { ticker: string }): Promise<boolean>;
public isTickerAvailable(
args: { ticker: string },
callback: SubCallback<boolean>
): Promise<UnsubCallback>;

// eslint-disable-next-line require-jsdoc
public async isTickerAvailable(
args: { ticker: string },
callback?: SubCallback<boolean>
): Promise<boolean | UnsubCallback> {
const reservation = new TickerReservation(args, this.context);

if (callback) {
return reservation.details(({ status }) => {
// eslint-disable-next-line standard/no-callback-literal
callback(status === TickerReservationStatus.Free);
});
}
const { status } = await reservation.details();

return status === TickerReservationStatus.Free;
Expand Down
25 changes: 25 additions & 0 deletions src/__tests__/Polymesh.ts
Expand Up @@ -364,6 +364,31 @@ describe('Polymesh Class', () => {

expect(isTickerAvailable).toBeFalsy();
});

test('should allow subscription', async () => {
const unsubCallback = 'unsubCallBack';

entityMockUtils.getTickerReservationDetailsStub().callsFake(async cbFunc => {
cbFunc({
owner: entityMockUtils.getIdentityInstance(),
expiryDate: new Date(),
status: TickerReservationStatus.Free,
});

return unsubCallback;
});

const polymesh = await Polymesh.connect({
nodeUrl: 'wss://some.url',
accountUri: '//uri',
});

const callback = sinon.stub();
const result = await polymesh.isTickerAvailable({ ticker: 'someTicker' }, callback);

expect(result).toBe(unsubCallback);
sinon.assert.calledWithExactly(callback, true);
});
});

describe('method: getTickerReservations', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/api/entities/Identity/Authorizations.ts
@@ -1,8 +1,8 @@
import { u64 } from '@polkadot/types';
import { Authorization } from 'polymesh-types/types';

import { AuthorizationRequest } from '~/api/entities';
import { Namespace } from '~/base';
import { Authorization } from '~/polkadot';
import { PaginationOptions, ResultSet } from '~/types';
import { SignerType } from '~/types/internal';
import { tuple } from '~/types/utils';
Expand Down Expand Up @@ -57,6 +57,8 @@ export class Authorizations extends Namespace<Identity> {

/**
* Fetch all pending authorization requests issued by this identity
*
* @note supports pagination
*/
public async getSent(
paginationOpts?: PaginationOptions
Expand Down
2 changes: 1 addition & 1 deletion src/api/entities/Identity/__tests__/index.ts
@@ -1,10 +1,10 @@
import { Balance } from '@polkadot/types/interfaces';
import BigNumber from 'bignumber.js';
import { IdentityId, Ticker } from 'polymesh-types/types';
import sinon from 'sinon';

import { Entity } from '~/base';
import { Context } from '~/context';
import { IdentityId, Ticker } from '~/polkadot';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import { Role, RoleType } from '~/types';
import * as utilsModule from '~/utils';
Expand Down
9 changes: 5 additions & 4 deletions src/api/entities/SecurityToken/Compliance/Rules.ts
@@ -1,9 +1,9 @@
import { QueryableStorageEntry } from '@polkadot/api/types';
import { Vec } from '@polkadot/types/codec';
import { AssetTransferRules, IdentityId } from 'polymesh-types/types';

import { setTokenRules, SetTokenRulesParams, togglePauseRules } from '~/api/procedures';
import { Namespace, TransactionQueue } from '~/base';
import { AssetTransferRules, IdentityId } from '~/polkadot';
import { Rule, SubCallback, UnsubCallback } from '~/types';
import { assetTransferRuleToRule, identityIdToString, stringToTicker } from '~/utils';

Expand Down Expand Up @@ -32,14 +32,15 @@ export class Rules extends Namespace<SecurityToken> {
return setTokenRules.prepare({ ticker, ...args }, context);
}

public get(): Promise<Rule[]>;
public get(callback: SubCallback<Rule[]>): Promise<UnsubCallback>;

/**
* Retrieve all of the Security Token's transfer rules
*
* @note can be subscribed to
*/
public get(): Promise<Rule[]>;
public get(callback: SubCallback<Rule[]>): Promise<UnsubCallback>;

// eslint-disable-next-line require-jsdoc
public async get(callback?: SubCallback<Rule[]>): Promise<Rule[] | UnsubCallback> {
const {
parent: { ticker },
Expand Down
10 changes: 6 additions & 4 deletions src/api/entities/SecurityToken/Compliance/TrustedClaimIssuers.ts
@@ -1,7 +1,8 @@
import { IdentityId } from 'polymesh-types/types';

import { Identity } from '~/api/entities';
import { setTokenTrustedClaimIssuers, SetTokenTrustedClaimIssuersParams } from '~/api/procedures';
import { Namespace, TransactionQueue } from '~/base';
import { IdentityId } from '~/polkadot';
import { SubCallback, UnsubCallback } from '~/types';
import { identityIdToString, stringToTicker } from '~/utils';

Expand All @@ -26,14 +27,15 @@ export class TrustedClaimIssuers extends Namespace<SecurityToken> {
return setTokenTrustedClaimIssuers.prepare({ ticker, ...args }, context);
}

public get(): Promise<Identity[]>;
public get(callback: SubCallback<Identity[]>): Promise<UnsubCallback>;

/**
* Retrieve the current default trusted claim issuers of the Security Token
*
* @note can be subscribed to
*/
public get(): Promise<Identity[]>;
public get(callback: SubCallback<Identity[]>): Promise<UnsubCallback>;

// eslint-disable-next-line require-jsdoc
public async get(callback?: SubCallback<Identity[]>): Promise<Identity[] | UnsubCallback> {
const {
context: {
Expand Down
@@ -1,12 +1,12 @@
import { Vec } from '@polkadot/types/codec';
import { AssetTransferRules, IdentityId } from 'polymesh-types/types';
import sinon from 'sinon';

import { SecurityToken } from '~/api/entities';
import { setTokenRules, togglePauseRules } from '~/api/procedures';
import { Params } from '~/api/procedures/setTokenRules';
import { Namespace, TransactionQueue } from '~/base';
import { Context } from '~/context';
import { AssetTransferRules, IdentityId } from '~/polkadot';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import { ClaimType, ConditionTarget, ConditionType, Rule } from '~/types';

Expand Down
@@ -1,11 +1,11 @@
import { IdentityId, Ticker } from 'polymesh-types/types';
import sinon from 'sinon';

import { SecurityToken } from '~/api/entities';
import { Identity } from '~/api/entities/Identity';
import { setTokenTrustedClaimIssuers } from '~/api/procedures';
import { Namespace, TransactionQueue } from '~/base';
import { Context } from '~/context';
import { IdentityId, Ticker } from '~/polkadot';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import * as utilsModule from '~/utils';

Expand Down
15 changes: 9 additions & 6 deletions src/api/entities/SecurityToken/TokenHolders.ts
@@ -1,11 +1,11 @@
import BigNumber from 'bignumber.js';
import P from 'bluebird';
import { chunk } from 'lodash';
import { IdentityId } from 'polymesh-types/types';

import { Identity } from '~/api/entities/Identity';
import { IdentityBalance } from '~/api/entities/types';
import { Namespace } from '~/base';
import { IdentityId } from '~/polkadot';
import { PaginationOptions, ResultSet, TransferStatus } from '~/types';
import { balanceToBigNumber, identityIdToString, requestPaginated, stringToTicker } from '~/utils';
import { MAX_CONCURRENT_REQUESTS } from '~/utils/constants';
Expand All @@ -17,18 +17,21 @@ import { TokenHolderOptions, TokenHolderProperties } from './types';
* Handles all Security Token Holders related functionality
*/
export class TokenHolders extends Namespace<SecurityToken> {
/**
* Retrieve all the token holders with balance
*
* @param opts - object that represents whether extra properties should be fetched for each token holder
*
* @note supports pagination
*/
public async get(
opts: Pick<TokenHolderOptions, 'canBeIssuedTo'>,
paginationOpts?: PaginationOptions
): Promise<ResultSet<IdentityBalance & Pick<TokenHolderProperties, 'canBeIssuedTo'>>>;

public async get(paginationOpts?: PaginationOptions): Promise<ResultSet<IdentityBalance>>;

/**
* Retrieve all the token holders with balance
*
* @param opts - object that represents whether extra properties should be fetched for each token holder
*/
// eslint-disable-next-line require-jsdoc
public async get(
opts?: Pick<TokenHolderOptions, 'canBeIssuedTo'> | PaginationOptions,
paginationOpts?: PaginationOptions
Expand Down
9 changes: 5 additions & 4 deletions src/api/entities/SecurityToken/Transfers.ts
@@ -1,9 +1,9 @@
import BigNumber from 'bignumber.js';
import { CanTransferResult } from 'polymesh-types/types';

import { Identity } from '~/api/entities/Identity';
import { toggleFreezeTransfers, transferToken, TransferTokenParams } from '~/api/procedures';
import { Namespace, TransactionQueue } from '~/base';
import { CanTransferResult } from '~/polkadot';
import { SubCallback, TransferStatus, UnsubCallback } from '~/types';
import {
boolToBoolean,
Expand Down Expand Up @@ -44,14 +44,15 @@ export class Transfers extends Namespace<SecurityToken> {
return toggleFreezeTransfers.prepare({ ticker, freeze: false }, context);
}

public areFrozen(): Promise<boolean>;
public areFrozen(callback: SubCallback<boolean>): Promise<UnsubCallback>;

/**
* Check whether transfers are frozen for the Security Token
*
* @note can be subscribed to
*/
public areFrozen(): Promise<boolean>;
public areFrozen(callback: SubCallback<boolean>): Promise<UnsubCallback>;

// eslint-disable-next-line require-jsdoc
public async areFrozen(callback?: SubCallback<boolean>): Promise<boolean | UnsubCallback> {
const {
parent: { ticker },
Expand Down
2 changes: 1 addition & 1 deletion src/api/entities/SecurityToken/__tests__/TokenHolders.ts
@@ -1,12 +1,12 @@
import { StorageKey } from '@polkadot/types';
import { Balance } from '@polkadot/types/interfaces';
import BigNumber from 'bignumber.js';
import { IdentityId, Ticker } from 'polymesh-types/types';
import sinon from 'sinon';

import { Identity } from '~/api/entities/Identity';
import { Namespace } from '~/base';
import { Context } from '~/context';
import { IdentityId, Ticker } from '~/polkadot';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import { Mocked } from '~/testUtils/types';
import { IdentityBalance, TokenHolderProperties, TransferStatus } from '~/types';
Expand Down
2 changes: 1 addition & 1 deletion src/api/entities/SecurityToken/__tests__/Transfers.ts
@@ -1,13 +1,13 @@
import { AccountId, Balance } from '@polkadot/types/interfaces';
import { bool } from '@polkadot/types/primitive';
import BigNumber from 'bignumber.js';
import { IdentityId, Ticker } from 'polymesh-types/types';
import sinon, { SinonStub } from 'sinon';

import { toggleFreezeTransfers, transferToken } from '~/api/procedures';
import { Params } from '~/api/procedures/toggleFreezeTransfers';
import { Namespace, TransactionQueue } from '~/base';
import { Context } from '~/context';
import { IdentityId, Ticker } from '~/polkadot';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import { Mocked } from '~/testUtils/types';
import { TransferStatus } from '~/types';
Expand Down

0 comments on commit 092d3af

Please sign in to comment.