Flash-Loans
閃貸如何保證借來的乙太幣/代幣會得到償還?
我想知道契約如何確保償還借款?誰能提供一個例子?
這一切都在一個功能中完成,所以如果錢沒有還清,你可以恢復。(例如,我寄給您 100 美元,您可以隨心所欲地使用它,但如果它在區塊結束時沒有回到我的手中,則 txn 會恢復)。這是標準:
function flashLoan( IERC3156FlashBorrower receiver, address token, uint256 amount, bytes calldata data ) external override returns (bool) { require(token == address(this), "FlashMinter: Unsupported currency"); uint256 fee = flashFee(token, amount); _mint(address(receiver), amount); bytes32 CALLBACK_SUCCESS = keccak256("ERC3156FlashBorrower.onFlashLoan"); require( receiver.onFlashLoan(msg.sender, token, amount, fee, data) == CALLBACK_SUCCESS, "FlashMinter: Callback failed" ); uint256 _allowance = allowance(address(receiver), address(this)); require(_allowance >= (amount + fee), "FlashMinter: Repay not approved"); _approve(address(receiver), address(this), _allowance - (amount + fee)); _burn(address(receiver), amount + fee);