Skip to content

Commit

Permalink
Sort assets also by their value in users wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
aleots committed Mar 29, 2024
1 parent 530a9f3 commit e0710bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/components/_pages/PageHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,20 @@ export const PageHome = () => {
return filteredData.sort((a, b) => {

// 1. Priority - does user own same assets as in strategy
if (isConnected) {
const doesUserHaveStrategyAssets = (strategy: StrategyData) => strategy?.tradedAssets?.some(
asset => userData?.depositAssetBalances.some(
balance => asset.symbol === balance.symbol)
)

const userHasAAssets = doesUserHaveStrategyAssets(a);
const userHasBAssets = doesUserHaveStrategyAssets(b);

if ((userHasAAssets || userHasBAssets) && !(userHasAAssets && userHasBAssets)) {
return userHasAAssets ? -1 : 1;
if (isConnected && userData?.depositAssetBalances) {
for (const balance of userData?.depositAssetBalances) {
const doesStrategyHaveAsset = (strategy: StrategyData) => strategy?.tradedAssets?.some(
asset => asset.symbol === balance.symbol
)
const strategyAHasAsset = doesStrategyHaveAsset(a);
const strategyBHasAsset = doesStrategyHaveAsset(b);

if (strategyAHasAsset && strategyBHasAsset) {
break;
}
if ((strategyAHasAsset || strategyBHasAsset) && !(strategyAHasAsset && strategyBHasAsset)) {
return strategyAHasAsset ? -1 : 1;
}
}
}
// 2. Priority - new strategies
Expand Down
6 changes: 6 additions & 0 deletions src/data/actions/common/getUserDataAllStrategies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { fetchBalance } from "@wagmi/core"
import { getAddress } from "ethers/lib/utils"
import { getAcceptedDepositAssetsByChain } from "data/tokenConfig"
import { ResolvedConfig } from "abitype"
import BigNumber from "bignumber.js"

export const getUserDataAllStrategies = async ({
allContracts,
Expand Down Expand Up @@ -85,6 +86,7 @@ export const getUserDataAllStrategies = async ({
symbol: string;
value: ResolvedConfig['BigIntType'];
}[] = [];

for (const token of tokenList) {
await fetchBalance({
token: getAddress(token!.address),
Expand All @@ -97,6 +99,10 @@ export const getUserDataAllStrategies = async ({
.catch((error) => console.log("error", error))
}

depositAssetBalances.sort(
(x, y) => new BigNumber(y.value._hex).minus(new BigNumber(x.value._hex)).toNumber()
)

const userData = userDataRes.filter((item) => !!item)

const totalNetValue = (() => {
Expand Down

0 comments on commit e0710bc

Please sign in to comment.