FlashMintModule
https://github.com/Into-the-Fathom/fathom-stablecoin-smart-contracts/blob/master/contracts/main/flash-mint/FlashMintModule.sol
The FlashMintModule
contract facilitates the issuance of flash loans within the protocol. This contract allows borrowing a specified amount of stablecoins, executing arbitrary operations, and repaying the loan within a single transaction. It adheres to the ERC-3156 standard for flash loans.
Storage Variables
bookKeeper
(IBookKeeper
):This variable stores the address of the
BookKeeper
contract, which is used for managing accounting and collateral within the system. It interacts with the bookkeeping functionalities to record transactions related to flash loans.
stablecoinAdapter
(IStablecoinAdapter
):This variable holds the address of the
StablecoinAdapter
contract. The adapter is used to interact with the stablecoin in a manner consistent with the internal accounting system, such as depositing or withdrawing stablecoin for flash loans.
stablecoin
(IStablecoin
):This variable represents the ERC20 stablecoin used for flash loans. It facilitates operations such as minting, transferring, and burning the stablecoin.
systemDebtEngine
(address
):A crucial contract address for managing the system’s overall debt and fee accrual. It is used primarily to settle fees or bad debt that accrues through flash loan operations.
max
(uint256
):This variable sets the maximum amount of stablecoin that can be borrowed through a single flash loan. It is important for managing liquidity and risk.
feeRate
(uint256
):The fee rate charged for taking out a flash loan. It is expressed as a percentage and calculated over the amount of the loan to generate revenue from flash loan operations.
locked
(uint256
):A reentrancy guard used to prevent reentry into critical functions such as flash loan issuance. This helps protect against certain types of exploits where recursive calls can lead to unintended consequences.
flashMintWhitelist
(mapping(address => bool)
):A whitelist of addresses that are permitted to initiate flash loans. This control mechanism ensures that only approved contracts or addresses can utilize flash loan functionality, which can be essential for maintaining system security.
isDecentralizedState
(bool
):Indicates whether the contract is operating in a decentralized mode. In a decentralized state, the contract might allow a broader set of interactions than when it is in a more controlled state.
Flash Loan Functions
flashLoan(IERC3156FlashBorrower _receiver, address _token, uint256 _amount, bytes calldata _data)
: Executes a flash loan. The_receiver
must implement theIERC3156FlashBorrower
interface. This function checks the token type, enforces the maximum loan amount, calculates the fee, mints the required stablecoin, sends it to the receiver, and expects repayment within the same transaction.bookKeeperFlashLoan(IBookKeeperFlashBorrower _receiver, uint256 _amount, bytes calldata _data)
: Similar toflashLoan
but interacts with receivers that handle bookkeeping entries directly. It is meant for more integrated system operations, managing loans in terms of the bookkeeping system's RAD units.
Administrative Functions
setMax(uint256 _data)
andsetFeeRate(uint256 _data)
: Set the maximum loanable amount and the fee rate for loans. The maximum is set in stablecoin units, and the fee rate is a percentage that applies to the borrowed amount.setDecentralizedStatesStatus(bool _status)
: Toggle the operational mode between a more decentralized setting (where whitelist control might be relaxed) and a more controlled setting.addToWhitelist(address _toBeWhitelisted)
andremoveFromWhitelist(address _usr)
: Manage addresses that are allowed to initiate flash loans. This is particularly relevant in a more centralized operational mode to ensure only vetted contracts or addresses can initiate loans.pause()
andunpause()
: Enable or disable the flash loan operations. This can be critical in situations where the protocol needs to be paused for upgrades or security issues.
Utility Functions
convert()
andaccrue()
: Handle internal conversions and accounting adjustments.convert()
can be used to convert balance discrepancies into the bookkeeping system, whileaccrue()
moves accrued fees to the system debt engine.refreshApproval()
: Refresh the approval of the stablecoin to the maximum value for the stablecoin adapter, ensuring that the contract can always interact with the stablecoin as expected.
Events
LogSetMax(uint256 _data)
,LogSetFeeRate(uint256 _data)
, andLogFlashLoan(address indexed _receiver, address indexed _token, uint256 _amount, uint256 _fee)
: Log significant actions and state changes within the contract, providing transparency over flash loan parameters and administrative adjustments.LogDecentralizedStateStatus(bool _oldDecentralizedStateStatus, bool _newDecentralizedStateStatus)
: Tracks changes in the operational mode of the contract, signaling shifts between centralized and decentralized control.LogAddToWhitelist(address indexed _user)
andLogRemoveFromWhitelist(address indexed _user)
: Document changes to the list of addresses authorized to initiate flash loans.
Last updated