Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Further sett upgrades #57

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/badger_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
withdrawalFee=0,
),
),
digg=DotMap(
strategyName="StrategyDiggRewards",
params=DotMap(
performanceFeeStrategist=0,
performanceFeeGovernance=0,
withdrawalFee=0,
),
),
uniBadgerWbtc=DotMap(
strategyName="StrategyBadgerLpMetaFarm",
params=DotMap(
Expand Down
6 changes: 5 additions & 1 deletion contracts/badger-sett/DiggSett.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ contract DiggSett is Sett {

// Check balance
uint256 _sharesInSett = digg.sharesOf(address(this));
uint256 _fee;

// If we don't have sufficient idle want in Sett, withdraw from Strategy
if (_sharesInSett < _sharesToRedeem) {
Expand All @@ -109,9 +110,12 @@ contract DiggSett is Sett {
if (_diff < _toWithdraw) {
_sharesToRedeem = _sharesInSett.add(_diff);
}
_fee = _processWithdrawalFee(digg.sharesToFragments(_sharesToRedeem.sub(_sharesInSett)));
} else {
_fee = _processWithdrawalFee(digg.sharesToFragments(_sharesToRedeem));
}

// Transfer the corresponding number of shares, scaled to DIGG fragments, to recipient
digg.transfer(msg.sender, digg.sharesToFragments(_sharesToRedeem));
digg.transfer(msg.sender, digg.sharesToFragments(_sharesToRedeem).sub(_fee));
}
}
40 changes: 35 additions & 5 deletions contracts/badger-sett/Sett.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ import "./SettAccessControlDefended.sol";
V1.2
* Transfer functions are now pausable along with all other non-permissioned write functions
* All permissioned write functions, with the exception of pause() & unpause(), are pausable as well

V1.3
* Withdrawals are processed from idle want in sett.
*/

contract Sett is ERC20Upgradeable, SettAccessControlDefended, PausableUpgradeable {
contract Sett is ERC20Upgradeable, PausableUpgradeable, SettAccessControlDefended {
using SafeERC20Upgradeable for IERC20Upgradeable;
using AddressUpgradeable for address;
using SafeMathUpgradeable for uint256;
Expand All @@ -50,6 +53,9 @@ contract Sett is ERC20Upgradeable, SettAccessControlDefended, PausableUpgradeabl

address public guardian;

uint256 public withdrawalFee;
uint256 public constant MAX_FEE = 10000;

event FullPricePerShareUpdated(uint256 value, uint256 indexed timestamp, uint256 indexed blockNumber);

function initialize(
Expand Down Expand Up @@ -111,10 +117,10 @@ contract Sett is ERC20Upgradeable, SettAccessControlDefended, PausableUpgradeabl
/// ===== View Functions =====

function version() public view returns (string memory) {
return "1.2";
return "1.3";
}

function getPricePerFullShare() public view virtual returns (uint256) {
function getPricePerFullShare() public virtual view returns (uint256) {
if (totalSupply() == 0) {
return 1e18;
}
Expand All @@ -130,7 +136,7 @@ contract Sett is ERC20Upgradeable, SettAccessControlDefended, PausableUpgradeabl
/// @notice Defines how much of the Setts' underlying can be borrowed by the Strategy for use
/// @notice Custom logic in here for how much the vault allows to be borrowed
/// @notice Sets minimum required on-hand to keep small withdrawals cheap
function available() public view virtual returns (uint256) {
function available() public virtual view returns (uint256) {
return token.balanceOf(address(this)).mul(min).div(max);
}

Expand Down Expand Up @@ -197,6 +203,12 @@ contract Sett is ERC20Upgradeable, SettAccessControlDefended, PausableUpgradeabl
guardian = _guardian;
}

function setWithdrawalFee(uint256 _withdrawalFee) external {
_onlyGovernance();
require(_withdrawalFee <= MAX_FEE, "sett/excessive-withdrawal-fee");
withdrawalFee = _withdrawalFee;
}

/// ===== Permissioned Actions: Controller =====

/// @notice Used to swap any borrowed reserve over the debt limit to liquidate to 'token'
Expand Down Expand Up @@ -262,6 +274,7 @@ contract Sett is ERC20Upgradeable, SettAccessControlDefended, PausableUpgradeabl
_burn(msg.sender, _shares);

// Check balance
uint256 _fee;
uint256 b = token.balanceOf(address(this));
if (b < r) {
uint256 _toWithdraw = r.sub(b);
Expand All @@ -271,9 +284,12 @@ contract Sett is ERC20Upgradeable, SettAccessControlDefended, PausableUpgradeabl
if (_diff < _toWithdraw) {
r = b.add(_diff);
}
_fee = _processWithdrawalFee(r.sub(b));
} else {
_fee = _processWithdrawalFee(r);
}

token.safeTransfer(msg.sender, r);
token.safeTransfer(msg.sender, r.sub(_fee));
}

function _lockForBlock(address account) internal {
Expand All @@ -296,4 +312,18 @@ contract Sett is ERC20Upgradeable, SettAccessControlDefended, PausableUpgradeabl
_blockLocked();
return super.transferFrom(sender, recipient, amount);
}

/// ===== Internal Helper Functions =====

/// @notice If withdrawal fee is active, take the appropriate amount from the given value and transfer to rewards recipient
/// @return The withdrawal fee that was taken
function _processWithdrawalFee(uint256 _amount) internal returns (uint256) {
if (withdrawalFee == 0) {
return 0;
}

uint256 fee = _amount.mul(withdrawalFee).div(MAX_FEE);
token.safeTransfer(IController(controller).rewards(), fee);
return fee;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@
}


def strategy_name_to_artifact(name):
def contract_name_to_artifact(name):
return name_to_artifact[name]
1 change: 1 addition & 0 deletions helpers/multicall/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def as_original(value):
strategist="strategist()(address)",
keeper="keeper()(address)",
shares="shares()(uint256)",
withdrawalFee="withdrawalFee()(uint256)",
)
strategy = DotMap(
balanceOfPool="balanceOfPool()(uint256)",
Expand Down
6 changes: 3 additions & 3 deletions helpers/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@
router="0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F",
factory="0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac",
lpTokens=DotMap(
sushiBadgerWBtc="0x110492b31c59716AC47337E616804E3E3AdC0b4a",
sushiWbtcWeth="0xCEfF51756c56CeFFCA006cD410B03FFC46dd3a58"
sushiBadgerWbtc="0x110492b31c59716AC47337E616804E3E3AdC0b4a",
sushiWbtcEth="0xCEfF51756c56CeFFCA006cD410B03FFC46dd3a58"
),
pids=DotMap(sushiBadgerWBtc=73, sushiEthWBtc=21),
pids=DotMap(sushiBadgerWbtc=73, sushiEthWBtc=21),
)

curve_registry = DotMap(
Expand Down
31 changes: 27 additions & 4 deletions helpers/sett/resolvers/StrategyCoreResolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ def add_sett_snap(self, calls):
calls.append(
Call(sett.address, [func.erc20.totalSupply], [["sett.totalSupply", as_wei]])
)
calls.append(
Call(
sett.address,
[func.sett.withdrawalFee],
[["sett.withdrawalFee", as_wei]],
)
)

return calls

Expand Down Expand Up @@ -239,13 +246,29 @@ def confirm_withdraw(self, before, after, params, tx):

# Controller rewards should earn
if (
before.get("strategy.withdrawalFee") > 0 and
# Fees are only processed when withdrawing from the strategy.
before.balances("want", "strategy") > after.balances("want", "strategy")
(
before.get("strategy.withdrawalFee") > 0 and
# Strategy fees are only processed when withdrawing from the strategy.
before.balances("want", "strategy") > after.balances("want", "strategy")
) or (
before.get("sett.withdrawalFee") > 0 and
# Sett fees are always processed.
before.balances("want", "sett") > after.balances("want", "sett")
)
):
assert after.balances("want", "governanceRewards") > before.balances(
# Fees can be split between sett and strategy withdrawals.
rewardsDiff = after.balances("want", "governanceRewards") - before.balances(
"want", "governanceRewards"
)
userDiff = after.balances("want", "user") - before.balances("want", "user")
strategyDiff = before.balances("want", "strategy") - after.balances("want", "strategy")
settDiff = before.balances("want", "sett") - after.balances("want", "sett")
# Want diff in user/rewards should be equal to diff in strategy/sett
assert approx(
rewardsDiff + userDiff,
strategyDiff + settDiff,
1,
)

def confirm_deposit(self, before, after, params):
"""
Expand Down
13 changes: 10 additions & 3 deletions helpers/sett/simulation/actors/UserActor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import random
from brownie import Sett
from brownie import Sett, Controller
from typing import Any

from helpers.utils import approx
from helpers.constants import MaxUint256
from helpers.sett.SnapshotManager import SnapshotManager
from .BaseAction import BaseAction
Expand All @@ -24,6 +25,7 @@ def run(self):
user = self.user
want = self.want
sett = self.sett
rewards = Controller.at(sett.controller()).rewards()

beforeSettBalance = sett.balanceOf(user)
startingBalance = want.balanceOf(user)
Expand All @@ -46,12 +48,17 @@ def run(self):

afterSettBalance = sett.balanceOf(user)
settDeposited = afterSettBalance - beforeSettBalance
beforeRewardsBalance = want.balanceOf(rewards)
# Confirm that before and after balance does not exceed
# max precision loss.
self.snap.settWithdraw(settDeposited, {"from": self.user})

rewardsDiff = want.balanceOf(rewards) - beforeRewardsBalance
endingBalance = want.balanceOf(user)
assert startingBalance - endingBalance <= 2
assert approx(
startingBalance,
endingBalance + rewardsDiff,
1,
)


class DepositAction(BaseAction):
Expand Down
4 changes: 4 additions & 0 deletions interfaces/digg/IDigg.sol
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.5.0 <0.8.0;

interface IDigg {
Expand Down Expand Up @@ -41,6 +43,7 @@ interface IDigg {
function sharesOf(address who) external view returns (uint256);

function _sharesPerFragment() external view returns (uint256);

function _initialSharesPerFragment() external view returns (uint256);

/**
Expand All @@ -56,6 +59,7 @@ interface IDigg {
function sharesToFragments(uint256 shares) external view returns (uint256);

function scaledSharesToShares(uint256 fragments) external view returns (uint256);

function sharesToScaledShares(uint256 shares) external view returns (uint256);

/**
Expand Down
8 changes: 8 additions & 0 deletions interfaces/keeper/ILiquidityPoolV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.6.0;

interface ILiquidityPoolV2 {
// ===== Write =====
function deposit(address _token, uint256 _amount) external;
}
58 changes: 58 additions & 0 deletions scripts/deploy/upgrade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from brownie.network.contract import ProjectContract

from scripts.systems.badger_system import BadgerSystem
from helpers.gnosis_safe import GnosisSafe, MultisigTxMetadata


# Upgrades versioned proxy contract if not latest version.
def upgrade_versioned_proxy(
badger: BadgerSystem,
# upgradeable proxy contract
proxy: ProjectContract,
# latest logic contract
logic: ProjectContract,
) -> None:
# Do nothing if the proxy and logic contracts have the same version
# or if the logic contract has an older version.
proxyVersion = _get_version(proxy)
logicVersion = _get_version(logic)
if (
proxyVersion == logicVersion or
float(logicVersion) < float(proxyVersion)
):
return

multi = GnosisSafe(badger.devMultisig)

multi.execute(
MultisigTxMetadata(description="Upgrade versioned proxy contract with new logic version",),
{
"to": badger.devProxyAdmin.address,
"data": badger.devProxyAdmin.upgrade.encode_input(
proxy, logic
),
},
)


def _get_version(contract: ProjectContract) -> float:
# try all the methods in priority order
methods = [
"version",
"baseStrategyVersion",
]

for method in methods:
version, ok = _try_get_version(contract, method)
if ok:
return version

# NB: Prior to V1.1, Setts do not have a version function.
return 0.0


def _try_get_version(contract: ProjectContract, method: str) -> (float, bool):
try:
return getattr(contract, method)(), True
except Exception:
return 0.0, False
Loading