Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,18 @@ export class RefRewardService {
return volume ?? 0;
}

async getInProgressRefRewardVolumeEur(): Promise<number> {
const { volume } = await this.rewardRepo
.createQueryBuilder('refReward')
.select('SUM(amountInEur)', 'volume')
.where('status NOT IN (:...status)', {
status: [RewardStatus.COMPLETE, RewardStatus.FAILED, RewardStatus.USER_SWITCH],
})
.getRawOne<{ volume: number }>();

return volume ?? 0;
}

// --- HELPER METHODS --- //
async updatePaidRefCredit(userIds: number[]): Promise<void> {
userIds = userIds.filter((u, j) => userIds.indexOf(u) === j).filter((i) => i); // distinct, not null
Expand Down
10 changes: 10 additions & 0 deletions src/subdomains/generic/user/models/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,16 @@ export class UserService {
.then((r) => r.paidRefCredit);
}

public async getTotalOpenRefCredit(): Promise<number> {
const { openCredit } = await this.userRepo
.createQueryBuilder('user')
.select('SUM(user.refCredit - user.paidRefCredit)', 'openCredit')
.where('user.refCredit - user.paidRefCredit > 0')
.getRawOne<{ openCredit: number }>();

return openCredit ?? 0;
}

// --- API KEY --- //
async deleteApiKey(userId: number): Promise<void> {
await this.userRepo.update(userId, { apiKeyCT: null });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class DashboardFinancialService {
ref: {
total: changes.minus?.ref?.total ?? 0,
amount: changes.minus?.ref?.amount ?? 0,
accrued: changes.minus?.ref?.accrued ?? 0,
fee: changes.minus?.ref?.fee ?? 0,
},
binance: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class FinancialChangesEntryDto {
total: number;
bank: number;
kraken: { total: number; withdraw: number; trading: number };
ref: { total: number; amount: number; fee: number };
ref: { total: number; amount: number; accrued: number; fee: number };
binance: { total: number; withdraw: number; trading: number };
blockchain: { total: number; txIn: number; txOut: number; trading: number };
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import { RefRewardService } from 'src/subdomains/core/referral/reward/services/r
import { BuyFiatService } from 'src/subdomains/core/sell-crypto/process/services/buy-fiat.service';
import { TradingOrderService } from 'src/subdomains/core/trading/services/trading-order.service';
import { TradingRuleService } from 'src/subdomains/core/trading/services/trading-rule.service';
import { UserService } from 'src/subdomains/generic/user/models/user/user.service';
import { BankTxService } from 'src/subdomains/supporting/bank-tx/bank-tx/services/bank-tx.service';
import { BankTxRepeatService } from '../../bank-tx/bank-tx-repeat/bank-tx-repeat.service';
import { BankTxReturnService } from '../../bank-tx/bank-tx-return/bank-tx-return.service';
import { createCustomBankTx } from '../../bank-tx/bank-tx/__mocks__/bank-tx.entity.mock';
import { BankService } from '../../bank/bank/bank.service';
import { PayInService } from '../../payin/services/payin.service';
import { PayoutService } from '../../payout/services/payout.service';
import { PricingService } from '../../pricing/services/pricing.service';
import { LogJobService } from '../log-job.service';
import { LogService } from '../log.service';

Expand All @@ -50,6 +52,8 @@ describe('LogJobService', () => {
let payoutService: PayoutService;
let processService: ProcessService;
let paymentBalanceService: PaymentBalanceService;
let userService: UserService;
let pricingService: PricingService;

beforeEach(async () => {
tradingRuleService = createMock<TradingRuleService>();
Expand All @@ -72,6 +76,8 @@ describe('LogJobService', () => {
payoutService = createMock<PayoutService>();
processService = createMock<ProcessService>();
paymentBalanceService = createMock<PaymentBalanceService>();
userService = createMock<UserService>();
pricingService = createMock<PricingService>();

const module: TestingModule = await Test.createTestingModule({
imports: [TestSharedModule],
Expand All @@ -97,6 +103,8 @@ describe('LogJobService', () => {
{ provide: PayoutService, useValue: payoutService },
{ provide: ProcessService, useValue: processService },
{ provide: PaymentBalanceService, useValue: paymentBalanceService },
{ provide: UserService, useValue: userService },
{ provide: PricingService, useValue: pricingService },
TestUtil.provideConfig(),
],
}).compile();
Expand Down
1 change: 1 addition & 0 deletions src/subdomains/supporting/log/dto/log.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,6 @@ type ChangeBlockchainTxBalance = {
type ChangeRefBalance = {
total: number;
amount?: number;
accrued?: number;
fee?: number;
};
4 changes: 4 additions & 0 deletions src/subdomains/supporting/log/log-job.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { PaymentLinkPaymentModule } from 'src/subdomains/core/payment-link/payme
import { ReferralModule } from 'src/subdomains/core/referral/referral.module';
import { SellCryptoModule } from 'src/subdomains/core/sell-crypto/sell-crypto.module';
import { TradingModule } from 'src/subdomains/core/trading/trading.module';
import { UserModule } from 'src/subdomains/generic/user/user.module';
import { PricingModule } from '../pricing/pricing.module';
import { BankTxModule } from '../bank-tx/bank-tx.module';
import { BankModule } from '../bank/bank.module';
import { PayInModule } from '../payin/payin.module';
Expand All @@ -31,6 +33,8 @@ import { LogModule } from './log.module';
ReferralModule,
PayoutModule,
PaymentLinkPaymentModule,
UserModule,
PricingModule,
],
controllers: [],
providers: [LogJobService],
Expand Down
14 changes: 13 additions & 1 deletion src/subdomains/supporting/log/log-job.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { LiquidityManagementPipelineService } from 'src/subdomains/core/liquidit
import { PaymentBalanceService } from 'src/subdomains/core/payment-link/services/payment-balance.service';
import { RefReward } from 'src/subdomains/core/referral/reward/ref-reward.entity';
import { RefRewardService } from 'src/subdomains/core/referral/reward/services/ref-reward.service';
import { UserService } from 'src/subdomains/generic/user/models/user/user.service';
import { PriceCurrency, PriceValidity, PricingService } from '../pricing/services/pricing.service';
import { BuyFiat } from 'src/subdomains/core/sell-crypto/process/buy-fiat.entity';
import { BuyFiatService } from 'src/subdomains/core/sell-crypto/process/services/buy-fiat.service';
import { TradingOrder } from 'src/subdomains/core/trading/entities/trading-order.entity';
Expand Down Expand Up @@ -82,6 +84,8 @@ export class LogJobService {
private readonly payoutService: PayoutService,
private readonly processService: ProcessService,
private readonly paymentBalanceService: PaymentBalanceService,
private readonly userService: UserService,
private readonly pricingService: PricingService,
) {}

@DfxCron(CronExpression.EVERY_MINUTE, { process: Process.TRADING_LOG, timeout: 1800 })
Expand Down Expand Up @@ -994,10 +998,17 @@ export class LogJobService {
);
const payoutOrderFee = this.getFeeAmount(payoutOrders.filter((p) => p.context !== PayoutOrderContext.REF_PAYOUT));

// accrued ref rewards: open credit not yet turned into RefReward records
const totalOpenRefCreditEur = await this.userService.getTotalOpenRefCredit();
const inProgressRefRewardEur = await this.refRewardService.getInProgressRefRewardVolumeEur();
const unprocessedCreditEur = Math.max(0, totalOpenRefCreditEur - inProgressRefRewardEur);
const eurChfPrice = await this.pricingService.getPrice(PriceCurrency.EUR, PriceCurrency.CHF, PriceValidity.PREFER_VALID);
const accruedRefRewardChf = eurChfPrice.convert(unprocessedCreditEur, 8);

const totalKrakenFee = krakenTxWithdrawFee + krakenTxTradingFee;
const totalBinanceFee = binanceTxWithdrawFee + binanceTxTradingFee;

const totalRefReward = refRewards + payoutOrderRefFee;
const totalRefReward = refRewards + accruedRefRewardChf + payoutOrderRefFee;
const totalTxFee = cryptoInputFee + payoutOrderFee;
const totalBlockchainFee = totalTxFee + tradingOrderFee;

Expand Down Expand Up @@ -1049,6 +1060,7 @@ export class LogJobService {
? {
total: totalRefReward,
amount: refRewards || undefined,
accrued: accruedRefRewardChf || undefined,
fee: payoutOrderRefFee || undefined,
}
: undefined,
Expand Down
Loading