Skip to content

Commit

Permalink
Split funnel init libs to deploy/funnels
Browse files Browse the repository at this point in the history
  • Loading branch information
oldchili committed Aug 26, 2024
1 parent d941bae commit ad86694
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 238 deletions.
64 changes: 6 additions & 58 deletions deploy/AllocatorDeploy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,13 @@ pragma solidity ^0.8.16;

import { ScriptTools } from "dss-test/ScriptTools.sol";

import { AllocatorOracle } from "src/AllocatorOracle.sol";
import { AllocatorRoles } from "src/AllocatorRoles.sol";
import { AllocatorRegistry } from "src/AllocatorRegistry.sol";
import { AllocatorBuffer } from "src/AllocatorBuffer.sol";
import { AllocatorVault } from "src/AllocatorVault.sol";
import { Swapper } from "src/funnels/Swapper.sol";
import { DepositorUniV3 } from "src/funnels/DepositorUniV3.sol";
import { VaultMinter } from "src/funnels/automation/VaultMinter.sol";
import { StableSwapper } from "src/funnels/automation/StableSwapper.sol";
import { StableDepositorUniV3 } from "src/funnels/automation/StableDepositorUniV3.sol";
import { ConduitMover } from "src/funnels/automation/ConduitMover.sol";

import { AllocatorSharedInstance, AllocatorIlkInstance, AllocatorIlkFunnelInstance } from "./AllocatorInstances.sol";
import { AllocatorOracle } from "src/AllocatorOracle.sol";
import { AllocatorRoles } from "src/AllocatorRoles.sol";
import { AllocatorRegistry } from "src/AllocatorRegistry.sol";
import { AllocatorBuffer } from "src/AllocatorBuffer.sol";
import { AllocatorVault } from "src/AllocatorVault.sol";

import { AllocatorSharedInstance, AllocatorIlkInstance } from "./AllocatorInstances.sol";

library AllocatorDeploy {

Expand Down Expand Up @@ -71,49 +64,4 @@ library AllocatorDeploy {

ilkInstance.owner = owner;
}

// Note: owner is assumed to be the allocator proxy
function deployIlkFunnel(
address deployer,
address owner,
address roles,
bytes32 ilk,
address uniV3Factory,
address vault,
address buffer
) internal returns (AllocatorIlkFunnelInstance memory ilkFunnelInstance) {
address _swapper = address(new Swapper(roles, ilk, buffer));
ScriptTools.switchOwner(_swapper, deployer, owner);
ilkFunnelInstance.swapper = _swapper;

address _depositorUniV3 = address(new DepositorUniV3(roles, ilk, uniV3Factory, buffer));
ScriptTools.switchOwner(_depositorUniV3, deployer, owner);
ilkFunnelInstance.depositorUniV3 = _depositorUniV3;

{
address _vaultMinter = address(new VaultMinter(vault));
ScriptTools.switchOwner(_vaultMinter, deployer, owner);
ilkFunnelInstance.vaultMinter = _vaultMinter;
}

{
address _stableSwapper = address(new StableSwapper(_swapper));
ScriptTools.switchOwner(_stableSwapper, deployer, owner);
ilkFunnelInstance.stableSwapper = _stableSwapper;
}

{
address _stableDepositorUniV3 = address(new StableDepositorUniV3(_depositorUniV3));
ScriptTools.switchOwner(_stableDepositorUniV3, deployer, owner);
ilkFunnelInstance.stableDepositorUniV3 = _stableDepositorUniV3;
}

{
address _conduitMover = address(new ConduitMover(ilk, buffer));
ScriptTools.switchOwner(_conduitMover, deployer, owner);
ilkFunnelInstance.conduitMover = _conduitMover;
}

ilkFunnelInstance.owner = owner;
}
}
167 changes: 1 addition & 166 deletions deploy/AllocatorInit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ pragma solidity >=0.8.0;

import { ScriptTools } from "dss-test/ScriptTools.sol";
import { DssInstance } from "dss-test/MCD.sol";
import { AllocatorSharedInstance, AllocatorIlkInstance, AllocatorIlkFunnelInstance } from "./AllocatorInstances.sol";

interface WardsLike {
function rely(address) external;
function deny(address) external;
}
import { AllocatorSharedInstance, AllocatorIlkInstance } from "./AllocatorInstances.sol";

interface IlkRegistryLike {
function put(
Expand All @@ -41,8 +36,6 @@ interface IlkRegistryLike {

interface RolesLike {
function setIlkAdmin(bytes32, address) external;
function setUserRole(bytes32, address, uint8, bool) external;
function setRoleAction(bytes32, uint8, address, bytes4, bool) external;
}

interface RegistryLike {
Expand All @@ -56,73 +49,12 @@ interface VaultLike {
function vat() external view returns (address);
function nst() external view returns (address);
function file(bytes32, address) external;
function draw(uint256) external;
function wipe(uint256) external;
}

interface BufferLike {
function approve(address, address, uint256) external;
}

interface SwapperLike {
function roles() external view returns (address);
function ilk() external view returns (bytes32);
function buffer() external view returns (address);
function swap(address, address, uint256, uint256, address, bytes calldata) external returns (uint256);
}

interface DepositorUniV3Like {
struct LiquidityParams {
address gem0;
address gem1;
uint24 fee;
int24 tickLower;
int24 tickUpper;
uint128 liquidity;
uint256 amt0Desired;
uint256 amt1Desired;
uint256 amt0Min;
uint256 amt1Min;
}

struct CollectParams {
address gem0;
address gem1;
uint24 fee;
int24 tickLower;
int24 tickUpper;
}

function roles() external view returns (address);
function ilk() external view returns (bytes32);
function uniV3Factory() external view returns (address);
function buffer() external view returns (address);
function deposit(LiquidityParams memory) external returns (uint128, uint256, uint256);
function withdraw(LiquidityParams memory, bool) external returns (uint128, uint256, uint256, uint256, uint256);
function collect(CollectParams memory) external returns (uint256, uint256);
}

interface VaultMinterLike {
function vault() external view returns (address);
}

interface StableSwapperLike {
function swapper() external view returns (address);
}

interface StableDepositorUniV3Like {
function depositor() external view returns (address);
}

interface ConduitMoverLike {
function ilk() external view returns (bytes32);
function buffer() external view returns (address);
}

interface KissLike {
function kiss(address) external;
}

interface AutoLineLike {
function setIlk(bytes32, uint256, uint256, uint256) external;
}
Expand All @@ -137,21 +69,6 @@ struct AllocatorIlkConfig {
address ilkRegistry;
}

struct AllocatorIlkFunnelConfig {
bytes32 ilk;
address allocatorProxy;
uint8 facilitatorRole;
uint8 automationRole;
address[] facilitators;
address[] vaultMinterKeepers;
address[] stableSwapperKeepers;
address[] stableDepositorUniV3Keepers;
address[] conduitMoverKeepers;
address[] swapTokens;
address[] depositTokens;
address uniV3Factory;
}

function bytes32ToStr(bytes32 _bytes32) pure returns (string memory) {
uint256 len;
while(len < 32 && _bytes32[len] != 0) len++;
Expand All @@ -165,7 +82,6 @@ function bytes32ToStr(bytes32 _bytes32) pure returns (string memory) {
library AllocatorInit {
uint256 constant WAD = 10 ** 18;
uint256 constant RAY = 10 ** 27;
uint256 constant RAD = 10 ** 45;

uint256 constant RATES_ONE_HUNDRED_PCT = 1000000021979553151239153027;

Expand Down Expand Up @@ -246,85 +162,4 @@ library AllocatorInit {
_symbol : bytes32ToStr(ilk)
});
}

// Please note this should be executed by the allocator proxy
function initIlkFunnel(
AllocatorSharedInstance memory sharedInstance,
AllocatorIlkInstance memory ilkInstance,
AllocatorIlkFunnelInstance memory ilkFunnelInstance,
AllocatorIlkFunnelConfig memory cfg
) internal {
bytes32 ilk = cfg.ilk;

require(SwapperLike(ilkFunnelInstance.swapper).roles() == sharedInstance.roles, "AllocatorInit/swapper-roles-mismatch");
require(SwapperLike(ilkFunnelInstance.swapper).ilk() == ilk, "AllocatorInit/swapper-ilk-mismatch");
require(SwapperLike(ilkFunnelInstance.swapper).buffer() == ilkInstance.buffer, "AllocatorInit/swapper-buffer-mismatch");

require(DepositorUniV3Like(ilkFunnelInstance.depositorUniV3).roles() == sharedInstance.roles, "AllocatorInit/depositorUniV3-roles-mismatch");
require(DepositorUniV3Like(ilkFunnelInstance.depositorUniV3).ilk() == ilk, "AllocatorInit/depositorUniV3-ilk-mismatch");
require(DepositorUniV3Like(ilkFunnelInstance.depositorUniV3).uniV3Factory() == cfg.uniV3Factory, "AllocatorInit/depositorUniV3-uniV3Factory-mismatch");
require(DepositorUniV3Like(ilkFunnelInstance.depositorUniV3).buffer() == ilkInstance.buffer, "AllocatorInit/depositorUniV3-buffer-mismatch");

require(VaultMinterLike(ilkFunnelInstance.vaultMinter).vault() == ilkInstance.vault, "AllocatorInit/vaultMinter-vault-mismatch");

require(StableSwapperLike(ilkFunnelInstance.stableSwapper).swapper() == ilkFunnelInstance.swapper, "AllocatorInit/stableSwapper-swapper-mismatch");
require(StableDepositorUniV3Like(ilkFunnelInstance.stableDepositorUniV3).depositor() == ilkFunnelInstance.depositorUniV3, "AllocatorInit/stableDepositorUniV3-depositorUniV3-mismatch");

require(ConduitMoverLike(ilkFunnelInstance.conduitMover).ilk() == ilk, "AllocatorInit/conduitMover-ilk-mismatch");
require(ConduitMoverLike(ilkFunnelInstance.conduitMover).buffer() == ilkInstance.buffer, "AllocatorInit/conduitMover-buffer-mismatch");

// Allow vault and funnels to pull funds from the buffer
for(uint256 i = 0; i < cfg.swapTokens.length; i++) {
BufferLike(ilkInstance.buffer).approve(cfg.swapTokens[i], ilkFunnelInstance.swapper, type(uint256).max);
}
for(uint256 i = 0; i < cfg.depositTokens.length; i++) {
BufferLike(ilkInstance.buffer).approve(cfg.depositTokens[i], ilkFunnelInstance.depositorUniV3, type(uint256).max);
}

// Allow the facilitators to operate on the vault and funnels directly
for(uint256 i = 0; i < cfg.facilitators.length; i++) {
RolesLike(sharedInstance.roles).setUserRole(ilk, cfg.facilitators[i], cfg.facilitatorRole, true);
}

RolesLike(sharedInstance.roles).setRoleAction(ilk, cfg.facilitatorRole, ilkInstance.vault, VaultLike.draw.selector, true);
RolesLike(sharedInstance.roles).setRoleAction(ilk, cfg.facilitatorRole, ilkInstance.vault, VaultLike.wipe.selector, true);
RolesLike(sharedInstance.roles).setRoleAction(ilk, cfg.facilitatorRole, ilkFunnelInstance.swapper, SwapperLike.swap.selector, true);
RolesLike(sharedInstance.roles).setRoleAction(ilk, cfg.facilitatorRole, ilkFunnelInstance.depositorUniV3, DepositorUniV3Like.deposit.selector, true);
RolesLike(sharedInstance.roles).setRoleAction(ilk, cfg.facilitatorRole, ilkFunnelInstance.depositorUniV3, DepositorUniV3Like.withdraw.selector, true);
RolesLike(sharedInstance.roles).setRoleAction(ilk, cfg.facilitatorRole, ilkFunnelInstance.depositorUniV3, DepositorUniV3Like.collect.selector, true);

// Allow the automation contracts to operate on the funnels
RolesLike(sharedInstance.roles).setUserRole(ilk, ilkFunnelInstance.vaultMinter, cfg.automationRole, true);
RolesLike(sharedInstance.roles).setUserRole(ilk, ilkFunnelInstance.stableSwapper, cfg.automationRole, true);
RolesLike(sharedInstance.roles).setUserRole(ilk, ilkFunnelInstance.stableDepositorUniV3, cfg.automationRole, true);

RolesLike(sharedInstance.roles).setRoleAction(ilk, cfg.automationRole, ilkInstance.vault, VaultLike.draw.selector, true);
RolesLike(sharedInstance.roles).setRoleAction(ilk, cfg.automationRole, ilkInstance.vault, VaultLike.wipe.selector, true);
RolesLike(sharedInstance.roles).setRoleAction(ilk, cfg.automationRole, ilkFunnelInstance.swapper, SwapperLike.swap.selector, true);
RolesLike(sharedInstance.roles).setRoleAction(ilk, cfg.automationRole, ilkFunnelInstance.depositorUniV3, DepositorUniV3Like.deposit.selector, true);
RolesLike(sharedInstance.roles).setRoleAction(ilk, cfg.automationRole, ilkFunnelInstance.depositorUniV3, DepositorUniV3Like.withdraw.selector, true);
RolesLike(sharedInstance.roles).setRoleAction(ilk, cfg.automationRole, ilkFunnelInstance.depositorUniV3, DepositorUniV3Like.collect.selector, true);

// Allow facilitator to set configurations in the automation contracts
for(uint256 i = 0; i < cfg.facilitators.length; i++) {
WardsLike(ilkFunnelInstance.vaultMinter).rely(cfg.facilitators[i]);
WardsLike(ilkFunnelInstance.stableSwapper).rely(cfg.facilitators[i]);
WardsLike(ilkFunnelInstance.stableDepositorUniV3).rely(cfg.facilitators[i]);
WardsLike(ilkFunnelInstance.conduitMover).rely(cfg.facilitators[i]);
}

// Add keepers to the automation contracts
for(uint256 i = 0; i < cfg.vaultMinterKeepers.length; i++) {
KissLike(ilkFunnelInstance.vaultMinter).kiss(cfg.vaultMinterKeepers[i]);
}
for(uint256 i = 0; i < cfg.stableSwapperKeepers.length; i++) {
KissLike(ilkFunnelInstance.stableSwapper).kiss(cfg.stableSwapperKeepers[i]);
}
for(uint256 i = 0; i < cfg.stableDepositorUniV3Keepers.length; i++) {
KissLike(ilkFunnelInstance.stableDepositorUniV3).kiss(cfg.stableDepositorUniV3Keepers[i]);
}
for(uint256 i = 0; i < cfg.conduitMoverKeepers.length; i++) {
KissLike(ilkFunnelInstance.conduitMover).kiss(cfg.conduitMoverKeepers[i]);
}
}
}
10 changes: 0 additions & 10 deletions deploy/AllocatorInstances.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,3 @@ struct AllocatorIlkInstance {
address vault;
address buffer;
}

struct AllocatorIlkFunnelInstance {
address owner;
address swapper;
address depositorUniV3;
address vaultMinter;
address stableSwapper;
address stableDepositorUniV3;
address conduitMover;
}
76 changes: 76 additions & 0 deletions deploy/funnels/AllocatorFunnelDeploy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-FileCopyrightText: © 2023 Dai Foundation <www.daifoundation.org>
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

pragma solidity ^0.8.16;

import { ScriptTools } from "dss-test/ScriptTools.sol";

import { Swapper } from "src/funnels/Swapper.sol";
import { DepositorUniV3 } from "src/funnels/DepositorUniV3.sol";
import { VaultMinter } from "src/funnels/automation/VaultMinter.sol";
import { StableSwapper } from "src/funnels/automation/StableSwapper.sol";
import { StableDepositorUniV3 } from "src/funnels/automation/StableDepositorUniV3.sol";
import { ConduitMover } from "src/funnels/automation/ConduitMover.sol";

import { AllocatorIlkFunnelInstance } from "./AllocatorFunnelInstance.sol";

library AllocatorFunnelDeploy {

// Note: owner is assumed to be the allocator proxy
function deployIlkFunnel(
address deployer,
address owner,
address roles,
bytes32 ilk,
address uniV3Factory,
address vault,
address buffer
) internal returns (AllocatorIlkFunnelInstance memory ilkFunnelInstance) {
address _swapper = address(new Swapper(roles, ilk, buffer));
ScriptTools.switchOwner(_swapper, deployer, owner);
ilkFunnelInstance.swapper = _swapper;

address _depositorUniV3 = address(new DepositorUniV3(roles, ilk, uniV3Factory, buffer));
ScriptTools.switchOwner(_depositorUniV3, deployer, owner);
ilkFunnelInstance.depositorUniV3 = _depositorUniV3;

{
address _vaultMinter = address(new VaultMinter(vault));
ScriptTools.switchOwner(_vaultMinter, deployer, owner);
ilkFunnelInstance.vaultMinter = _vaultMinter;
}

{
address _stableSwapper = address(new StableSwapper(_swapper));
ScriptTools.switchOwner(_stableSwapper, deployer, owner);
ilkFunnelInstance.stableSwapper = _stableSwapper;
}

{
address _stableDepositorUniV3 = address(new StableDepositorUniV3(_depositorUniV3));
ScriptTools.switchOwner(_stableDepositorUniV3, deployer, owner);
ilkFunnelInstance.stableDepositorUniV3 = _stableDepositorUniV3;
}

{
address _conduitMover = address(new ConduitMover(ilk, buffer));
ScriptTools.switchOwner(_conduitMover, deployer, owner);
ilkFunnelInstance.conduitMover = _conduitMover;
}

ilkFunnelInstance.owner = owner;
}
}
Loading

0 comments on commit ad86694

Please sign in to comment.