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

Stabilizer tests #213

Open
wants to merge 3 commits into
base: stabilizer-tests
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
35 changes: 22 additions & 13 deletions contracts/badger-sett/strategies/digg/StabilizeStrategyDiggV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ contract StabilizeStrategyDiggV1 is BaseStrategyMultiSwapper {
bool public diggInExpansion;
uint256 public lastDiggTotalSupply; // The last recorded total supply of the digg token
uint256 public lastDiggPrice; // The price of Digg at last trade in BTC units
uint256 public diggSupplyChangeFactor = 50000; // This is a factor used by the strategy to determine how much digg to sell in expansion
uint256 public wbtcSupplyChangeFactor = 20000; // This is a factor used by the strategy to determine how much wbtc to sell in contraction
uint256 public wbtcSellAmplificationFactor = 2; // The higher this number the more aggressive the buyback in contraction
uint256 public maxGainedDiggSellPercent = 100000; // The maximum percent of sellable Digg gains through rebase
uint256 public maxWBTCSellPercent = 50000; // The maximum percent of sellable wBTC;
uint256 public tradeBatchSize = 10e18; // The normalized size of the trade batches, can be adjusted
uint256 public tradeAmountLeft = 0; // The amount left to trade
uint256 private _maxOracleLag = 24 hours; // Maximum amount of lag the oracle can have before reverting the price
uint256 public diggSupplyChangeFactor; // This is a factor used by the strategy to determine how much digg to sell in expansion
uint256 public wbtcSupplyChangeFactor; // This is a factor used by the strategy to determine how much wbtc to sell in contraction
uint256 public wbtcSellAmplificationFactor; // The higher this number the more aggressive the buyback in contraction
uint256 public maxGainedDiggSellPercent; // The maximum percent of sellable Digg gains through rebase
uint256 public maxWBTCSellPercent; // The maximum percent of sellable wBTC;
uint256 public tradeBatchSize; // The normalized size of the trade batches, can be adjusted
uint256 public tradeAmountLeft; // The amount left to trade
uint256 public maxOracleLag; // Maximum amount of lag the oracle can have before reverting the price

// Constants
uint256 constant DIVISION_FACTOR = 100000;
Expand Down Expand Up @@ -112,6 +112,15 @@ contract StabilizeStrategyDiggV1 is BaseStrategyMultiSwapper {
stabilizeFee = _feeConfig[3];
strategyLockedUntil = _lockedUntil; // Deployer can optionally lock strategy from withdrawing until a certain blocknumber

diggSupplyChangeFactor = 50000;
wbtcSupplyChangeFactor = 20000;
wbtcSellAmplificationFactor = 2;
maxGainedDiggSellPercent = 100000;
maxWBTCSellPercent = 50000;
tradeBatchSize = 10e18;
tradeAmountLeft = 0;
maxOracleLag = 56 hours;

setupTradeTokens();
lastDiggPrice = getDiggPrice();
lastDiggTotalSupply = tokenList[0].token.totalSupply(); // The supply only changes at rebase
Expand Down Expand Up @@ -151,26 +160,26 @@ contract StabilizeStrategyDiggV1 is BaseStrategyMultiSwapper {
function getDiggUSDPrice() public view returns (uint256) {
AggregatorV3Interface priceOracle = AggregatorV3Interface(DIGG_ORACLE_ADDRESS);
(, int256 intPrice, , uint256 lastUpdateTime, ) = priceOracle.latestRoundData(); // We only want the answer
require(block.timestamp.sub(lastUpdateTime) < _maxOracleLag, "Price data is too old to use");
require(block.timestamp.sub(lastUpdateTime) < maxOracleLag, "Price data is too old to use");
uint256 usdPrice = uint256(intPrice);
priceOracle = AggregatorV3Interface(BTC_ORACLE_ADDRESS);
(, intPrice, , lastUpdateTime, ) = priceOracle.latestRoundData(); // We only want the answer
require(block.timestamp.sub(lastUpdateTime) < _maxOracleLag, "Price data is too old to use");
require(block.timestamp.sub(lastUpdateTime) < maxOracleLag, "Price data is too old to use");
usdPrice = usdPrice.mul(uint256(intPrice)).mul(10**2);
return usdPrice; // Digg Price in USD
}

function getDiggPrice() public view returns (uint256) {
AggregatorV3Interface priceOracle = AggregatorV3Interface(DIGG_ORACLE_ADDRESS);
(, int256 intPrice, , uint256 lastUpdateTime, ) = priceOracle.latestRoundData(); // We only want the answer
require(block.timestamp.sub(lastUpdateTime) < _maxOracleLag, "Price data is too old to use");
require(block.timestamp.sub(lastUpdateTime) < maxOracleLag, "Price data is too old to use");
return uint256(intPrice).mul(10**10);
}

function getWBTCUSDPrice() public view returns (uint256) {
AggregatorV3Interface priceOracle = AggregatorV3Interface(BTC_ORACLE_ADDRESS);
(, int256 intPrice, , uint256 lastUpdateTime, ) = priceOracle.latestRoundData(); // We only want the answer
require(block.timestamp.sub(lastUpdateTime) < _maxOracleLag, "Price data is too old to use");
require(block.timestamp.sub(lastUpdateTime) < maxOracleLag, "Price data is too old to use");
return uint256(intPrice).mul(10**10);
}

Expand Down Expand Up @@ -305,7 +314,7 @@ contract StabilizeStrategyDiggV1 is BaseStrategyMultiSwapper {

function setOracleLagTime(uint256 _time) external {
_onlyAnyAuthorizedParties();
_maxOracleLag = _time;
maxOracleLag = _time;
}

function setStabilizeFee(uint256 _fee) external {
Expand Down
2 changes: 1 addition & 1 deletion helpers/sett/DiggSnapshotManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def rebase(self, value, overrides, confirm=True):
# as the rebase logic checks if block ts w/in rebase window.
self._shift_into_next_rebase_window(digg, value)

digg.orchestrator.rebase({"from": digg.owner},)
digg.orchestrator.rebase({"from": digg.owner})

after = self.snap(trackedUsers)
if confirm:
Expand Down
17 changes: 0 additions & 17 deletions helpers/sett/DiggStabilizerSnapshotManager.py

This file was deleted.

11 changes: 5 additions & 6 deletions helpers/sett/resolvers/StabilizeStrategyDiggV1Resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def confirm_rebalance_events(self, before, after, tx):
]
for key in keys:
assert key in event
console.print("[blue]== Convex Strat rebalance() NoTrade State ==[/blue]")
console.print("[blue]== rebalance() NoTrade State ==[/blue]")
self.printState(event, keys)

key = "TradeState"
Expand All @@ -42,7 +42,7 @@ def confirm_rebalance_events(self, before, after, tx):
]
for key in keys:
assert key in event
console.print("[blue]== Convex Strat rebalance() TradeState State ==[/blue]")
console.print("[blue]== rebalance() TradeState State ==[/blue]")
self.printState(event, keys)

key = "Approval"
Expand All @@ -57,7 +57,7 @@ def confirm_rebalance_events(self, before, after, tx):
assert key in event

console.print(
"[blue]== Convex Strat rebalance() Approval State ==[/blue]"
"[blue]== rebalance() Approval State ==[/blue]"
)
self.printState(event, keys)

Expand Down Expand Up @@ -100,9 +100,9 @@ def confirm_rebalance(self, before, after, tx):
newSupply = event["newSupply"]

# Sold Digg
if event["diggInExpansion"]:
if event["diggInExpansion"]:
rebasePercentage = (newSupply - oldSupply) / oldSupply
changedDigg = before.balances("want", "strategy") * rebasePercentage
changedDigg = before.balances("want", "strategy") - (before.balances("want", "strategy")/(1 + rebasePercentage))
assert approx(
changedDigg * (soldPercent/100000),
before.balances("want", "strategy") - after.balances("want", "strategy"),
Expand Down Expand Up @@ -291,7 +291,6 @@ def add_balances_snap(self, calls, entities):
diggSLP = interface.IERC20("0x9a13867048e01c663ce8Ce2fE0cDAE69Ff9F35E3")
diggUniLP = interface.IERC20("0xE86204c4eDDd2f70eE00EAd6805f917671F56c52")
wbtc = interface.IERC20("0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599")
digg = interface.IERC20("0x798D1bE841a82a273720CE31c822C61a67a601C3")


calls = self.add_entity_balances_for_tokens(calls, "diggSLP", diggSLP, entities)
Expand Down
13 changes: 8 additions & 5 deletions tests/sett/test_digg_stabilize_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ def test_single_user_rebalance_flow(settConfig):
chain.mine()

# Rebase
# Push/rebase on an exchange rate of 1.2
# Push/rebase on an exchange rate of 1.
print("TotalSupply before:", want.totalSupply())
snap.rebase(1.2 * 10 ** 18, {"from": deployer})
print("TotalSupply after:", want.totalSupply())
print("Time After First Rebase: ", chain.time())

print(diggOracle.latestRoundData())

# Rebalance
snap.rebalance({"from": settKeeper})
snap.rebalance({"from": strategyKeeper})

chain.sleep(hours(0.25))
chain.mine()
Expand All @@ -88,13 +90,15 @@ def test_single_user_rebalance_flow(settConfig):

# Rebase
# Push/rebase on an exchange rate of 0.6
snap.rebase(0.6 * 10 ** 18, {"from": deployer})
print("TotalSupply before:", want.totalSupply())
snap.rebase(0.4 * 10 ** 18, {"from": deployer})
print("TotalSupply after:", want.totalSupply())
print("Time After First Rebase: ", chain.time())

print(diggOracle.latestRoundData())

# Rebalance
snap.rebalance({"from": settKeeper})
snap.rebalance({"from": strategyKeeper})

chain.sleep(hours(0.25))
chain.mine()
Expand All @@ -103,7 +107,6 @@ def test_single_user_rebalance_flow(settConfig):
amount = sett.balanceOf(randomUser.address)
snap.settWithdraw(amount // 2, {"from": randomUser})

assert False

def rebase(badger: BadgerSystem, account):
digg = badger.digg
Expand Down