Skip to content

Commit 7db2940

Browse files
committed
use helper lib to reduce boilerplate code around delegatecall
1 parent 19624e6 commit 7db2940

File tree

3 files changed

+29
-38
lines changed

3 files changed

+29
-38
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-License-Identifier: AGPLv3
2+
pragma solidity ^0.8.23;
3+
4+
import { IYieldBackend } from "../interfaces/superfluid/IYieldBackend.sol";
5+
6+
7+
/**
8+
* @dev Helper to delegatecall yield backend methods.
9+
* Reverts if the call fails.
10+
* Does NOT return anything!
11+
*/
12+
library YieldBackendHelperLib {
13+
function dCall(IYieldBackend yieldBackend, bytes memory callData) internal {
14+
(bool success,) = address(yieldBackend).delegatecall(callData);
15+
require(success, "yield backend delegatecall failed");
16+
}
17+
}

packages/ethereum-contracts/contracts/superfluid/AaveYieldBackend.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pragma solidity ^0.8.23;
44
import { IYieldBackend } from "../interfaces/superfluid/IYieldBackend.sol";
55
import { IERC20, ISuperToken } from "../interfaces/superfluid/ISuperfluid.sol";
66
import { IPool } from "aave-v3/interfaces/IPool.sol";
7-
import { IWETH } from "aave-v3//helpers/interfaces/IWETH.sol";
7+
import { IWETH } from "aave-v3/helpers/interfaces/IWETH.sol";
88

99

1010
/**

packages/ethereum-contracts/contracts/superfluid/SuperToken.sol

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
IPoolAdminNFT
1313
} from "../interfaces/superfluid/ISuperfluid.sol";
1414
import { IYieldBackend } from "../interfaces/superfluid/IYieldBackend.sol";
15+
import { YieldBackendHelperLib } from "../libs/YieldBackendHelperLib.sol";
1516
import { SuperfluidToken } from "./SuperfluidToken.sol";
1617
import { ERC777Helper } from "../libs/ERC777Helper.sol";
1718
import { SafeERC20 } from "@openzeppelin-v5/contracts/token/ERC20/utils/SafeERC20.sol";
@@ -38,6 +39,7 @@ contract SuperToken is
3839
using SafeCast for uint256;
3940
using ERC777Helper for ERC777Helper.Operators;
4041
using SafeERC20 for IERC20;
42+
using YieldBackendHelperLib for IYieldBackend;
4143

4244
// See: https://eips.ethereum.org/EIPS/eip-1967#admin-address
4345
bytes32 constant private _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
@@ -204,29 +206,16 @@ contract SuperToken is
204206
function enableYieldBackend(IYieldBackend newYieldBackend) external onlyAdmin {
205207
require(address(_yieldBackend) == address(0), "yield backend already set");
206208
_yieldBackend = newYieldBackend;
207-
(bool success, ) = address(_yieldBackend).delegatecall(
208-
abi.encodeCall(IYieldBackend.enable, ())
209-
);
210-
require(success, "delegatecall failed");
211-
(success, ) = address(_yieldBackend).delegatecall(
212-
abi.encodeCall(IYieldBackend.depositMax, ())
213-
);
214-
require(success, "delegatecall failed");
209+
_yieldBackend.dCall(abi.encodeCall(IYieldBackend.enable, ()));
210+
_yieldBackend.dCall(abi.encodeCall(IYieldBackend.depositMax, ()));
215211
// TODO: emit event
216212
}
217213

218214
// withdraws everything and removes allowances
219215
function disableYieldBackend() external onlyAdmin {
220216
require(address(_yieldBackend) != address(0), "yield backend not set");
221-
(bool success, ) = address(_yieldBackend).delegatecall(
222-
abi.encodeCall(IYieldBackend.withdrawMax, ())
223-
);
224-
require(success, "delegatecall failed");
225-
(success, ) = address(_yieldBackend).delegatecall(
226-
abi.encodeCall(IYieldBackend.disable, ())
227-
);
228-
// TODO: should this be allowed to fail?
229-
require(success, "delegatecall failed");
217+
_yieldBackend.dCall(abi.encodeCall(IYieldBackend.withdrawMax, ()));
218+
_yieldBackend.dCall(abi.encodeCall(IYieldBackend.disable, ()));
230219
_yieldBackend = IYieldBackend(address(0));
231220
// TODO: emit event
232221
}
@@ -237,10 +226,7 @@ contract SuperToken is
237226

238227
function withdrawSurplusFromYieldBackend() external onlyAdmin {
239228
require(address(_yieldBackend) != address(0), "yield backend not set");
240-
(bool success, ) = address(_yieldBackend).delegatecall(
241-
abi.encodeCall(IYieldBackend.withdrawSurplus, (_totalSupply))
242-
);
243-
require(success, "delegatecall failed");
229+
_yieldBackend.dCall(abi.encodeCall(IYieldBackend.withdrawSurplus, (_totalSupply)));
244230
}
245231

246232
/**************************************************************************
@@ -778,10 +764,7 @@ contract SuperToken is
778764
if (!_skipSelfMint) {
779765
if (address(_yieldBackend) != address(0)) {
780766
// TODO: shall we deposit all, or just the upgradeAmount?
781-
(bool success, ) = address(_yieldBackend).delegatecall(
782-
abi.encodeCall(IYieldBackend.deposit, (amount))
783-
);
784-
require(success, "delegatecall failed");
767+
_yieldBackend.dCall(abi.encodeCall(IYieldBackend.deposit, (amount)));
785768
}
786769

787770
_mint(msg.sender, account, amount, userData.length != 0 /* invokeHook */,
@@ -802,10 +785,7 @@ contract SuperToken is
802785
if (address(_yieldBackend) != address(0)) {
803786
_skipSelfMint = true;
804787
// TODO: we may want to skip if enough underlying already in the contract
805-
(bool success, ) = address(_yieldBackend).delegatecall(
806-
abi.encodeCall(IYieldBackend.withdraw, (amount))
807-
);
808-
require(success, "delegatecall failed");
788+
_yieldBackend.dCall(abi.encodeCall(IYieldBackend.withdraw, (amount)));
809789
_skipSelfMint = false;
810790
}
811791
}
@@ -908,10 +888,7 @@ contract SuperToken is
908888

909889
if (address(_yieldBackend) != address(0)) {
910890
// TODO: shall we deposit all, or just the upgradeAmount?
911-
(bool success, ) = address(_yieldBackend).delegatecall(
912-
abi.encodeCall(IYieldBackend.deposit, (actualUpgradedAmount))
913-
);
914-
require(success, "delegatecall failed");
891+
_yieldBackend.dCall(abi.encodeCall(IYieldBackend.deposit, (actualUpgradedAmount)));
915892
}
916893

917894
_mint(operator, to, adjustedAmount,
@@ -938,10 +915,7 @@ contract SuperToken is
938915

939916
if (address(_yieldBackend) != address(0)) {
940917
// TODO: we may want to skip if enough underlying already in the contract
941-
(bool success, ) = address(_yieldBackend).delegatecall(
942-
abi.encodeCall(IYieldBackend.withdraw, (underlyingAmount))
943-
);
944-
require(success, "delegatecall failed");
918+
_yieldBackend.dCall(abi.encodeCall(IYieldBackend.withdraw, (underlyingAmount)));
945919
}
946920

947921
uint256 amountBefore = _underlyingToken.balanceOf(address(this));

0 commit comments

Comments
 (0)