add roundedDecay field to Decay Model. This is used to calibrate the decay since the last transaction

This commit is contained in:
Moriz Wahl 2022-04-12 22:52:51 +02:00
parent 0bcf2cbd8c
commit 55ff0ee964
4 changed files with 15 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import Decimal from 'decimal.js-light'
interface DecayInterface {
balance: Decimal
decay: Decimal
roundedDecay: Decimal
start: Date | null
end: Date | null
duration: number | null
@ -14,6 +15,7 @@ export class Decay {
constructor(data: DecayInterface) {
this.balance = data.balance
this.decay = data.decay
this.roundedDecay = data.roundedDecay
this.start = data.start
this.end = data.end
this.duration = data.duration
@ -25,6 +27,9 @@ export class Decay {
@Field(() => Decimal)
decay: Decimal
@Field(() => Decimal)
roundedDecay: Decimal
@Field(() => Date, { nullable: true })
start: Date | null

View File

@ -20,6 +20,7 @@ export class Transaction {
this.decay = new Decay({
balance: transaction.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN),
decay: new Decimal(0),
roundedDecay: new Decimal(0),
start: null,
end: null,
duration: null,
@ -28,6 +29,8 @@ export class Transaction {
this.decay = new Decay({
balance: transaction.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN),
decay: transaction.decay.toDecimalPlaces(2, Decimal.ROUND_FLOOR),
// TODO
roundedDecay: new Decimal(0),
start: transaction.decayStart,
end: transaction.balanceDate,
duration: Math.round(

View File

@ -29,6 +29,7 @@ function calculateDecay(
const decay: Decay = {
balance: amount,
decay: new Decimal(0),
roundedDecay: new Decimal(0),
start: null,
end: null,
duration: null,
@ -52,6 +53,10 @@ function calculateDecay(
decay.end = to
decay.balance = decayFormula(amount, decay.duration)
decay.decay = decay.balance.minus(amount)
decay.roundedDecay = amount
.toDecimalPlaces(2, Decimal.ROUND_DOWN)
.minus(decay.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN).toString())
.mul(-1)
return decay
}

View File

@ -68,12 +68,12 @@ const virtualDecayTransaction = (
userId: -1,
previous: -1,
typeId: TransactionTypeId.DECAY,
amount: decay.decay ? decay.decay.toDecimalPlaces(2, Decimal.ROUND_FLOOR) : new Decimal(0), // new Decimal(0), // this kinda is wrong, but helps with the frontend query
amount: decay.decay ? decay.roundedDecay : new Decimal(0), // this kinda is wrong, but helps with the frontend query
balance: decay.balance
.minus(holdAvailabeAmount.toString())
.toDecimalPlaces(2, Decimal.ROUND_DOWN),
balanceDate: time,
decay: decay.decay ? decay.decay.toDecimalPlaces(2, Decimal.ROUND_FLOOR) : new Decimal(0),
decay: decay.decay ? decay.roundedDecay : new Decimal(0),
decayStart: decay.start,
memo: '',
creationDate: null,