End-users can request flash loans from Xfai pools. Taking advantage of the flash loan capabilities however, requires interacting with the DexfaiV0Core contract directly. Let's briefly look at the flashloan implementation of the DexfaiV0Core contract to get a better grasp how to interact with it:
We can see that an IDexfaiV0FlashLoan interface is being used to interact with an external smart contract, defined as _to. This is the address where the flash loan gets sent to. To take advantage of flash loans therefore, developers have to implement a smart contract that implements the IDexfaiV0FlashLoan functionalities. Let's now create a mock smart contract capable of receiving a flash loan:
// SPDX-License-Identifier: GPL-3.0-or-laterpragmasolidity ^0.8.19;import'../../interfaces/IERC20.sol';contract MockFlashLoan {address token;address weth;constructor(address_token,address_weth) { token = _token; weth = _weth; }functionflashLoan(address_pool,uint_tokenAmount,uint_wethAmount,bytescalldata/*_data*/ ) external {uint tokenBalance =IERC20(token).balanceOf(address(this)); // used only for testing purposesuint wethBalance =IERC20(weth).balanceOf(address(this)); // used only for testing purposes/* Insert here the lines of code that uses the flash loan. */if (_tokenAmount !=0) _safeTransfer(token, _pool, tokenBalance);if (_wethAmount !=0) _safeTransfer(weth, _pool, wethBalance); }function_safeTransfer(address_token,address_to,uint256_value) internal {require(_token.code.length >0); (bool success,bytesmemory data) = _token.call( abi.encodeWithSelector(IERC20.transfer.selector, _to, _value) );require(success && (data.length ==0|| abi.decode(data, (bool)))); }}
We can call the flashloan function of the DexfaiV0Core contract, to activate the logic of our MockFlashLoan contract. This is all it takes to request a flash loan on Xfai.
For a more in depth exploration of the conceptual aspects of Xfai flash loans, visit the Flash Loans section: