Skip to content

Commit

Permalink
Standardize use of pips for tests and make use more logic prices for …
Browse files Browse the repository at this point in the history
…the assets
  • Loading branch information
sunbreak1211 committed Sep 23, 2024
1 parent 614f694 commit 41e8019
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 59 deletions.
33 changes: 8 additions & 25 deletions test/FlapperUniV2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { FlapperUniV2Config, FlapperInit } from "deploy/FlapperInit.sol";
import { FlapperUniV2 } from "src/FlapperUniV2.sol";
import { Babylonian } from "src/Babylonian.sol";
import { SplitterMock } from "test/mocks/SplitterMock.sol";
import { MedianizerMock } from "test/mocks/MedianizerMock.sol";
import "./helpers/UniswapV2Library.sol";

interface ChainlogLike {
Expand Down Expand Up @@ -67,32 +68,14 @@ interface UniV2FactoryLike {
function createPair(address, address) external returns (address);
}

contract MockMedianizer {
uint256 public price;
mapping (address => uint256) public bud;

function setPrice(uint256 price_) external {
price = price_;
}

function kiss(address a) external {
bud[a] = 1;
}

function read() external view returns (bytes32) {
require(bud[msg.sender] == 1, "MockMedianizer/not-authorized");
return bytes32(price);
}
}

contract FlapperUniV2Test is DssTest {
using stdStorage for StdStorage;

SplitterMock public splitter;
FlapperUniV2 public flapper;
FlapperUniV2 public imxFlapper;
MockMedianizer public medianizer;
MockMedianizer public imxMedianizer;
MedianizerMock public medianizer;
MedianizerMock public imxMedianizer;

address USDS_JOIN;
address SPOT;
Expand Down Expand Up @@ -142,10 +125,10 @@ contract FlapperUniV2Test is DssTest {
PairLike(UNIV2_USDS_IMX_PAIR).sync();
}

(flapper, medianizer) = setUpFlapper(SKY, UNIV2_SKY_USDS_PAIR, 727 * WAD, "MCD_FLAP") ;
(flapper, medianizer) = setUpFlapper(SKY, UNIV2_SKY_USDS_PAIR, 0.06 * 1e18, "MCD_FLAP") ;
assertEq(flapper.usdsFirst(), false);

(imxFlapper, imxMedianizer) = setUpFlapper(IMX, UNIV2_USDS_IMX_PAIR, 654 * WAD / 100, bytes32(0));
(imxFlapper, imxMedianizer) = setUpFlapper(IMX, UNIV2_USDS_IMX_PAIR, 1.85 * 1e18, bytes32(0));
assertEq(imxFlapper.usdsFirst(), true);

changeFlapper(address(flapper)); // Use SKY flapper by default
Expand All @@ -166,9 +149,9 @@ contract FlapperUniV2Test is DssTest {

function setUpFlapper(address gem, address pair, uint256 price, bytes32 prevChainlogKey)
internal
returns (FlapperUniV2 _flapper, MockMedianizer _medianizer)
returns (FlapperUniV2 _flapper, MedianizerMock _medianizer)
{
_medianizer = new MockMedianizer();
_medianizer = new MedianizerMock();
_medianizer.kiss(address(this));

_flapper = FlapperUniV2(FlapperDeploy.deployFlapperUniV2({
Expand Down Expand Up @@ -225,7 +208,7 @@ contract FlapperUniV2Test is DssTest {
}

function refAmountOut(uint256 amountIn, address pip) internal view returns (uint256) {
return amountIn * WAD / (uint256(MockMedianizer(pip).read()) * RAY / SpotterLike(SPOT).par());
return amountIn * WAD / (uint256(MedianizerMock(pip).read()) * RAY / SpotterLike(SPOT).par());
}

function uniV2GemForUsds(uint256 amountIn, address gem) internal view returns (uint256 amountOut) {
Expand Down
33 changes: 8 additions & 25 deletions test/FlapperUniV2SwapOnly.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { FlapperDeploy } from "deploy/FlapperDeploy.sol";
import { FlapperUniV2Config, FlapperInit } from "deploy/FlapperInit.sol";
import { FlapperUniV2SwapOnly } from "src/FlapperUniV2SwapOnly.sol";
import { SplitterMock } from "test/mocks/SplitterMock.sol";
import { MedianizerMock } from "test/mocks/MedianizerMock.sol";
import "./helpers/UniswapV2Library.sol";

interface ChainlogLike {
Expand Down Expand Up @@ -65,32 +66,14 @@ interface UniV2FactoryLike {
function createPair(address, address) external returns (address);
}

contract MockMedianizer {
uint256 public price;
mapping (address => uint256) public bud;

function setPrice(uint256 price_) external {
price = price_;
}

function kiss(address a) external {
bud[a] = 1;
}

function read() external view returns (bytes32) {
require(bud[msg.sender] == 1, "MockMedianizer/not-authorized");
return bytes32(price);
}
}

contract FlapperUniV2SwapOnlyTest is DssTest {
using stdStorage for StdStorage;

SplitterMock public splitter;
FlapperUniV2SwapOnly public flapper;
FlapperUniV2SwapOnly public imxFlapper;
MockMedianizer public medianizer;
MockMedianizer public imxMedianizer;
MedianizerMock public medianizer;
MedianizerMock public imxMedianizer;

address USDS_JOIN;
address SPOT;
Expand Down Expand Up @@ -140,10 +123,10 @@ contract FlapperUniV2SwapOnlyTest is DssTest {
PairLike(UNIV2_USDS_IMX_PAIR).sync();
}

(flapper, medianizer) = setUpFlapper(SKY, UNIV2_SKY_USDS_PAIR, 727 * WAD, "MCD_FLAP") ;
(flapper, medianizer) = setUpFlapper(SKY, UNIV2_SKY_USDS_PAIR, 0.06 * 1e18, "MCD_FLAP") ;
assertEq(flapper.usdsFirst(), false);

(imxFlapper, imxMedianizer) = setUpFlapper(IMX, UNIV2_USDS_IMX_PAIR, 654 * WAD / 100, bytes32(0));
(imxFlapper, imxMedianizer) = setUpFlapper(IMX, UNIV2_USDS_IMX_PAIR, 1.85 * 1e18, bytes32(0));
assertEq(imxFlapper.usdsFirst(), true);

changeFlapper(address(flapper)); // Use SKY flapper by default
Expand All @@ -164,9 +147,9 @@ contract FlapperUniV2SwapOnlyTest is DssTest {

function setUpFlapper(address gem, address pair, uint256 price, bytes32 prevChainlogKey)
internal
returns (FlapperUniV2SwapOnly _flapper, MockMedianizer _medianizer)
returns (FlapperUniV2SwapOnly _flapper, MedianizerMock _medianizer)
{
_medianizer = new MockMedianizer();
_medianizer = new MedianizerMock();
_medianizer.kiss(address(this));

_flapper = FlapperUniV2SwapOnly(FlapperDeploy.deployFlapperUniV2({
Expand Down Expand Up @@ -219,7 +202,7 @@ contract FlapperUniV2SwapOnlyTest is DssTest {
}

function refAmountOut(uint256 amountIn, address pip) internal view returns (uint256) {
return amountIn * WAD / (uint256(MockMedianizer(pip).read()) * RAY / SpotterLike(SPOT).par());
return amountIn * WAD / (uint256(MedianizerMock(pip).read()) * RAY / SpotterLike(SPOT).par());
}

function uniV2GemForUsds(uint256 amountIn, address gem) internal view returns (uint256 amountOut) {
Expand Down
15 changes: 6 additions & 9 deletions test/Splitter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Splitter } from "src/Splitter.sol";
import { FlapperUniV2SwapOnly } from "src/FlapperUniV2SwapOnly.sol";
import { StakingRewardsMock } from "test/mocks/StakingRewardsMock.sol";
import { GemMock } from "test/mocks/GemMock.sol";
import { MedianizerMock } from "test/mocks/MedianizerMock.sol";
import "./helpers/UniswapV2Library.sol";

interface ChainlogLike {
Expand Down Expand Up @@ -75,7 +76,7 @@ contract SplitterTest is DssTest {
Splitter public splitter;
StakingRewardsMock public farm;
FlapperUniV2SwapOnly public flapper;
PipLike public medianizer;
MedianizerMock public medianizer;

address USDS_JOIN;
address SPOT;
Expand All @@ -102,7 +103,7 @@ contract SplitterTest is DssTest {
USDS = ChainlogLike(LOG).getAddress("USDS");
SKY = ChainlogLike(LOG).getAddress("SKY");
PAUSE_PROXY = ChainlogLike(LOG).getAddress("MCD_PAUSE_PROXY");
medianizer = PipLike(ChainlogLike(LOG).getAddress("PIP_MKR"));
medianizer = new MedianizerMock();
vat = VatLike(ChainlogLike(LOG).getAddress("MCD_VAT"));
vow = VowLike(ChainlogLike(LOG).getAddress("MCD_VOW"));
end = EndLike(ChainlogLike(LOG).getAddress("MCD_END"));
Expand Down Expand Up @@ -177,14 +178,14 @@ contract SplitterTest is DssTest {
(uint256 reserveUsds, ) = UniswapV2Library.getReserves(UNIV2_FACTORY, USDS, SKY);
uint256 minimalUsdsReserve = 280_000 * WAD;
if (reserveUsds < minimalUsdsReserve) {
changeMedianizerPrice(727 * WAD);
changeUniV2Price(medianizer.read(), SKY, UNIV2_SKY_USDS_PAIR);
medianizer.setPrice(0.06 * 1e18);
changeUniV2Price(uint256(medianizer.read()), SKY, UNIV2_SKY_USDS_PAIR);
(reserveUsds, ) = UniswapV2Library.getReserves(UNIV2_FACTORY, USDS, SKY);
if (reserveUsds < minimalUsdsReserve) {
topUpLiquidity(minimalUsdsReserve - reserveUsds, SKY, UNIV2_SKY_USDS_PAIR);
}
} else {
changeMedianizerPrice(uniV2UsdsForGem(WAD, SKY));
medianizer.setPrice(uniV2UsdsForGem(WAD, SKY));
}

// Create additional surplus if needed
Expand Down Expand Up @@ -228,10 +229,6 @@ contract SplitterTest is DssTest {
PairLike(pair).sync();
}

function changeMedianizerPrice(uint256 usdsForGem) internal {
vm.store(address(medianizer), bytes32(uint256(1)), bytes32(block.timestamp << 128 | usdsForGem));
}

function topUpLiquidity(uint256 usdsAmt, address gem, address pair) internal {
(uint256 reserveUsds, uint256 reserveGem) = UniswapV2Library.getReserves(UNIV2_FACTORY, USDS, gem);
uint256 gemAmt = UniswapV2Library.quote(usdsAmt, reserveUsds, reserveGem);
Expand Down
21 changes: 21 additions & 0 deletions test/mocks/MedianizerMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

pragma solidity ^0.8.21;

contract MedianizerMock {
uint256 public price;
mapping (address => uint256) public bud;

function setPrice(uint256 price_) external {
price = price_;
}

function kiss(address a) external {
bud[a] = 1;
}

function read() external view returns (bytes32) {
require(bud[msg.sender] == 1, "MedianizerMock/not-authorized");
return bytes32(price);
}
}

0 comments on commit 41e8019

Please sign in to comment.