Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions djed-sdk/src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
export function web3Promise(contract, method, ...args) {
return contract.methods[method](...args).call();
}

// Function to build a transaction
// Set gas limit to 500,000 by default
export function buildTx(from_, to_, value_, data_, setGasLimit = true) {
const tx = {
to: to_,
from: from_,
value: "0x" + BigInt(value_).toString(16), // Use BigInt instead of BN
value: "0x" + BigInt(value_).toString(16),
data: data_,
};
if (setGasLimit) {
tx.gasLimit = 500_000;
}
return tx;
}

export function convertInt(promise) {
return promise.then((value) => parseInt(value));
}
Expand All @@ -32,6 +34,9 @@ function intersperseCommas(s) {
}
}

/* ============================================================
FIXED decimalScaling (No commas in fractional part)
============================================================ */
export function decimalScaling(unscaledString, decimals, show = 6) {
if (decimals <= 0) {
return unscaledString + "0".repeat(-decimals);
Expand All @@ -48,14 +53,10 @@ export function decimalScaling(unscaledString, decimals, show = 6) {
suffix = unscaledString.slice(-decimals);
}

// Limit visible decimals
suffix = suffix.slice(0, show);
suffix = intersperseCommas(suffix);

if (show <= decimals) {
// Remove commas after the decimal point
suffix = suffix.replace(/,/g, "");
}

// Apply commas ONLY to integer part
prefix = reverseString(intersperseCommas(reverseString(prefix)));

return prefix + "." + suffix;
Expand All @@ -71,14 +72,18 @@ export function decimalUnscaling(scaledString, decimals) {
let s =
scaledString.slice(0, pos) +
scaledString.slice(pos + 1, pos + 1 + decimals);

if (scaledString.length - pos - 1 < decimals) {
s += "0".repeat(decimals - (scaledString.length - pos - 1));
}

return s;
}

export function scaledPromise(promise, scaling) {
return promise.then((value) => decimalScaling(value.toString(10), scaling));
return promise.then((value) =>
decimalScaling(value.toString(10), scaling)
);
}

export function scaledUnscaledPromise(promise, scaling) {
Expand All @@ -89,17 +94,27 @@ export function scaledUnscaledPromise(promise, scaling) {
}

export function percentageScale(value, scaling, showSymbol = false) {
const calculatedValue = decimalScaling(value.toString(10), scaling - 2, 2);
const calculatedValue = decimalScaling(
value.toString(10),
scaling - 2,
2
);
if (showSymbol) {
return calculatedValue + "%";
}
return calculatedValue;
}

export function percentScaledPromise(promise, scaling) {
return promise.then((value) => percentageScale(value, scaling, true));
return promise.then((value) =>
percentageScale(value, scaling, true)
);
}
// currency conversions:

/* ============================================================
Currency conversions (unchanged from your version)
============================================================ */

export function calculateBcUsdEquivalent(coinsDetails, amountFloat) {
const adaPerUsd = parseFloat(
coinsDetails?.scaledScExchangeRate.replaceAll(",", "")
Expand All @@ -120,12 +135,15 @@ export function calculateRcUsdEquivalent(coinsDetails, amountFloat) {
const eqPrice = (1e6 * amountFloat * adaPerRc) / adaPerUsd;
return decimalScaling(eqPrice.toFixed(0).toString(10), 6);
}

export function getRcUsdEquivalent(coinsDetails, amountFloat) {
return "$" + calculateRcUsdEquivalent(coinsDetails, amountFloat);
}

export function getScAdaEquivalent(coinsDetails, amountFloat) {
const adaPerSc = parseFloat(coinsDetails?.scaledPriceSc.replaceAll(",", ""));
const adaPerSc = parseFloat(
coinsDetails?.scaledPriceSc.replaceAll(",", "")
);
const eqPrice = 1e6 * amountFloat * adaPerSc;
return decimalScaling(eqPrice.toFixed(0).toString(10), 6);
}
}