Skip to content

Commit

Permalink
Feat: owner setters functions (#925)
Browse files Browse the repository at this point in the history
* feat: owner setters

* Lint

* fix

* testing

* lint

* fix

* add comment

* lint
  • Loading branch information
haythemsellami authored Jan 18, 2023
1 parent cb1bf21 commit e875084
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 56 deletions.
13 changes: 7 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ jobs:

################ zen-bull-netting jobs
lint-zen-bull-netting:
working_directory: ~/squeeth/packages/zen-bull
working_directory: ~/squeeth/packages/zen-bull-netting
docker:
- image: ghcr.io/foundry-rs/foundry:latest
steps:
Expand All @@ -315,7 +315,7 @@ jobs:
command: cd packages/zen-bull-netting && FOUNDRY_PROFILE=fmt forge fmt --check

compile-zen-bull-netting:
working_directory: ~/squeeth/packages/zen-bull
working_directory: ~/squeeth/packages/zen-bull-netting
docker:
- image: ghcr.io/foundry-rs/foundry:latest
steps:
Expand All @@ -325,15 +325,15 @@ jobs:
command: cd packages/zen-bull-netting && forge build --force

test-zen-bull-netting:
working_directory: ~/squeeth/packages/zen-bull
working_directory: ~/squeeth/packages/zen-bull-netting
docker:
- image: ghcr.io/foundry-rs/foundry:latest
steps:
- checkout
- run: cd packages/zen-bull-netting && FOUNDRY_PROFILE=test forge test -vv --gas-report

fuzzing-zen-bull-netting:
working_directory: ~/squeeth/packages/zen-bull
working_directory: ~/squeeth/packages/zen-bull-netting
docker:
- image: ghcr.io/foundry-rs/foundry:latest
steps:
Expand All @@ -344,9 +344,10 @@ jobs:
- run: cd packages/zen-bull-netting && FOUNDRY_PROFILE=fuzz forge test -vv

coverage-zen-bull-netting:
working_directory: ~/squeeth/packages/zen-bull
working_directory: ~/squeeth/packages/zen-bull-netting
docker:
- image: ghcr.io/foundry-rs/foundry:nightly-e9f274df045d36527eff66f8a6d4e836c7227231
# - image: ghcr.io/foundry-rs/foundry:nightly-e9f274df045d36527eff66f8a6d4e836c7227231
- image: ghcr.io/foundry-rs/foundry:latest
steps:
- checkout
- run: cd packages/zen-bull-netting && FOUNDRY_PROFILE=coverage forge coverage
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@
[submodule "packages/zen-bull-netting/lib/forge-std"]
path = packages/zen-bull-netting/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "packages/zen-bull-netting/lib/openzeppelin-contracts"]
path = packages/zen-bull-netting/lib/openzeppelin-contracts
url = https://github.com/openzeppelin/openzeppelin-contracts
1 change: 1 addition & 0 deletions packages/zen-bull-netting/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ optimizer_runs = 2000
[profile.test]
no_match_test = "Fuzzing"
no_match_contract = "Fuzz"
gas_reports = ["ZenBullNetting"]

[profile.fuzz]
runs = 2000
Expand Down
1 change: 1 addition & 0 deletions packages/zen-bull-netting/lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at 045704
12 changes: 0 additions & 12 deletions packages/zen-bull-netting/script/Counter.s.sol

This file was deleted.

14 changes: 0 additions & 14 deletions packages/zen-bull-netting/src/Counter.sol

This file was deleted.

144 changes: 144 additions & 0 deletions packages/zen-bull-netting/src/ZenBullNetting.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.13;

// contract
import { Ownable } from "openzeppelin/access/Ownable.sol";
import { EIP712 } from "openzeppelin/utils/cryptography/draft-EIP712.sol";

/**
* @dev ZenBullNetting contract
* @notice Contract for Netting Deposits and Withdrawals in ZenBull
* @author Opyn team
*/
contract ZenBullNetting is Ownable, EIP712 {
/// @dev typehash for signed orders
bytes32 private constant _ZENBULL_NETTING_TYPEHASH = keccak256(
"Order(uint256 bidId,address trader,uint256 quantity,uint256 price,bool isBuying,uint256 expiry,uint256 nonce)"
);
/// @dev OTC price tolerance cannot exceed 20%
uint256 public constant MAX_OTC_PRICE_TOLERANCE = 2e17; // 20%
/// @dev min auction TWAP
uint32 public constant MIN_AUCTION_TWAP = 180 seconds;

/// @dev owner sets to true when starting auction
bool public isAuctionLive;

/// @dev min WETH amounts to withdraw or deposit via netting
uint256 public minWethAmount;
/// @dev min ZenBull amounts to withdraw or deposit via netting
uint256 public minBullAmount;
/// @dev array index of last processed deposits
uint256 public depositsIndex;
/// @dev array index of last processed withdraws
uint256 public withdrawsIndex;
// @dev OTC price must be within this distance of the uniswap twap price
uint256 public otcPriceTolerance;
/// @dev twap period to use for auction calculations
uint32 public auctionTwapPeriod;

/// @dev order struct for a signed order from market maker
struct Order {
uint256 bidId;
address trader;
uint256 quantity;
uint256 price;
bool isBuying;
uint256 expiry;
uint256 nonce;
uint8 v;
bytes32 r;
bytes32 s;
}

event SetMinBullAmount(uint256 oldAmount, uint256 newAmount);
event SetMinWethAmount(uint256 oldAmount, uint256 newAmount);
event SetDepositsIndex(uint256 oldDepositsIndex, uint256 newDepositsIndex);
event SetWithdrawsIndex(uint256 oldWithdrawsIndex, uint256 newWithdrawsIndex);
event SetAuctionTwapPeriod(uint32 previousTwap, uint32 newTwap);
event SetOTCPriceTolerance(uint256 previousTolerance, uint256 newOtcPriceTolerance);
event ToggledAuctionLive(bool isAuctionLive);

constructor() EIP712("ZenBullNetting", "1") {
otcPriceTolerance = 5e16; // 5%
auctionTwapPeriod = 420 seconds;
}

/**
* @dev view function to get the domain seperator used in signing
*/
function DOMAIN_SEPARATOR() external view returns (bytes32) {
return _domainSeparatorV4();
}

/**
* @dev toggles the value of isAuctionLive
*/
function toggleAuctionLive() external onlyOwner {
isAuctionLive = !isAuctionLive;
emit ToggledAuctionLive(isAuctionLive);
}

/**
* @notice set min Weth amount
* @param _amount the amount to be set as minWethAmount
*/
function setMinWethAmount(uint256 _amount) external onlyOwner {
emit SetMinWethAmount(minWethAmount, _amount);
minWethAmount = _amount;
}

/**
* @notice set minBullAmount
* @param _amount the number to be set as minBullAmount
*/
function setMinBullAmount(uint256 _amount) external onlyOwner {
emit SetMinBullAmount(minBullAmount, _amount);

minBullAmount = _amount;
}

/**
* @notice set the depositIndex so that we want to skip processing some deposits
* @param _newDepositsIndex the new deposits index
*/
function setDepositsIndex(uint256 _newDepositsIndex) external onlyOwner {
emit SetDepositsIndex(depositsIndex, _newDepositsIndex);

depositsIndex = _newDepositsIndex;
}

/**
* @notice set the withdraw index so that we want to skip processing some withdraws
* @param _newWithdrawsIndex the new withdraw index
*/
function setWithdrawsIndex(uint256 _newWithdrawsIndex) external onlyOwner {
emit SetWithdrawsIndex(withdrawsIndex, _newWithdrawsIndex);

withdrawsIndex = _newWithdrawsIndex;
}

/**
* @notice owner can set the twap period in seconds that is used for obtaining TWAP prices
* @param _auctionTwapPeriod the twap period, in seconds
*/
function setAuctionTwapPeriod(uint32 _auctionTwapPeriod) external onlyOwner {
require(_auctionTwapPeriod >= MIN_AUCTION_TWAP, "ZBN01");

emit SetAuctionTwapPeriod(auctionTwapPeriod, _auctionTwapPeriod);

auctionTwapPeriod = _auctionTwapPeriod;
}

/**
* @notice owner can set a threshold, scaled by 1e18 that determines the maximum discount of a clearing sale price to the current uniswap twap price
* @param _otcPriceTolerance the OTC price tolerance, in percent, scaled by 1e18
*/
function setOTCPriceTolerance(uint256 _otcPriceTolerance) external onlyOwner {
// Tolerance cannot be more than 20%
require(_otcPriceTolerance <= MAX_OTC_PRICE_TOLERANCE, "ZBN02");

emit SetOTCPriceTolerance(otcPriceTolerance, _otcPriceTolerance);

otcPriceTolerance = _otcPriceTolerance;
}
}
24 changes: 0 additions & 24 deletions packages/zen-bull-netting/test/Counter.t.sol

This file was deleted.

43 changes: 43 additions & 0 deletions packages/zen-bull-netting/test/ZenBullNettingBaseSetup.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
pragma solidity ^0.8.13;

pragma abicoder v2;

// test dependency
import "forge-std/Test.sol";
import { console } from "forge-std/console.sol";

// contracts
import { ZenBullNetting } from "../src/ZenBullNetting.sol";

/**
* Unit tests
*/
contract ZenBullNettingBaseSetup is Test {
ZenBullNetting internal zenBullNetting;

uint256 public deployerPk;
uint256 public ownerPk;
address public deployer;
address public owner;

function setUp() public virtual {
string memory FORK_URL = vm.envString("FORK_URL");
vm.createSelectFork(FORK_URL, 16419302);

deployerPk = 0xA11CD;
deployer = vm.addr(deployerPk);
ownerPk = 0xB11CD;
owner = vm.addr(ownerPk);

vm.startPrank(deployer);
zenBullNetting = new ZenBullNetting();
zenBullNetting.transferOwnership(owner);
vm.stopPrank();

vm.label(deployer, "Deployer");
vm.label(owner, "Owner");
vm.label(address(zenBullNetting), "ZenBullNetting");
}

function testIgnoreCoverageReport() public { }
}
Loading

1 comment on commit e875084

@vercel
Copy link

@vercel vercel bot commented on e875084 Jan 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.