-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(issuer): add batch issuing facade
- Loading branch information
1 parent
dd0bdd5
commit 4e7f321
Showing
9 changed files
with
207 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
packages/traceability/issuer/src/blockchain-facade/CertificateBatchOperations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { BigNumber, ContractTransaction, utils } from 'ethers'; | ||
|
||
import { IBlockchainProperties } from './BlockchainProperties'; | ||
import { Certificate, IClaimData, IData } from './Certificate'; | ||
import { encodeClaimData, encodeData } from './CertificateUtils'; | ||
|
||
interface CertificateInfoInBatch extends IData { | ||
to: string; | ||
amount: BigNumber; | ||
} | ||
|
||
export async function issueCertificates( | ||
certificateInfo: CertificateInfoInBatch[], | ||
blockchainProperties: IBlockchainProperties | ||
): Promise<Certificate['id'][]> { | ||
const { issuer, registry, activeUser } = blockchainProperties; | ||
const issuerWithSigner = issuer.connect(activeUser); | ||
|
||
const data = certificateInfo.map((info) => | ||
encodeData({ | ||
generationStartTime: info.generationStartTime, | ||
generationEndTime: info.generationEndTime, | ||
deviceId: info.deviceId, | ||
metadata: info.metadata | ||
}) | ||
); | ||
|
||
const batchIssueTx = await issuerWithSigner.issueBatch( | ||
certificateInfo.map((info) => info.to), | ||
certificateInfo.map((info) => info.amount), | ||
data | ||
); | ||
|
||
const { events } = await batchIssueTx.wait(); | ||
|
||
let issuanceEvent: utils.LogDescription; | ||
|
||
for (const event of events) { | ||
try { | ||
issuanceEvent = issuer.interface.parseLog(event); | ||
} catch (e) { | ||
issuanceEvent = registry.interface.parseLog(event); | ||
} | ||
|
||
if (issuanceEvent.name === 'IssuanceBatch') { | ||
break; | ||
} | ||
} | ||
|
||
return issuanceEvent.args[2].map((id: BigNumber) => id.toNumber()); | ||
} | ||
|
||
export async function transferCertificates( | ||
certificateIds: number[], | ||
to: string, | ||
blockchainProperties: IBlockchainProperties, | ||
from?: string | ||
): Promise<ContractTransaction> { | ||
const certificatesPromises = certificateIds.map((certId) => | ||
new Certificate(certId, blockchainProperties).sync() | ||
); | ||
|
||
const { registry, activeUser } = blockchainProperties; | ||
const registryWithSigner = registry.connect(activeUser); | ||
|
||
const activeUserAddress = await activeUser.getAddress(); | ||
const fromAddress = from ?? activeUserAddress; | ||
|
||
const certificates = await Promise.all(certificatesPromises); | ||
|
||
const values = certificates.map((cert) => BigNumber.from(cert.owners[fromAddress] ?? 0)); | ||
|
||
const transferTx = await registryWithSigner.safeBatchTransferFrom( | ||
fromAddress, | ||
to, | ||
certificateIds, | ||
values, | ||
utils.randomBytes(32) // TO-DO: replace with proper data | ||
); | ||
|
||
await transferTx.wait(); | ||
|
||
return transferTx; | ||
} | ||
|
||
export async function claimCertificates( | ||
certificateIds: number[], | ||
claimData: IClaimData, | ||
blockchainProperties: IBlockchainProperties, | ||
forAddress?: string | ||
): Promise<ContractTransaction> { | ||
const certificatesPromises = certificateIds.map((certId) => | ||
new Certificate(certId, blockchainProperties).sync() | ||
); | ||
const certificates = await Promise.all(certificatesPromises); | ||
|
||
const { activeUser, registry } = blockchainProperties; | ||
const claimer = forAddress ?? (await activeUser.getAddress()); | ||
|
||
const values = certificates.map((cert) => BigNumber.from(cert.owners[claimer] ?? 0)); | ||
|
||
const encodedClaimData = encodeClaimData(claimData); | ||
const data = utils.randomBytes(32); | ||
|
||
const registryWithSigner = registry.connect(activeUser); | ||
|
||
const claimTx = await registryWithSigner.safeBatchTransferAndClaimFrom( | ||
claimer, | ||
claimer, | ||
certificateIds, | ||
values, | ||
data, | ||
certificates.map(() => encodedClaimData) | ||
); | ||
|
||
await claimTx.wait(); | ||
|
||
return claimTx; | ||
} |
Oops, something went wrong.