Skip to content

Commit

Permalink
Support multicall for LockstakeEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
oldchili committed Dec 18, 2023
1 parent fae51fb commit eb233c6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/LockstakeEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
pragma solidity ^0.8.16;

import { LockstakeUrn } from "src/LockstakeUrn.sol";
import { Multicall } from "src/Multicall.sol";

interface DelegateFactoryLike {
function gov() external view returns (GemLike);
Expand Down Expand Up @@ -55,7 +56,7 @@ interface JugLike {
function drip(bytes32) external returns (uint256);
}

contract LockstakeEngine {
contract LockstakeEngine is Multicall {
// --- storage variables ---

mapping(address => uint256) public wards; // usr => 1 == access
Expand Down Expand Up @@ -192,7 +193,6 @@ contract LockstakeEngine {
function isUrnAuth(address urn, address usr) external view returns (bool ok) {
ok = _urnAuth(urn, usr);
}

// --- urn/delegation functions ---

function open() external returns (address urn) {
Expand Down
26 changes: 26 additions & 0 deletions src/Multicall.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: GPL-2.0-or-later

// Based on https://github.com/Uniswap/v3-periphery/blob/697c2474757ea89fec12a4e6db16a574fe259610/contracts/base/Multicall.sol

pragma solidity ^0.8.16;

// Enables calling multiple methods in a single call to the contract
abstract contract Multicall {
function multicall(bytes[] calldata data) public returns (bytes[] memory results) {
results = new bytes[](data.length);
for (uint256 i = 0; i < data.length; i++) {
(bool success, bytes memory result) = address(this).delegatecall(data[i]);

if (!success) {
// Next 5 lines from https://ethereum.stackexchange.com/a/83577
if (result.length < 68) revert();
assembly {
result := add(result, 0x04)
}
revert(abi.decode(result, (string)));
}

results[i] = result;
}
}
}
14 changes: 14 additions & 0 deletions test/LockstakeEngine.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,20 @@ contract AllocatorVaultTest is DssTest {
assertEq(farm.balanceOf(address(urn)), 45_000 * 10**18);
}

function testOpenLockStakeMulticall() public {
vm.prank(pauseProxy); engine.addFarm(address(farm));
gov.approve(address(engine), 100_000 * 10**18);

address urn = engine.getUrn(address(this), 0);

bytes[] memory callsToExecute = new bytes[](4);
callsToExecute[0] = abi.encodeWithSignature("open()");
callsToExecute[1] = abi.encodeWithSignature("lock(address,uint256)", urn, 100_000 * 10**18);
callsToExecute[2] = abi.encodeWithSignature("selectFarm(address,address)", urn, address(farm));
callsToExecute[3] = abi.encodeWithSignature("stake(address,uint256,uint16)", urn, 100_000 * 10**18, 1);
engine.multicall(callsToExecute);
}

function testGetReward() public {
vm.prank(pauseProxy); engine.addFarm(address(farm));
address urn = engine.open();
Expand Down

0 comments on commit eb233c6

Please sign in to comment.