Skip to content

Commit

Permalink
fix: optimize issuer and issuer-api transfer and claim
Browse files Browse the repository at this point in the history
  • Loading branch information
soanvig committed Mar 8, 2022
1 parent eba5c80 commit 088e0e0
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class BatchClaimCertificatesHandler

return {
...claim,
creationTransactionHash: cert.creationTransactionHash,
schemaVersion: cert.schemaVersion
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class BatchTransferCertificatesHandler

return {
...transfer,
creationTransactionHash: cert.creationTransactionHash,
schemaVersion: cert.schemaVersion
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export class ClaimCertificateHandler implements ICommandHandler<ClaimCertificate
certificate.id,
blockchainProperties,
certificate.schemaVersion
).sync();
).sync({
creationTransactionHash: certificate.creationTransactionHash
});

const claimerBalance = BigNumber.from(
(certificate.issuedPrivately
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export class SyncCertificateHandler implements IEventHandler<SyncCertificateEven
certificate.id,
blockchainProperties,
certificate.schemaVersion
).sync();
).sync({
creationTransactionHash: certificate.creationTransactionHash
});

try {
const updateResult = await this.repository.update(certificate.id, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export class TransferCertificateHandler implements ICommandHandler<TransferCerti
certificate.id,
blockchainProperties,
certificate.schemaVersion
).sync();
).sync({
creationTransactionHash: certificate.creationTransactionHash,
});

if (certificate.issuedPrivately) {
const senderBalance = BigNumber.from(
Expand Down
32 changes: 21 additions & 11 deletions packages/traceability/issuer/src/blockchain-facade/Certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export interface ICertificate extends IData {
schemaVersion: CertificateSchemaVersion;
}

export interface ICertificateSyncParams {
creationTransactionHash?: string;
}

export class Certificate implements ICertificate {
public deviceId: string;

Expand Down Expand Up @@ -179,9 +183,10 @@ export class Certificate implements ICertificate {
blockchainProperties,
schemaVersion
);
newCertificate.creationTransactionHash = txHash;

return newCertificate.sync();
return newCertificate.sync({
creationTransactionHash: txHash
});
}

/**
Expand Down Expand Up @@ -241,13 +246,18 @@ export class Certificate implements ICertificate {
* @description Retrieves current data for a Certificate
*
*/
async sync(): Promise<Certificate> {
async sync(params: ICertificateSyncParams = {}): Promise<Certificate> {
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);
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface BatchCertificateTransfer {
from?: string;
amount?: BigNumber;
schemaVersion: CertificateSchemaVersion;
creationTransactionHash?: string;
}

export interface BatchCertificateClaim {
Expand All @@ -24,6 +25,7 @@ export interface BatchCertificateClaim {
amount?: BigNumber;
claimData: IClaimData;
schemaVersion: CertificateSchemaVersion;
creationTransactionHash?: string;
}

/**
Expand Down Expand Up @@ -101,7 +103,9 @@ export async function transferCertificates(
blockchainProperties: IBlockchainProperties
): Promise<ContractTransaction> {
const certificatesPromises = certificateBatch.map((cert) =>
new Certificate(cert.id, blockchainProperties, cert.schemaVersion).sync()
new Certificate(cert.id, blockchainProperties, cert.schemaVersion).sync({
creationTransactionHash: cert.creationTransactionHash
})
);

const { registry, activeUser } = blockchainProperties;
Expand Down Expand Up @@ -139,7 +143,9 @@ export async function claimCertificates(
blockchainProperties: IBlockchainProperties
): Promise<ContractTransaction> {
const certificatesPromises = certificateBatch.map((cert) =>
new Certificate(cert.id, blockchainProperties, cert.schemaVersion).sync()
new Certificate(cert.id, blockchainProperties, cert.schemaVersion).sync({
creationTransactionHash: cert.creationTransactionHash
})
);
const certificates = await Promise.all(certificatesPromises);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class CertificationRequest implements ICertificationRequestBlockchain {

constructor(public id: number, public blockchainProperties: IBlockchainProperties) {}
/**
*
*
*
* @description Uses Issuer contract to create Certificate request
*
Expand Down Expand Up @@ -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(
Expand All @@ -105,8 +105,8 @@ export class CertificationRequest implements ICertificationRequestBlockchain {
);
}

/**
*
/**
*
*
* @description Retrieves Certificate data
*
Expand Down Expand Up @@ -152,8 +152,8 @@ export class CertificationRequest implements ICertificationRequestBlockchain {
return this;
}

/**
*
/**
*
*
* @description Uses Issuer contract to approve a Certificate request
*
Expand All @@ -177,8 +177,8 @@ export class CertificationRequest implements ICertificationRequestBlockchain {
return certificateId;
}

/**
*
/**
*
*
* @description Uses Issuer contract to revoke a Certificate request
*
Expand Down
2 changes: 1 addition & 1 deletion packages/traceability/issuer/test/Certificate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 088e0e0

Please sign in to comment.