From 55be539a56d9cdab6d71430c11ca196107638024 Mon Sep 17 00:00:00 2001 From: Erik Nucibella Date: Sun, 21 Jan 2024 16:16:27 +0100 Subject: [PATCH] fix: working simple borrow gho in getGhoSimpleFlow --- apps/web/src/components/GetGhoSimpleFlow.tsx | 54 +++++++++++++++---- apps/web/src/hooks/useBorrowAsset.ts | 7 ++- .../src/hooks/useMultipleSupplyWithBorrow.ts | 3 +- 3 files changed, 52 insertions(+), 12 deletions(-) diff --git a/apps/web/src/components/GetGhoSimpleFlow.tsx b/apps/web/src/components/GetGhoSimpleFlow.tsx index 03e809a..420a519 100644 --- a/apps/web/src/components/GetGhoSimpleFlow.tsx +++ b/apps/web/src/components/GetGhoSimpleFlow.tsx @@ -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); @@ -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( () => ( - + + + + ), - [totalSupplyUsdSelected, isSupplyTxLoading, multipleSupplyAndBorrow] + [ + totalSupplyUsdSelected, + isSupplyTxLoading, + multipleSupplyAndBorrow, + borrowGho, + isBorrowGhoLoading, + availableToBorrowInGho + ] ); const assetsDataWithAvailableBalance = useMemo( @@ -123,6 +158,7 @@ export const GetGhoSimpleFlow = ({ address }: { address: string }) => { ), [assetsData] ); + return ( diff --git a/apps/web/src/hooks/useBorrowAsset.ts b/apps/web/src/hooks/useBorrowAsset.ts index 902a56b..8b9bdd3 100644 --- a/apps/web/src/hooks/useBorrowAsset.ts +++ b/apps/web/src/hooks/useBorrowAsset.ts @@ -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 @@ -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, diff --git a/apps/web/src/hooks/useMultipleSupplyWithBorrow.ts b/apps/web/src/hooks/useMultipleSupplyWithBorrow.ts index e1b4879..2224e33 100644 --- a/apps/web/src/hooks/useMultipleSupplyWithBorrow.ts +++ b/apps/web/src/hooks/useMultipleSupplyWithBorrow.ts @@ -49,7 +49,8 @@ export const useMultipleSupplyWithBorrow = ({ toSupply, toBorrow }: Props) => { }); totalEstimatedAvailableUsdToBorrow += - BigNumber(amountInUsd).toNumber(); + BigNumber(amountInUsd).toNumber() * + Number(reserve.baseLTVasCollateral); allTxs.push(...supplyTxs); }