Skip to content

Commit

Permalink
feat(account): move all CurrentAccount methods to Account
Browse files Browse the repository at this point in the history
The CurrentAccount entity has been removed

BREAKING CHANGE: The `CurrentAccount` entity has been removed, and all of its methods moved to
`Account`. The error thrown by `account.leaveIdentity` when there is no Identity associated to the
Account is now different
  • Loading branch information
monitz87 committed Aug 13, 2021
1 parent 7f73017 commit 43b703b
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 314 deletions.
7 changes: 3 additions & 4 deletions src/Polymesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
claimClassicTicker,
ClaimClassicTickerParams,
Context,
CurrentAccount,
Identity,
PolymeshError,
registerIdentity,
Expand Down Expand Up @@ -454,11 +453,11 @@ export class Polymesh {
/**
* Create an Account instance from an address. If no address is passed, the current Account is returned
*/
public getAccount(): CurrentAccount;
public getAccount(): Account;
public getAccount(args: { address: string }): Account;

// eslint-disable-next-line require-jsdoc
public getAccount(args?: { address: string }): Account | CurrentAccount {
public getAccount(args?: { address: string }): Account {
const { context } = this;

if (args) {
Expand All @@ -473,7 +472,7 @@ export class Polymesh {
*
* @throws — if there is no current Account associated to the SDK instance
*/
public getAccounts(): [CurrentAccount, ...Account[]] {
public getAccounts(): Account[] {
return this.context.getAccounts();
}

Expand Down
5 changes: 1 addition & 4 deletions src/__tests__/Polymesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,10 +736,7 @@ describe('Polymesh Class', () => {

describe('method: getAccounts', () => {
test('should return the list of signer accounts associated to the SDK', async () => {
const accounts = [
entityMockUtils.getCurrentAccountInstance(),
entityMockUtils.getAccountInstance(),
];
const accounts = [entityMockUtils.getAccountInstance()];
dsMockUtils.configureMocks({
contextOptions: {
getAccounts: accounts,
Expand Down
20 changes: 18 additions & 2 deletions src/api/entities/Account.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BigNumber from 'bignumber.js';
import { difference, differenceBy, differenceWith, isEqual, union } from 'lodash';

import { Authorizations, Context, Entity, Identity } from '~/internal';
import { Authorizations, Context, Entity, Identity, leaveIdentity } from '~/internal';
import { transactions as transactionsQuery } from '~/middleware/queries';
import { Query, TransactionOrderByInput } from '~/middleware/types';
import {
Expand All @@ -13,6 +13,7 @@ import {
NumberedPortfolio,
Permissions,
PermissionType,
ProcedureMethod,
ResultSet,
SimplePermissions,
SubCallback,
Expand All @@ -29,7 +30,12 @@ import {
stringToAccountId,
txTagToExtrinsicIdentifier,
} from '~/utils/conversion';
import { assertFormatValid, calculateNextKey, isModuleOrTagMatch } from '~/utils/internal';
import {
assertFormatValid,
calculateNextKey,
createProcedureMethod,
isModuleOrTagMatch,
} from '~/utils/internal';

export interface UniqueIdentifiers {
address: string;
Expand Down Expand Up @@ -75,8 +81,18 @@ export class Account extends Entity<UniqueIdentifiers, string> {
this.address = address;
this.key = addressToKey(address, context);
this.authorizations = new Authorizations(this, context);

this.leaveIdentity = createProcedureMethod(
{ getProcedureAndArgs: () => [leaveIdentity, { account: this }] },
context
);
}

/**
* Leave the Account's Identity. This operation can only be done if the Account is a secondary key for the Identity
*/
public leaveIdentity: ProcedureMethod<void, void>;

/**
* Get the free/locked POLYX balance of the account
*
Expand Down
35 changes: 0 additions & 35 deletions src/api/entities/CurrentAccount.ts

This file was deleted.

34 changes: 31 additions & 3 deletions src/api/entities/__tests__/Account.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import BigNumber from 'bignumber.js';
import sinon from 'sinon';

import { Account, Context, Entity } from '~/internal';
import { Account, Context, Entity, TransactionQueue } from '~/internal';
import { heartbeat, transactions } from '~/middleware/queries';
import { CallIdEnum, ExtrinsicResult, ModuleIdEnum } from '~/middleware/types';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import { dsMockUtils, entityMockUtils, procedureMockUtils } from '~/testUtils/mocks';
import { Mocked } from '~/testUtils/types';
import { AccountBalance, Permissions, PermissionType, TxTags } from '~/types';
import * as utilsConversionModule from '~/utils/conversion';
import * as utilsInternalModule from '~/utils/internal';

jest.mock(
'~/base/Procedure',
require('~/testUtils/mocks/procedure').mockProcedureModule('~/base/Procedure')
);

describe('Account class', () => {
let context: Mocked<Context>;

Expand All @@ -22,6 +27,7 @@ describe('Account class', () => {
beforeAll(() => {
entityMockUtils.initMocks();
dsMockUtils.initMocks();
procedureMockUtils.initMocks();
assertFormatValidStub = sinon.stub(utilsInternalModule, 'assertFormatValid');
addressToKeyStub = sinon.stub(utilsConversionModule, 'addressToKey');

Expand All @@ -37,12 +43,13 @@ describe('Account class', () => {
afterEach(() => {
entityMockUtils.reset();
dsMockUtils.reset();
sinon.reset();
procedureMockUtils.reset();
});

afterAll(() => {
entityMockUtils.cleanup();
dsMockUtils.cleanup();
procedureMockUtils.cleanup();
sinon.restore();
});

Expand All @@ -57,6 +64,8 @@ describe('Account class', () => {
// eslint-disable-next-line no-new
new Account({ address: 'ajYMsCKsEAhEvHpeA4XqsfiA9v1CdzZPrCfS6pEfeGHW9j8' }, context);
}).toThrow();

sinon.reset();
});

describe('method: isUniqueIdentifiers', () => {
Expand Down Expand Up @@ -436,4 +445,23 @@ describe('Account class', () => {
expect(result).toEqual(true);
});
});

describe('method: leaveIdentity', () => {
test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', async () => {
const expectedQueue = ('someQueue' as unknown) as TransactionQueue<void>;

const args = {
account,
};

procedureMockUtils
.getPrepareStub()
.withArgs({ args, transformer: undefined }, context)
.resolves(expectedQueue);

const queue = await account.leaveIdentity();

expect(queue).toBe(expectedQueue);
});
});
});
79 changes: 0 additions & 79 deletions src/api/entities/__tests__/CurrentAccount.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/api/entities/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
AuthorizationRequest as AuthorizationRequestClass,
CheckpointSchedule as CheckpointScheduleClass,
CorporateAction as CorporateActionClass,
CurrentAccount as CurrentAccountClass,
DefaultPortfolio as DefaultPortfolioClass,
DefaultTrustedClaimIssuer as DefaultTrustedClaimIssuerClass,
DividendDistribution as DividendDistributionClass,
Expand All @@ -24,7 +23,6 @@ export type TickerReservation = InstanceType<typeof TickerReservationClass>;
export type AuthorizationRequest = InstanceType<typeof AuthorizationRequestClass>;
export type Identity = InstanceType<typeof IdentityClass>;
export type Account = InstanceType<typeof AccountClass>;
export type CurrentAccount = InstanceType<typeof CurrentAccountClass>;
export type Venue = InstanceType<typeof VenueClass>;
export type Instruction = InstanceType<typeof InstructionClass>;
export type Portfolio = InstanceType<typeof PortfolioClass>;
Expand Down
35 changes: 25 additions & 10 deletions src/api/procedures/__tests__/leaveIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,26 @@ describe('modifyCaCheckpoint procedure', () => {
dsMockUtils.cleanup();
});

test('should throw an error if the account is not a secondary key', async () => {
test('should throw an error if the Account is not associated to any Identity', async () => {
const proc = procedureMockUtils.getInstance<LeaveIdentityParams, void>(mockContext);
const account = entityMockUtils.getCurrentAccountInstance();
const account = entityMockUtils.getAccountInstance({
getIdentity: null,
});

let error;

try {
await prepareLeaveIdentity.call(proc, { account });
} catch (err) {
error = err;
}

expect(error.message).toBe('There is no Identity associated to this Account');
});

test('should throw an error if the Account is not a secondary key', async () => {
const proc = procedureMockUtils.getInstance<LeaveIdentityParams, void>(mockContext);
const account = entityMockUtils.getAccountInstance();

let error;

Expand All @@ -63,20 +80,18 @@ describe('modifyCaCheckpoint procedure', () => {
'identity',
'leaveIdentityAsKey'
);

dsMockUtils.configureMocks({
contextOptions: {
secondaryKeys: [
const account = entityMockUtils.getAccountInstance({
address,
getIdentity: entityMockUtils.getIdentityInstance({
getSecondaryKeys: [
({
signer: entityMockUtils.getAccountInstance({ address }),
} as unknown) as SecondaryKey,
],
},
}),
});

const proc = procedureMockUtils.getInstance<LeaveIdentityParams, void>(mockContext);
const account = entityMockUtils.getCurrentAccountInstance({
address,
});

await prepareLeaveIdentity.call(proc, { account });

Expand Down

0 comments on commit 43b703b

Please sign in to comment.