Skip to content
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CustomMarket } from 'src/ui-config/marketsConfig';

import { SwapType } from '../types';

export const SAFETY_MODULE_TOKENS = [
Expand All @@ -23,3 +25,8 @@ export const APP_CODE_PER_SWAP_TYPE: Record<SwapType, string> = {
};

export const APP_CODE_VALUES = Object.values(APP_CODE_PER_SWAP_TYPE);

export const isHorizonMarket = (currentMarket: string) =>
currentMarket === CustomMarket.proto_horizon_v3 ||
currentMarket === CustomMarket.proto_sepolia_horizon_v3 ||
currentMarket === ('fork_proto_horizon_v3' as CustomMarket);
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const hasFlashLoanDisabled = (state: SwapState): boolean => {
? state.sourceReserve?.reserve
: state.destinationReserve?.reserve;

console.error('reserve symbol', reserve?.symbol, 'flashLoanEnabled', reserve?.flashLoanEnabled);

if (state.useFlashloan === true && reserve && !reserve.flashLoanEnabled) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { TOKEN_LIST, TokenInfo } from 'src/ui-config/TokenList';
import { displayGhoForMintableMarket } from 'src/utils/ghoUtilities';
import { useShallow } from 'zustand/shallow';

import { isHorizonMarket } from '../../constants/shared.constants';
import { invalidateAppStateForSwap } from '../../helpers/shared';
import { SwappableToken, SwapParams, SwapType, TokenType } from '../../types';
import { BaseSwapModalContent } from './BaseSwapModalContent';
Expand All @@ -23,6 +24,7 @@ export const CollateralSwapModalContent = ({ underlyingAsset }: { underlyingAsse
);
const queryClient = useQueryClient();
const currentNetworkConfig = useRootStore((store) => store.currentNetworkConfig);
const isHorizon = isHorizonMarket(currentMarketName);
const baseTokens: TokenInfo[] = reserves.map((reserve) => {
return {
address: reserve.underlyingAsset,
Expand All @@ -34,8 +36,8 @@ export const CollateralSwapModalContent = ({ underlyingAsset }: { underlyingAsse
};
});

const tokensFrom = getTokensFrom(user, baseTokens, chainId);
const tokensTo = getTokensTo(user, reserves, baseTokens, currentMarketName, chainId);
const tokensFrom = getTokensFrom(user, baseTokens, chainId, isHorizon);
const tokensTo = getTokensTo(user, reserves, baseTokens, currentMarketName, chainId, isHorizon);

const userSelectedInputToken = tokensFrom.find(
(token) => token.underlyingAddress.toLowerCase() === underlyingAsset?.toLowerCase()
Expand Down Expand Up @@ -135,11 +137,14 @@ const getDefaultOutputToken = (
const getTokensFrom = (
user: ExtendedFormattedUser | undefined,
baseTokensInfo: TokenInfo[],
chainId: number
chainId: number,
isHorizon: boolean
): SwappableToken[] => {
// Tokens From should be the supplied tokens
const suppliedPositions =
user?.userReservesData.filter((userReserve) => userReserve.underlyingBalance !== '0') || [];
user?.userReservesData
.filter((userReserve) => userReserve.underlyingBalance !== '0')
.filter((userReserve) => !isHorizon || userReserve.reserve.borrowingEnabled) || [];

return suppliedPositions
.map<SwappableToken | undefined>((position) => {
Expand Down Expand Up @@ -209,12 +214,14 @@ const getTokensTo = (
reserves: ComputedReserveData[],
baseTokensInfo: TokenInfo[],
currentMarket: string,
chainId: number
chainId: number,
isHorizon: boolean
): SwappableToken[] => {
// Tokens To should be the potential supply tokens (so we have an aToken)
const tokensToSupply = reserves.filter(
(reserve: ComputedReserveData) =>
!(reserve.isFrozen || reserve.isPaused) &&
(!isHorizon || reserve.borrowingEnabled) &&
!displayGhoForMintableMarket({ symbol: reserve.symbol, currentMarket: currentMarket })
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { fetchIconSymbolAndName } from 'src/ui-config/reservePatches';
import { TOKEN_LIST, TokenInfo } from 'src/ui-config/TokenList';
import { useShallow } from 'zustand/shallow';

import { isHorizonMarket } from '../../constants/shared.constants';
import { invalidateAppStateForSwap } from '../../helpers/shared';
import { SwappableToken, SwapParams, SwapType } from '../../types';
import { BaseSwapModalContent } from './BaseSwapModalContent';
Expand All @@ -26,9 +27,10 @@ export const RepayWithCollateralModalContent = ({
}) => {
const { user, reserves } = useAppDataContext();
const currentNetworkConfig = useRootStore((store) => store.currentNetworkConfig);
const [account, chainId] = useRootStore(
useShallow((store) => [store.account, store.currentChainId])
const [account, chainId, currentMarket] = useRootStore(
useShallow((store) => [store.account, store.currentChainId, store.currentMarket])
);
const isHorizon = isHorizonMarket(currentMarket);

const baseTokens: TokenInfo[] = reserves.map((reserve) => {
return {
Expand All @@ -42,7 +44,8 @@ export const RepayWithCollateralModalContent = ({
});

const tokensFrom = getTokensFrom(user, currentNetworkConfig.wagmiChain.id, currentNetworkConfig);
const tokensTo = getTokensTo(user, baseTokens, currentNetworkConfig.wagmiChain.id);
const tokensTo = getTokensTo(user, baseTokens, currentNetworkConfig.wagmiChain.id, isHorizon);

const defaultInputToken = tokensFrom.find(
(token) => token.underlyingAddress.toLowerCase() === underlyingAsset?.toLowerCase()
);
Expand All @@ -55,6 +58,7 @@ export const RepayWithCollateralModalContent = ({
token.addressToSwap.toLowerCase() !== defaultInputToken?.addressToSwap.toLowerCase() &&
token.underlyingAddress.toLowerCase() !== defaultInputToken?.underlyingAddress.toLowerCase()
);

const defaultOutputToken = tokensWithoutInputToken.sort(
(a, b) => Number(b.balance) - Number(a.balance)
)[0];
Expand All @@ -74,7 +78,7 @@ export const RepayWithCollateralModalContent = ({
// allowLimitOrders: false,
invalidateAppState,
sourceTokens: tokensFrom,
destinationTokens: tokensTo,
destinationTokens: tokensWithoutInputToken,
chainId,
forcedInputToken: defaultInputToken,
suggestedDefaultOutputToken: defaultOutputToken,
Expand Down Expand Up @@ -185,11 +189,14 @@ const getTokensFrom = (
const getTokensTo = (
user: ExtendedFormattedUser | undefined,
baseTokensInfo: TokenInfo[],
chainId: number
chainId: number,
isHorizon: boolean
): SwappableToken[] => {
// Tokens From should be the supplied tokens
const suppliedPositions =
user?.userReservesData.filter((userReserve) => userReserve.underlyingBalance !== '0') || [];
user?.userReservesData
.filter((userReserve) => userReserve.underlyingBalance !== '0')
.filter((userReserve) => !isHorizon || userReserve.reserve.borrowingEnabled) || [];

return suppliedPositions
.map<SwappableToken | undefined>((position) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,32 @@ import { useRootStore } from 'src/store/root';
import { TOKEN_LIST, TokenInfo } from 'src/ui-config/TokenList';
import { useShallow } from 'zustand/shallow';

import { isHorizonMarket } from '../../constants/shared.constants';
import { invalidateAppStateForSwap } from '../../helpers/shared';
import { SwappableToken, SwapParams, SwapType, TokenType } from '../../types';
import { BaseSwapModalContent } from './BaseSwapModalContent';
import { getDefaultOutputToken, getFilteredTokensForSwitch } from './SwapModalContent';

export const WithdrawAndSwapModalContent = ({ underlyingAsset }: { underlyingAsset: string }) => {
const { account, chainIdInApp: chainId } = useRootStore(
const {
account,
chainIdInApp: chainId,
currentMarket,
} = useRootStore(
useShallow((store) => ({
account: store.account,
chainIdInApp: store.currentChainId,
currentMarket: store.currentMarket,
}))
);

const queryClient = useQueryClient();
const { user } = useAppDataContext();
const isHorizon = isHorizonMarket(currentMarket);

const initialDefaultTokens = useMemo(() => getFilteredTokensForSwitch(chainId), [chainId]);

const tokensFrom = getTokensFrom(user, initialDefaultTokens, chainId);
const tokensFrom = getTokensFrom(user, initialDefaultTokens, chainId, isHorizon);

const reserves = useAppDataContext().reserves;
const { data: initialTokens, isFetching: tokensLoading } = useTokensBalance(
Expand All @@ -41,6 +48,7 @@ export const WithdrawAndSwapModalContent = ({ underlyingAsset }: { underlyingAss
const reserve = reserves.find(
(reserve) => reserve.underlyingAsset.toLowerCase() === token.address.toLowerCase()
);
if (isHorizon && !reserve?.borrowingEnabled) return undefined;
return {
addressToSwap: token.address,
addressForUsdPrice: token.address,
Expand All @@ -57,6 +65,7 @@ export const WithdrawAndSwapModalContent = ({ underlyingAsset }: { underlyingAss
tokenType: token.extensions?.isNative ? TokenType.NATIVE : TokenType.ERC20,
};
})
.filter((token) => token != undefined)
.filter((token) => token.balance !== '0')
.sort((a, b) => Number(b.balance) - Number(a.balance));

Expand Down Expand Up @@ -117,11 +126,14 @@ export const WithdrawAndSwapModalContent = ({ underlyingAsset }: { underlyingAss
const getTokensFrom = (
user: ExtendedFormattedUser | undefined,
baseTokensInfo: TokenInfo[],
chainId: number
chainId: number,
isHorizon: boolean
): SwappableToken[] => {
// Tokens From should be the supplied tokens
const suppliedPositions =
user?.userReservesData.filter((userReserve) => userReserve.underlyingBalance !== '0') || [];
user?.userReservesData
.filter((userReserve) => userReserve.underlyingBalance !== '0')
.filter((userReserve) => !isHorizon || userReserve.reserve.borrowingEnabled) || [];

return suppliedPositions
.map<SwappableToken | undefined>((position) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ProtocolAction } from '@aave/contract-helpers';
import { Trans } from '@lingui/macro';
import { Button } from '@mui/material';
import { isHorizonMarket } from 'src/components/transactions/Swap/constants/shared.constants';
import { useAppDataContext } from 'src/hooks/app-data-provider/useAppDataProvider';
import { useAssetCaps } from 'src/hooks/useAssetCaps';
import { useModalContext } from 'src/hooks/useModal';
Expand Down Expand Up @@ -33,7 +34,10 @@ export const SuppliedPositionsListItem = ({
useShallow((store) => [store.trackEvent, store.currentMarketData, store.currentMarket])
);

const showSwitchButton = isFeatureEnabled.liquiditySwap(currentMarketData);
const isHorizon = isHorizonMarket(currentMarket);
const collateralSwapEnabledForReserve = !isHorizon || reserve.borrowingEnabled;
const showSwitchButton =
isFeatureEnabled.liquiditySwap(currentMarketData) && collateralSwapEnabledForReserve;

const canBeEnabledAsCollateral = user
? !debtCeiling.isMaxed &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ProtocolAction } from '@aave/contract-helpers';
import { Trans } from '@lingui/macro';
import { Box, Button } from '@mui/material';
import { isHorizonMarket } from 'src/components/transactions/Swap/constants/shared.constants';
import { useAppDataContext } from 'src/hooks/app-data-provider/useAppDataProvider';
import { useAssetCaps } from 'src/hooks/useAssetCaps';
import { useRootStore } from 'src/store/root';
Expand Down Expand Up @@ -29,7 +30,12 @@ export const SuppliedPositionsListMobileItem = ({
);
const { openSupply, openCollateralSwap, openWithdraw, openCollateralChange } = useModalContext();
const { debtCeiling } = useAssetCaps();
const isSwapButton = isFeatureEnabled.liquiditySwap(currentMarketData);

const isHorizon = isHorizonMarket(currentMarket);
const collateralSwapEnabledForReserve = !isHorizon || reserve.borrowingEnabled;
const isSwapButton =
isFeatureEnabled.liquiditySwap(currentMarketData) && collateralSwapEnabledForReserve;

const {
symbol,
iconSymbol,
Expand Down
6 changes: 6 additions & 0 deletions src/ui-config/marketsConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,12 @@ export const marketsData: {
chainId: ChainId.mainnet,
v3: true,
logo: '/icons/markets/horizon.svg',
enabledFeatures: {
liquiditySwap: true,
withdrawAndSwitch: true,
collateralRepay: true,
debtSwitch: true,
},
addresses: {
LENDING_POOL_ADDRESS_PROVIDER: '0x5D39E06b825C1F2B80bf2756a73e28eFAA128ba0',
LENDING_POOL: '0xAe05Cd22df81871bc7cC2a04BeCfb516bFe332C8',
Expand Down
Loading