/ src / components / transactions / utils.ts
utils.ts
 1  import { BigNumber } from 'bignumber.js';
 2  import { CollateralType } from 'src/helpers/types';
 3  import { ComputedUserReserveData } from 'src/hooks/app-data-provider/useAppDataProvider';
 4  
 5  export enum ErrorType {
 6    SUPPLY_CAP_REACHED,
 7    NOT_ENOUGH_COLLATERAL_TO_REPAY_WITH,
 8    ZERO_LTV_WITHDRAW_BLOCKED,
 9    FLASH_LOAN_NOT_AVAILABLE,
10  }
11  
12  export const useFlashloan = (healthFactor: string, hfEffectOfFromAmount: string) => {
13    return (
14      healthFactor !== '-1' &&
15      new BigNumber(healthFactor).minus(new BigNumber(hfEffectOfFromAmount)).lt('1.05')
16    );
17  };
18  
19  export const APPROVAL_GAS_LIMIT = 65000;
20  export const APPROVE_DELEGATION_GAS_LIMIT = 55000;
21  
22  export const checkRequiresApproval = ({
23    approvedAmount,
24    signedAmount,
25    amount,
26  }: {
27    approvedAmount: string;
28    signedAmount: string;
29    amount: string;
30  }) => {
31    // Returns false if the user has a max approval, an approval > amountToSupply, or a valid signature for amountToSupply
32    if (
33      approvedAmount === '-1' ||
34      signedAmount === '-1' ||
35      (approvedAmount !== '0' && Number(approvedAmount) >= Number(amount)) ||
36      Number(signedAmount) >= Number(amount)
37    ) {
38      return false;
39    } else {
40      return true;
41    }
42  };
43  
44  export const getAssetCollateralType = (
45    userReserve: ComputedUserReserveData,
46    userTotalCollateralUSD: string,
47    userIsInIsolationMode: boolean,
48    debtCeilingIsMaxed: boolean
49  ) => {
50    const poolReserve = userReserve.reserve;
51  
52    if (!poolReserve.usageAsCollateralEnabled) {
53      return CollateralType.UNAVAILABLE;
54    }
55  
56    let collateralType: CollateralType = CollateralType.ENABLED;
57    const userHasSuppliedReserve = userReserve && userReserve.scaledATokenBalance !== '0';
58    const userHasCollateral = userTotalCollateralUSD !== '0';
59  
60    if (poolReserve.isIsolated) {
61      if (debtCeilingIsMaxed) {
62        collateralType = CollateralType.UNAVAILABLE;
63      } else if (userIsInIsolationMode) {
64        if (userHasSuppliedReserve) {
65          collateralType = userReserve.usageAsCollateralEnabledOnUser
66            ? CollateralType.ISOLATED_ENABLED
67            : CollateralType.DISABLED;
68        } else {
69          if (userHasCollateral) {
70            collateralType = CollateralType.UNAVAILABLE_DUE_TO_ISOLATION;
71          }
72        }
73      } else {
74        if (userHasCollateral) {
75          collateralType = CollateralType.ISOLATED_DISABLED;
76        } else {
77          collateralType = CollateralType.ISOLATED_ENABLED;
78        }
79      }
80    } else {
81      if (userIsInIsolationMode) {
82        collateralType = CollateralType.UNAVAILABLE_DUE_TO_ISOLATION;
83      } else {
84        if (userHasSuppliedReserve) {
85          collateralType = userReserve.usageAsCollateralEnabledOnUser
86            ? CollateralType.ENABLED
87            : CollateralType.DISABLED;
88        } else {
89          collateralType = CollateralType.ENABLED;
90        }
91      }
92    }
93  
94    return collateralType;
95  };