Skip to content

Commit

Permalink
Merge pull request #3348 from energywebfoundation/fix/irec-issues
Browse files Browse the repository at this point in the history
fix: IREC integration issues
  • Loading branch information
soanvig authored Apr 21, 2022
2 parents 000a6a4 + e823505 commit 4403871
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 50 deletions.
85 changes: 45 additions & 40 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CommandHandler, EventBus, ICommandHandler } from '@nestjs/cqrs';
import { InjectRepository } from '@nestjs/typeorm';
import { BadRequestException, Inject } from '@nestjs/common';
import { Organization, OrganizationService } from '@energyweb/origin-backend';
import { randomBytes } from 'crypto';

import { RegistrationService } from '../../registration';
import { IREC_SERVICE, IrecService } from '../../irec';
Expand Down Expand Up @@ -129,8 +130,11 @@ export class CreateConnectionHandler implements ICommandHandler<CreateConnection

for (const account of accounts) {
if (!irecAccounts.some((a) => a.type === account)) {
const numberOfChars = 4;
const randomCode = randomBytes(numberOfChars / 2).toString('hex'); // 1 byte is 2 characters

await this.irecService.createAccountByTokens(clientId, clientSecret, tokens, {
code: `${account}-account`,
code: `${account}-${randomCode}`,
type: account,
name: `origin ${account} account`,
private: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export type IClaimData = {
periodStartDate: string;
periodEndDate: string;
purpose: string;

/** Amount in Wh */
amount?: string;
};

export interface IIrecService {
Expand Down Expand Up @@ -405,7 +408,8 @@ export class IrecService implements IIrecService {
fromUser: UserIdentifier,
toTradeAccount: string,
assetId: string,
fromTradeAccount?: string
fromTradeAccount?: string,
amount?: string
): Promise<TransactionResult> {
const fromUserClient = await this.getIrecClient(fromUser);
const fromUserConnectionInfo = await this.getConnectionInfo(fromUser);
Expand All @@ -420,7 +424,9 @@ export class IrecService implements IIrecService {

const transferItem = new ReservationItem();
transferItem.code = item.code;
transferItem.amount = item.volume;

// Optionally convert Wh to MWh
transferItem.amount = amount ? Number(amount) / 1_000_000 : item.volume;

return fromUserClient.transfer({
sender: fromUserTradeAccount,
Expand Down Expand Up @@ -456,7 +462,9 @@ export class IrecService implements IIrecService {

const claimItem = new ReservationItem();
claimItem.code = item.code;
claimItem.amount = item.volume;

// Optionally convert Wh to MWh
claimItem.amount = claimData.amount ? Number(claimData.amount) / 1_000_000 : item.volume;

return userClient.redeem({
sender: userTradeAccount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ export class ExportAssetHandler implements ICommandHandler<ExportAssetCommand>,
platformOrganization.id,
recipientTradeAccount,
irecCertificate.asset,
fromTradeAccount
fromTradeAccount,
amount
);

if (!result) {
Expand Down
Loading

0 comments on commit 4403871

Please sign in to comment.