-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ea87ee
commit 41e1aeb
Showing
12 changed files
with
272 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity ^0.8.19; | ||
|
||
/** | ||
* @title IFlashLoanReceiver interface | ||
* @notice Interface for the IFlashLoanReceiver. | ||
* @dev implement this interface to develop a flashloan-compatible flashLoanReceiver contract | ||
**/ | ||
interface IFlashLoanReceiver { | ||
function executeOperationERC20( | ||
address[] calldata nftAssets, | ||
uint256[] calldata amounts, | ||
address initiator, | ||
address operator, | ||
bytes calldata params | ||
) external returns (bool); | ||
|
||
function executeOperationERC721( | ||
address[] calldata nftAssets, | ||
uint256[] calldata tokenIds, | ||
address initiator, | ||
address operator, | ||
bytes calldata params | ||
) external returns (bool); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,86 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity ^0.8.19; | ||
|
||
interface IPoolManager {} | ||
interface IPoolManager { | ||
/****************************************************************************/ | ||
/* supply */ | ||
/****************************************************************************/ | ||
function depositERC20(uint32 poolId, address asset, uint256 amount) external; | ||
|
||
function withdrawERC20(uint32 poolId, address asset, uint256 amount) external; | ||
|
||
function depositERC721(uint32 poolId, address asset, uint256[] calldata tokenIds, uint8 supplyMode) external; | ||
|
||
function withdrawERC721(uint32 poolId, address asset, uint256[] calldata tokenIds, uint8 supplyMode) external; | ||
|
||
function setERC721SupplyMode(uint32 poolId, address asset, uint256[] calldata tokenIds, uint8 supplyMode) external; | ||
|
||
/****************************************************************************/ | ||
/* cross lending */ | ||
/****************************************************************************/ | ||
function crossBorrowERC20(uint32 poolId, address asset, uint8[] calldata groups, uint256[] calldata amounts) external; | ||
|
||
function crossRepayERC20(uint32 poolId, address asset, uint8[] calldata groups, uint256[] calldata amounts) external; | ||
|
||
function crossLiquidateERC20( | ||
uint32 poolId, | ||
address user, | ||
address collateralAsset, | ||
address debtAsset, | ||
uint256 debtToCover, | ||
bool supplyAsCollateral | ||
) external; | ||
|
||
function crossLiquidateERC721( | ||
uint32 poolId, | ||
address user, | ||
address collateralAsset, | ||
uint256[] calldata collateralTokenIds, | ||
address debtAsset, | ||
bool supplyAsCollateral | ||
) external; | ||
|
||
/****************************************************************************/ | ||
/* isolate lending */ | ||
/****************************************************************************/ | ||
function isolateBorrow( | ||
uint32 poolId, | ||
address nftAsset, | ||
uint256[] calldata nftTokenIds, | ||
address asset, | ||
uint256[] calldata amounts | ||
) external; | ||
|
||
function isolateRepay( | ||
uint32 poolId, | ||
address nftAsset, | ||
uint256[] calldata nftTokenIds, | ||
address asset, | ||
uint256[] calldata amounts | ||
) external; | ||
|
||
function isolateAuction( | ||
uint32 poolId, | ||
address nftAsset, | ||
uint256[] calldata nftTokenIds, | ||
address asset, | ||
uint256[] calldata amounts | ||
) external; | ||
|
||
function isolateRedeem(uint32 poolId, address nftAsset, uint256[] calldata nftTokenIds, address asset) external; | ||
|
||
function isolateLiquidate( | ||
uint32 poolId, | ||
address nftAsset, | ||
uint256[] calldata nftTokenIds, | ||
address asset, | ||
bool supplyAsCollateral | ||
) external; | ||
|
||
/****************************************************************************/ | ||
/* Yield */ | ||
/****************************************************************************/ | ||
function yieldBorrowERC20(uint32 poolId, address asset, uint256 amount) external; | ||
|
||
function yieldRepayERC20(uint32 poolId, address asset, uint256 amount) external; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity ^0.8.19; | ||
|
||
import {IFlashLoanReceiver} from '../../interfaces/IFlashLoanReceiver.sol'; | ||
|
||
import {Constants} from '../helpers/Constants.sol'; | ||
import {Errors} from '../helpers/Errors.sol'; | ||
import {Events} from '../helpers/Events.sol'; | ||
|
||
import {PercentageMath} from '../math/PercentageMath.sol'; | ||
|
||
import {InputTypes} from '../types/InputTypes.sol'; | ||
import {DataTypes} from '../types/DataTypes.sol'; | ||
import {StorageSlot} from './StorageSlot.sol'; | ||
|
||
import {VaultLogic} from './VaultLogic.sol'; | ||
import {ValidateLogic} from './ValidateLogic.sol'; | ||
|
||
import 'forge-std/console.sol'; | ||
|
||
library FlashLoanLogic { | ||
function executeFlashLoanERC721(InputTypes.ExecuteFlashLoanERC721Params memory inputParams) public { | ||
DataTypes.PoolStorage storage ps = StorageSlot.getPoolStorage(); | ||
DataTypes.PoolData storage poolData = ps.poolLookup[inputParams.poolId]; | ||
|
||
uint256 i; | ||
IFlashLoanReceiver receiver = IFlashLoanReceiver(inputParams.receiverAddress); | ||
|
||
ValidateLogic.validateFlashLoanERC721Basic(inputParams, poolData); | ||
|
||
// only token owner can do flashloan | ||
for (i = 0; i < inputParams.nftTokenIds.length; i++) { | ||
DataTypes.AssetData storage assetData = poolData.assetLookup[inputParams.nftAssets[i]]; | ||
ValidateLogic.validateAssetBasic(assetData); | ||
require(assetData.assetType == Constants.ASSET_TYPE_ERC721, Errors.ASSET_TYPE_NOT_ERC721); | ||
require(assetData.isFlashLoanEnabled, Errors.ASSET_IS_FLASHLOAN_DISABLED); | ||
|
||
DataTypes.ERC721TokenData storage tokenData = VaultLogic.erc721GetTokenData( | ||
assetData, | ||
inputParams.nftTokenIds[i] | ||
); | ||
require(tokenData.owner == msg.sender, Errors.INVALID_TOKEN_OWNER); | ||
} | ||
|
||
// step 1: moving underlying asset forward to receiver contract | ||
VaultLogic.erc721TransferOutOnFlashLoan( | ||
inputParams.receiverAddress, | ||
inputParams.nftAssets, | ||
inputParams.nftTokenIds | ||
); | ||
|
||
// setup 2: execute receiver contract, doing something like aidrop | ||
bool execOpRet = receiver.executeOperationERC721( | ||
inputParams.nftAssets, | ||
inputParams.nftTokenIds, | ||
msg.sender, | ||
address(this), | ||
inputParams.params | ||
); | ||
require(execOpRet, Errors.FLASH_LOAN_EXEC_FAILED); | ||
|
||
// setup 3: moving underlying asset backward from receiver contract | ||
VaultLogic.erc721TransferInOnFlashLoan(inputParams.receiverAddress, inputParams.nftAssets, inputParams.nftTokenIds); | ||
|
||
emit Events.FlashLoanERC721( | ||
msg.sender, | ||
inputParams.nftAssets, | ||
inputParams.nftTokenIds, | ||
inputParams.receiverAddress | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.