Decay Model + Utils

This commit is contained in:
Ulf Gebhardt 2021-10-01 20:15:39 +02:00 committed by einhornimmond
parent e49b17f2ad
commit cdd336cfbb
2 changed files with 6 additions and 11 deletions

View File

@ -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
}

View File

@ -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