import { BigNumber } from 'ethers' ; import { IDebtTokenBase , IDebtTokenBase__factory } from '../contract-types' ; import BaseDebtTokenInterface from '../interfaces/BaseDebtToken' ; import IERC20ServiceInterface from '../interfaces/ERC20' ; import { Configuration , eEthereumTxType , EthereumTransactionTypeExtended , tEthereumAddress , transactionType , tStringCurrencyUnits , tStringDecimalUnits , } from '../types' ; import { parseNumber } from '../utils/parsings' ; import BaseService from './BaseService' ; export default class BaseDebtToken extends BaseService < IDebtTokenBase > implements BaseDebtTokenInterface { readonly erc20Service : IERC20ServiceInterface ; constructor ( config , erc20Service ) { super ( config , IDebtTokenBase__factory ) ; this . erc20Service = erc20Service ; } public approveDelegation ( user , delegatee , debtTokenAddress , amount ) { const debtTokenContract = this . getContractInstance ( debtTokenAddress ) ; const txCallback = this . generateTxCallback ( { rawTxMethod : ( ) => debtTokenContract . populateTransaction . approveDelegation ( delegatee , amount ) , from : user , } ) ; return { tx : txCallback , txType : eEthereumTxType . ERC20_APPROVAL , gas : this . generateTxPriceEstimation ( [ ] , txCallback ) , } ; } public async isDelegationApproved ( debtTokenAddress , allowanceGiver , allowanceReceiver , amount ) { const decimals = await this . erc20Service . decimalsOf ( debtTokenAddress ) ; const debtTokenContract = this . getContractInstance ( debtTokenAddress ) ; const delegatedAllowance = await debtTokenContract . borrowAllowance ( allowanceGiver , allowanceReceiver ) ; const amountBNWithDecimals = BigNumber . from ( parseNumber ( amount , decimals ) ) ; return delegatedAllowance . gt ( amountBNWithDecimals ) ; } }