Skip to content

Commit

Permalink
fix: working simple borrow gho in getGhoSimpleFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
akanoce committed Jan 21, 2024
1 parent af87b8a commit 55be539
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
54 changes: 45 additions & 9 deletions apps/web/src/components/GetGhoSimpleFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { useMemo, useState } from 'react';
import { AssetsTableWithCheckBox } from './AssetsTableWithCheckBox';
import { useMultipleSupplyWithBorrow } from '@/hooks/useMultipleSupplyWithBorrow';
import { useBorrowAsset } from '@/hooks/useBorrowAsset';

export const GetGhoSimpleFlow = ({ address }: { address: string }) => {
const { data: userReserves } = useUserReservesIncentives(address);
Expand Down Expand Up @@ -102,18 +103,52 @@ export const GetGhoSimpleFlow = ({ address }: { address: string }) => {
}))
});

const availableToBorrowInGho = useMemo(
() =>
(Number(
userReserves?.formattedUserSummary.availableBorrowsUSD ?? 0
) /
Number(ghoReserve?.priceInUSD ?? 0)) *
Number(userReserves?.formattedUserSummary.currentLoanToValue),
[userReserves, ghoReserve]
);

const { mutate: borrowGho, isSupplyTxLoading: isBorrowGhoLoading } =
useBorrowAsset({
reserve: ghoReserve?.underlyingAsset,
amount: availableToBorrowInGho.toString()
});

const tableCaption = useMemo(
() => (
<Button
isDisabled={totalSupplyUsdSelected === 0}
onClick={() => multipleSupplyAndBorrow()}
isLoading={isSupplyTxLoading}
>
Get GHO using an additional {totalSupplyUsdSelected.toFixed(2)}{' '}
USD worth of assets
</Button>
<VStack spacing={1} justifyContent={'center'}>
<Button
isDisabled={totalSupplyUsdSelected === 0}
onClick={() => multipleSupplyAndBorrow()}
isLoading={isSupplyTxLoading}
>
Get GHO using an additional{' '}
{totalSupplyUsdSelected.toFixed(2)} USD worth of assets
</Button>
<Button
size="sm"
variant={'link'}
isDisabled={availableToBorrowInGho <= 0}
onClick={() => borrowGho()}
isLoading={isBorrowGhoLoading}
>
Get GHO using your available collateral
</Button>
</VStack>
),
[totalSupplyUsdSelected, isSupplyTxLoading, multipleSupplyAndBorrow]
[
totalSupplyUsdSelected,
isSupplyTxLoading,
multipleSupplyAndBorrow,
borrowGho,
isBorrowGhoLoading,
availableToBorrowInGho
]
);

const assetsDataWithAvailableBalance = useMemo(
Expand All @@ -123,6 +158,7 @@ export const GetGhoSimpleFlow = ({ address }: { address: string }) => {
),
[assetsData]
);

return (
<Card>
<CardHeader>
Expand Down
7 changes: 5 additions & 2 deletions apps/web/src/hooks/useBorrowAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { InterestRate } from '@aave/aave-utilities';
import { useAccountAdapter } from './useAccountAdapter';

type Props = {
amount: string;
reserve: string;
amount?: string;
reserve?: string;
};
/**
* Hook to borrow an asset to a reserve of the AAVE protocol pool
Expand All @@ -25,6 +25,9 @@ export const useBorrowAsset = ({ amount, reserve }: Props) => {
if (!poolContract) throw new Error('no poolContract');
if (!account) throw new Error('no account found');

if (!amount) throw new Error('no amount');
if (!reserve) throw new Error('no reserve ');

const data: LPBorrowParamsType = {
amount: amount,
user: account,
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/hooks/useMultipleSupplyWithBorrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export const useMultipleSupplyWithBorrow = ({ toSupply, toBorrow }: Props) => {
});

totalEstimatedAvailableUsdToBorrow +=
BigNumber(amountInUsd).toNumber();
BigNumber(amountInUsd).toNumber() *
Number(reserve.baseLTVasCollateral);

allTxs.push(...supplyTxs);
}
Expand Down

0 comments on commit 55be539

Please sign in to comment.