-
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.
some testcases for flash loan erc721
- Loading branch information
1 parent
41e1aeb
commit ce2bf1d
Showing
6 changed files
with
268 additions
and
1 deletion.
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,125 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity ^0.8.19; | ||
|
||
import 'src/libraries/helpers/Constants.sol'; | ||
import 'src/libraries/helpers/Errors.sol'; | ||
|
||
import 'test/mocks/MockFlashLoanReceiver.sol'; | ||
|
||
import 'test/helpers/TestUser.sol'; | ||
import 'test/setup/TestWithBaseAction.sol'; | ||
|
||
contract TestIntFlashLoanERC721 is TestWithBaseAction { | ||
MockFlashLoanReceiver mockReceiver; | ||
|
||
struct TestCaseLocalVars { | ||
// prepare | ||
uint256[] baycTokenIds; | ||
uint256[] maycTokenIds; | ||
address[] flNftAssets; | ||
uint256[] flTokenIds; | ||
// results | ||
uint256 poolBalanceBefore; | ||
uint256 poolBalanceAfter; | ||
} | ||
|
||
function onSetUp() public virtual override { | ||
super.onSetUp(); | ||
|
||
initCommonPools(); | ||
|
||
mockReceiver = new MockFlashLoanReceiver(); | ||
} | ||
|
||
function prepareNftTokens(TestCaseLocalVars memory testVars, bool isolate) internal { | ||
// deposit | ||
if (isolate) { | ||
testVars.baycTokenIds = prepareIsolateBAYC(tsDepositor1); | ||
testVars.maycTokenIds = prepareIsolateMAYC(tsDepositor1); | ||
} else { | ||
testVars.baycTokenIds = prepareCrossBAYC(tsDepositor1); | ||
testVars.maycTokenIds = prepareCrossMAYC(tsDepositor1); | ||
} | ||
|
||
testVars.flNftAssets = new address[](testVars.baycTokenIds.length + testVars.maycTokenIds.length); | ||
testVars.flTokenIds = new uint256[](testVars.baycTokenIds.length + testVars.maycTokenIds.length); | ||
|
||
uint idx = 0; | ||
for (uint i = 0; i < testVars.baycTokenIds.length; i++) { | ||
testVars.flNftAssets[idx] = address(tsBAYC); | ||
testVars.flTokenIds[idx] = testVars.baycTokenIds[i]; | ||
idx++; | ||
} | ||
for (uint i = 0; i < testVars.maycTokenIds.length; i++) { | ||
testVars.flNftAssets[idx] = address(tsMAYC); | ||
testVars.flTokenIds[idx] = testVars.maycTokenIds[i]; | ||
idx++; | ||
} | ||
} | ||
|
||
function test_RevertIf_IdListEmpty() public { | ||
address[] memory flNftAssets; | ||
uint256[] memory flTokenIds; | ||
|
||
tsHEVM.expectRevert(bytes(Errors.INVALID_ID_LIST)); | ||
tsDepositor1.flashLoanERC721(tsCommonPoolId, flNftAssets, flTokenIds, address(mockReceiver), ''); | ||
} | ||
|
||
function test_RevertIf_FlashLoanDisabled() public { | ||
address[] memory flNftAssets = new address[](1); | ||
flNftAssets[0] = address(tsBAYC); | ||
uint256[] memory flTokenIds = new uint256[](1); | ||
flTokenIds[0] = 10001; | ||
|
||
tsHEVM.expectRevert(bytes(Errors.ASSET_IS_FLASHLOAN_DISABLED)); | ||
tsDepositor1.flashLoanERC721(tsCommonPoolId, flNftAssets, flTokenIds, address(mockReceiver), ''); | ||
} | ||
|
||
function test_RevertIf_InvalidTokenOwner() public { | ||
TestCaseLocalVars memory testVars; | ||
|
||
tsPoolManager.setAssetFlashLoan(tsCommonPoolId, address(tsBAYC), true); | ||
tsPoolManager.setAssetFlashLoan(tsCommonPoolId, address(tsMAYC), true); | ||
|
||
prepareNftTokens(testVars, false); | ||
|
||
tsHEVM.expectRevert(bytes(Errors.INVALID_TOKEN_OWNER)); | ||
tsDepositor2.flashLoanERC721(tsCommonPoolId, testVars.flNftAssets, testVars.flTokenIds, address(mockReceiver), ''); | ||
} | ||
|
||
function test_Should_FlashLoan_CrossTokens() public { | ||
TestCaseLocalVars memory testVars; | ||
|
||
tsPoolManager.setAssetFlashLoan(tsCommonPoolId, address(tsBAYC), true); | ||
tsPoolManager.setAssetFlashLoan(tsCommonPoolId, address(tsMAYC), true); | ||
|
||
prepareNftTokens(testVars, false); | ||
|
||
testVars.poolBalanceBefore = tsBAYC.balanceOf(address(tsPoolManager)); | ||
|
||
// flash loan | ||
tsDepositor1.flashLoanERC721(tsCommonPoolId, testVars.flNftAssets, testVars.flTokenIds, address(mockReceiver), ''); | ||
|
||
// check results | ||
testVars.poolBalanceAfter = tsBAYC.balanceOf(address(tsPoolManager)); | ||
assertEq(testVars.poolBalanceAfter, testVars.poolBalanceBefore, 'tsPoolManager balance'); | ||
} | ||
|
||
function test_Should_FlashLoan_IsolateTokens() public { | ||
TestCaseLocalVars memory testVars; | ||
|
||
tsPoolManager.setAssetFlashLoan(tsCommonPoolId, address(tsBAYC), true); | ||
tsPoolManager.setAssetFlashLoan(tsCommonPoolId, address(tsMAYC), true); | ||
|
||
prepareNftTokens(testVars, true); | ||
|
||
testVars.poolBalanceBefore = tsBAYC.balanceOf(address(tsPoolManager)); | ||
|
||
// flash loan | ||
tsDepositor1.flashLoanERC721(tsCommonPoolId, testVars.flNftAssets, testVars.flTokenIds, address(mockReceiver), ''); | ||
|
||
// check results | ||
testVars.poolBalanceAfter = tsBAYC.balanceOf(address(tsPoolManager)); | ||
assertEq(testVars.poolBalanceAfter, testVars.poolBalanceBefore, 'tsPoolManager balance'); | ||
} | ||
} |
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,56 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity ^0.8.19; | ||
|
||
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; | ||
import {IERC721} from '@openzeppelin/contracts/token/ERC721/IERC721.sol'; | ||
import {ERC721Holder} from '@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol'; | ||
|
||
contract MockFlashLoanReceiver is ERC721Holder { | ||
address[] public savedAssets; | ||
uint256[] public savedAmounts; | ||
address[] public savedNftAssets; | ||
uint256[] public savedTokenIds; | ||
address public savedInitiator; | ||
address public savedOperator; | ||
bytes public savedParams; | ||
|
||
function executeOperationERC20( | ||
address[] calldata assets, | ||
uint256[] calldata amounts, | ||
address initiator, | ||
address operator, | ||
bytes calldata params | ||
) public returns (bool) { | ||
savedAssets = assets; | ||
savedAmounts = amounts; | ||
savedInitiator = initiator; | ||
savedOperator = operator; | ||
savedParams = params; | ||
|
||
for (uint i = 0; i < assets.length; i++) { | ||
IERC20(assets[i]).approve(operator, amounts[i]); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function executeOperationERC721( | ||
address[] calldata nftAssets, | ||
uint256[] calldata tokenIds, | ||
address initiator, | ||
address operator, | ||
bytes calldata params | ||
) public returns (bool) { | ||
savedNftAssets = nftAssets; | ||
savedTokenIds = tokenIds; | ||
savedInitiator = initiator; | ||
savedOperator = operator; | ||
savedParams = params; | ||
|
||
for (uint i = 0; i < nftAssets.length; i++) { | ||
IERC721(nftAssets[i]).approve(operator, tokenIds[i]); | ||
} | ||
|
||
return true; | ||
} | ||
} |
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