describe('Flash Loan Liquidity Manipulation Attack', function() {
let s, poolId, attacker, flashLoanAmount;
beforeEach(async () => {
s = await setupWithPool();
poolId = s.poolId;
attacker = s.acc2;
});
it('Should detect potential liquidity manipulation via flash loan', async () => {
const tokenHolder = s.impersonatedSigner;
await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: [tokenHolder.address],
});
const originalPoolBalance = await s.diva.getPoolParameters(poolId);
const flashLoanAmount = originalPoolBalance.collateralBalance.mul(100);
await s.collateralTokenContract
.connect(tokenHolder)
.approve(s.aaveDIVAWrapper.target, flashLoanAmount);
await expect(
s.aaveDIVAWrapper
.connect(tokenHolder)
.addLiquidity(
poolId,
flashLoanAmount,
tokenHolder.address,
tokenHolder.address
)
).to.be.revertedWith("Liquidity addition exceeds safe limits");
});
it('Should prevent massive liquidity addition beyond percentage threshold', async () => {
const originalPoolBalance = await s.diva.getPoolParameters(poolId);
const maxAllowedLiquidity = originalPoolBalance.collateralBalance.mul(10);
await expect(
s.aaveDIVAWrapper
.connect(s.impersonatedSigner)
.addLiquidity(
poolId,
maxAllowedLiquidity,
s.impersonatedSigner.address,
s.impersonatedSigner.address
)
).to.be.revertedWith("Liquidity exceeds maximum allowed percentage");
});
});
function _addLiquidity(
bytes32 _poolId,
uint256 _collateralAmount,
address _longRecipient,
address _shortRecipient
) internal {
IDIVA.Pool memory _pool = IDIVA(_diva).getPoolParameters(_poolId);
uint256 maxAllowedLiquidity = _pool.collateralBalance * 10;
require(
_collateralAmount <= maxAllowedLiquidity,
"Liquidity exceeds maximum allowed percentage"
);
}