Skip to content

Commit

Permalink
feat: add batchSize to document extrinsics
Browse files Browse the repository at this point in the history
  • Loading branch information
monitz87 committed May 28, 2020
1 parent e218003 commit 360a256
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
8 changes: 7 additions & 1 deletion src/api/procedures/__tests__/createSecurityToken.ts
Expand Up @@ -266,7 +266,13 @@ describe('createSecurityToken procedure', () => {

const result = await prepareCreateSecurityToken.call(proc, { ...args, documents });

sinon.assert.calledWith(addTransactionStub, tx, { isCritical: false }, rawTicker, rawDocuments);
sinon.assert.calledWith(
addTransactionStub,
tx,
{ isCritical: false, batchSize: rawDocuments.length },
rawTicker,
rawDocuments
);

expect(result).toMatchObject(new SecurityToken({ ticker }, mockContext));
});
Expand Down
8 changes: 4 additions & 4 deletions src/api/procedures/__tests__/setTokenDocuments.ts
Expand Up @@ -144,14 +144,14 @@ describe('setTokenDocuments procedure', () => {
sinon.assert.calledWith(
addTransactionStub.firstCall,
removeDocumentsTransaction,
{},
{ batchSize: 1 },
rawTicker,
[links[0].link_id]
);
sinon.assert.calledWith(
addTransactionStub.secondCall,
addDocumentsTransaction,
{},
{ batchSize: rawDocuments.length },
rawTicker,
rawDocuments
);
Expand All @@ -170,7 +170,7 @@ describe('setTokenDocuments procedure', () => {
sinon.assert.calledWith(
addTransactionStub.firstCall,
addDocumentsTransaction,
{},
{ batchSize: rawDocuments.length },
rawTicker,
rawDocuments
);
Expand All @@ -187,7 +187,7 @@ describe('setTokenDocuments procedure', () => {
sinon.assert.calledWith(
addTransactionStub.firstCall,
removeDocumentsTransaction,
{},
{ batchSize: 1 },
rawTicker,
[links[0].link_id]
);
Expand Down
14 changes: 11 additions & 3 deletions src/api/procedures/createSecurityToken.ts
@@ -1,5 +1,6 @@
import BigNumber from 'bignumber.js';
import { AssetIdentifier, IdentifierType } from 'polymesh-types/types';
import { chunk } from 'lodash';
import { AssetIdentifier, IdentifierType, TxTags } from 'polymesh-types/types';

import { SecurityToken, TickerReservation } from '~/api/entities';
import { PolymeshError, Procedure } from '~/base';
Expand All @@ -23,6 +24,7 @@ import {
tokenIdentifierTypeToIdentifierType,
tokenTypeToAssetType,
} from '~/utils';
import { MAX_BATCH_ELEMENTS } from '~/utils/constants';

export interface CreateSecurityTokenParams {
name: string;
Expand Down Expand Up @@ -109,8 +111,14 @@ export async function prepareCreateSecurityToken(

if (documents) {
const rawDocuments = documents.map(document => tokenDocumentToDocument(document, context));

this.addTransaction(tx.asset.addDocuments, { isCritical: false }, rawTicker, rawDocuments);
chunk(rawDocuments, MAX_BATCH_ELEMENTS[TxTags.asset.AddDocuments]).forEach(rawDocumentChunk => {
this.addTransaction(
tx.asset.addDocuments,
{ isCritical: false, batchSize: rawDocumentChunk.length },
rawTicker,
rawDocumentChunk
);
});
}

return new SecurityToken({ ticker }, context);
Expand Down
23 changes: 19 additions & 4 deletions src/api/procedures/setTokenDocuments.ts
@@ -1,6 +1,6 @@
import { u64 } from '@polkadot/types';
import { differenceWith } from 'lodash';
import { Document } from 'polymesh-types/types';
import { chunk, differenceWith } from 'lodash';
import { Document, TxTags } from 'polymesh-types/types';

import { SecurityToken } from '~/api/entities';
import { PolymeshError, Procedure } from '~/base';
Expand All @@ -13,6 +13,7 @@ import {
tickerToDid,
tokenDocumentToDocument,
} from '~/utils';
import { MAX_BATCH_ELEMENTS } from '~/utils/constants';

export interface SetTokenDocumentsParams {
documents: TokenDocument[];
Expand Down Expand Up @@ -71,11 +72,25 @@ export async function prepareSetTokenDocuments(
const rawTicker = stringToTicker(ticker, context);

if (currentDocIds.length) {
this.addTransaction(tx.asset.removeDocuments, {}, rawTicker, currentDocIds);
chunk(currentDocIds, MAX_BATCH_ELEMENTS[TxTags.asset.RemoveDocuments]).forEach(docIdChunk => {
this.addTransaction(
tx.asset.removeDocuments,
{ batchSize: docIdChunk.length },
rawTicker,
docIdChunk
);
});
}

if (rawDocuments.length) {
this.addTransaction(tx.asset.addDocuments, {}, rawTicker, rawDocuments);
chunk(rawDocuments, MAX_BATCH_ELEMENTS[TxTags.asset.AddDocuments]).forEach(rawDocumentChunk => {
this.addTransaction(
tx.asset.addDocuments,
{ batchSize: rawDocumentChunk.length },
rawTicker,
rawDocumentChunk
);
});
}

return new SecurityToken({ ticker }, context);
Expand Down
4 changes: 3 additions & 1 deletion src/base/Procedure.ts
Expand Up @@ -147,7 +147,9 @@ export class Procedure<Args extends unknown = void, ReturnValue extends unknown

try {
const rawFee = await protocolFee.baseFees(stringToProtocolOp(protocolOp, context));
fee = balanceToBigNumber(rawFee).multipliedBy(coefficient);
fee = balanceToBigNumber(rawFee)
.multipliedBy(coefficient)
.multipliedBy(batchSize || 1);
} catch (err) {
fee = new BigNumber(0);
}
Expand Down
2 changes: 2 additions & 0 deletions src/utils/constants.ts
Expand Up @@ -10,6 +10,8 @@ export const HARVESTER_ENDPOINT =
'https://24pha89jxg.execute-api.us-east-1.amazonaws.com/dev/graphql';
export const MAX_BATCH_ELEMENTS = {
[TxTags.asset.BatchIssue]: 500,
[TxTags.asset.AddDocuments]: 500,
[TxTags.asset.RemoveDocuments]: 500,
[TxTags.identity.BatchAcceptAuthorization]: 500,
[TxTags.identity.BatchRemoveAuthorization]: 500,
[TxTags.identity.AddClaimsBatch]: 500,
Expand Down

0 comments on commit 360a256

Please sign in to comment.