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 { export class Balance {
constructor(data: { constructor(data: {
balance: Decimal balance: Decimal
decay: Decimal
lastBookedBalance: Decimal
balanceGDT: number | null balanceGDT: number | null
count: number count: number
linkCount: number linkCount: number
lastBookedDate?: Date | null
}) { }) {
this.balance = data.balance this.balance = data.balance
this.decay = data.decay
this.lastBookedBalance = data.lastBookedBalance
this.balanceGDT = data.balanceGDT || null this.balanceGDT = data.balanceGDT || null
this.count = data.count this.count = data.count
this.linkCount = data.linkCount this.linkCount = data.linkCount
this.lastBookedDate = data.lastBookedDate || null
} }
// the actual balance, decay included // the actual balance, decay included
@Field(() => Decimal) @Field(() => Decimal)
balance: Decimal balance: Decimal
// the decay since the last booked balance
@Field(() => Decimal)
decay: Decimal
@Field(() => Decimal)
lastBookedBalance: Decimal
@Field(() => Number, { nullable: true }) @Field(() => Number, { nullable: true })
balanceGDT: number | null balanceGDT: number | null
@ -42,8 +29,4 @@ export class Balance {
// the count of transaction links // the count of transaction links
@Field(() => Number) @Field(() => Number)
linkCount: 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 { ObjectType, Field, Int } from 'type-graphql'
import Decimal from 'decimal.js-light' import Decimal from 'decimal.js-light'
interface DecayInterface {
balance: Decimal
decay: Decimal
roundedDecay: Decimal
start: Date | null
end: Date | null
duration: number | null
}
@ObjectType() @ObjectType()
export class Decay { export class Decay {
constructor( constructor({ balance, decay, roundedDecay, start, end, duration }: DecayInterface) {
balance: Decimal,
decay: Decimal,
start: Date | null,
end: Date | null,
duration: number | null,
) {
this.balance = balance this.balance = balance
this.decay = decay this.decay = decay
this.roundedDecay = roundedDecay
this.start = start this.start = start
this.end = end this.end = end
this.duration = duration this.duration = duration
@ -23,6 +27,9 @@ export class Decay {
@Field(() => Decimal) @Field(() => Decimal)
decay: Decimal decay: Decimal
@Field(() => Decimal)
roundedDecay: Decimal
@Field(() => Date, { nullable: true }) @Field(() => Date, { nullable: true })
start: Date | null start: Date | null

View File

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

View File

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

View File

@ -29,6 +29,7 @@ function calculateDecay(
const decay: Decay = { const decay: Decay = {
balance: amount, balance: amount,
decay: new Decimal(0), decay: new Decimal(0),
roundedDecay: new Decimal(0),
start: null, start: null,
end: null, end: null,
duration: null, duration: null,
@ -52,6 +53,10 @@ function calculateDecay(
decay.end = to decay.end = to
decay.balance = decayFormula(amount, decay.duration) decay.balance = decayFormula(amount, decay.duration)
decay.decay = decay.balance.minus(amount) 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 return decay
} }

View File

@ -68,12 +68,12 @@ const virtualDecayTransaction = (
userId: -1, userId: -1,
previous: -1, previous: -1,
typeId: TransactionTypeId.DECAY, 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 balance: decay.balance
.minus(holdAvailabeAmount.toString()) .minus(holdAvailabeAmount.toString())
.toDecimalPlaces(2, Decimal.ROUND_DOWN), .toDecimalPlaces(2, Decimal.ROUND_DOWN),
balanceDate: time, 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, decayStart: decay.start,
memo: '', memo: '',
creationDate: null, creationDate: null,

View File

@ -30,10 +30,3 @@ yarn dev_down
yarn dev_reset yarn dev_reset
``` ```
Runs all down migrations and after this all up migrations. 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"> <b-col cols="7">
<div> <div>
{{ previousBookedBalance | GDD }} {{ previousBookedBalance | GDD }}
{{ decay === '0' ? $t('math.minus') : '' }}
{{ decay | GDD }} {{ $t('math.equal') }} {{ decay | GDD }} {{ $t('math.equal') }}
<b>{{ balance | GDD }}</b> <b>{{ balance | GDD }}</b>
</div> </div>

View File

@ -5,7 +5,7 @@
const pkg = require('../../package') const pkg = require('../../package')
const constants = { 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: { CONFIG_VERSION: {
DEFAULT: 'DEFAULT', DEFAULT: 'DEFAULT',
EXPECTED: 'v1.2022-03-18', EXPECTED: 'v1.2022-03-18',

View File

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

View File

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

View File

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

View File

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