diff --git a/backend/src/blockchain.ts b/backend/src/blockchain.ts index 60d12bc..1c1ef63 100644 --- a/backend/src/blockchain.ts +++ b/backend/src/blockchain.ts @@ -24,6 +24,10 @@ export enum ValidatorStatus { UNKNOWN = 'UNKNOWN', } +function convertNimiqTimestamp(timestamp: bigint): number { + return Number(timestamp); +} + /** * Gets the RPC client to retrieve data from the blockchain * @returns @@ -84,16 +88,16 @@ export async function getBlock(blockNumber: number) { * @returns */ -async function getPaymentTime(amount: number, timestamp: bigint) { +async function getPaymentTime(amount: number, timestamp: number) { const price = await getDollarPriceHistory(timestamp); const transactionValue = lunaToNim(amount) * price; if (transactionValue > periodCost * (1 - tolerance)) { - return BigInt(period); + return period; } else { console.log('Payment not close to tolerance. Not processing. '); } - return 0n; + return 0; } /** @@ -127,14 +131,21 @@ export async function getPaymentStatus(address: Address): Promise { } } valdatorPayemnts.sort((a, b) => (a.timestamp > b.timestamp ? 1 : -1)); - let paymentEndTS: bigint = 0n; + let paymentEndTS: number = 0; for (const payment of valdatorPayemnts) { - if (payment.timestamp > paymentEndTS) { - const addedTime = await getPaymentTime(payment.value, payment.timestamp); - paymentEndTS = payment.timestamp + addedTime; + if (convertNimiqTimestamp(payment.timestamp) > paymentEndTS) { + const addedTime = await getPaymentTime( + payment.value, + convertNimiqTimestamp(payment.timestamp), + ); + paymentEndTS = convertNimiqTimestamp(payment.timestamp) + addedTime; } else { paymentEndTS = - paymentEndTS + (await getPaymentTime(payment.value, payment.timestamp)); + paymentEndTS + + (await getPaymentTime( + payment.value, + convertNimiqTimestamp(payment.timestamp), + )); } } return paymentEndTS; diff --git a/backend/src/pricing.ts b/backend/src/pricing.ts index 270fef2..f57b368 100644 --- a/backend/src/pricing.ts +++ b/backend/src/pricing.ts @@ -8,24 +8,20 @@ export function nimToLuna(amount: number): number { return Math.round(amount * 1e5); } -function getSecondsTimestamp(timestamp: bigint): number { - return Number(timestamp / 1000n); -} - /** * Returns the price given a timestamp. TODO: INACCURATE as coingecko only returns the price of the day * Caches prices in node-persist * @param timestamp * @returns */ -export async function getDollarPriceHistory(timestamp: bigint) { +export async function getDollarPriceHistory(timestamp: number) { await storage.init(); if ((await storage.getItem(timestamp.toString())) !== undefined) { console.log('Found price in cache'); return await storage.getItem(timestamp.toString()); } - const date = new Date(getSecondsTimestamp(timestamp)); + const date = new Date(timestamp); const datestring = `${date.getDate()}-${ date.getMonth() + 1 }-${date.getFullYear()}`; // 30-12-2022