Skip to content

Commit

Permalink
feat: 馃幐 getPendingProposals on Account if it is MultiSig
Browse files Browse the repository at this point in the history
  • Loading branch information
sansan committed Apr 19, 2024
1 parent 2fcea85 commit d1951ce
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
54 changes: 53 additions & 1 deletion src/api/entities/Account/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
AccountKeyType,
HistoricPolyxTransaction,
} from '~/api/entities/Account/types';
import { Account, Context, Entity } from '~/internal';
import { Account, Context, Entity, PolymeshError } from '~/internal';
import { extrinsicsByArgs } from '~/middleware/queries';
import { CallIdEnum, ExtrinsicsOrderBy, ModuleIdEnum } from '~/middleware/types';
import { dsMockUtils, entityMockUtils, procedureMockUtils } from '~/testUtils/mocks';
import {
createMockAccountId,
createMockCall,
createMockIdentityId,
createMockOption,
createMockPermissions,
Expand All @@ -21,6 +22,7 @@ import { Mocked } from '~/testUtils/types';
import {
AccountBalance,
Balance,
ErrorCode,
ModuleName,
Permissions,
PermissionType,
Expand Down Expand Up @@ -946,4 +948,54 @@ describe('Account class', () => {
expect(result).toEqual(fakeResult);
});
});

describe('method: getPendingProposals', () => {
it('should return the MultiSig the Account is a signer for', async () => {
dsMockUtils.createQueryMock('identity', 'keyRecords', {
returnValue: dsMockUtils.createMockOption(
dsMockUtils.createMockKeyRecord({
MultiSigSignerKey: dsMockUtils.createMockAccountId('multiAddress'),
})
),
});

account = new Account({ address }, context);

const id = new BigNumber(0);

dsMockUtils.createQueryMock('multiSig', 'proposals', {
entries: [
[
[dsMockUtils.createMockAccountId(address), dsMockUtils.createMockU64(id)],
createMockOption(createMockCall()),
],
],
});

const result = await account.getPendingProposals();

expect(result).toMatchObject([
{ multiSig: expect.objectContaining({ address: 'multiAddress' }), id },
]);
});

it('should throw an error if Account is not part of MultiSig', async () => {
dsMockUtils.createQueryMock('identity', 'keyRecords', {
returnValue: dsMockUtils.createMockOption(
dsMockUtils.createMockKeyRecord({
PrimaryKey: dsMockUtils.createMockAccountId(),
})
),
});

account = new Account({ address }, context);

const expectedError = new PolymeshError({
code: ErrorCode.UnmetPrerequisite,
message: 'This Account is not a signer on any MultiSig',
});

return expect(account.getPendingProposals()).rejects.toThrow(expectedError);
});
});
});
29 changes: 28 additions & 1 deletion src/api/entities/Account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ import {
HistoricPolyxTransaction,
} from '~/api/entities/Account/types';
import { Subsidies } from '~/api/entities/Subsidies';
import { Authorizations, Context, Entity, Identity, MultiSig, PolymeshError } from '~/internal';
import {
Authorizations,
Context,
Entity,
Identity,
MultiSig,
MultiSigProposal,
PolymeshError,
} from '~/internal';
import { extrinsicsByArgs } from '~/middleware/queries';
import { CallIdEnum, ExtrinsicsOrderBy, ModuleIdEnum, Query } from '~/middleware/types';
import {
Expand Down Expand Up @@ -551,4 +559,23 @@ export class Account extends Entity<UniqueIdentifiers, string> {
...filters,
});
}

/**
* Returns pending MultiSig proposals for this Account
*
* @note uses the middleware
* @throws if the Account is not a signer on any MultiSig
*/
public async getPendingProposals(): Promise<MultiSigProposal[]> {
const multiSig = await this.getMultiSig();

if (!multiSig) {
throw new PolymeshError({
code: ErrorCode.UnmetPrerequisite,
message: 'This Account is not a signer on any MultiSig',
});
}

return multiSig.getProposals();
}
}

0 comments on commit d1951ce

Please sign in to comment.