Skip to content

Commit

Permalink
Fix rpc (#113)
Browse files Browse the repository at this point in the history
* update rpc transaction

* fix transaction

* fix conversion, just type change

* prettier
  • Loading branch information
faberto authored Oct 19, 2024
1 parent 69cdae1 commit 75dd439
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
27 changes: 19 additions & 8 deletions backend/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -127,14 +131,21 @@ export async function getPaymentStatus(address: Address): Promise<any> {
}
}
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;
Expand Down
8 changes: 2 additions & 6 deletions backend/src/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 75dd439

Please sign in to comment.