Skip to content

Commit

Permalink
Renaming (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunbreak1211 authored Aug 28, 2024
1 parent afed2bb commit 95431f3
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 104 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ Implementations of MakerDAO surplus auctions, triggered on `vow.flap`.

### Splitter

Exposes a `kick` operation to be triggered periodically. Its logic withdraws `NST` from the `vow` and splits it in two parts. The first part (`burn`) is sent to the underlying `flapper` contract to be processed by the burn engine. The second part (`WAD - burn`) is distributed as reward to a `farm` contract. The `kick` cadence is determined by the `hop` value.
Exposes a `kick` operation to be triggered periodically. Its logic withdraws `USDS` from the `vow` and splits it in two parts. The first part (`burn`) is sent to the underlying `flapper` contract to be processed by the burn engine. The second part (`WAD - burn`) is distributed as reward to a `farm` contract. The `kick` cadence is determined by the `hop` value.

Configurable Parameters:
* `burn` - The percentage of the `vow.bump` to be moved to the underlying `flapper`. For example, a value of 0.70 \* `WAD` corresponds to funneling 70% of the `NST` to the burn engine.
* `burn` - The percentage of the `vow.bump` to be moved to the underlying `flapper`. For example, a value of 0.70 \* `WAD` corresponds to funneling 70% of the `USDS` to the burn engine.
* `hop` - Minimum seconds interval between kicks.
* `flapper` - The underlying burner strategy (e.g. the address of `FlapperUniV2SwapOnly`).
* `farm` - The staking rewards contract receiving the rewards.

### FlapperUniV2

Exposes an `exec` operation to be triggered periodically by the `Splitter` (at a cadence determined by `Splitter.hop()`). Its logic withdraws `NST` from the `Splitter` and buys `gem` tokens on Uniswap v2. The acquired tokens, along with a proportional amount of `NST` (saved from the initial withdraw) are deposited back into the liquidity pool. Finally, the minted LP tokens are sent to a predefined `receiver` address.
Exposes an `exec` operation to be triggered periodically by the `Splitter` (at a cadence determined by `Splitter.hop()`). Its logic withdraws `USDS` from the `Splitter` and buys `gem` tokens on Uniswap v2. The acquired tokens, along with a proportional amount of `USDS` (saved from the initial withdraw) are deposited back into the liquidity pool. Finally, the minted LP tokens are sent to a predefined `receiver` address.

Configurable Parameters:
* `pip` - A reference price oracle, used for bounding the exchange rate of the swap.
Expand All @@ -26,7 +26,7 @@ Configurable Parameters:

### FlapperUniV2SwapOnly

Exposes an `exec` operation to be triggered periodically by the `Splitter` (at a cadence determined by `Splitter.hop()`). Its logic withdraws `NST` from the `Splitter` and buys `gem` tokens on Uniswap v2. The acquired tokens are sent to a predefined `receiver` address.
Exposes an `exec` operation to be triggered periodically by the `Splitter` (at a cadence determined by `Splitter.hop()`). Its logic withdraws `USDS` from the `Splitter` and buys `gem` tokens on Uniswap v2. The acquired tokens are sent to a predefined `receiver` address.

Configurable Parameters:
* `pip` - A reference price oracle, used for bounding the exchange rate of the swap.
Expand All @@ -42,4 +42,4 @@ Allows for scaling down an oracle price by a certain value. This can be useful w

### General Note:

* Availability and accounting of the withdrawn `NST` is the responsibility of the `vow`. At the time of a `kick`, the `vow` is expected to hold at least the drawn amount (`vow.bump`) over the configured flapping threshold (`vow.hump`).
* Availability and accounting of the withdrawn `USDS` is the responsibility of the `vow`. At the time of a `kick`, the `vow` is expected to hold at least the drawn amount (`vow.bump`) over the configured flapping threshold (`vow.hump`).
10 changes: 5 additions & 5 deletions deploy/FlapperDeploy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ library FlapperDeploy {
address deployer,
address owner,
address spotter,
address nst,
address usds,
address gem,
address pair,
address receiver,
bool swapOnly
) internal returns (address flapper) {
flapper =
swapOnly ? address(new FlapperUniV2SwapOnly(spotter, nst, gem, pair, receiver))
: address(new FlapperUniV2(spotter, nst, gem, pair, receiver))
swapOnly ? address(new FlapperUniV2SwapOnly(spotter, usds, gem, pair, receiver))
: address(new FlapperUniV2(spotter, usds, gem, pair, receiver))
;

ScriptTools.switchOwner(flapper, deployer, owner);
Expand All @@ -57,9 +57,9 @@ library FlapperDeploy {
function deploySplitter(
address deployer,
address owner,
address nstJoin
address usdsJoin
) internal returns (SplitterInstance memory splitterInstance) {
address splitter = address(new Splitter(nstJoin));
address splitter = address(new Splitter(usdsJoin));
address mom = address(new SplitterMom(splitter));

ScriptTools.switchOwner(splitter, deployer, owner);
Expand Down
26 changes: 13 additions & 13 deletions deploy/FlapperInit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { SplitterInstance } from "./SplitterInstance.sol";
interface FlapperUniV2Like {
function pip() external view returns (address);
function spotter() external view returns (address);
function nst() external view returns (address);
function usds() external view returns (address);
function gem() external view returns (address);
function receiver() external view returns (address);
function pair() external view returns (address);
Expand Down Expand Up @@ -50,14 +50,14 @@ interface PairLike {
function token1() external view returns (address);
}

interface NstJoinLike {
interface UsdsJoinLike {
function dai() external view returns (address); // TODO: Replace when new join is ready by the new getter
}

interface SplitterLike {
function live() external view returns (uint256);
function vat() external view returns (address);
function nstJoin() external view returns (address);
function usdsJoin() external view returns (address);
function hop() external view returns (uint256);
function rely(address) external;
function file(bytes32, uint256) external;
Expand All @@ -74,15 +74,15 @@ struct FlapperUniV2Config {
uint256 want;
address pip;
address pair;
address nst;
address usds;
address splitter;
bytes32 prevChainlogKey;
bytes32 chainlogKey;
}

struct FarmConfig {
address splitter;
address nstJoin;
address usdsJoin;
uint256 hop;
bytes32 prevChainlogKey;
bytes32 chainlogKey;
Expand All @@ -93,7 +93,7 @@ struct SplitterConfig {
uint256 bump;
uint256 hop;
uint256 burn;
address nstJoin;
address usdsJoin;
bytes32 splitterChainlogKey;
bytes32 prevMomChainlogKey;
bytes32 momChainlogKey;
Expand All @@ -112,15 +112,15 @@ library FlapperInit {

// Sanity checks
require(flapper.spotter() == address(dss.spotter), "Flapper spotter mismatch");
require(flapper.nst() == cfg.nst, "Flapper nst mismatch");
require(flapper.usds() == cfg.usds, "Flapper usds mismatch");
require(flapper.pair() == cfg.pair, "Flapper pair mismatch");
require(flapper.receiver() == dss.chainlog.getAddress("MCD_PAUSE_PROXY"), "Flapper receiver mismatch");

PairLike pair = PairLike(flapper.pair());
(address pairNst, address pairGem) = pair.token0() == cfg.nst ? (pair.token0(), pair.token1())
: (pair.token1(), pair.token0());
require(pairNst == cfg.nst, "Nst mismatch");
require(pairGem == flapper.gem(), "Gem mismatch");
(address pairUsds, address pairGem) = pair.token0() == cfg.usds ? (pair.token0(), pair.token1())
: (pair.token1(), pair.token0());
require(pairUsds == cfg.usds, "Usds mismatch");
require(pairGem == flapper.gem(), "Gem mismatch");

require(cfg.want >= WAD * 90 / 100, "want too low");

Expand Down Expand Up @@ -158,7 +158,7 @@ library FlapperInit {
FarmLike farm = FarmLike(farm_);
SplitterLike splitter = SplitterLike(cfg.splitter);

require(farm.rewardsToken() == NstJoinLike(cfg.nstJoin).dai(), "Farm rewards not nst");
require(farm.rewardsToken() == UsdsJoinLike(cfg.usdsJoin).dai(), "Farm rewards not usds");
// Staking token is checked in the Lockstake script

// The following two checks enforce the initSplitter function has to be called first
Expand All @@ -185,7 +185,7 @@ library FlapperInit {
// Sanity checks
require(splitter.live() == 1, "Splitter not live");
require(splitter.vat() == address(dss.vat), "Splitter vat mismatch");
require(splitter.nstJoin() == cfg.nstJoin, "Splitter nstJoin mismatch");
require(splitter.usdsJoin() == cfg.usdsJoin, "Splitter usdsJoin mismatch");
require(mom.splitter() == splitterInstance.splitter, "Mom splitter mismatch");

require(cfg.hump > 0, "hump too low");
Expand Down
48 changes: 24 additions & 24 deletions src/FlapperUniV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ contract FlapperUniV2 {
// For example: 0.98 * WAD allows 2% worse price than the reference.

SpotterLike public immutable spotter;
address public immutable nst;
address public immutable usds;
address public immutable gem;
address public immutable receiver;

PairLike public immutable pair;
bool public immutable nstFirst;
bool public immutable usdsFirst;

event Rely(address indexed usr);
event Deny(address indexed usr);
Expand All @@ -64,20 +64,20 @@ contract FlapperUniV2 {

constructor(
address _spotter,
address _nst,
address _usds,
address _gem,
address _pair,
address _receiver
) {
spotter = SpotterLike(_spotter);

nst = _nst;
gem = _gem;
usds = _usds;
gem = _gem;
require(GemLike(gem).decimals() == 18, "FlapperUniV2/gem-decimals-not-18");

pair = PairLike(_pair);
nstFirst = pair.token0() == nst;
receiver = _receiver;
pair = PairLike(_pair);
usdsFirst = pair.token0() == usds;
receiver = _receiver;

wards[msg.sender] = 1;
emit Rely(msg.sender);
Expand Down Expand Up @@ -109,27 +109,27 @@ contract FlapperUniV2 {
emit File(what, data);
}

function _getReserves() internal returns (uint256 reserveNst, uint256 reserveGem) {
function _getReserves() internal returns (uint256 reserveUsds, uint256 reserveGem) {
(uint256 _reserveA, uint256 _reserveB,) = pair.getReserves();
(reserveNst, reserveGem) = nstFirst ? (_reserveA, _reserveB) : (_reserveB, _reserveA);
(reserveUsds, reserveGem) = usdsFirst ? (_reserveA, _reserveB) : (_reserveB, _reserveA);

uint256 _nstBalance = GemLike(nst).balanceOf(address(pair));
uint256 _gemBalance = GemLike(gem).balanceOf(address(pair));
if (_nstBalance > reserveNst || _gemBalance > reserveGem) {
uint256 _usdsBalance = GemLike(usds).balanceOf(address(pair));
uint256 _gemBalance = GemLike(gem).balanceOf(address(pair));
if (_usdsBalance > reserveUsds || _gemBalance > reserveGem) {
pair.sync();
(reserveNst, reserveGem) = (_nstBalance, _gemBalance);
(reserveUsds, reserveGem) = (_usdsBalance, _gemBalance);
}
}

// The Uniswap invariant needs to hold through the swap.
// Additionally, The deposited funds need to be in the same ratio as the reserves after the swap.
//
// (1) reserveNst * reserveGem = (reserveNst + sell * 997 / 1000) * (reserveGem - bought)
// (2) (lot - sell) / bought = (reserveNst + sell) / (reserveGem - bought)
// (1) reserveUsds * reserveGem = (reserveUsds + sell * 997 / 1000) * (reserveGem - bought)
// (2) (lot - sell) / bought = (reserveUsds + sell) / (reserveGem - bought)
//
// The solution for the these equations for variables `sell` and `bought` is used below.
function _getNstToSell(uint256 lot, uint256 reserveNst) internal pure returns (uint256 sell) {
sell = (Babylonian.sqrt(reserveNst * (lot * 3_988_000 + reserveNst * 3_988_009)) - reserveNst * 1997) / 1994;
function _getUsdsToSell(uint256 lot, uint256 reserveUsds) internal pure returns (uint256 sell) {
sell = (Babylonian.sqrt(reserveUsds * (lot * 3_988_000 + reserveUsds * 3_988_009)) - reserveUsds * 1997) / 1994;
}

// Based on: https://github.com/Uniswap/v2-periphery/blob/0335e8f7e1bd1e8d8329fd300aea2ef2f36dd19f/contracts/libraries/UniswapV2Library.sol#L43
Expand All @@ -140,22 +140,22 @@ contract FlapperUniV2 {

function exec(uint256 lot) external auth {
// Check Amounts
(uint256 _reserveNst, uint256 _reserveGem) = _getReserves();
(uint256 _reserveUsds, uint256 _reserveGem) = _getReserves();

uint256 _sell = _getNstToSell(lot, _reserveNst);
uint256 _sell = _getUsdsToSell(lot, _reserveUsds);

uint256 _buy = _getAmountOut(_sell, _reserveNst, _reserveGem);
uint256 _buy = _getAmountOut(_sell, _reserveUsds, _reserveGem);
require(_buy >= _sell * want / (uint256(pip.read()) * RAY / spotter.par()), "FlapperUniV2/insufficient-buy-amount");
//

// Swap
GemLike(nst).transfer(address(pair), _sell);
(uint256 _amt0Out, uint256 _amt1Out) = nstFirst ? (uint256(0), _buy) : (_buy, uint256(0));
GemLike(usds).transfer(address(pair), _sell);
(uint256 _amt0Out, uint256 _amt1Out) = usdsFirst ? (uint256(0), _buy) : (_buy, uint256(0));
pair.swap(_amt0Out, _amt1Out, address(this), new bytes(0));
//

// Deposit
GemLike(nst).transfer(address(pair), lot - _sell);
GemLike(usds).transfer(address(pair), lot - _sell);
GemLike(gem).transfer(address(pair), _buy);
uint256 _liquidity = pair.mint(receiver);
//
Expand Down
28 changes: 14 additions & 14 deletions src/FlapperUniV2SwapOnly.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ contract FlapperUniV2SwapOnly {
// For example: 0.98 * WAD allows 2% worse price than the reference.

SpotterLike public immutable spotter;
address public immutable nst;
address public immutable usds;
address public immutable gem;
address public immutable receiver;

PairLike public immutable pair;
bool public immutable nstFirst;
bool public immutable usdsFirst;

event Rely(address indexed usr);
event Deny(address indexed usr);
Expand All @@ -59,20 +59,20 @@ contract FlapperUniV2SwapOnly {

constructor(
address _spotter,
address _nst,
address _usds,
address _gem,
address _pair,
address _receiver
) {
spotter = SpotterLike(_spotter);

nst = _nst;
gem = _gem;
usds = _usds;
gem = _gem;
require(GemLike(gem).decimals() == 18, "FlapperUniV2SwapOnly/gem-decimals-not-18");

pair = PairLike(_pair);
nstFirst = pair.token0() == nst;
receiver = _receiver;
pair = PairLike(_pair);
usdsFirst = pair.token0() == usds;
receiver = _receiver;

wards[msg.sender] = 1;
emit Rely(msg.sender);
Expand Down Expand Up @@ -104,9 +104,9 @@ contract FlapperUniV2SwapOnly {
emit File(what, data);
}

function _getReserves() internal view returns (uint256 reserveNst, uint256 reserveGem) {
function _getReserves() internal view returns (uint256 reserveUsds, uint256 reserveGem) {
(uint256 _reserveA, uint256 _reserveB,) = pair.getReserves();
(reserveNst, reserveGem) = nstFirst ? (_reserveA, _reserveB) : (_reserveB, _reserveA);
(reserveUsds, reserveGem) = usdsFirst ? (_reserveA, _reserveB) : (_reserveB, _reserveA);
}

// Based on: https://github.com/Uniswap/v2-periphery/blob/0335e8f7e1bd1e8d8329fd300aea2ef2f36dd19f/contracts/libraries/UniswapV2Library.sol#L43
Expand All @@ -117,15 +117,15 @@ contract FlapperUniV2SwapOnly {

function exec(uint256 lot) external auth {
// Check Amount to buy
(uint256 _reserveNst, uint256 _reserveGem) = _getReserves();
(uint256 _reserveUsds, uint256 _reserveGem) = _getReserves();

uint256 _buy = _getAmountOut(lot, _reserveNst, _reserveGem);
uint256 _buy = _getAmountOut(lot, _reserveUsds, _reserveGem);
require(_buy >= lot * want / (uint256(pip.read()) * RAY / spotter.par()), "FlapperUniV2SwapOnly/insufficient-buy-amount");
//

// Swap
GemLike(nst).transfer(address(pair), lot);
(uint256 _amt0Out, uint256 _amt1Out) = nstFirst ? (uint256(0), _buy) : (_buy, uint256(0));
GemLike(usds).transfer(address(pair), lot);
(uint256 _amt0Out, uint256 _amt1Out) = usdsFirst ? (uint256(0), _buy) : (_buy, uint256(0));
pair.swap(_amt0Out, _amt1Out, receiver, new bytes(0));
//

Expand Down
2 changes: 1 addition & 1 deletion src/OracleWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ contract OracleWrapper {
address _flapper,
uint256 _divisor
) {
pip = PipLike(_pip);
pip = PipLike(_pip);
flapper = _flapper;
divisor = _divisor;
}
Expand Down
Loading

0 comments on commit 95431f3

Please sign in to comment.