hats-finance / Tokemak-0x4a2d708ea6b0c04186ecb774cfad1e50fb5efc0b

0 stars 0 forks source link

swapCost calculation is wrong in getRebalanceValueStats #2

Open hats-bug-reporter[bot] opened 5 months ago

hats-bug-reporter[bot] commented 5 months ago

Github username: @0xLogos Twitter username: -- Submission hash (on-chain): 0x5bf82eb6ab1b98d2e17926c06b24d8eb7e286afd766862cb0ae9e6f40cdd2017 Severity: high

Description: Here is how swap cost is calculated:

uint swapCost = outEthValue.subSaturate(inEthValue);

But it's wrong. For example if out value > in value swap cost will be > 0, but in reality it should be 0.

Correct way to calculate swap cost is:

uint swapCost = inEthValue.subSaturate(outEthValue);

Impact is bad rebalance can pass validation, this can lead to huge amount of loss for vault.

codenutt commented 5 months ago

The idea here is that "out value" is the amount leaving the vault and the "in value" is what we are getting back. When the in value is less, i.e. we lost value, we consider that a cost.