Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
davidecarpini committed Jan 21, 2024
1 parent fe30978 commit 65bf13b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 13 deletions.
29 changes: 26 additions & 3 deletions apps/web/src/hooks/useBorrowAsset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMutation } from 'wagmi';
import { createBorrowTx } from '@/api';
import { createBorrowTx, useActionWithToastAndRefresh } from '@/api';
import { LPBorrowParamsType } from '@aave/aave-utilities/packages/contract-helpers/dist/esm/v3-pool-contract/lendingPoolTypes';
import { useAaveContracts } from '@/providers';
import { InterestRate } from '@aave/aave-utilities';
Expand All @@ -20,6 +20,7 @@ export const useBorrowAsset = ({ amount, reserve }: Props) => {
const { sendTransaction, account } = useAccountAdapter();

const { poolContract } = useAaveContracts();
const actionWithToastAndRefresh = useActionWithToastAndRefresh();

const borrowAsset = async () => {
if (!poolContract) throw new Error('no poolContract');
Expand All @@ -44,7 +45,29 @@ export const useBorrowAsset = ({ amount, reserve }: Props) => {

console.log({ txs });
console.log('Submitting txs...');
await sendTransaction({ txs });
return await sendTransaction({ txs });
};

const withdrawWithToast = async () => {
await actionWithToastAndRefresh(borrowAsset, {
success: {
title: 'Borrow Successful',
description: (tx) =>
`Your assets have been borrow with transaction ${tx}`
},
error: {
title: 'Error Borrowing',
description: 'Retry later'
},
loadingTransactionCreation: {
title: 'Borrow in progress',
description: `Creating transaction...`
},
loadingReciptConfirmation: {
title: 'Borrow in progress',
description: (tx) => `waiting for transaction ${tx}`
}
});
};

const {
Expand All @@ -53,7 +76,7 @@ export const useBorrowAsset = ({ amount, reserve }: Props) => {
error: supplyTxError,
mutate: borrow
} = useMutation({
mutationFn: borrowAsset
mutationFn: withdrawWithToast
});

return {
Expand Down
58 changes: 53 additions & 5 deletions apps/web/src/hooks/useRepayAsset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { useMutation } from 'wagmi';
import { createRepayTx, createRepayWithATokens } from '@/api';
import {
createRepayTx,
createRepayWithATokens,
useActionWithToastAndRefresh
} from '@/api';
import {
LPRepayParamsType,
LPRepayWithATokensType
Expand All @@ -21,6 +25,7 @@ export const useRepayAsset = ({ amount, reserve }: Props) => {
const { sendTransaction, account } = useAccountAdapter();

const { poolContract } = useAaveContracts();
const actionWithToastAndRefresh = useActionWithToastAndRefresh();

const repayAsset = async () => {
if (!poolContract) throw new Error('no poolContract');
Expand All @@ -40,7 +45,7 @@ export const useRepayAsset = ({ amount, reserve }: Props) => {

console.log({ txs });
console.log('Submitting txs...');
await sendTransaction({ txs });
return await sendTransaction({ txs });
};

const repayAssetWithATokens = async () => {
Expand All @@ -61,14 +66,57 @@ export const useRepayAsset = ({ amount, reserve }: Props) => {

console.log({ txs });
console.log('Submitting txs...');
await sendTransaction({ txs });
return await sendTransaction({ txs });
};

const repayWithToast = async () => {
await actionWithToastAndRefresh(repayAsset, {
success: {
title: 'Repay Successful',
description: (tx) =>
`Your assets have been repay with transaction ${tx}`
},
error: {
title: 'Error Repaying',
description: 'Retry later'
},
loadingTransactionCreation: {
title: 'Repay in progress',
description: `Creating transaction...`
},
loadingReciptConfirmation: {
title: 'Repay in progress',
description: (tx) => `waiting for transaction ${tx}`
}
});
};
const repayAssetWithATokensWithToast = async () => {
await actionWithToastAndRefresh(repayAssetWithATokens, {
success: {
title: 'Repay Successful',
description: (tx) =>
`Your assets have been repay with transaction ${tx}`
},
error: {
title: 'Error Repaying',
description: 'Retry later'
},
loadingTransactionCreation: {
title: 'Repay in progress',
description: `Creating transaction...`
},
loadingReciptConfirmation: {
title: 'Repay in progress',
description: (tx) => `waiting for transaction ${tx}`
}
});
};

const mutationFn = (withATokens = true) => {
if (withATokens) {
return repayAssetWithATokens();
return repayAssetWithATokensWithToast();
}
return repayAsset();
return repayWithToast();
};

return useMutation({
Expand Down
29 changes: 26 additions & 3 deletions apps/web/src/hooks/useSupplyAsset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMutation } from 'wagmi';
import { createSupplyTxs } from '@/api';
import { createSupplyTxs, useActionWithToastAndRefresh } from '@/api';
import dayjs from 'dayjs';
import { useAaveContracts } from '@/providers';
import { useAccountAdapter } from './useAccountAdapter';
Expand All @@ -19,6 +19,7 @@ type Props = {
export const useSupplyAsset = ({ amount, reserve }: Props) => {
const { poolContract } = useAaveContracts();
const { sendTransaction, account } = useAccountAdapter();
const actionWithToastAndRefresh = useActionWithToastAndRefresh();

const supplyAsset = async () => {
if (!poolContract) throw new Error('no poolContract');
Expand All @@ -37,7 +38,29 @@ export const useSupplyAsset = ({ amount, reserve }: Props) => {
...data
});
console.log('Submitting txs...');
await sendTransaction({ txs });
return sendTransaction({ txs });
};

const supplywWithToast = async () => {
await actionWithToastAndRefresh(supplyAsset, {
success: {
title: 'Supply Successful',
description: (tx) =>
`Your assets have been supplied with transaction ${tx}`
},
error: {
title: 'Error Supplying',
description: 'Retry later'
},
loadingTransactionCreation: {
title: 'Supply in progress',
description: `Creating transaction...`
},
loadingReciptConfirmation: {
title: 'Supply in progress',
description: (tx) => `waiting for transaction ${tx}`
}
});
};

const {
Expand All @@ -46,7 +69,7 @@ export const useSupplyAsset = ({ amount, reserve }: Props) => {
error: supplyTxError,
mutate: supply
} = useMutation({
mutationFn: supplyAsset
mutationFn: supplywWithToast
});

return {
Expand Down
3 changes: 1 addition & 2 deletions apps/web/src/hooks/useWithdrawAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ type Props = {
export const useWithdrawAsset = ({ amount, reserve }: Props) => {
const { sendTransaction, account } = useAccountAdapter();

const actionWithToastAndRefresh = useActionWithToastAndRefresh();

const { poolContract } = useAaveContracts();
const actionWithToastAndRefresh = useActionWithToastAndRefresh();

const withdrawAsset = async () => {
if (!poolContract) throw new Error('no poolContract');
Expand Down

0 comments on commit 65bf13b

Please sign in to comment.