Skip to content

Commit

Permalink
refactor: for batch sync only certs without amount
Browse files Browse the repository at this point in the history
  • Loading branch information
soanvig committed Mar 8, 2022
1 parent 088e0e0 commit c8954c2
Showing 1 changed file with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,20 @@ export async function transferCertificates(
certificateBatch: BatchCertificateTransfer[],
blockchainProperties: IBlockchainProperties
): Promise<ContractTransaction> {
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),
Expand All @@ -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
)
Expand All @@ -142,18 +144,21 @@ export async function claimCertificates(
certificateBatch: BatchCertificateClaim[],
blockchainProperties: IBlockchainProperties
): Promise<ContractTransaction> {
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),
Expand All @@ -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
)
Expand Down

0 comments on commit c8954c2

Please sign in to comment.