Division by zero will result in reverting the transaction.
Division by zero makes it impossible to calculate the weights and update them.
function testUpdatesNotSuccessfullyAfterUpdateInterval() public {
int256[] memory initialWeights = new int256[]();
initialWeights[0] = 0.0000000005e18;
initialWeights[1] = 0.0000000005e18;
initialWeights[2] = 0;
initialWeights[3] = 0;
mockPool.setInitialWeights(initialWeights);
mockPool.setPoolRegistry(9);
vm.startPrank(owner);
updateWeightRunner.setApprovedActionsForPool(address(mockPool), 9);
vm.stopPrank();
int216 fixedValue = 1000;
chainlinkOracle = deployOracle(fixedValue, 3601);
vm.startPrank(owner);
updateWeightRunner.addOracle(OracleWrapper(chainlinkOracle));
vm.stopPrank();
vm.startPrank(address(mockPool));
address[][] memory oracles = new address[][]();
oracles[0] = new address[]();
oracles[0][0] = address(chainlinkOracle);
uint64[] memory lambda = new uint64[]();
lambda[0] = 0.0000000005e18;
updateWeightRunner.setRuleForPool(
IQuantAMMWeightedPool.PoolSettings({
assets: new IERC20[](0),
rule: IUpdateRule(mockRule),
oracles: oracles,
updateInterval: 0,
lambda: lambda,
epsilonMax: 0.2e18,
absoluteWeightGuardRail: 0.2e18,
maxTradeSizeRatio: 0.2e18,
ruleParameters: new int256[][](),
poolManager: addr2
})
);
vm.stopPrank();
vm.startPrank(addr2);
updateWeightRunner.InitialisePoolLastRunTime(address(mockPool), 1);
vm.stopPrank();
vm.warp(block.timestamp + 10000000);
mockRule.CalculateNewWeights(
initialWeights,
new int256[](0),
address(mockPool),
new int256[][](),
new uint64[](0),
0.2e18,
0.2e18
);
vm.expectRevert(stdError.divisionError);
updateWeightRunner.performUpdate(address(mockPool));
}
function testCalculateMultiplierAndSetWeightsFromRuleFailureDueToDivisionByZero() public {
int256[] memory weights = new int256[]();
weights[0] = 0.5e18;
weights[1] = 0.5e18;
weights[2] = 0;
weights[3] = 0;
mockPool.setPoolRegistry(8);
vm.startPrank(owner);
updateWeightRunner.setApprovedActionsForPool(address(mockPool), 8);
vm.stopPrank();
uint40 blockTime = uint40(block.timestamp);
int216 fixedValue = 1000;
uint delay = 3600;
chainlinkOracle = deployOracle(fixedValue, delay);
vm.startPrank(owner);
updateWeightRunner.addOracle(OracleWrapper(chainlinkOracle));
vm.stopPrank();
vm.startPrank(address(mockPool));
address[][] memory oracles = new address[][]();
oracles[0] = new address[]();
oracles[0][0] = address(chainlinkOracle);
uint64[] memory lambda = new uint64[]();
lambda[0] = 0.0000000005e18;
updateWeightRunner.setRuleForPool(
IQuantAMMWeightedPool.PoolSettings({
assets: new IERC20[](0),
rule: mockRule,
oracles: oracles,
updateInterval: 100,
lambda: lambda,
epsilonMax: 0.2e18,
absoluteWeightGuardRail: 0.2e18,
maxTradeSizeRatio: 0.2e18,
ruleParameters: new int256[][](),
poolManager: addr2
})
);
vm.stopPrank();
vm.startPrank(addr2);
updateWeightRunner.InitialisePoolLastRunTime(address(mockPool), blockTime);
vm.stopPrank();
vm.startPrank(addr2);
updateWeightRunner.setWeightsManually(weights, address(mockPool), 6, 2);
vm.stopPrank();
mockPool.setPoolRegistry(32);
int256[] memory poolWeights = new int256[]();
poolWeights[0] = 0.6e18;
poolWeights[1] = 0.4e18;
uint256[] memory currentWeightsUnsigned = IWeightedPool(address(mockPool)).getNormalizedWeights();
int256[] memory currentWeights = new int256[]();
for (uint i; i < currentWeights.length; ) {
currentWeights[i] = int256(currentWeightsUnsigned[i]);
unchecked {
i++;
}
}
vm.startPrank(address(mockRule));
vm.expectRevert(stdError.divisionError);
updateWeightRunner.calculateMultiplierAndSetWeightsFromRule(UpdateWeightRunner.CalculateMuliplierAndSetWeightsLocal({
currentWeights: currentWeights,
updatedWeights: poolWeights,
updateInterval: 0,
absoluteWeightGuardRail18: 0.2e18,
poolAddress: address(mockPool)
})
);
vm.stopPrank();
}
Add a requirement that it's bigger than zero.
And make a condition that if it's zero the blockMultiplier will be its default (0)