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

Modularize zkBob pool and add energy redeemer example #78

Merged
merged 6 commits into from
Sep 5, 2023
Merged
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
2 changes: 1 addition & 1 deletion script/scripts/BobSeller.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity 0.8.15;

import "forge-std/Script.sol";
import "./Env.s.sol";
import "../../src/utils/UniswapV3Seller.sol";
import "../../src/infra/UniswapV3Seller.sol";

contract DeployBobSeller is Script {
function run() external {
Expand Down
86 changes: 86 additions & 0 deletions script/scripts/DeployZkBobPoolModules.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// SPDX-License-Identifier: CC0-1.0

pragma solidity 0.8.15;

import "forge-std/Script.sol";
import "forge-std/Test.sol";
import "./Env.s.sol";
import "../../src/zkbob/ZkBobPool.sol";
import "../../src/zkbob/utils/ZkBobAccounting.sol";
import "../../src/proxy/EIP1967Proxy.sol";
import "../../src/zkbob/ZkBobPoolBOB.sol";
import "../../src/zkbob/ZkBobPoolUSDC.sol";

contract DummyDelegateCall {
function delegate(address to, bytes calldata data) external {
(bool status,) = address(to).delegatecall(data);
require(status);
}
}

contract Migrator {
function migrate(address _target, address _newImpl, address _accounting) external {
address kycManager = address(ZkBobAccounting(_target).kycProvidersManager());

EIP1967Proxy(payable(_target)).upgradeTo(_newImpl);

bytes memory dump = ZkBobPool(_target).extsload(bytes32(uint256(1)), 2);
uint32 txCount = uint32(_load(dump, 0, 4));
uint88 cumTvl = uint88(_load(dump, 4, 11));
uint32 maxWeeklyTxCount = uint32(_load(dump, 21, 4));
uint56 maxWeeklyAvgTvl = uint56(_load(dump, 25, 7));
uint72 tvl = uint72(_load(dump, 55, 9));

ZkBobPool(_target).initializePoolIndex(txCount * 128);
ZkBobPool(_target).setAccounting(IZkBobAccounting(_accounting));
ZkBobAccounting(_accounting).initialize(txCount + 1, tvl, cumTvl, maxWeeklyTxCount, maxWeeklyAvgTvl);
ZkBobAccounting(_accounting).setKycProvidersManager(IKycProvidersManager(kycManager));
ZkBobAccounting(_accounting).setLimits(
0, 2_000_000 gwei, 300_000 gwei, 300_000 gwei, 10_000 gwei, 10_000 gwei, 10_000 gwei, 1_000 gwei
);
ZkBobAccounting(_accounting).setLimits(
1, 2_000_000 gwei, 300_000 gwei, 300_000 gwei, 100_000 gwei, 100_000 gwei, 10_000 gwei, 1_000 gwei
);
ZkBobAccounting(_accounting).setLimits(
254, 2_000_000 gwei, 300_000 gwei, 300_000 gwei, 20_000 gwei, 20_000 gwei, 10_000 gwei, 1_000 gwei
);
}

function _load(bytes memory _dump, uint256 _from, uint256 _len) internal returns (uint256 res) {
assembly {
res := shr(sub(256, shl(3, _len)), mload(add(_dump, add(32, _from))))
}
}
}

contract DeployZkBobPoolModules is Script, Test {
function run() external {
ZkBobPoolUSDC pool = ZkBobPoolUSDC(address(zkBobPool));
address owner = pool.owner();
vm.etch(owner, type(DummyDelegateCall).runtimeCode);

address tokenSeller = address(pool.tokenSeller());
uint256 poolIndex = uint256(pool.pool_index());

vm.startBroadcast();

ZkBobPoolUSDC impl = new ZkBobPoolUSDC(
pool.pool_id(), pool.token(), pool.transfer_verifier(), pool.tree_verifier(),
pool.batch_deposit_verifier(), address(pool.direct_deposit_queue())
);
Migrator mig = new Migrator();
ZkBobAccounting acc = new ZkBobAccounting(address(pool), 1_000_000_000);
acc.transferOwnership(owner);
DummyDelegateCall(owner).delegate(
address(mig), abi.encodeWithSelector(Migrator.migrate.selector, address(pool), address(impl), address(acc))
);

vm.stopBroadcast();

acc.slot0();
acc.slot1();

assertEq(address(pool.tokenSeller()), tokenSeller);
assertEq(uint256(pool.pool_index()), poolIndex);
}
}
24 changes: 15 additions & 9 deletions script/scripts/Local.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import "../../src/proxy/EIP1967Proxy.sol";
import "../../src/zkbob/ZkBobPoolBOB.sol";
import "../../src/zkbob/manager/MutableOperatorManager.sol";
import "../../src/zkbob/ZkBobDirectDepositQueue.sol";
import "../../src/zkbob/utils/ZkBobAccounting.sol";

contract DeployLocal is Script {
function run() external {
Expand Down Expand Up @@ -52,9 +53,19 @@ contract DeployLocal is Script {
address(queueProxy)
);
{
bytes memory initData = abi.encodeWithSelector(
ZkBobPool.initialize.selector,
zkBobInitialRoot,
bytes memory initData = abi.encodeWithSelector(ZkBobPool.initialize.selector, zkBobInitialRoot);
poolProxy.upgradeToAndCall(address(poolImpl), initData);
}
ZkBobPoolBOB pool = ZkBobPoolBOB(address(poolProxy));

ZkBobDirectDepositQueue queueImpl = new ZkBobDirectDepositQueue(address(pool), address(bob), 1_000_000_000);
queueProxy.upgradeTo(address(queueImpl));
ZkBobDirectDepositQueue queue = ZkBobDirectDepositQueue(address(queueProxy));

{
ZkBobAccounting accounting = new ZkBobAccounting(address(pool), 1_000_000_000);
accounting.setLimits(
0,
zkBobPoolCap,
zkBobDailyDepositCap,
zkBobDailyWithdrawalCap,
Expand All @@ -63,13 +74,8 @@ contract DeployLocal is Script {
zkBobDailyUserDirectDepositCap,
zkBobDirectDepositCap
);
poolProxy.upgradeToAndCall(address(poolImpl), initData);
pool.setAccounting(accounting);
}
ZkBobPoolBOB pool = ZkBobPoolBOB(address(poolProxy));

ZkBobDirectDepositQueue queueImpl = new ZkBobDirectDepositQueue(address(pool), address(bob), 1_000_000_000);
queueProxy.upgradeTo(address(queueImpl));
ZkBobDirectDepositQueue queue = ZkBobDirectDepositQueue(address(queueProxy));

{
IOperatorManager operatorManager =
Expand Down
93 changes: 54 additions & 39 deletions script/scripts/ZkBobPool.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,24 @@ import "../../src/zkbob/ZkBobPoolBOB.sol";
import "../../src/zkbob/ZkBobPoolETH.sol";
import "../../src/zkbob/ZkBobPoolUSDC.sol";
import "../../src/zkbob/ZkBobPoolERC20.sol";
import "../../src/zkbob/utils/ZkBobAccounting.sol";

contract DeployZkBobPool is Script {
struct Vars {
uint8 decimals;
uint256 denominator;
uint256 precision;
EIP1967Proxy poolProxy;
EIP1967Proxy queueProxy;
ZkBobPool poolImpl;
}

function run() external {
Vars memory vars;
vars.decimals = IERC20Metadata(zkBobToken).decimals();
vars.denominator = vars.decimals > 9 ? 10 ** (vars.decimals - 9) : 1;
vars.precision = vars.decimals > 9 ? 1_000_000_000 : 10 ** vars.decimals;

vm.startBroadcast();

ITransferVerifier transferVerifier;
Expand All @@ -33,64 +48,50 @@ contract DeployZkBobPool is Script {
batchDepositVerifier := create(0, add(code3, 0x20), mload(code3))
}

EIP1967Proxy poolProxy = new EIP1967Proxy(tx.origin, mockImpl, "");
EIP1967Proxy queueProxy = new EIP1967Proxy(tx.origin, mockImpl, "");
vars.poolProxy = new EIP1967Proxy(tx.origin, mockImpl, "");
vars.queueProxy = new EIP1967Proxy(tx.origin, mockImpl, "");

ZkBobPool poolImpl;
if (zkBobPoolType == PoolType.ETH) {
poolImpl = new ZkBobPoolETH(
vars.poolImpl = new ZkBobPoolETH(
zkBobPoolId, zkBobToken,
transferVerifier, treeVerifier, batchDepositVerifier,
address(queueProxy), permit2
address(vars.queueProxy), permit2
);
} else if (zkBobPoolType == PoolType.BOB) {
poolImpl = new ZkBobPoolBOB(
vars.poolImpl = new ZkBobPoolBOB(
zkBobPoolId, zkBobToken,
transferVerifier, treeVerifier, batchDepositVerifier,
address(queueProxy)
address(vars.queueProxy)
);
} else if (zkBobPoolType == PoolType.USDC) {
poolImpl = new ZkBobPoolUSDC(
vars.poolImpl = new ZkBobPoolUSDC(
zkBobPoolId, zkBobToken,
transferVerifier, treeVerifier, batchDepositVerifier,
address(queueProxy)
address(vars.queueProxy)
);
} else if (zkBobPoolType == PoolType.ERC20) {
uint8 decimals = IERC20Metadata(zkBobToken).decimals();
uint256 denominator = decimals > 9 ? 10 ** (decimals - 9) : 1;
uint256 precision = decimals > 9 ? 1_000_000_000 : 10 ** decimals;
poolImpl = new ZkBobPoolERC20(
vars.poolImpl = new ZkBobPoolERC20(
zkBobPoolId, zkBobToken,
transferVerifier, treeVerifier, batchDepositVerifier,
address(queueProxy), permit2,
denominator, precision
address(vars.queueProxy), permit2,
vars.denominator
);
} else {
revert("Unknown pool type");
}

bytes memory initData = abi.encodeWithSelector(
ZkBobPool.initialize.selector,
zkBobInitialRoot,
zkBobPoolCap,
zkBobDailyDepositCap,
zkBobDailyWithdrawalCap,
zkBobDailyUserDepositCap,
zkBobDepositCap,
zkBobDailyUserDirectDepositCap,
zkBobDirectDepositCap
);
poolProxy.upgradeToAndCall(address(poolImpl), initData);
ZkBobPool pool = ZkBobPool(address(poolProxy));
bytes memory initData = abi.encodeWithSelector(ZkBobPool.initialize.selector, zkBobInitialRoot);
vars.poolProxy.upgradeToAndCall(address(vars.poolImpl), initData);
ZkBobPool pool = ZkBobPool(address(vars.poolProxy));

ZkBobDirectDepositQueue queueImpl;
if (zkBobPoolType == PoolType.ETH) {
queueImpl = new ZkBobDirectDepositQueueETH(address(pool), zkBobToken, pool.denominator());
queueImpl = new ZkBobDirectDepositQueueETH(address(pool), zkBobToken, vars.denominator);
} else {
queueImpl = new ZkBobDirectDepositQueue(address(pool), zkBobToken, pool.denominator());
queueImpl = new ZkBobDirectDepositQueue(address(pool), zkBobToken, vars.denominator);
}
queueProxy.upgradeTo(address(queueImpl));
ZkBobDirectDepositQueue queue = ZkBobDirectDepositQueue(address(queueProxy));
vars.queueProxy.upgradeTo(address(queueImpl));
ZkBobDirectDepositQueue queue = ZkBobDirectDepositQueue(address(vars.queueProxy));

IOperatorManager operatorManager =
new MutableOperatorManager(zkBobRelayer, zkBobRelayerFeeReceiver, zkBobRelayerURL);
Expand All @@ -99,32 +100,46 @@ contract DeployZkBobPool is Script {
queue.setDirectDepositFee(uint64(zkBobDirectDepositFee));
queue.setDirectDepositTimeout(uint40(zkBobDirectDepositTimeout));

ZkBobAccounting accounting = new ZkBobAccounting(address(pool), vars.precision);
accounting.setLimits(
0,
zkBobPoolCap,
zkBobDailyDepositCap,
zkBobDailyWithdrawalCap,
zkBobDailyUserDepositCap,
zkBobDepositCap,
zkBobDailyUserDirectDepositCap,
zkBobDirectDepositCap
);
pool.setAccounting(accounting);

if (owner != address(0)) {
pool.transferOwnership(owner);
queue.transferOwnership(owner);
}

if (admin != tx.origin) {
poolProxy.setAdmin(admin);
queueProxy.setAdmin(admin);
vars.poolProxy.setAdmin(admin);
vars.queueProxy.setAdmin(admin);
}

vm.stopBroadcast();

require(poolProxy.implementation() == address(poolImpl), "Invalid implementation address");
require(poolProxy.admin() == admin, "Proxy admin is not configured");
require(vars.poolProxy.implementation() == address(vars.poolImpl), "Invalid implementation address");
require(vars.poolProxy.admin() == admin, "Proxy admin is not configured");
require(pool.owner() == owner, "Owner is not configured");
require(queueProxy.implementation() == address(queueImpl), "Invalid implementation address");
require(queueProxy.admin() == admin, "Proxy admin is not configured");
require(vars.queueProxy.implementation() == address(queueImpl), "Invalid implementation address");
require(vars.queueProxy.admin() == admin, "Proxy admin is not configured");
require(queue.owner() == owner, "Owner is not configured");
require(pool.transfer_verifier() == transferVerifier, "Transfer verifier is not configured");
require(pool.tree_verifier() == treeVerifier, "Tree verifier is not configured");
require(pool.batch_deposit_verifier() == batchDepositVerifier, "Batch deposit verifier is not configured");

console2.log("ZkBobPool:", address(pool));
console2.log("ZkBobPool implementation:", address(poolImpl));
console2.log("ZkBobPool implementation:", address(vars.poolImpl));
console2.log("ZkBobDirectDepositQueue:", address(queue));
console2.log("ZkBobDirectDepositQueue implementation:", address(queueImpl));
console2.log("ZkBobAccounting:", address(accounting));
console2.log("TransferVerifier:", address(transferVerifier));
console2.log("TreeUpdateVerifier:", address(treeVerifier));
console2.log("BatchDepositVierifier:", address(batchDepositVerifier));
Expand Down
Loading
Loading