Skip to content

Commit

Permalink
fix: self report refund override
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlagonia committed Oct 28, 2024
1 parent e5cf2af commit 0d952b2
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 8 deletions.
10 changes: 7 additions & 3 deletions contracts/VaultV3.vy
Original file line number Diff line number Diff line change
Expand Up @@ -1257,16 +1257,20 @@ def _process_report(strategy: address) -> (uint256, uint256):
self.strategies[strategy].current_debt = current_debt
self.total_debt += gain
else:
self.total_idle = total_assets

# Add in any refunds since it is idle.
current_debt = unsafe_add(current_debt, total_refunds)
self.total_idle = current_debt

# Or record any reported loss
elif loss > 0:
current_debt = unsafe_sub(current_debt, loss)
if strategy != self:
self.strategies[strategy].current_debt = current_debt
self.total_debt -= loss
else:
self.total_idle = total_assets
# Add in any refunds since it is idle.
current_debt = unsafe_add(current_debt, total_refunds)
self.total_idle = current_debt

# Issue shares for fees that were calculated above if applicable.
if total_fees_shares > 0:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "yearn-vaults-v3",
"devDependencies": {
"@openzeppelin/contracts": "^4.9.0",
"@openzeppelin/contracts": "^4.9.5",
"hardhat": "^2.12.2",
"prettier": "^2.6.0",
"prettier-plugin-solidity": "^1.0.0-beta.19",
Expand Down
115 changes: 115 additions & 0 deletions tests/unit/vault/test_strategy_accounting.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,3 +651,118 @@ def test_set_accountant__with_accountant(gov, vault, deploy_accountant):
assert event[0].accountant == accountant.address

assert vault.accountant() == accountant.address


def test_process_report_on_self__gain_and_refunds(
chain,
gov,
asset,
vault,
set_fees_for_strategy,
airdrop_asset,
deploy_flexible_accountant,
):
vault_balance = asset.balanceOf(vault)
gain = vault_balance // 10
loss = 0
management_fee = 0
performance_fee = 0
refund_ratio = 5_000
refund = gain * refund_ratio // MAX_BPS_ACCOUNTANT

accountant = deploy_flexible_accountant(vault)
# set up accountant
asset.mint(accountant, gain, sender=gov)

set_fees_for_strategy(
gov, vault, accountant, management_fee, performance_fee, refund_ratio
)

initial_idle = vault.totalIdle()

airdrop_asset(gov, asset, vault, gain)

# Not yet recorded
assert vault.totalIdle() == initial_idle
assert asset.balanceOf(vault) == initial_idle + gain
pps_before = vault.pricePerShare()
assets_before = vault.totalAssets()
supply_before = vault.totalSupply()
tx = vault.process_report(vault.address, sender=gov)
event = list(tx.decode_logs(vault.StrategyReported))

assert len(event) == 1
assert event[0].strategy == vault.address
assert event[0].gain == gain
assert event[0].loss == 0
assert event[0].current_debt == vault_balance + gain + refund
assert event[0].total_fees == 0
assert event[0].total_refunds == refund

# Due to refunds, pps should have increased
assert vault.pricePerShare() == pps_before
assert vault.totalAssets() == vault_balance + gain + refund
assert vault.totalSupply() > supply_before
assert vault.totalDebt() == 0
assert vault.totalIdle() == vault_balance + gain + refund
assert asset.balanceOf(vault) == vault_balance + gain + refund

chain.pending_timestamp += DAY
chain.mine(timestamp=chain.pending_timestamp)

assert vault.pricePerShare() > pps_before


def test_process_report_on_self__loss_and_refunds(
chain,
gov,
asset,
vault,
set_fees_for_strategy,
airdrop_asset,
deploy_flexible_accountant,
):
vault_balance = asset.balanceOf(vault)
gain = 0
loss = vault_balance // 10
management_fee = 0
performance_fee = 0
refund_ratio = 5_000
refund = loss * refund_ratio // MAX_BPS_ACCOUNTANT

accountant = deploy_flexible_accountant(vault)
# set up accountant
asset.mint(accountant, loss, sender=gov)

set_fees_for_strategy(
gov, vault, accountant, management_fee, performance_fee, refund_ratio
)

initial_idle = vault.totalIdle()

asset.transfer(gov, loss, sender=vault.address)

# Not yet recorded
assert vault.totalIdle() == initial_idle
assert asset.balanceOf(vault) == initial_idle - loss
pps_before = vault.pricePerShare()
assets_before = vault.totalAssets()
supply_before = vault.totalSupply()
tx = vault.process_report(vault.address, sender=gov)
event = list(tx.decode_logs(vault.StrategyReported))

assert len(event) == 1
assert event[0].strategy == vault.address
assert event[0].gain == gain
assert event[0].loss == loss
assert event[0].current_debt == vault_balance + refund - loss
assert event[0].total_fees == 0
assert event[0].total_refunds == refund

# Due to refunds, pps should have increased
assert vault.pricePerShare() < pps_before
assert vault.totalAssets() == vault_balance + refund - loss
assert vault.totalSupply() == supply_before
assert vault.totalDebt() == 0
assert vault.totalIdle() == vault_balance + refund - loss
assert asset.balanceOf(vault) == vault_balance + refund - loss
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,10 @@
"@nomicfoundation/solidity-analyzer-win32-ia32-msvc" "0.1.0"
"@nomicfoundation/solidity-analyzer-win32-x64-msvc" "0.1.0"

"@openzeppelin/contracts@^4.7.3":
version "4.8.2"
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.8.2.tgz#d815ade0027b50beb9bcca67143c6bcc3e3923d6"
integrity sha512-kEUOgPQszC0fSYWpbh2kT94ltOJwj1qfT2DWo+zVttmGmf97JZ99LspePNaeeaLhCImaHVeBbjaQFZQn7+Zc5g==
"@openzeppelin/contracts@^4.9.5":
version "4.9.6"
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.9.6.tgz#2a880a24eb19b4f8b25adc2a5095f2aa27f39677"
integrity sha512-xSmezSupL+y9VkHZJGDoCBpmnB2ogM13ccaYDWqJTfS3dbuHkgjuwDFUmaFauBCboQMGB/S5UqUl2y54X99BmA==

"@scure/base@~1.1.0":
version "1.1.1"
Expand Down

0 comments on commit 0d952b2

Please sign in to comment.