From c8954c2f7c7b14b1192818bbc7d9a285cf553143 Mon Sep 17 00:00:00 2001 From: Soanvig Date: Tue, 8 Mar 2022 11:05:57 +0100 Subject: [PATCH] refactor: for batch sync only certs without amount --- .../CertificateBatchOperations.ts | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/packages/traceability/issuer/src/blockchain-facade/CertificateBatchOperations.ts b/packages/traceability/issuer/src/blockchain-facade/CertificateBatchOperations.ts index 4c0367d44c..c304fe8a1a 100644 --- a/packages/traceability/issuer/src/blockchain-facade/CertificateBatchOperations.ts +++ b/packages/traceability/issuer/src/blockchain-facade/CertificateBatchOperations.ts @@ -102,18 +102,20 @@ export async function transferCertificates( certificateBatch: BatchCertificateTransfer[], blockchainProperties: IBlockchainProperties ): Promise { - const certificatesPromises = certificateBatch.map((cert) => - new Certificate(cert.id, blockchainProperties, cert.schemaVersion).sync({ - creationTransactionHash: cert.creationTransactionHash - }) - ); - 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), @@ -123,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 ) @@ -142,18 +144,21 @@ export async function claimCertificates( certificateBatch: BatchCertificateClaim[], blockchainProperties: IBlockchainProperties ): Promise { - const certificatesPromises = certificateBatch.map((cert) => - new Certificate(cert.id, blockchainProperties, cert.schemaVersion).sync({ - creationTransactionHash: cert.creationTransactionHash - }) - ); - 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), @@ -162,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 )