Skip to content

Commit

Permalink
refactor: add tests & standarize tokenToDecimal fn
Browse files Browse the repository at this point in the history
  • Loading branch information
joeperpetua committed Oct 28, 2024
1 parent 43bda1a commit 7dff03b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/[addressOrDomain]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export default function Page({ params }: AddressOrDomainProps) {
let debt: DebtStatus = { hasDebt: false, tokens: [] };

for (const dapp of userDapps) {
if (!dapp.products[0]) { return; }
if (!dapp.products[0]) { continue; }
for (const position of dapp.products[0].positions) {
for (const tokenAddress of Object.keys(position.totalBalances)) {
const tokenBalance = Number(position.totalBalances[tokenAddress]);
Expand Down
15 changes: 8 additions & 7 deletions components/dashboard/PortfolioSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type PortfolioSummaryProps = {
title: string,
data: ChartItem[],
totalBalance: number,
isProtocol: boolean
isProtocol: boolean,
minSlicePercentage?: number
}

const ChartEntry: FunctionComponent<ChartItem> = ({
Expand All @@ -38,12 +39,12 @@ const ChartEntry: FunctionComponent<ChartItem> = ({
);
};

const PortfolioSummary: FunctionComponent<PortfolioSummaryProps> = ({ title, data, totalBalance, isProtocol }) => {
const normalizeMinValue = (data: ChartItem[], minPercentage: number) => {
const PortfolioSummary: FunctionComponent<PortfolioSummaryProps> = ({ title, data, totalBalance, isProtocol, minSlicePercentage = 0.05 }) => {

const normalizeMinValue = (data: ChartItem[]) => {
return data.map(entry =>
Number(entry.itemValue) < totalBalance * minPercentage ?
(totalBalance * minPercentage).toFixed(2) :
Number(entry.itemValue) < totalBalance * minSlicePercentage ?
(totalBalance * minSlicePercentage).toFixed(2) :
entry.itemValue
);
}
Expand Down Expand Up @@ -109,7 +110,7 @@ const PortfolioSummary: FunctionComponent<PortfolioSummaryProps> = ({ title, dat
labels: data.map(entry => entry.itemLabel),
datasets: [{
label: '',
data: normalizeMinValue(data, .05),
data: normalizeMinValue(data),
backgroundColor: data.map(entry => entry.color),
borderColor: data.map(entry => entry.color),
borderWidth: 1,
Expand Down
4 changes: 2 additions & 2 deletions services/argentPortfolioService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ export const fetchUserDapps = async (walletAddress: string) => {

export const calculateTokenPrice = async (
tokenAddress: string,
tokenAmount: Big.Big,
tokenAmount: string,
currency: "USD" | "EUR" | "GBP" = "USD"
) => {
const data = await fetchData<ArgentTokenValue>(`${API_BASE}/${API_VERSION}/tokens/prices/${tokenAddress}?chain=starknet&currency=${currency}`);
try {
return tokenAmount.mul(data.ccyValue).toNumber();
return Number(tokenAmount) * Number(data.ccyValue);
} catch (err) {
console.log("Error while calculating token price", err);
throw err;
Expand Down
20 changes: 20 additions & 0 deletions tests/utils/feltService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
decimalToHex,
stringToHex,
gweiToEth,
tokenToDecimal
} from "@utils/feltService";

describe("Should test hexToDecimal function", () => {
Expand Down Expand Up @@ -75,3 +76,22 @@ describe("Should test gweiToEth function", () => {
expect(gweiToEth("")).toEqual("0");
});
});

describe("Should test tokenToDecimal function", () => {
it("Should return the right decimal value from a given token with dynamic decimals", () => {
expect(tokenToDecimal("113623892493328485", 18)).toEqual("0.11362");
expect(tokenToDecimal("3477473", 6)).toEqual("3.47747");
});

it("Should return 0 if the value is an empty string", () => {
expect(tokenToDecimal("", 6)).toEqual("0");
});

it("Should return 0 if the decimal is not a valid number", () => {
expect(tokenToDecimal("8943032", 'hello')).toEqual("0");
});

it("Should return 0 if the decimal is a negative number", () => {
expect(tokenToDecimal("22341256543", -5)).toEqual("0");
});
});
9 changes: 3 additions & 6 deletions utils/feltService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@ export function gweiToEth(gwei: string): string {
}

export function tokenToDecimal(tokenValue: string, decimals: number) {
if (!tokenValue || isNaN(Number(tokenValue))) {
return new Big(0);
}
if (decimals < 0) {
throw new Error("Decimals must be non-negative");
if (!tokenValue || isNaN(Number(tokenValue)) || isNaN(Number(decimals)) || decimals < 0) {
return "0";
}

const tokenValueBigInt = new Big(tokenValue);
const scaleFactor = new Big(10 ** decimals);

const tokenDecimal = tokenValueBigInt.div(scaleFactor).round(5);

return tokenDecimal;
return tokenDecimal.toString();
}

0 comments on commit 7dff03b

Please sign in to comment.