Skip to content

Commit

Permalink
fix: reward finalized (#285)
Browse files Browse the repository at this point in the history
* call reward from raw substrate api

* remove console

* fix format
  • Loading branch information
rubenkristian authored Feb 2, 2023
1 parent a09db94 commit b5ec933
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { Option } from '@polkadot/types';
import { OrderFulfilledCommand } from './order-fulfilled.command';
import {
DateTimeProxy,
DebioConversionService,
NotificationService,
SubstrateService,
TransactionLoggingService,
} from '@common/index';
import {
Order,
queryEthAdressByAccountId,
ServiceFlow,
} from '@debionetwork/polkadot-provider';
import { TransactionLoggingDto } from '@common/transaction-logging/dto/transaction-logging.dto';
import { NotificationDto } from '@common/notification/dto/notification.dto';
Expand All @@ -30,6 +32,7 @@ export class OrderFulfilledHandler
private readonly substrateService: SubstrateService,
private readonly notificationService: NotificationService,
private readonly dateTimeProxy: DateTimeProxy,
private readonly exchangeCacheService: DebioConversionService,
) {}

async execute(command: OrderFulfilledCommand) {
Expand Down Expand Up @@ -89,6 +92,14 @@ export class OrderFulfilledHandler
return null;
}

if (order.orderFlow === ServiceFlow.StakingRequestService) {
await this.callbackSendReward(
order,
amountToForward / currencyUnit[order.currency],
blockNumber,
);
}

await this.loggingService.create(orderLogging);

const currDateTime = this.dateTimeProxy.new();
Expand Down Expand Up @@ -123,6 +134,109 @@ export class OrderFulfilledHandler
}
}

private async callbackSendReward(
order: Order,
totalPrice: number,
blockNumber: string,
) {
try {
const exchangeFromTo = await this.exchangeCacheService.getExchangeFromTo(
order.currency.toUpperCase(),
'DAI',
);
const exchange = await this.exchangeCacheService.getExchange();
const dbioToDai = exchange ? exchange['dbioToDai'] : 1;
const daiToDbio = 1 / dbioToDai;

const rewardCustomer = totalPrice * exchangeFromTo.conversion * daiToDbio;
const rewardLab = rewardCustomer / 10;
const fixedRewardCustomer = rewardCustomer.toFixed(0);
const fixedRewardLab = rewardLab.toFixed(0);
const dbioRewardCustomer = (
BigInt(fixedRewardCustomer) * BigInt(currencyUnit.DBIO)
).toString();
const dbioRewardLab = (
BigInt(fixedRewardLab) * BigInt(currencyUnit.DBIO)
).toString();

await this.substrateService.api.tx.rewards
.rewardFunds(order.customerId, dbioRewardCustomer)
.signAndSend(this.substrateService.pair, { nonce: -1 });

// Write Logging Notification Customer Reward From Request Service
const customerNotificationInput: NotificationDto = {
role: 'Customer',
entity_type: 'Order',
entity: 'Order Fulfilled',
reference_id: order.dnaSampleTrackingId,
description: `Congrats! You’ve received ${fixedRewardCustomer} DBIO as a reward for completing the request test for [] from the service requested, kindly check your balance.`,
read: false,
created_at: this.dateTimeProxy.new(),
updated_at: this.dateTimeProxy.new(),
deleted_at: null,
from: 'Debio Network',
to: order.customerId,
block_number: blockNumber,
};

await this.callbackInsertNotificationLogging(customerNotificationInput);

// Write Logging Reward Customer Staking Request Service
const dataCustomerLoggingInput: TransactionLoggingDto = {
address: order.customerId,
amount: rewardCustomer,
created_at: new Date(),
currency: 'DBIO',
parent_id: BigInt(0),
ref_number: order.id,
transaction_type: TransactionTypeList.Reward,
transaction_status: TransactionStatusList.CustomerStakeRequestService,
};
await this.loggingService.create(dataCustomerLoggingInput);

await this.substrateService.api.tx.rewards
.rewardFunds(order.sellerId, dbioRewardLab)
.signAndSend(this.substrateService.pair, { nonce: -1 });

// Write Logging Notification Lab Reward From Request Service
const labNotificationInput: NotificationDto = {
role: 'Lab',
entity_type: 'Reward',
entity: 'Request Service Staking',
reference_id: order.dnaSampleTrackingId,
description: `Congrats! You’ve received ${fixedRewardLab} DBIO for completing the request test for [] from the service requested.`,
read: false,
created_at: this.dateTimeProxy.new(),
updated_at: this.dateTimeProxy.new(),
deleted_at: null,
from: 'Debio Network',
to: order.sellerId,
block_number: blockNumber,
};

await this.callbackInsertNotificationLogging(labNotificationInput);

// Write Logging Reward Lab
const dataLabLoggingInput: TransactionLoggingDto = {
address: order.customerId,
amount: rewardLab,
created_at: new Date(),
currency: 'DBIO',
parent_id: BigInt(0),
ref_number: order.id,
transaction_type: TransactionTypeList.Reward,
transaction_status: TransactionStatusList.LabProvideRequestedService,
};
await this.loggingService.create(dataLabLoggingInput);
} catch (err) {
console.log('error', err);
}
}

private async callbackInsertNotificationLogging(data: NotificationDto) {
await this.notificationService.insert(data);
}

private convertToDate(date: Date) {
return new Date(Number(date.toString().split(',').join('')));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {
Order,
queryOrderDetailByOrderID,
queryServiceRequestById,
RequestStatus,
sendRewards,
ServiceFlow,
ServiceRequest,
setOrderPaid,
} from '@debionetwork/polkadot-provider';
Expand All @@ -13,7 +9,6 @@ import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { NotificationDto } from '@common/notification/dto/notification.dto';
import {
DateTimeProxy,
DebioConversionService,
NotificationService,
SubstrateService,
TransactionLoggingService,
Expand All @@ -22,7 +17,6 @@ import { TransactionLoggingDto } from '@common/transaction-logging/dto/transacti
import { ServiceRequestUpdatedCommand } from './service-request-updated.command';
import { TransactionTypeList } from '@common/transaction-type/models/transaction-type.list';
import { TransactionStatusList } from '@common/transaction-status/models/transaction-status.list';
import currencyUnit from '@listeners/substrate-listener/models/currencyUnit';

@Injectable()
@CommandHandler(ServiceRequestUpdatedCommand)
Expand All @@ -34,7 +28,6 @@ export class ServiceRequestUpdatedHandler
);

constructor(
private readonly exchangeCacheService: DebioConversionService,
private readonly loggingService: TransactionLoggingService,
private readonly dateTimeProxy: DateTimeProxy,
private readonly substrateService: SubstrateService,
Expand All @@ -49,45 +42,19 @@ export class ServiceRequestUpdatedHandler
await queryServiceRequestById(this.substrateService.api, requestId)
).normalize();

if (status === RequestStatus.Claimed) {
await this.onStatusClaimed(serviceRequest, blockMetaData.blockNumber);
} else if (status === RequestStatus.Processed) {
await this.onStatusProcess(serviceRequest);
} else if (status === RequestStatus.Unstaked) {
await this.onStatusUnstaked(serviceRequest);
} else if (status === RequestStatus.WaitingForUnstaked) {
await this.onStatusWaitingForUnstaked(serviceRequest);
} else if (status === RequestStatus.Finalized) {
await this.onStatusFinalized(
serviceRequest,
blockMetaData.blockNumber.toString(),
);
}
}

async onStatusFinalized(serviceRequest: ServiceRequest, blockNumber: string) {
const orderDetail = await queryOrderDetailByOrderID(
this.substrateService.api,
serviceRequest.orderId,
);

const totalPrice = orderDetail.prices.reduce(
(acc, price) => acc + Number(price.value.split(',').join('')),
0,
);
const totalAdditionalPrice = orderDetail.additionalPrices.reduce(
(acc, price) => acc + Number(price.value.split(',').join('')),
0,
);

const amountToForward = totalPrice + totalAdditionalPrice;

if (orderDetail.orderFlow === ServiceFlow.StakingRequestService) {
await this.callbackSendReward(
orderDetail,
amountToForward / currencyUnit[orderDetail.currency],
blockNumber,
);
switch (status) {
case RequestStatus.Claimed:
await this.onStatusClaimed(serviceRequest, blockMetaData.blockNumber);
break;
case RequestStatus.Processed:
await this.onStatusProcess(serviceRequest);
break;
case RequestStatus.Unstaked:
await this.onStatusUnstaked(serviceRequest);
break;
case RequestStatus.WaitingForUnstaked:
await this.onStatusWaitingForUnstaked(serviceRequest);
break;
}
}

Expand Down Expand Up @@ -174,111 +141,4 @@ export class ServiceRequestUpdatedHandler

await this.notificationService.insert(serviceAvailableNotificationInput);
}

private async callbackInsertNotificationLogging(data: NotificationDto) {
await this.notificationService.insert(data);
}

private async callbackSendReward(
order: Order,
totalPrice: number,
blockNumber: string,
) {
const exchangeFromTo = await this.exchangeCacheService.getExchangeFromTo(
order.currency.toUpperCase(),
'DAI',
);
const exchange = await this.exchangeCacheService.getExchange();
const dbioToDai = exchange ? exchange['dbioToDai'] : 1;
const daiToDbio = 1 / dbioToDai;

const rewardCustomer = totalPrice * exchangeFromTo.conversion * daiToDbio;
const rewardLab = rewardCustomer / 10;
const fixedRewardCustomer = rewardCustomer.toFixed(0);
const fixedRewardLab = rewardLab.toFixed(0);
const dbioRewardCustomer = (
BigInt(fixedRewardCustomer) * BigInt(currencyUnit.DBIO)
).toString();
const dbioRewardLab = (
BigInt(fixedRewardLab) * BigInt(currencyUnit.DBIO)
).toString();

// Send reward to Customer
await sendRewards(
this.substrateService.api as any,
this.substrateService.pair,
order.customerId,
dbioRewardCustomer,
);

// Write Logging Notification Customer Reward From Request Service
const customerNotificationInput: NotificationDto = {
role: 'Customer',
entity_type: 'Order',
entity: 'Order Fulfilled',
reference_id: order.dnaSampleTrackingId,
description: `Congrats! You’ve received ${fixedRewardCustomer} DBIO as a reward for completing the request test for [] from the service requested, kindly check your balance.`,
read: false,
created_at: this.dateTimeProxy.new(),
updated_at: this.dateTimeProxy.new(),
deleted_at: null,
from: 'Debio Network',
to: order.customerId,
block_number: blockNumber,
};

await this.callbackInsertNotificationLogging(customerNotificationInput);

// Write Logging Reward Customer Staking Request Service
const dataCustomerLoggingInput: TransactionLoggingDto = {
address: order.customerId,
amount: rewardCustomer,
created_at: new Date(),
currency: 'DBIO',
parent_id: BigInt(0),
ref_number: order.id,
transaction_type: TransactionTypeList.Reward,
transaction_status: TransactionStatusList.CustomerStakeRequestService,
};
await this.loggingService.create(dataCustomerLoggingInput);

// Send reward to Lab
await sendRewards(
this.substrateService.api as any,
this.substrateService.pair,
order.sellerId,
dbioRewardLab,
);

// Write Logging Notification Lab Reward From Request Service
const labNotificationInput: NotificationDto = {
role: 'Lab',
entity_type: 'Reward',
entity: 'Request Service Staking',
reference_id: order.dnaSampleTrackingId,
description: `Congrats! You’ve received ${fixedRewardLab} DBIO for completing the request test for [] from the service requested.`,
read: false,
created_at: this.dateTimeProxy.new(),
updated_at: this.dateTimeProxy.new(),
deleted_at: null,
from: 'Debio Network',
to: order.sellerId,
block_number: blockNumber,
};

await this.callbackInsertNotificationLogging(labNotificationInput);

// Write Logging Reward Lab
const dataLabLoggingInput: TransactionLoggingDto = {
address: order.customerId,
amount: rewardLab,
created_at: new Date(),
currency: 'DBIO',
parent_id: BigInt(0),
ref_number: order.id,
transaction_type: TransactionTypeList.Reward,
transaction_status: TransactionStatusList.LabProvideRequestedService,
};
await this.loggingService.create(dataLabLoggingInput);
}
}

0 comments on commit b5ec933

Please sign in to comment.