Skip to content

Commit

Permalink
unblock rebalancing after an auction ends early (#1195)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrent committed Sep 10, 2024
1 parent 36ab2de commit cee7bc2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions contracts/p0/BackingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity 0.8.19;

import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/math/Math.sol";
import "./mixins/TradingLib.sol";
import "./mixins/Trading.sol";
import "../interfaces/IAsset.sol";
Expand Down Expand Up @@ -65,6 +66,8 @@ contract BackingManagerP0 is TradingP0, IBackingManager {
trade = super.settleTrade(sell);
delete tokensOut[trade.sell()];

tradeEnd[trade.KIND()] = uint48(Math.min(tradeEnd[trade.KIND()], block.timestamp));

// if the settler is the trade contract itself, try chaining with another rebalance()
if (_msgSender() == address(trade)) {
// solhint-disable-next-line no-empty-blocks
Expand Down
3 changes: 3 additions & 0 deletions contracts/p1/BackingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ contract BackingManagerP1 is TradingP1, IBackingManager {
delete tokensOut[sell];
trade = super.settleTrade(sell); // nonReentrant

TradeKind kind = trade.KIND();
if (tradeEnd[kind] > block.timestamp) tradeEnd[kind] = uint48(block.timestamp);

// if the settler is the trade contract itself, try chaining with another rebalance()
if (_msgSender() == address(trade)) {
// solhint-disable-next-line no-empty-blocks
Expand Down
25 changes: 25 additions & 0 deletions test/Recollateralization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3412,11 +3412,36 @@ describe(`Recollateralization - P${IMPLEMENTATION}`, () => {
expect(await trade2.canSettle()).to.equal(false)

// Bid + settle RSR auction
await expect(await router.connect(addr1).bid(trade2.address, addr1.address)).to.emit(
backingManager,
'TradeSettled'
)
})

it('and be able to continue rebalance immediately -- regression test 09/05/2024', async () => {
// Context: https://github.com/code-423n4/2024-07-reserve-findings/issues/6

await token1.connect(addr1).approve(trade2.address, initialBal)

// Advance to midpoint of auction
await advanceToTimestamp((await getLatestBlockTimestamp()) + 900)
expect(await trade2.status()).to.equal(1) // TradeStatus.OPEN
expect(await trade2.canSettle()).to.equal(false)

// Bid + settle RSR auction
await expect(await router.connect(addr1).bid(trade2.address, addr1.address)).to.emit(
backingManager,
'TradeSettled'
)
expect(await broker.dutchTradeDisabled(token1.address)).to.equal(false)

// Should be able to immediately continue
const amt = await token1.balanceOf(backingManager.address)
await token1.burn(backingManager.address, amt)

// Should not revert, if we were to continue
await backingManager.callStatic.rebalance(TradeKind.DUTCH_AUCTION)
await token1.mint(backingManager.address, amt)
})

it('via fallback to Batch Auction', async () => {
Expand Down

0 comments on commit cee7bc2

Please sign in to comment.