The _depositToVaults function in VaultControllerStrategy.sol does not correctly update the total deposits and principal deposits, leading to incorrect state variables and potential inconsistencies in the contract's behavior.
The _depositToVaults function is responsible for depositing tokens into various vaults according to specified limits and conditions. However, the function does not correctly update the state variables totalPrincipalDeposits and totalDeposits, resulting in incorrect balances and deposits being reported. This discrepancy can lead to incorrect calculations and decisions based on the contract's state.
The root cause of the issue appears to be that the _depositToVaults function does not properly update the state variables after performing deposit operations. This can be due to variable shadowing or incorrect logic within the function that prevents the state from being updated correctly.
it('should handle deposit limits correctly in _depositToVaults', async () => {
const { adrs, strategy, token, stakingController, vaults } = await loadFixture(deployFixture);
await token.transfer(adrs.strategy, toEther(1000));
await strategy.deposit(toEther(500), encodeVaults([]));
console.log('Initial balance of stakingController:', fromEther(await token.balanceOf(adrs.stakingController)));
console.log('Initial totalPrincipalDeposits:', fromEther(await strategy.totalPrincipalDeposits()));
console.log('Initial totalDeposits:', fromEther(await strategy.getTotalDeposits()));
const minDepositLimit = toEther(10);
const maxDepositLimit = toEther(120);
await stakingController.setDepositLimits(minDepositLimit, maxDepositLimit);
console.log('Deposit limits set: minDepositLimit =', fromEther(minDepositLimit), ', maxDepositLimit =', fromEther(maxDepositLimit));
try {
await strategy.deposit(toEther(5), encodeVaults([]));
} catch (error) {
if (error instanceof Error) {
console.log('Error when depositing below minimum limit:', error.message);
} else {
console.log('Unknown error when depositing below minimum limit:', error);
}
}
try {
await strategy.deposit(toEther(200), encodeVaults([]));
} catch (error) {
if (error instanceof Error) {
console.log('Error when depositing above maximum limit:', error.message);
} else {
console.log('Unknown error when depositing above maximum limit:', error);
}
}
await strategy.deposit(toEther(50), encodeVaults([]));
console.log('Balance of stakingController after deposit within limits:', fromEther(await token.balanceOf(adrs.stakingController)));
console.log('TotalPrincipalDeposits after deposit within limits:', fromEther(await strategy.totalPrincipalDeposits()));
console.log('TotalDeposits after deposit within limits:', fromEther(await strategy.getTotalDeposits()));
await stakingController.setDepositLimits(toEther(20), toEther(100));
console.log('New deposit limits set: minDepositLimit =', fromEther(toEther(20)), ', maxDepositLimit =', fromEther(toEther(100)));
try {
await strategy.deposit(toEther(15), encodeVaults([]));
} catch (error) {
if (error instanceof Error) {
console.log('Error when depositing below new minimum limit:', error.message);
} else {
console.log('Unknown error when depositing below new minimum limit:', error);
}
}
try {
await strategy.deposit(toEther(150), encodeVaults([]));
} catch (error) {
if (error instanceof Error) {
console.log('Error when depositing above new maximum limit:', error.message);
} else {
console.log('Unknown error when depositing above new maximum limit:', error);
}
}
await strategy.deposit(toEther(80), encodeVaults([]));
console.log('Balance of stakingController after deposit within new limits:', fromEther(await token.balanceOf(adrs.stakingController)));
console.log('TotalPrincipalDeposits after deposit within new limits:', fromEther(await strategy.totalPrincipalDeposits()));
console.log('TotalDeposits after deposit within new limits:', fromEther(await strategy.getTotalDeposits()));
assert.equal(
fromEther(await token.balanceOf(adrs.stakingController)),
370,
'Total deposits should be updated correctly'
);
assert.equal(
fromEther(await strategy.totalPrincipalDeposits()),
630,
'Total principal deposits should be updated correctly'
);
assert.equal(
fromEther(await strategy.getTotalDeposits()),
630,
'Total deposits should be updated correctly'
);
});
The test case demonstrates that the initial balance and deposits remain unchanged after the deposit operations, indicating that the _depositToVaults function is not updating the state variables correctly. The expected total deposits should be 370 (1000 - 630), but the actual value remains 1500, showing that the deposits were not processed correctly.
The incorrect state updates can lead to inconsistencies in the contract's behavior, potentially affecting the accuracy of the reported balances and deposits. This can result in incorrect calculations and decisions based on the contract's state. Users may be misled by the incorrect balances, leading to potential financial losses or incorrect strategic decisions.