diff --git a/packages/traceability/issuer-api/src/pods/certificate/handlers/batch-claim-certificates.handler.ts b/packages/traceability/issuer-api/src/pods/certificate/handlers/batch-claim-certificates.handler.ts index b810234a6a..c859ab6f16 100644 --- a/packages/traceability/issuer-api/src/pods/certificate/handlers/batch-claim-certificates.handler.ts +++ b/packages/traceability/issuer-api/src/pods/certificate/handlers/batch-claim-certificates.handler.ts @@ -60,6 +60,7 @@ export class BatchClaimCertificatesHandler return { ...claim, + creationTransactionHash: cert.creationTransactionHash, schemaVersion: cert.schemaVersion }; }); diff --git a/packages/traceability/issuer-api/src/pods/certificate/handlers/batch-transfer-certificates.handler.ts b/packages/traceability/issuer-api/src/pods/certificate/handlers/batch-transfer-certificates.handler.ts index eeaf157573..91b78a7a0b 100644 --- a/packages/traceability/issuer-api/src/pods/certificate/handlers/batch-transfer-certificates.handler.ts +++ b/packages/traceability/issuer-api/src/pods/certificate/handlers/batch-transfer-certificates.handler.ts @@ -50,6 +50,7 @@ export class BatchTransferCertificatesHandler return { ...transfer, + creationTransactionHash: cert.creationTransactionHash, schemaVersion: cert.schemaVersion }; }); diff --git a/packages/traceability/issuer-api/src/pods/certificate/handlers/claim-certificate.handler.ts b/packages/traceability/issuer-api/src/pods/certificate/handlers/claim-certificate.handler.ts index a94432fb02..af51ecb627 100644 --- a/packages/traceability/issuer-api/src/pods/certificate/handlers/claim-certificate.handler.ts +++ b/packages/traceability/issuer-api/src/pods/certificate/handlers/claim-certificate.handler.ts @@ -42,7 +42,9 @@ export class ClaimCertificateHandler implements ICommandHandler { + async sync(params: ICertificateSyncParams = {}): Promise { if (this.id === null) { return this; } const { registry } = this.blockchainProperties; + if (params.creationTransactionHash) { + // This will speed up getIssuanceTransaction required for starting block + this.creationTransactionHash = params.creationTransactionHash; + } + const issuanceTransaction = await this.getIssuanceTransaction(); const certOnChain = await registry.getCertificate(this.id); @@ -501,23 +511,23 @@ export class Certificate implements ICertificate { return await registry.provider.getTransactionReceipt(this.creationTransactionHash); } - const allIssuanceLogs = await getEventsFromContract( + const allBatchIssuanceLogs = await getEventsFromContract( registry, - registry.filters.IssuanceSingle(null, null, null) + registry.filters.IssuanceBatch(null, null, null, null) ); - let issuanceLog = allIssuanceLogs.find( - (event) => event._id.toString() === this.id.toString() + let issuanceLog = allBatchIssuanceLogs.find((event) => + event._ids.map((id: BigNumber) => id.toString()).includes(this.id.toString()) ); if (!issuanceLog) { - const allBatchIssuanceLogs = await getEventsFromContract( + const allIssuanceLogs = await getEventsFromContract( registry, - registry.filters.IssuanceBatch(null, null, null, null) + registry.filters.IssuanceSingle(null, null, null) ); - issuanceLog = allBatchIssuanceLogs.find((event) => - event._ids.map((id: BigNumber) => id.toString()).includes(this.id.toString()) + issuanceLog = allIssuanceLogs.find( + (event) => event._id.toString() === this.id.toString() ); if (!issuanceLog) { diff --git a/packages/traceability/issuer/src/blockchain-facade/CertificateBatchOperations.ts b/packages/traceability/issuer/src/blockchain-facade/CertificateBatchOperations.ts index 7742cdb89e..c304fe8a1a 100644 --- a/packages/traceability/issuer/src/blockchain-facade/CertificateBatchOperations.ts +++ b/packages/traceability/issuer/src/blockchain-facade/CertificateBatchOperations.ts @@ -15,6 +15,7 @@ export interface BatchCertificateTransfer { from?: string; amount?: BigNumber; schemaVersion: CertificateSchemaVersion; + creationTransactionHash?: string; } export interface BatchCertificateClaim { @@ -24,6 +25,7 @@ export interface BatchCertificateClaim { amount?: BigNumber; claimData: IClaimData; schemaVersion: CertificateSchemaVersion; + creationTransactionHash?: string; } /** @@ -100,16 +102,20 @@ export async function transferCertificates( certificateBatch: BatchCertificateTransfer[], blockchainProperties: IBlockchainProperties ): Promise { - const certificatesPromises = certificateBatch.map((cert) => - new Certificate(cert.id, blockchainProperties, cert.schemaVersion).sync() - ); - const { registry, activeUser } = blockchainProperties; const registryWithSigner = registry.connect(activeUser); const activeUserAddress = await activeUser.getAddress(); - const certificates = await Promise.all(certificatesPromises); + const certsWithoutAmount = await Promise.all( + certificateBatch + .filter((b) => !b.amount) // we sync only certs that don't have amount configured + .map((cert) => + new Certificate(cert.id, blockchainProperties, cert.schemaVersion).sync({ + creationTransactionHash: cert.creationTransactionHash + }) + ) + ); return await registryWithSigner.safeBatchTransferFromMultiple( certificateBatch.map((cert) => cert.from ?? activeUserAddress), @@ -119,7 +125,7 @@ export async function transferCertificates( (cert) => cert.amount ?? BigNumber.from( - certificates.find((certificate) => certificate.id === cert.id).owners[ + certsWithoutAmount.find((certificate) => certificate.id === cert.id).owners[ cert.from ?? activeUserAddress ] ?? 0 ) @@ -138,16 +144,21 @@ export async function claimCertificates( certificateBatch: BatchCertificateClaim[], blockchainProperties: IBlockchainProperties ): Promise { - const certificatesPromises = certificateBatch.map((cert) => - new Certificate(cert.id, blockchainProperties, cert.schemaVersion).sync() - ); - const certificates = await Promise.all(certificatesPromises); - const { activeUser, registry } = blockchainProperties; const activeUserAddress = await activeUser.getAddress(); const registryWithSigner = registry.connect(activeUser); + const certsWithoutAmount = await Promise.all( + certificateBatch + .filter((c) => !c.amount) + .map((cert) => + new Certificate(cert.id, blockchainProperties, cert.schemaVersion).sync({ + creationTransactionHash: cert.creationTransactionHash + }) + ) + ); + return await registryWithSigner.safeBatchTransferAndClaimFromMultiple( certificateBatch.map((cert) => cert.from ?? activeUserAddress), certificateBatch.map((cert) => cert.to ?? cert.from ?? activeUserAddress), @@ -156,7 +167,7 @@ export async function claimCertificates( (cert) => cert.amount ?? BigNumber.from( - certificates.find((certificate) => certificate.id === cert.id).owners[ + certsWithoutAmount.find((certificate) => certificate.id === cert.id).owners[ cert.from ?? activeUserAddress ] ?? 0 ) diff --git a/packages/traceability/issuer/src/blockchain-facade/CertificateUtils.ts b/packages/traceability/issuer/src/blockchain-facade/CertificateUtils.ts index be1460fcb3..f2a751ca60 100644 --- a/packages/traceability/issuer/src/blockchain-facade/CertificateUtils.ts +++ b/packages/traceability/issuer/src/blockchain-facade/CertificateUtils.ts @@ -109,7 +109,9 @@ export async function getAllCertificates( ); } - return new Certificate(certificateId, blockchainProperties, schemaVersion).sync(); + return new Certificate(certificateId, blockchainProperties, schemaVersion).sync({ + creationTransactionHash: event.transactionHash + }); }); return Promise.all(certificatePromises); diff --git a/packages/traceability/issuer/src/blockchain-facade/CertificationRequest.ts b/packages/traceability/issuer/src/blockchain-facade/CertificationRequest.ts index c497c1187b..2beb27b061 100644 --- a/packages/traceability/issuer/src/blockchain-facade/CertificationRequest.ts +++ b/packages/traceability/issuer/src/blockchain-facade/CertificationRequest.ts @@ -47,7 +47,7 @@ export class CertificationRequest implements ICertificationRequestBlockchain { constructor(public id: number, public blockchainProperties: IBlockchainProperties) {} /** - * + * * * @description Uses Issuer contract to create Certificate request * @@ -84,10 +84,10 @@ export class CertificationRequest implements ICertificationRequestBlockchain { return newCertificationRequest.sync(); } - /** - * + /** + * * - * @description Returns all Certification Requests for an Issuer + * @description Returns all Certification Requests for an Issuer * */ public static async getAll( @@ -105,8 +105,8 @@ export class CertificationRequest implements ICertificationRequestBlockchain { ); } - /** - * + /** + * * * @description Retrieves Certificate data * @@ -152,8 +152,8 @@ export class CertificationRequest implements ICertificationRequestBlockchain { return this; } - /** - * + /** + * * * @description Uses Issuer contract to approve a Certificate request * @@ -177,8 +177,8 @@ export class CertificationRequest implements ICertificationRequestBlockchain { return certificateId; } - /** - * + /** + * * * @description Uses Issuer contract to revoke a Certificate request * diff --git a/packages/traceability/issuer/test/Certificate.test.ts b/packages/traceability/issuer/test/Certificate.test.ts index fddea6c221..e7e1e86c39 100644 --- a/packages/traceability/issuer/test/Certificate.test.ts +++ b/packages/traceability/issuer/test/Certificate.test.ts @@ -87,7 +87,7 @@ describe('Certificate tests', () => { ); }; - it('migrates Registry and Issuer', async () => { + before(async () => { const registry = await migrateRegistry(provider, issuerPK); const issuer = await migrateIssuer(provider, issuerPK, registry.address);