Skip to content

Commit

Permalink
feat(origin-247-certificate): Update issuer-api, make batch methods e…
Browse files Browse the repository at this point in the history
…asier to use

BREAKING CHANGE: now batch methods (transfer and claim) accept array of transfers/claims
  • Loading branch information
Mateusz Koteja committed Aug 25, 2021
1 parent fb5b07f commit 574d36b
Show file tree
Hide file tree
Showing 9 changed files with 395 additions and 120 deletions.
352 changes: 289 additions & 63 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions packages/origin-247-certificate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@ Which allows to run migration required by the module.
```ts
import { entities as IssuerEntities } from '@energyweb/issuer-api';

const [Certificate, , BlockchainProperties] = IssuerEntities;

[...]

TypeOrmModule.forRoot({
...
entities: [
...,
Certificate,
BlockchainProperties,
...IssuerEntities,
],
...
});
Expand Down
12 changes: 6 additions & 6 deletions packages/origin-247-certificate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
"typeorm": "0.2.34"
},
"devDependencies": {
"@energyweb/issuer": "3.2.1-alpha.1626693085.0",
"@energyweb/issuer-api": "0.2.1-alpha.1626693085.0",
"@energyweb/origin-backend-utils": "1.5.2-alpha.1626693085.0",
"@energyweb/utils-general": "11.0.3-alpha.1626693085.0",
"@energyweb/issuer": "3.2.1-alpha.1629812330.0",
"@energyweb/issuer-api": "0.2.1-alpha.1629812330.0",
"@energyweb/origin-backend-utils": "1.5.2-alpha.1629812330.0",
"@energyweb/utils-general": "11.0.3-alpha.1629812330.0",
"@nestjs/passport": "7.1.5",
"@nestjs/platform-express": "7.6.18",
"@nestjs/testing": "7.6.18",
Expand All @@ -46,8 +46,8 @@
"wait-on": "5.3.0"
},
"peerDependencies": {
"@energyweb/issuer": "3.2.1-alpha.1626693085.0",
"@energyweb/issuer-api": "0.2.1-alpha.1626693085.0"
"@energyweb/issuer": "3.2.1-alpha.1629812330.0",
"@energyweb/issuer-api": "0.2.1-alpha.1629812330.0"
},
"jest": {
"moduleFileExtensions": [
Expand Down
23 changes: 12 additions & 11 deletions packages/origin-247-certificate/src/blockchain-actions.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Logger } from '@nestjs/common';
import { Job } from 'bull';
import { CommandBus } from '@nestjs/cqrs';
import { BlockchainAction, BlockchainActionType } from './types';
import { BigNumber } from 'ethers';
import {
ClaimCertificateCommand,
IssueCertificateCommand,
Expand Down Expand Up @@ -110,12 +111,12 @@ export class BlockchainActionsProcessor {

return await this.commandBus.execute(
new BatchTransferCertificatesCommand(
batchTransferParams.certificates.map((c) => ({
id: c.certificateId,
amount: c.energyValue
})),
batchTransferParams.toAddress,
batchTransferParams.fromAddress
batchTransferParams.transfers.map((t) => ({
id: t.certificateId,
to: t.toAddress,
from: t.fromAddress,
amount: t.energyValue ? BigNumber.from(t.energyValue) : undefined
}))
)
);

Expand All @@ -127,12 +128,12 @@ export class BlockchainActionsProcessor {

return await this.commandBus.execute(
new BatchClaimCertificatesCommand(
batchClaimParams.certificates.map((c) => ({
batchClaimParams.claims.map((c) => ({
id: c.certificateId,
amount: c.energyValue
})),
batchClaimParams.claimData,
batchClaimParams.forAddress
from: c.forAddress,
amount: c.energyValue ? BigNumber.from(c.energyValue) : undefined,
claimData: c.claimData
}))
)
);
}
Expand Down
7 changes: 5 additions & 2 deletions packages/origin-247-certificate/src/certificate.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Module } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { BullModule } from '@nestjs/bull';
import { CertificateModule as IssuerCertificateModule } from '@energyweb/issuer-api';
import { IssuerModule } from '@energyweb/issuer-api';

import { CertificateService } from './certificate.service';
import { BlockchainActionsProcessor } from './blockchain-actions.processor';
Expand All @@ -16,7 +16,10 @@ const serviceProvider = {
providers: [serviceProvider, BlockchainActionsProcessor],
exports: [serviceProvider],
imports: [
IssuerCertificateModule,
IssuerModule.register({
enableCertificationRequest: false,
enableTransactionLogging: true
}),
CqrsModule,
BullModule.registerQueue({
name: 'blockchain-actions',
Expand Down
16 changes: 9 additions & 7 deletions packages/origin-247-certificate/src/certificate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import {
BlockchainActionType,
BlockchainAction,
IIssuedCertificate,
IIssueCommandParams,
IBatchClaimCommand,
IBatchTransferCommand
IIssueCommandParams
} from './types';

const jobOptions = {
Expand Down Expand Up @@ -117,10 +115,12 @@ export class CertificateService<T = null> {
return result;
}

public async batchClaim(command: IBatchClaimCommand): Promise<ISuccessResponse> {
public async batchClaim(command: IClaimCommand[]): Promise<ISuccessResponse> {
const job = await this.blockchainActionsQueue.add(
{
payload: command,
payload: {
claims: command
},
type: BlockchainActionType.BatchClaim
},
jobOptions
Expand All @@ -131,10 +131,12 @@ export class CertificateService<T = null> {
return result;
}

public async batchTransfer(command: IBatchTransferCommand): Promise<ISuccessResponse> {
public async batchTransfer(command: ITransferCommand[]): Promise<ISuccessResponse> {
const job = await this.blockchainActionsQueue.add(
{
payload: command,
payload: {
transfers: command
},
type: BlockchainActionType.BatchTransfer
},
jobOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,32 +129,16 @@ export class CertificateForUnitTestsService<T> implements PublicPart<Certificate
return result.map((r) => r.id);
}

public async batchClaim(command: IBatchClaimCommand): Promise<ISuccessResponse> {
const result = await Promise.all(
command.certificates.map((certificate) =>
this.claim({
...certificate,
forAddress: command.forAddress,
claimData: command.claimData
})
)
);
public async batchClaim(command: IClaimCommand[]): Promise<ISuccessResponse> {
const result = await Promise.all(command.map((claim) => this.claim(claim)));

return {
success: result.every((r) => r.success)
};
}

public async batchTransfer(command: IBatchTransferCommand): Promise<ISuccessResponse> {
const result = await Promise.all(
command.certificates.map((certificate) =>
this.transfer({
...certificate,
fromAddress: command.fromAddress,
toAddress: command.toAddress
})
)
);
public async batchTransfer(command: ITransferCommand[]): Promise<ISuccessResponse> {
const result = await Promise.all(command.map((transfer) => this.transfer(transfer)));

return {
success: result.every((r) => r.success)
Expand Down
12 changes: 6 additions & 6 deletions packages/origin-247-certificate/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { IClaimData, IClaim } from '@energyweb/issuer';
import {
BatchCertificateClaim,
BatchCertificateTransfer
} from '@energyweb/issuer/dist/js/src/blockchain-facade/CertificateBatchOperations';

export type UnixTimestamp = number;

Expand Down Expand Up @@ -71,15 +75,11 @@ export interface IBatchIssueCommand<T> {
}

export interface IBatchClaimCommand {
certificates: { certificateId: number; energyValue: string }[];
claimData: IClaimData;
forAddress: string;
claims: IClaimCommand[];
}

export interface IBatchTransferCommand {
certificates: { certificateId: number; energyValue: string }[];
fromAddress: string;
toAddress: string;
transfers: ITransferCommand[];
}

export interface ISuccessResponse {
Expand Down
64 changes: 63 additions & 1 deletion packages/origin-247-certificate/test/certificate.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ describe('Certificate service', () => {

const result = await certificateService.claim({
certificateId: certificate.id,
claimData: {},
claimData: {
beneficiary: '',
countryCode: '',
location: '',
periodEndDate: '',
periodStartDate: '',
purpose: ''
},
forAddress: userWallet.address,
energyValue: '50'
});
Expand Down Expand Up @@ -131,6 +138,61 @@ describe('Certificate service', () => {
});
});

it.only('allows to batch issue, batch transfer, and batch claim certificate', async () => {
const [certificateId] = await certificateService.batchIssue([
{
deviceId: 'd1',
energyValue: '100',
fromTime: new Date(),
toTime: new Date(),
metadata: null,
toAddress: userWallet.address,
userId: userWallet.address
}
]);

await transactionTime();

await certificateService.batchTransfer([
{
certificateId,
fromAddress: userWallet.address,
toAddress: user2Wallet.address,
energyValue: '50'
}
]);

await transactionTime();

await certificateService.batchClaim([
{
certificateId,
claimData: {
beneficiary: '',
countryCode: '',
location: '',
periodEndDate: '',
periodStartDate: '',
purpose: ''
},
forAddress: user2Wallet.address,
energyValue: '50'
}
]);

await transactionTime();

const certificate = await certificateService.getById(certificateId);

expect(certificate?.owners).toEqual({
[userWallet.address]: '50',
[user2Wallet.address]: '0'
});
expect(certificate?.claimers).toEqual({
[user2Wallet.address]: '50'
});
});

it('Properly parses metadata', async () => {
const certificateService2 = (certificateService as any) as CertificateService<{
custom: string;
Expand Down

0 comments on commit 574d36b

Please sign in to comment.