Merge branch 'master' into eliminate-server-user-table

This commit is contained in:
Moriz Wahl 2022-04-20 13:08:43 +02:00 committed by GitHub
commit e3a807ed95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 44 additions and 58 deletions

View File

@ -5,33 +5,20 @@ import Decimal from 'decimal.js-light'
export class Balance {
constructor(data: {
balance: Decimal
decay: Decimal
lastBookedBalance: Decimal
balanceGDT: number | null
count: number
linkCount: number
lastBookedDate?: Date | null
}) {
this.balance = data.balance
this.decay = data.decay
this.lastBookedBalance = data.lastBookedBalance
this.balanceGDT = data.balanceGDT || null
this.count = data.count
this.linkCount = data.linkCount
this.lastBookedDate = data.lastBookedDate || null
}
// the actual balance, decay included
@Field(() => Decimal)
balance: Decimal
// the decay since the last booked balance
@Field(() => Decimal)
decay: Decimal
@Field(() => Decimal)
lastBookedBalance: Decimal
@Field(() => Number, { nullable: true })
balanceGDT: number | null
@ -42,8 +29,4 @@ export class Balance {
// the count of transaction links
@Field(() => Number)
linkCount: number
// may be null as there may be no transaction
@Field(() => Date, { nullable: true })
lastBookedDate: Date | null
}

View File

@ -1,17 +1,21 @@
import { ObjectType, Field, Int } from 'type-graphql'
import Decimal from 'decimal.js-light'
interface DecayInterface {
balance: Decimal
decay: Decimal
roundedDecay: Decimal
start: Date | null
end: Date | null
duration: number | null
}
@ObjectType()
export class Decay {
constructor(
balance: Decimal,
decay: Decimal,
start: Date | null,
end: Date | null,
duration: number | null,
) {
constructor({ balance, decay, roundedDecay, start, end, duration }: DecayInterface) {
this.balance = balance
this.decay = decay
this.roundedDecay = roundedDecay
this.start = start
this.end = end
this.duration = duration
@ -23,6 +27,9 @@ export class Decay {
@Field(() => Decimal)
decay: Decimal
@Field(() => Decimal)
roundedDecay: Decimal
@Field(() => Date, { nullable: true })
start: Date | null

View File

@ -17,21 +17,26 @@ export class Transaction {
this.balanceDate = transaction.balanceDate
if (!transaction.decayStart) {
// TODO: hot fix, we should separate decay calculation from decay graphql model
this.decay = new Decay(
transaction.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN),
new Decimal(0),
null,
null,
null,
)
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,
})
} else {
this.decay = new Decay(
transaction.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN),
transaction.decay.toDecimalPlaces(2, Decimal.ROUND_FLOOR),
transaction.decayStart,
transaction.balanceDate,
Math.round((transaction.balanceDate.getTime() - transaction.decayStart.getTime()) / 1000),
)
this.decay = new Decay({
balance: transaction.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN),
decay: transaction.decay.toDecimalPlaces(2, Decimal.ROUND_FLOOR),
// TODO: add correct value when decay must be rounded in transaction context
roundedDecay: new Decimal(0),
start: transaction.decayStart,
end: transaction.balanceDate,
duration: Math.round(
(transaction.balanceDate.getTime() - transaction.decayStart.getTime()) / 1000,
),
})
}
this.memo = transaction.memo
this.creationDate = transaction.creationDate

View File

@ -29,8 +29,6 @@ export class BalanceResolver {
if (!lastTransaction) {
return new Balance({
balance: new Decimal(0),
decay: new Decimal(0),
lastBookedBalance: new Decimal(0),
balanceGDT,
count: 0,
linkCount: 0,
@ -69,12 +67,9 @@ export class BalanceResolver {
balance: calculatedDecay.balance
.minus(sumHoldAvailableAmount.toString())
.toDecimalPlaces(2, Decimal.ROUND_DOWN), // round towards zero
decay: calculatedDecay.decay.toDecimalPlaces(2, Decimal.ROUND_FLOOR), // round towards - infinity
lastBookedBalance: lastTransaction.balance.toDecimalPlaces(2, Decimal.ROUND_DOWN),
balanceGDT,
count,
linkCount,
lastBookedDate: lastTransaction.balanceDate,
})
}
}

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),
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,

View File

@ -30,10 +30,3 @@ yarn dev_down
yarn dev_reset
```
Runs all down migrations and after this all up migrations.
## Reset DB
```
yarn dev_reset
```

View File

@ -17,6 +17,7 @@
<b-col cols="7">
<div>
{{ previousBookedBalance | GDD }}
{{ decay === '0' ? $t('math.minus') : '' }}
{{ decay | GDD }} {{ $t('math.equal') }}
<b>{{ balance | GDD }}</b>
</div>

View File

@ -5,7 +5,7 @@
const pkg = require('../../package')
const constants = {
DECAY_START_TIME: new Date('2021-05-13 17:46:31'), // GMT+0
DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0
CONFIG_VERSION: {
DEFAULT: 'DEFAULT',
EXPECTED: 'v1.2022-03-18',

View File

@ -47,12 +47,9 @@ export const transactionsQuery = gql`
transactionList(currentPage: $currentPage, pageSize: $pageSize, order: $order) {
balance {
balance
decay
lastBookedBalance
balanceGDT
count
linkCount
lastBookedDate
}
transactions {
id

View File

@ -53,11 +53,9 @@ export default {
},
data() {
return {
logo: 'img/brand/green.png',
balance: 0,
GdtBalance: 0,
transactions: [],
bookedBalance: 0,
transactionCount: 0,
transactionLinkCount: 0,
pending: true,

View File

@ -147,6 +147,7 @@
"aprox": "~",
"equal": "=",
"exclaim": "!",
"minus": "",
"pipe": "|"
},
"navigation": {

View File

@ -147,6 +147,7 @@
"aprox": "~",
"equal": "=",
"exclaim": "!",
"minus": "",
"pipe": "|"
},
"navigation": {