mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Decay Model + Utils
This commit is contained in:
parent
e49b17f2ad
commit
cdd336cfbb
@ -15,13 +15,6 @@ export class Decay {
|
||||
}
|
||||
}
|
||||
|
||||
static async getDecayStartBlock(): Promise<Transaction | undefined> {
|
||||
if (!this.decayStartBlockTransaction) {
|
||||
this.decayStartBlockTransaction = await Transaction.getDecayStartBlock()
|
||||
}
|
||||
return this.decayStartBlockTransaction
|
||||
}
|
||||
|
||||
@Field(() => Number)
|
||||
balance: number
|
||||
|
||||
@ -38,6 +31,4 @@ export class Decay {
|
||||
|
||||
@Field(() => Int, { nullable: true })
|
||||
decayStartBlock?: string
|
||||
|
||||
static decayStartBlockTransaction: Transaction | undefined
|
||||
}
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
import { getCustomRepository } from 'typeorm'
|
||||
import { Decay } from '../graphql/models/Decay'
|
||||
import { TransactionRepository } from '../typeorm/repository/Transaction'
|
||||
|
||||
function decayFormula(amount: number, durationInSeconds: number): number {
|
||||
return amount * Math.pow(0.99999997802044727, durationInSeconds)
|
||||
@ -6,7 +8,8 @@ function decayFormula(amount: number, durationInSeconds: number): number {
|
||||
|
||||
async function calculateDecay(amount: number, from: Date, to: Date): Promise<number> {
|
||||
// load decay start block
|
||||
const decayStartBlock = await Decay.getDecayStartBlock()
|
||||
const transactionRepository = getCustomRepository(TransactionRepository)
|
||||
const decayStartBlock = await transactionRepository.findDecayStartBlock()
|
||||
|
||||
// if decay hasn't started yet we return input amount
|
||||
if (!decayStartBlock) return amount
|
||||
@ -22,7 +25,8 @@ async function calculateDecayWithInterval(
|
||||
from: number | Date,
|
||||
to: number | Date,
|
||||
): Promise<Decay> {
|
||||
const decayStartBlock = await Decay.getDecayStartBlock()
|
||||
const transactionRepository = getCustomRepository(TransactionRepository)
|
||||
const decayStartBlock = await transactionRepository.findDecayStartBlock()
|
||||
|
||||
const result = new Decay(undefined)
|
||||
result.balance = amount
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user