diff --git a/backend/src/graphql/models/Decay.ts b/backend/src/graphql/models/Decay.ts index 9bc89f6ec..17ee8fbac 100644 --- a/backend/src/graphql/models/Decay.ts +++ b/backend/src/graphql/models/Decay.ts @@ -25,17 +25,19 @@ export class Decay { @Field(() => Number) balance: number + // timestamp in seconds @Field(() => Int, { nullable: true }) - decayStart: number + decayStart: string + // timestamp in seconds @Field(() => Int, { nullable: true }) - decayEnd: number + decayEnd: string @Field(() => String, { nullable: true }) decayDuration?: number @Field(() => Int, { nullable: true }) - decayStartBlock?: number + decayStartBlock?: string static decayStartBlockTransaction: Transaction | undefined } diff --git a/backend/src/graphql/models/Transaction.ts b/backend/src/graphql/models/Transaction.ts index c34cac1c5..69956cf9b 100644 --- a/backend/src/graphql/models/Transaction.ts +++ b/backend/src/graphql/models/Transaction.ts @@ -10,22 +10,11 @@ import { Decay } from './Decay' @ObjectType() export class Transaction { - constructor() - constructor(json: any) - constructor(json?: any) { - if (json) { - this.type = json.type - this.balance = Number(json.balance) - this.decayStart = json.decay_start - this.decayEnd = json.decay_end - - this.memo = json.memo - this.transactionId = json.transaction_id - this.name = json.name - this.email = json.email - this.date = json.date - this.decay = json.decay ? new Decay(json.decay) : undefined - } + constructor() { + this.type = '' + this.balance = 0 + this.totalBalance = 0 + this.memo = '' } @Field(() => String) @@ -38,10 +27,10 @@ export class Transaction { totalBalance: number @Field({ nullable: true }) - decayStart?: number + decayStart?: string @Field({ nullable: true }) - decayEnd?: number + decayEnd?: string @Field({ nullable: true }) decayDuration?: number @@ -67,15 +56,12 @@ export class Transaction { @ObjectType() export class TransactionList { - constructor(json: any) { - this.gdtSum = Number(json.gdtSum) - this.count = json.count - this.balance = Number(json.balance) - this.decay = Number(json.decay) - this.decayDate = json.decay_date - this.transactions = json.transactions.map((el: any) => { - return new Transaction(el) - }) + constructor() { + this.gdtSum = 0 + this.count = 0 + this.balance = 0 + this.decay = 0 + this.decayDate = '' } @Field(() => Number) diff --git a/backend/src/graphql/resolvers/TransactionResolver.ts b/backend/src/graphql/resolvers/TransactionResolver.ts index 3762cccee..31aac8e04 100644 --- a/backend/src/graphql/resolvers/TransactionResolver.ts +++ b/backend/src/graphql/resolvers/TransactionResolver.ts @@ -46,7 +46,6 @@ export class TransactionResolver { ) transactions.decayDate = now.toString() } - return transactions } diff --git a/backend/src/graphql/resolvers/listTransactions.ts b/backend/src/graphql/resolvers/listTransactions.ts index 8c4519776..596ccd03e 100644 --- a/backend/src/graphql/resolvers/listTransactions.ts +++ b/backend/src/graphql/resolvers/listTransactions.ts @@ -73,7 +73,9 @@ async function calculateAndAddDecayTransactions( prev.transactionId < decayStartTransaction.id && current.transactionId > decayStartTransaction.id ) { - finalTransaction.decay.decayStartBlock = decayStartTransaction.received.getTime() + finalTransaction.decay.decayStartBlock = ( + decayStartTransaction.received.getTime() / 1000 + ).toString() } } } @@ -181,14 +183,8 @@ export default async function listTransactions( } } - const transactionList = new TransactionList({ - gdtSum: 0, - count: userTransactionsCount, - balance: 0, - decay: 0, - decay_date: '', - transactions: transactions, - }) - + const transactionList = new TransactionList() + transactionList.count = userTransactionsCount + transactionList.transactions = transactions return transactionList } diff --git a/backend/src/typeorm/entity/UserTransaction.ts b/backend/src/typeorm/entity/UserTransaction.ts index 1f32dc454..2f5bd69fc 100644 --- a/backend/src/typeorm/entity/UserTransaction.ts +++ b/backend/src/typeorm/entity/UserTransaction.ts @@ -18,7 +18,7 @@ export class UserTransaction extends BaseEntity { balance: number @Column({ name: 'balance_date', type: 'timestamp' }) - balanceDate: number + balanceDate: Date static findByUserPaged( userId: number, diff --git a/backend/src/util/decay.ts b/backend/src/util/decay.ts index 1f2d3c152..2b30316b8 100644 --- a/backend/src/util/decay.ts +++ b/backend/src/util/decay.ts @@ -19,29 +19,31 @@ async function calculateDecay(amount: number, from: Date, to: Date): Promise { const decayStartBlock = await Decay.getDecayStartBlock() const result = new Decay(undefined) result.balance = amount - result.decayStart = from - result.decayEnd = from + const fromMillis = typeof from === 'number' ? from : from.getTime() + const toMillis = typeof to === 'number' ? to : to.getTime() + result.decayStart = (fromMillis / 1000).toString() + result.decayEnd = (toMillis / 1000).toString() // (amount, from.getTime(), to.getTime()) // if no decay start block exist or decay startet after end date - if (decayStartBlock === undefined || decayStartBlock.received.getTime() > to) { + if (decayStartBlock === undefined || decayStartBlock.received.getTime() > toMillis) { return result } // if decay start date is before start date we calculate decay for full duration - if (decayStartBlock.received.getTime() < from) { - result.decayDuration = to - from + if (decayStartBlock.received.getTime() < fromMillis) { + result.decayDuration = toMillis - fromMillis } // if decay start in between start date and end date we caculcate decay from decay start time to end date else { - result.decayDuration = to - decayStartBlock.received.getTime() + result.decayDuration = toMillis - decayStartBlock.received.getTime() } // js use timestamp in milliseconds but we calculate with seconds result.decayDuration /= 1000