Skip to content

Feat/looping poc #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
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,11 +1,22 @@
import { InterestRate } from '@aave/contract-helpers';
import { Trans } from '@lingui/macro';
import { Button } from '@mui/material';
import { useAssetCaps } from 'src/hooks/useAssetCaps';
import { Button, CircularProgress } from '@mui/material';
import { useWeb3React } from '@web3-react/core';
import { BigNumber, constants, Contract, providers } from 'ethers';
import { parseEther } from 'ethers/lib/utils';
import { useRouter } from 'next/router';
import { useState } from 'react';
import { useModalContext } from 'src/hooks/useModal';
import { useProtocolDataContext } from 'src/hooks/useProtocolDataContext';
import { DashboardReserve } from 'src/utils/dashboardSortUtils';

import { useAppDataContext } from '../../../../hooks/app-data-provider/useAppDataProvider';
import { useAssetCaps } from '../../../../hooks/useAssetCaps';
import { useWeb3Context } from '../../../../libs/hooks/useWeb3Context';
import { marketsData } from '../../../../ui-config/marketsConfig';
import { ERC20, ERC20ABI } from '../../../../utils/contracts/ERC20';
import { Leverage, LeverageABI } from '../../../../utils/contracts/Leverage';
import { availableMarkets } from '../../../../utils/marketsAndNetworksConfig';
import { SpkAirdropNoteInline } from '../BorrowAssetsList/BorrowAssetsListItem';
import { ListAPRColumn } from '../ListAPRColumn';
import { ListButtonsColumn } from '../ListButtonsColumn';
Expand All @@ -21,18 +32,64 @@ export const BorrowedPositionsListItem = ({
borrowRateMode,
stableBorrowAPY,
}: DashboardReserve) => {
const { reserves } = useAppDataContext();
const { openBorrow, openRepay } = useModalContext();
const { currentMarket } = useProtocolDataContext();
const [isUnlooping, setIsUnlooping] = useState(false);
const { library: provider } = useWeb3React<providers.Web3Provider>();
const { currentAccount } = useWeb3Context();
const router = useRouter();
const { borrowCap } = useAssetCaps();

const {
isActive,
isFrozen,
borrowingEnabled,
sIncentivesData,
vIncentivesData,
variableBorrowAPY,
borrowingEnabled,
} = reserve;

const handleUnloopingAction = async () => {
try {
setIsUnlooping(true);
const currentMarketData = marketsData[availableMarkets[0]];
const signer = provider?.getSigner(currentAccount);
const interestRateMode = 2;
const leverageInstance = new Contract(
currentMarketData.addresses.LEVERAGE!,
LeverageABI,
signer
) as Leverage;
// unlooping;
const aTokenAddress = reserves.filter((item) => item.symbol === 'wstXTZ')[0].aTokenAddress;
const aTokenInstance = new Contract(aTokenAddress, ERC20ABI, signer) as ERC20;

const approvalAmt = await aTokenInstance.allowance(
currentAccount,
currentMarketData.addresses.LEVERAGE!
);
if (approvalAmt.eq(BigNumber.from('0'))) {
await aTokenInstance.approve(
currentMarketData.addresses.LEVERAGE!,
constants.MaxUint256.toString()
);
}

const unloop = await leverageInstance.unLoop(
BigNumber.from(parseEther('1')),
currentAccount,
interestRateMode
);
await unloop.wait(1);

router.reload();
} catch (error) {
console.error('Error in doing the un looping action', error);
}
setIsUnlooping(false);
};

return (
<ListItemWrapper
symbol={reserve.symbol}
Expand Down Expand Up @@ -73,12 +130,26 @@ export const BorrowedPositionsListItem = ({

<ListButtonsColumn>
<Button
disabled={!isActive}
disabled={true}
// !isActive}
variant="contained"
onClick={() => openRepay(reserve.underlyingAsset, borrowRateMode, isFrozen)}
>
<Trans>Repay</Trans>
</Button>
<Button
disabled={!isActive || isUnlooping}
variant="contained"
onClick={() => handleUnloopingAction()}
>
{isUnlooping ? (
<Trans>
<CircularProgress sx={{ color: 'white' }} size="1.5rem" />
</Trans>
) : (
<Trans>Unloop</Trans>
)}
</Button>
<Button
disabled={!isActive || !borrowingEnabled || isFrozen || borrowCap.isMaxed}
variant="outlined"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Trans } from '@lingui/macro';
import { Button } from '@mui/material';
import { Button, CircularProgress } from '@mui/material';
import { useWeb3React } from '@web3-react/core';
import { BigNumber, constants, Contract, providers } from 'ethers';
import { parseEther } from 'ethers/lib/utils';
import { useRouter } from 'next/router';
import { useState } from 'react';
import { useAssetCaps } from 'src/hooks/useAssetCaps';
import { useModalContext } from 'src/hooks/useModal';
import { useProtocolDataContext } from 'src/hooks/useProtocolDataContext';
Expand All @@ -8,6 +13,15 @@ import { DashboardReserve } from 'src/utils/dashboardSortUtils';
import { CapsHint } from '../../../../components/caps/CapsHint';
import { CapType } from '../../../../components/caps/helper';
import { Link, ROUTES } from '../../../../components/primitives/Link';
import { useAppDataContext } from '../../../../hooks/app-data-provider/useAppDataProvider';
import { useWeb3Context } from '../../../../libs/hooks/useWeb3Context';
import { marketsData } from '../../../../ui-config/marketsConfig';
import {
CreditDelegationTokenABI,
ICreditDelegationToken,
} from '../../../../utils/contracts/CreditDelegationToken';
import { Leverage, LeverageABI } from '../../../../utils/contracts/Leverage';
import { availableMarkets } from '../../../../utils/marketsAndNetworksConfig';
import { SpkAirdropNoteInline } from '../BorrowAssetsList/BorrowAssetsListItem';
import { ListAPRColumn } from '../ListAPRColumn';
import { ListButtonsColumn } from '../ListButtonsColumn';
Expand All @@ -31,13 +45,68 @@ export const SupplyAssetsListItem = ({
showSwap,
hideSupply,
}: DashboardReserve) => {
const { reserves } = useAppDataContext();
const { library: provider } = useWeb3React<providers.Web3Provider>();
const { currentAccount } = useWeb3Context();
const { currentMarket } = useProtocolDataContext();
const { openSupply, openPSMSwap } = useModalContext();
const [isLooping, setIsLooping] = useState(false);

const router = useRouter();

// Hide the asset to prevent it from being supplied if supply cap has been reached
const { supplyCap: supplyCapUsage } = useAssetCaps();
if (supplyCapUsage.isMaxed) return null;

const handleLoopingAction = async () => {
try {
setIsLooping(true);
const currentMarketData = marketsData[availableMarkets[0]];
const signer = provider?.getSigner(currentAccount);
const interestRateMode = 2;
const leverageInstance = new Contract(
currentMarketData.addresses.LEVERAGE!,
LeverageABI,
signer
) as Leverage;
// looping
const debtTokenAddress = reserves.filter(
(item) =>
item.underlyingAsset.toLowerCase() ===
currentMarketData.addresses.WRAPPED_TOKEN?.toLowerCase()
)[0].variableDebtTokenAddress;
const debtTokenInstance = new Contract(
debtTokenAddress,
CreditDelegationTokenABI,
signer
) as ICreditDelegationToken;

const delegationAmount = await debtTokenInstance.borrowAllowance(
currentAccount,
currentMarketData.addresses.LEVERAGE!
);
if (delegationAmount.eq(BigNumber.from('0'))) {
await debtTokenInstance.approveDelegation(
currentMarketData.addresses.LEVERAGE!,
constants.MaxUint256.toString()
);
}

const loopCount = 5;
const borrowRatio = 8350; // 83.5%

const loopingTxn = await leverageInstance.loop(loopCount, borrowRatio, interestRateMode, 0, {
value: parseEther('1'),
});
await loopingTxn.wait(1);

router.reload();
} catch (error) {
console.error('Error in doing the looping action', error);
}
setIsLooping(false);
};

return (
<ListItemWrapper
symbol={symbol}
Expand Down Expand Up @@ -85,6 +154,28 @@ export const SupplyAssetsListItem = ({
<Trans>Deposit</Trans>
</Button>
)}
{!hideSupply && symbol === 'XTZ' && (
<Button
sx={(theme) => ({
color: theme.palette.common.white,
background: '#4caf50',
'&:hover, &.Mui-focusVisible': {
background: '#8bc34a',
},
})}
disabled={!isActive || isFreezed || Number(walletBalance) <= 0 || isLooping}
variant="contained"
onClick={() => handleLoopingAction()}
>
{isLooping ? (
<Trans>
<CircularProgress sx={{ color: 'white' }} size="1.5rem" />
</Trans>
) : (
<Trans>Loop</Trans>
)}
</Button>
)}
{showSwap && (
<Button variant="contained" onClick={() => openPSMSwap(underlyingAsset)}>
<Trans>Swap</Trans>
Expand Down
16 changes: 10 additions & 6 deletions src/ui-config/marketsConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export type MarketDataType = {
CHAINLOG?: string;
SAVINGS_DAI?: string;
V3_MIGRATOR?: string;
LEVERAGE?: string;
WRAPPED_TOKEN?: string;
};
faucetUrl: string;
/**
Expand Down Expand Up @@ -68,12 +70,14 @@ export const marketsData: {
faucet: false,
},
addresses: {
LENDING_POOL_ADDRESS_PROVIDER: '0x5e580E0FF1981E7c916D6D9a036A8596E35fCE31'.toLowerCase(),
LENDING_POOL: '0x837286C1d05735448F7d1942144eE98602206773',
WALLET_BALANCE_PROVIDER: '0xAE0f454b171dA3C0e3B8a75c92A449964f90f7fd',
UI_POOL_DATA_PROVIDER: '0xA69C04756c604b63514cBF13466eCE70a5BF755b',
UI_INCENTIVE_DATA_PROVIDER: '0xdfe6cC78B0A1ed393246B9151c83036AD3e165b8',
WETH_GATEWAY: '0x2ae2308F17667980582c6580556317EBdb61cc68',
LENDING_POOL_ADDRESS_PROVIDER: '0xB58cF2e8BBBE691f27f96eA32F54A21E7F75fD0C'.toLowerCase(),
LENDING_POOL: '0x717E0e99E4c71d896804bD2cF0532d3112ffd5D5',
WALLET_BALANCE_PROVIDER: '0x6B92955469e184F1eeF5567748f6F12D608bB698',
UI_POOL_DATA_PROVIDER: '0x325C2184D59bcB687eb777fd5FbB192fb8dD6CEB',
UI_INCENTIVE_DATA_PROVIDER: '0x65374686E598ae4AA22E2E4780436C029EEc6E3b',
WETH_GATEWAY: '0xB39537551422D916e6B0013Ef7Dd257D07a17FE3',
LEVERAGE: '0x7f70F6212b1FE38DC51Ca689Db0392783F5c7641',
WRAPPED_TOKEN: '0xc2ef9495272b43f5257b35a1b6dda78932839871',
},
faucetUrl: 'https://faucet.plend.finance/receiveFaucetTokens',
},
Expand Down
Loading