Skip to content

Commit

Permalink
refactor: add .clone(vault) method to proxy-able BaseStrategy (year…
Browse files Browse the repository at this point in the history
…n#214)

* refactor: add `.clone(vault)` method to proxy-able BaseStrategy
* refactor: add ability to choose overloaded clone
* fix: intitialize strategy with different roles

Co-authored-by: poolpitako <78830419+poolpitako@users.noreply.github.com>
  • Loading branch information
fubuloubu and poolpitako authored Feb 24, 2021
1 parent dd3bee8 commit 6b61d12
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 116 deletions.
29 changes: 29 additions & 0 deletions contracts/BaseStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,8 @@ abstract contract BaseStrategy {
}

abstract contract BaseStrategyInitializable is BaseStrategy {
event Cloned(address indexed clone);

constructor(address _vault) public BaseStrategy(_vault) {}

function initialize(
Expand All @@ -786,4 +788,31 @@ abstract contract BaseStrategyInitializable is BaseStrategy {
) external {
_initialize(_vault, _strategist, _rewards, _keeper);
}

function clone(address _vault) external returns (address) {
return this.clone(_vault, msg.sender, msg.sender, msg.sender);
}

function clone(
address _vault,
address _strategist,
address _rewards,
address _keeper
) external returns (address newStrategy) {
// Copied from https://github.com/optionality/clone-factory/blob/master/contracts/CloneFactory.sol
bytes20 addressBytes = bytes20(address(this));

assembly {
// EIP-1167 bytecode
let clone_code := mload(0x40)
mstore(clone_code, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(clone_code, 0x14), addressBytes)
mstore(add(clone_code, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
newStrategy := create(0, clone_code, 0x37)
}

BaseStrategyInitializable(newStrategy).initialize(_vault, msg.sender, msg.sender, msg.sender);

emit Cloned(newStrategy);
}
}
103 changes: 0 additions & 103 deletions contracts/utils/ProxyFactoryInitializable.sol

This file was deleted.

17 changes: 4 additions & 13 deletions tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,15 @@ def keeper(accounts):
yield accounts[5]


@pytest.fixture
def proxyFactory(gov, ProxyFactoryInitializable):
yield gov.deploy(ProxyFactoryInitializable)


@pytest.fixture(params=["NoProxy", "Proxy"])
def strategy(
gov, strategist, keeper, rewards, token, vault, TestStrategy, proxyFactory, request
):
def strategy(gov, strategist, keeper, rewards, vault, TestStrategy, request):
strategy = strategist.deploy(TestStrategy, vault)

if request.param == "Proxy":
# prepare call and data to initialize the proxy
data = strategy.initialize.encode_input(vault, strategist, rewards, keeper)
# deploy the proxy using as logic the original strategy
tx = proxyFactory.deployMinimal(strategy, data, {"from": strategist})
# strategy proxy address is returned in the event ProxyCreated
strategyAddress = tx.events["ProxyCreated"]["proxy"]
tx = strategy.clone(vault, strategist, rewards, keeper, {"from": strategist})
# strategy proxy address is returned in the event `Cloned`
strategyAddress = tx.events["Cloned"]["clone"]
# redefine strategy as the new proxy deployed
strategy = TestStrategy.at(strategyAddress, owner=strategist)

Expand Down

0 comments on commit 6b61d12

Please sign in to comment.