Skip to content

Commit

Permalink
fix: manual merge
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed May 28, 2020
2 parents 0b9c9c0 + fe87521 commit 6b5e572
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 145 deletions.
Expand Up @@ -45,7 +45,7 @@ describe('TrustedClaimIssuers class', () => {
const trustedClaimIssuers = new TrustedClaimIssuers(token, context);

const args = {
claimIssuerDids: ['someDid', 'otherDid'],
claimIssuerIdentities: ['someDid', 'otherDid'],
};

const expectedQueue = ('someQueue' as unknown) as TransactionQueue<SecurityToken>;
Expand Down
2 changes: 1 addition & 1 deletion src/api/entities/SecurityToken/__tests__/Issuance.ts
Expand Up @@ -41,7 +41,7 @@ describe('Issuance class', () => {
const args = {
issuanceData: [
{
did: 'someDid',
identity: 'someDid',
amount: new BigNumber(100),
},
],
Expand Down
6 changes: 1 addition & 5 deletions src/api/procedures/__tests__/addClaims.ts
Expand Up @@ -18,7 +18,6 @@ describe('addClaims procedure', () => {
let dateToMomentStub: sinon.SinonStub<[Date, Context], Moment>;
let identityIdToStringStub: sinon.SinonStub<[IdentityId], string>;
let stringToIdentityIdStub: sinon.SinonStub<[string, Context], IdentityId>;
let didRecordsStub: sinon.SinonStub & dsMockUtils.StubQuery;
let addTransactionStub: sinon.SinonStub;
let addClaimsBatchTransaction: PolymeshTx<[Vec<BatchAddClaimItem>]>;

Expand Down Expand Up @@ -82,9 +81,6 @@ describe('addClaims procedure', () => {
stringToIdentityIdStub.withArgs(someDid, mockContext).returns(rawSomeDid);
stringToIdentityIdStub.withArgs(otherDid, mockContext).returns(rawOtherDid);
dateToMomentStub.withArgs(expiry, mockContext).returns(rawExpiry);
didRecordsStub = dsMockUtils.createQueryStub('identity', 'didRecords', {
size: 1,
});
identityIdToStringStub.withArgs(rawOtherDid).returns(otherDid);
});

Expand All @@ -101,7 +97,7 @@ describe('addClaims procedure', () => {
});

test("should throw an error if some of the supplied target dids don't exist", () => {
didRecordsStub.size.withArgs(rawOtherDid).resolves(dsMockUtils.createMockU64(0));
dsMockUtils.configureMocks({ contextOptions: { invalidDids: [otherDid] } });

const proc = procedureMockUtils.getInstance<AddClaimsParams, void>();
proc.context = mockContext;
Expand Down
24 changes: 12 additions & 12 deletions src/api/procedures/__tests__/issueTokens.ts
Expand Up @@ -55,11 +55,11 @@ describe('issueTokens procedure', () => {
const args = {
issuanceData: [
{
did: 'someDid',
identity: 'someDid',
amount: new BigNumber(100),
},
{
did: 'anotherDid',
identity: 'anotherDid',
amount: new BigNumber(50.1234567),
},
],
Expand All @@ -86,11 +86,11 @@ describe('issueTokens procedure', () => {
const args = {
issuanceData: [
{
did: 'someDid',
identity: 'someDid',
amount: new BigNumber(100),
},
{
did: 'anotherDid',
identity: 'anotherDid',
amount: new BigNumber(50.1),
},
],
Expand All @@ -109,7 +109,7 @@ describe('issueTokens procedure', () => {
const args = {
issuanceData: [
{
did: 'someDid',
identity: 'someDid',
amount: new BigNumber(100),
},
],
Expand Down Expand Up @@ -139,7 +139,7 @@ describe('issueTokens procedure', () => {
const args = {
issuanceData: [
{
did: 'someDid',
identity: 'someDid',
amount: new BigNumber(100),
},
],
Expand All @@ -156,19 +156,19 @@ describe('issueTokens procedure', () => {
proc.context = mockContext;

return expect(prepareIssueTokens.call(proc, args)).rejects.toThrow(
`You can't issue tokens to some of the supplied identities: ${args.issuanceData[0].did} [${status}]`
`You can't issue tokens to some of the supplied identities: ${args.issuanceData[0].identity} [${status}]`
);
});

test('should add a batch issue transaction to the queue', async () => {
const args = {
issuanceData: [
{
did: 'someDid',
identity: 'someDid',
amount: new BigNumber(100),
},
{
did: 'otherDid',
identity: 'otherDid',
amount: new BigNumber(200),
},
],
Expand All @@ -181,14 +181,14 @@ describe('issueTokens procedure', () => {
const stringToIdentityIdStub = sinon.stub(utilsModule, 'stringToIdentityId');
const numberToBalanceStub = sinon.stub(utilsModule, 'numberToBalance');

args.issuanceData.forEach(({ did, amount }) => {
const identityId = dsMockUtils.createMockIdentityId(`${did}Identity`);
args.issuanceData.forEach(({ identity, amount }) => {
const identityId = dsMockUtils.createMockIdentityId(`${identity}Identity`);
const balance = dsMockUtils.createMockBalance(amount.toNumber());

investors.push(identityId);
balances.push(balance);

stringToIdentityIdStub.withArgs(did, mockContext).returns(identityId);
stringToIdentityIdStub.withArgs(identity, mockContext).returns(identityId);
numberToBalanceStub.withArgs(amount, mockContext).returns(balance);
});

Expand Down
6 changes: 1 addition & 5 deletions src/api/procedures/__tests__/revokeClaims.ts
Expand Up @@ -16,7 +16,6 @@ describe('revokeClaims procedure', () => {
let claimToMeshClaimStub: sinon.SinonStub<[Claim, Context], MeshClaim>;
let identityIdToStringStub: sinon.SinonStub<[IdentityId], string>;
let stringToIdentityIdStub: sinon.SinonStub<[string, Context], IdentityId>;
let didRecordsStub: sinon.SinonStub & dsMockUtils.StubQuery;
let addTransactionStub: sinon.SinonStub;
let revokeClaimsBatchTransaction: PolymeshTx<[Vec<BatchRevokeClaimItem>]>;

Expand Down Expand Up @@ -73,9 +72,6 @@ describe('revokeClaims procedure', () => {
claimToMeshClaimStub.withArgs(buyLockupClaim, mockContext).returns(rawBuyLockupClaim);
stringToIdentityIdStub.withArgs(someDid, mockContext).returns(rawSomeDid);
stringToIdentityIdStub.withArgs(otherDid, mockContext).returns(rawOtherDid);
didRecordsStub = dsMockUtils.createQueryStub('identity', 'didRecords', {
size: 1,
});
identityIdToStringStub.withArgs(rawOtherDid).returns(otherDid);
});

Expand All @@ -92,7 +88,7 @@ describe('revokeClaims procedure', () => {
});

test("should throw an error if some of the supplied target dids don't exist", () => {
didRecordsStub.size.withArgs(rawOtherDid).resolves(dsMockUtils.createMockU64(0));
dsMockUtils.configureMocks({ contextOptions: { invalidDids: [otherDid] } });

const proc = procedureMockUtils.getInstance<RevokeClaimsParams, void>();
proc.context = mockContext;
Expand Down
24 changes: 9 additions & 15 deletions src/api/procedures/__tests__/setTokenTrustedClaimIssuers.ts
Expand Up @@ -21,9 +21,8 @@ describe('setTokenTrustedClaimIssuers procedure', () => {
let stringToIdentityIdStub: sinon.SinonStub<[string, Context], IdentityId>;
let identityIdToStringStub: sinon.SinonStub<[IdentityId], string>;
let trustedClaimIssuerStub: sinon.SinonStub;
let didRecordsStub: sinon.SinonStub & dsMockUtils.StubQuery;
let ticker: string;
let claimIssuerDids: string[];
let claimIssuerIdentities: string[];
let rawTicker: Ticker;
let rawClaimIssuerDids: IdentityId[];
let args: Params;
Expand All @@ -36,13 +35,13 @@ describe('setTokenTrustedClaimIssuers procedure', () => {
stringToIdentityIdStub = sinon.stub(utilsModule, 'stringToIdentityId');
identityIdToStringStub = sinon.stub(utilsModule, 'identityIdToString');
ticker = 'someTicker';
claimIssuerDids = ['aDid', 'otherDid', 'differentDid'];
claimIssuerIdentities = ['aDid', 'otherDid', 'differentDid'];
rawTicker = dsMockUtils.createMockTicker(ticker);
rawClaimIssuerDids = claimIssuerDids.map(dsMockUtils.createMockIdentityId);
rawClaimIssuerDids = claimIssuerIdentities.map(dsMockUtils.createMockIdentityId);
/* eslint-enable @typescript-eslint/camelcase */
args = {
ticker,
claimIssuerDids,
claimIssuerIdentities,
};
});

Expand All @@ -61,9 +60,6 @@ describe('setTokenTrustedClaimIssuers procedure', () => {
returnValue: [],
}
);
didRecordsStub = dsMockUtils.createQueryStub('identity', 'didRecords', {
size: 1,
});

removeDefaultTrustedClaimIssuersBatchTransaction = dsMockUtils.createTxStub(
'complianceManager',
Expand All @@ -77,7 +73,7 @@ describe('setTokenTrustedClaimIssuers procedure', () => {
mockContext = dsMockUtils.getContextInstance();

stringToTickerStub.withArgs(ticker, mockContext).returns(rawTicker);
claimIssuerDids.forEach((did, index) => {
claimIssuerIdentities.forEach((did, index) => {
stringToIdentityIdStub.withArgs(did, mockContext).returns(rawClaimIssuerDids[index]);
identityIdToStringStub.withArgs(rawClaimIssuerDids[index]).returns(did);
});
Expand Down Expand Up @@ -106,15 +102,13 @@ describe('setTokenTrustedClaimIssuers procedure', () => {
});

test("should throw an error if some of the supplied dids don't exist", () => {
const nonExistentDidIndex = 1;
didRecordsStub.size
.withArgs(rawClaimIssuerDids[nonExistentDidIndex])
.resolves(dsMockUtils.createMockU64(0));
const nonExistendDid = claimIssuerIdentities[1];
dsMockUtils.configureMocks({ contextOptions: { invalidDids: [nonExistendDid] } });
const proc = procedureMockUtils.getInstance<Params, SecurityToken>();
proc.context = mockContext;

return expect(prepareSetTokenTrustedClaimIssuers.call(proc, args)).rejects.toThrow(
`Some of the supplied identity IDs do not exist: ${claimIssuerDids[nonExistentDidIndex]}`
`Some of the supplied identity IDs do not exist: ${nonExistendDid}`
);
});

Expand Down Expand Up @@ -168,7 +162,7 @@ describe('setTokenTrustedClaimIssuers procedure', () => {

const result = await prepareSetTokenTrustedClaimIssuers.call(proc, {
...args,
claimIssuerDids: [],
claimIssuerIdentities: [],
});

sinon.assert.calledWith(
Expand Down
44 changes: 11 additions & 33 deletions src/api/procedures/addClaims.ts
@@ -1,11 +1,11 @@
import { Moment } from '@polkadot/types/interfaces';
import { chunk } from 'lodash';
import { Claim as MeshClaim } from 'polymesh-types/types';

import { Identity } from '~/api/entities';
import { PolymeshError, Procedure } from '~/base';
import { IdentityId } from '~/polkadot';
import { ClaimTargets, ClaimType, ErrorCode, Role, RoleType } from '~/types';
import { claimToMeshClaim, dateToMoment, identityIdToString, stringToIdentityId } from '~/utils';
import { claimToMeshClaim, dateToMoment, stringToIdentityId, valueToDid } from '~/utils';

interface AddClaimItem {
target: IdentityId;
Expand All @@ -28,53 +28,31 @@ export async function prepareAddClaims(

const {
context: {
polymeshApi: {
tx,
query: {
identity: { didRecords },
},
},
polymeshApi: { tx },
},
context,
} = this;

const addClaimItems: AddClaimItem[] = [];
const allTargets: (string | Identity)[] = [];

claims.forEach(({ targets, claim, expiry }) => {
targets.forEach(target =>
targets.forEach(target => {
allTargets.push(target);
addClaimItems.push({
target: stringToIdentityId(target, context),
target: stringToIdentityId(valueToDid(target), context),
claim: claimToMeshClaim(claim, context),
expiry: expiry ? dateToMoment(expiry, context) : null,
})
);
});
});
});

const addClaimItemsChunks = chunk(addClaimItems, 10);

const nonExistentDids: IdentityId[] = [];

await Promise.all(
addClaimItemsChunks.map(async addClaimItemsChunk => {
// TODO: queryMulti
const sizes = await Promise.all(
addClaimItemsChunk.map(({ target }) => didRecords.size(target))
);

sizes.forEach((size, index) => {
if (size.isZero()) {
nonExistentDids.push(addClaimItemsChunk[index].target);
}
});
})
);
const nonExistentDids: string[] = await context.getInvalidDids(allTargets);

if (nonExistentDids.length) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: `Some of the supplied identity IDs do not exist: ${nonExistentDids
.map(identityIdToString)
.join(', ')}`,
message: `Some of the supplied identity IDs do not exist: ${nonExistentDids.join(', ')}`,
});
}

Expand Down
13 changes: 7 additions & 6 deletions src/api/procedures/issueTokens.ts
Expand Up @@ -5,7 +5,7 @@ import { SecurityToken } from '~/api/entities';
import { PolymeshError, Procedure } from '~/base';
import { IdentityId } from '~/polkadot';
import { ErrorCode, IssuanceData, Role, RoleType, TransferStatus } from '~/types';
import { numberToBalance, stringToIdentityId, stringToTicker } from '~/utils';
import { numberToBalance, stringToIdentityId, stringToTicker, valueToDid } from '~/utils';
import { MAX_DECIMALS, MAX_TOKEN_AMOUNT } from '~/utils/constants';

export interface IssueTokensParams {
Expand Down Expand Up @@ -78,17 +78,18 @@ export async function prepareIssueTokens(
issuanceDataItemsChunks.map(async issuanceDataItemsChunk => {
// TODO: queryMulti
const transferStatuses = await Promise.all(
issuanceDataItemsChunk.map(({ did, amount }) =>
securityToken.transfers.canMint({ to: did, amount })
issuanceDataItemsChunk.map(({ identity, amount }) =>
securityToken.transfers.canMint({ to: identity, amount })
)
);

transferStatuses.forEach((canTransfer, index) => {
investors.push(stringToIdentityId(issuanceDataItemsChunk[index].did, context));
balances.push(numberToBalance(issuanceDataItemsChunk[index].amount, context));
const { identity, amount } = issuanceDataItemsChunk[index];
investors.push(stringToIdentityId(valueToDid(identity), context));
balances.push(numberToBalance(amount, context));

if (canTransfer !== TransferStatus.Success) {
canNotMintDids.push(`${issuanceDataItemsChunk[index].did} [${canTransfer}]`);
canNotMintDids.push(`${identity} [${canTransfer}]`);
}
});
})
Expand Down

0 comments on commit 6b5e572

Please sign in to comment.