Verified contractExactMIT
PendleWrapper
Address0x831ac38c842a68af7d8a15c707c2d69bceea6b88
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.24
- Same code
Only this contract
Details
- Compiler
- solc 0.8.24+commit.e11b9ed9
- Language
- solidity
- Optimization
- Yes, 1,000 runs
- EVM version
- cancun
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,429,261
- Creation transaction
- 0x7c801c83cd…d17006f61e
21 files
- contracts/exchangeV3/offchainWrappersV3/PendleWrapper.solmain1.39 KB
- contracts/_vendor/openzeppelin/Address.sol3.62 KB
- contracts/_vendor/openzeppelin/ReentrancyGuardTransient.sol2.06 KB
- contracts/_vendor/openzeppelin/SafeERC20.sol4.14 KB
- contracts/_vendor/openzeppelin/StorageSlot.sol9.32 KB
- contracts/auth/AdminAuth.sol1.23 KB
- contracts/auth/helpers/AuthHelper.sol175 B
- contracts/auth/helpers/MainnetAuthAddresses.sol599 B
- contracts/exchangeV3/DFSExchangeData.sol1.77 KB
- contracts/exchangeV3/DFSWrapperHelper.sol2.91 KB
- contracts/exchangeV3/helpers/ExchangeHelper.sol1.01 KB
- contracts/exchangeV3/helpers/MainnetExchangeAddresses.sol479 B
- contracts/interfaces/auth/IAdminVault.sol295 B
- contracts/interfaces/core/IDFSRegistry.sol444 B
- contracts/interfaces/exchange/IExchangeAggregatorRegistry.sol273 B
- contracts/interfaces/exchange/IOffchainWrapper.sol1.37 KB
- contracts/interfaces/exchange/IWrapperExchangeRegistry.sol276 B
- contracts/interfaces/token/IERC20.sol894 B
- contracts/interfaces/token/IWETH.sol498 B
- contracts/utils/DFSIds.sol1.25 KB
- contracts/utils/token/TokenUtils.sol2.83 KB
contracts/exchangeV3/offchainWrappersV3/PendleWrapper.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
import { IOffchainWrapper } from "../../interfaces/exchange/IOffchainWrapper.sol";
import { AdminAuth } from "../../auth/AdminAuth.sol";
import { DFSWrapperHelper } from "../DFSWrapperHelper.sol";
import { DFSExchangeData } from "../DFSExchangeData.sol";
import { ReentrancyGuardTransient } from "../../_vendor/openzeppelin/ReentrancyGuardTransient.sol";
/// @title PendleWrapper
/// @notice Wrapper contract used when the off-chain exchange is Pendle Router.
/// @dev This wrapper is meant to be used with DFSSellNoFee because we are not scaling the source token amount.
/// @dev See IOffchainWrapper for the shared security assumptions.
contract PendleWrapper is
IOffchainWrapper,
DFSWrapperHelper,
DFSExchangeData,
AdminAuth,
ReentrancyGuardTransient
{
/// @inheritdoc IOffchainWrapper
function takeOrder(ExchangeData memory _exData)
public
payable
override
nonReentrant
returns (bool success, uint256 tokensSwapped)
{
(success, tokensSwapped) = _executeOffchainSwap(_exData, _exData.offchainData.callData);
// Sends leftover tokens including ETH to the caller.
_sendLeftover(_exData.srcAddr, _exData.destAddr, payable(msg.sender));
}
// solhint-disable-next-line no-empty-blocks
receive() external payable { }
}
contracts/_vendor/openzeppelin/Address.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
library Address {
//insufficient balance
error InsufficientBalance(uint256 available, uint256 required);
//unable to send value, recipient may have reverted
error SendingValueFail();
//insufficient balance for call
error InsufficientBalanceForCall(uint256 available, uint256 required);
//call to non-contract
error NonContractCall();
function isContract(address account) internal view returns (bool) {
// According to EIP-1052, 0x0 is the value returned for not-yet created accounts
// and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
// for accounts without code, i.e. `keccak256('')`
bytes32 codehash;
bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
// solhint-disable-next-line no-inline-assembly
assembly {
codehash := extcodehash(account)
}
return (codehash != accountHash && codehash != 0x0);
}
function sendValue(address payable recipient, uint256 amount) internal {
uint256 balance = address(this).balance;
if (balance < amount) {
revert InsufficientBalance(balance, amount);
}
// solhint-disable-next-line avoid-low-level-calls, avoid-call-value
(bool success,) = recipient.call{ value: amount }("");
if (!(success)) {
revert SendingValueFail();
}
}
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
function functionCall(address target, bytes memory data, string memory errorMessage)
internal
returns (bytes memory)
{
return _functionCallWithValue(target, data, 0, errorMessage);
}
function functionCallWithValue(address target, bytes memory data, uint256 value)
internal
returns (bytes memory)
{
return functionCallWithValue(
target, data, value, "Address: low-level call with value failed"
);
}
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
uint256 balance = address(this).balance;
if (balance < value) {
revert InsufficientBalanceForCall(balance, value);
}
return _functionCallWithValue(target, data, value, errorMessage);
}
function _functionCallWithValue(
address target,
bytes memory data,
uint256 weiValue,
string memory errorMessage
) private returns (bytes memory) {
if (!(isContract(target))) {
revert NonContractCall();
}
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory returndata) = target.call{ value: weiValue }(data);
if (success) {
return returndata;
} else {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
// solhint-disable-next-line no-inline-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
contracts/_vendor/openzeppelin/ReentrancyGuardTransient.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
import { StorageSlot } from "./StorageSlot.sol";
/**
* https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/ReentrancyGuardTransient.sol
*
* @dev Variant of {ReentrancyGuard} that uses transient storage.
*
* NOTE: This variant only works on networks where EIP-1153 is available.
*/
abstract contract ReentrancyGuardTransient {
using StorageSlot for *;
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.ReentrancyGuard")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant REENTRANCY_GUARD_STORAGE =
0x9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00;
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be NOT_ENTERED
if (_reentrancyGuardEntered()) {
revert ReentrancyGuardReentrantCall();
}
// Any calls to nonReentrant after this point will fail
REENTRANCY_GUARD_STORAGE.asBoolean().tstore(true);
}
function _nonReentrantAfter() private {
REENTRANCY_GUARD_STORAGE.asBoolean().tstore(false);
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return REENTRANCY_GUARD_STORAGE.asBoolean().tload();
}
}
contracts/_vendor/openzeppelin/SafeERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/utils/SafeERC20.sol)
// changes made - renamed forceApprove to safeApprove
// changes made - removed safePermit, safeIncreaseAllowance, safeDecreaseAllowance functions
pragma solidity =0.8.24;
import { IERC20 } from "../../interfaces/token/IERC20.sol";
import { Address } from "./Address.sol";
library SafeERC20 {
using Address for address;
/**
* @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
/**
* @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
* calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
*/
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(
token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value)
);
}
/**
* @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful. Compatible with tokens that require the approval to be set to
* 0 before setting it to a non-zero value.
*/
function safeApprove(IERC20 token, address spender, uint256 value) internal {
bytes memory approvalCall = abi.encodeWithSelector(token.approve.selector, spender, value);
if (!_callOptionalReturnBool(token, approvalCall)) {
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0));
_callOptionalReturn(token, approvalCall);
}
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/
function _callOptionalReturn(IERC20 token, bytes memory data) private {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata =
address(token).functionCall(data, "SafeERC20: low-level call failed");
require(
returndata.length == 0 || abi.decode(returndata, (bool)),
"SafeERC20: ERC20 operation did not succeed"
);
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*
* This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead.
*/
function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We cannot use {Address-functionCall} here since this should return false
// and not revert is the subcall reverts.
(bool success, bytes memory returndata) = address(token).call(data);
return success && (returndata.length == 0 || abi.decode(returndata, (bool)))
&& address(token).code.length > 0;
}
}
contracts/_vendor/openzeppelin/StorageSlot.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/StorageSlot.sol)
// This file was procedurally generated from scripts/generate/templates/StorageSlot.js.
pragma solidity =0.8.24;
/**
* @dev Library for reading and writing primitive types to specific storage slots.
*
* Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
* This library helps with reading and writing to such slots without the need for inline assembly.
*
* The functions in this library return Slot structs that contain a `value` member that can be used to read or write.
*
* Example usage to set ERC-1967 implementation slot:
* ```solidity
* contract ERC1967 {
* // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
* bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
*
* function _getImplementation() internal view returns (address) {
* return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
* }
*
* function _setImplementation(address newImplementation) internal {
* require(newImplementation.code.length > 0);
* StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
* }
* }
* ```
*
* Since version 5.1, this library also support writing and reading value types to and from transient storage.
*
* * Example using transient storage:
* ```solidity
* contract Lock {
* // Define the slot. Alternatively, use the SlotDerivation library to derive the slot.
* bytes32 internal constant _LOCK_SLOT = 0xf4678858b2b588224636b8522b729e7722d32fc491da849ed75b3fdf3c84f542;
*
* modifier locked() {
* require(!_LOCK_SLOT.asBoolean().tload());
*
* _LOCK_SLOT.asBoolean().tstore(true);
* _;
* _LOCK_SLOT.asBoolean().tstore(false);
* }
* }
* ```
*
* TIP: Consider using this library along with {SlotDerivation}.
*/
library StorageSlot {
struct AddressSlot {
address value;
}
struct BooleanSlot {
bool value;
}
struct Bytes32Slot {
bytes32 value;
}
struct Uint256Slot {
uint256 value;
}
struct Int256Slot {
int256 value;
}
struct StringSlot {
string value;
}
struct BytesSlot {
bytes value;
}
/**
* @dev Returns an `AddressSlot` with member `value` located at `slot`.
*/
function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
}
/**
* @dev Returns an `BooleanSlot` with member `value` located at `slot`.
*/
function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
}
/**
* @dev Returns an `Bytes32Slot` with member `value` located at `slot`.
*/
function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
}
/**
* @dev Returns an `Uint256Slot` with member `value` located at `slot`.
*/
function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
}
/**
* @dev Returns an `Int256Slot` with member `value` located at `slot`.
*/
function getInt256Slot(bytes32 slot) internal pure returns (Int256Slot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
}
/**
* @dev Returns an `StringSlot` with member `value` located at `slot`.
*/
function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
}
/**
* @dev Returns an `StringSlot` representation of the string storage pointer `store`.
*/
function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := store.slot
}
}
/**
* @dev Returns an `BytesSlot` with member `value` located at `slot`.
*/
function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
}
/**
* @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
*/
function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := store.slot
}
}
/**
* @dev UDVT that represent a slot holding a address.
*/
type AddressSlotType is bytes32;
/**
* @dev Cast an arbitrary slot to a AddressSlotType.
*/
function asAddress(bytes32 slot) internal pure returns (AddressSlotType) {
return AddressSlotType.wrap(slot);
}
/**
* @dev UDVT that represent a slot holding a bool.
*/
type BooleanSlotType is bytes32;
/**
* @dev Cast an arbitrary slot to a BooleanSlotType.
*/
function asBoolean(bytes32 slot) internal pure returns (BooleanSlotType) {
return BooleanSlotType.wrap(slot);
}
/**
* @dev UDVT that represent a slot holding a bytes32.
*/
type Bytes32SlotType is bytes32;
/**
* @dev Cast an arbitrary slot to a Bytes32SlotType.
*/
function asBytes32(bytes32 slot) internal pure returns (Bytes32SlotType) {
return Bytes32SlotType.wrap(slot);
}
/**
* @dev UDVT that represent a slot holding a uint256.
*/
type Uint256SlotType is bytes32;
/**
* @dev Cast an arbitrary slot to a Uint256SlotType.
*/
function asUint256(bytes32 slot) internal pure returns (Uint256SlotType) {
return Uint256SlotType.wrap(slot);
}
/**
* @dev UDVT that represent a slot holding a int256.
*/
type Int256SlotType is bytes32;
/**
* @dev Cast an arbitrary slot to a Int256SlotType.
*/
function asInt256(bytes32 slot) internal pure returns (Int256SlotType) {
return Int256SlotType.wrap(slot);
}
/**
* @dev Load the value held at location `slot` in transient storage.
*/
function tload(AddressSlotType slot) internal view returns (address value) {
/// @solidity memory-safe-assembly
assembly {
value := tload(slot)
}
}
/**
* @dev Store `value` at location `slot` in transient storage.
*/
function tstore(AddressSlotType slot, address value) internal {
/// @solidity memory-safe-assembly
assembly {
tstore(slot, value)
}
}
/**
* @dev Load the value held at location `slot` in transient storage.
*/
function tload(BooleanSlotType slot) internal view returns (bool value) {
/// @solidity memory-safe-assembly
assembly {
value := tload(slot)
}
}
/**
* @dev Store `value` at location `slot` in transient storage.
*/
function tstore(BooleanSlotType slot, bool value) internal {
/// @solidity memory-safe-assembly
assembly {
tstore(slot, value)
}
}
/**
* @dev Load the value held at location `slot` in transient storage.
*/
function tload(Bytes32SlotType slot) internal view returns (bytes32 value) {
/// @solidity memory-safe-assembly
assembly {
value := tload(slot)
}
}
/**
* @dev Store `value` at location `slot` in transient storage.
*/
function tstore(Bytes32SlotType slot, bytes32 value) internal {
/// @solidity memory-safe-assembly
assembly {
tstore(slot, value)
}
}
/**
* @dev Load the value held at location `slot` in transient storage.
*/
function tload(Uint256SlotType slot) internal view returns (uint256 value) {
/// @solidity memory-safe-assembly
assembly {
value := tload(slot)
}
}
/**
* @dev Store `value` at location `slot` in transient storage.
*/
function tstore(Uint256SlotType slot, uint256 value) internal {
/// @solidity memory-safe-assembly
assembly {
tstore(slot, value)
}
}
/**
* @dev Load the value held at location `slot` in transient storage.
*/
function tload(Int256SlotType slot) internal view returns (int256 value) {
/// @solidity memory-safe-assembly
assembly {
value := tload(slot)
}
}
/**
* @dev Store `value` at location `slot` in transient storage.
*/
function tstore(Int256SlotType slot, int256 value) internal {
/// @solidity memory-safe-assembly
assembly {
tstore(slot, value)
}
}
}
contracts/auth/AdminAuth.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
import { IAdminVault } from "../interfaces/auth/IAdminVault.sol";
import { IERC20 } from "../interfaces/token/IERC20.sol";
import { SafeERC20 } from "../_vendor/openzeppelin/SafeERC20.sol";
import { AuthHelper } from "./helpers/AuthHelper.sol";
/// @title AdminAuth Handles owner/admin privileges over smart contracts
contract AdminAuth is AuthHelper {
using SafeERC20 for IERC20;
IAdminVault public constant adminVault = IAdminVault(ADMIN_VAULT_ADDR);
error SenderNotOwner();
error SenderNotAdmin();
modifier onlyOwner() {
if (adminVault.owner() != msg.sender) {
revert SenderNotOwner();
}
_;
}
modifier onlyAdmin() {
if (adminVault.admin() != msg.sender) {
revert SenderNotAdmin();
}
_;
}
/// @notice withdraw stuck funds
function withdrawStuckFunds(address _token, address _receiver, uint256 _amount)
public
onlyOwner
{
if (_token == 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) {
payable(_receiver).transfer(_amount);
} else {
IERC20(_token).safeTransfer(_receiver, _amount);
}
}
}
contracts/auth/helpers/AuthHelper.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
import { MainnetAuthAddresses } from "./MainnetAuthAddresses.sol";
contract AuthHelper is MainnetAuthAddresses { }
contracts/auth/helpers/MainnetAuthAddresses.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
contract MainnetAuthAddresses {
address internal constant ADMIN_VAULT_ADDR = 0xCCf3d848e08b94478Ed8f46fFead3008faF581fD;
address internal constant DSGUARD_FACTORY_ADDRESS = 0x5a15566417e6C1c9546523066500bDDBc53F88C7;
address internal constant ADMIN_ADDR = 0x25eFA336886C74eA8E282ac466BdCd0199f85BB9; // USED IN ADMIN VAULT CONSTRUCTOR
address internal constant PROXY_AUTH_ADDRESS = 0x149667b6FAe2c63D1B4317C716b0D0e4d3E2bD70;
address internal constant MODULE_AUTH_ADDRESS = 0x7407974DDBF539e552F1d051e44573090912CC3D;
}
contracts/exchangeV3/DFSExchangeData.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
contract DFSExchangeData {
struct OffchainData {
address wrapper; // dfs wrapper address for the aggregator (must be in WrapperExchangeRegistry)
address exchangeAddr; // exchange address we are calling to execute the order (must be in ExchangeAggregatorRegistry)
address allowanceTarget; // exchange aggregator contract we give allowance to
uint256 price; // expected price that the aggregator sent us
uint256 protocolFee; // deprecated (used as a separate fee amount for 0x v1)
bytes callData; // 0ff-chain calldata the aggregator gives to perform the swap
}
struct ExchangeData {
address srcAddr; // source token address (which we're selling)
address destAddr; // destination token address (which we're buying)
uint256 srcAmount; // amount of source token in token decimals
uint256 destAmount; // amount of bought token in token decimals
uint256 minPrice; // minPrice we are expecting (checked in DFSExchangeCore)
uint256 dfsFeeDivider; // service fee divider
address user; // currently deprecated (used to check custom fees for the user)
address wrapper; // on-chain wrapper address (must be in WrapperExchangeRegistry)
bytes wrapperData; // on-chain additional data for on-chain (uniswap route for example)
OffchainData offchainData; // offchain aggregator order
}
struct InjectedExchangeData {
address wrapper; // on-chain wrapper address (must be in WrapperExchangeRegistry)
bytes wrapperData; // on-chain additional data for on-chain (uniswap route for example)
OffchainData offchainData; // offchain aggregator order
}
}
contracts/exchangeV3/DFSWrapperHelper.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
import { IERC20 } from "../interfaces/token/IERC20.sol";
import { TokenUtils } from "../utils/token/TokenUtils.sol";
import { DFSExchangeData } from "./DFSExchangeData.sol";
import { ExchangeHelper } from "./helpers/ExchangeHelper.sol";
import { SafeERC20 } from "../_vendor/openzeppelin/SafeERC20.sol";
/// @title DFSWrapperHelper
/// @notice Helper contract used by other Wrapper contracts
contract DFSWrapperHelper is ExchangeHelper {
using TokenUtils for address;
using SafeERC20 for IERC20;
error InvalidOffchainData();
error ZeroTokensSwapped();
/// @notice Sends leftover tokens including ETH to the recipient
/// @param _srcAddr Source token address
/// @param _destAddr Destination token address
/// @param _to Recipient address
function _sendLeftover(address _srcAddr, address _destAddr, address payable _to) internal {
TokenUtils.ETH_ADDR.withdrawTokens(_to, type(uint256).max);
_srcAddr.withdrawTokens(_to, type(uint256).max);
_destAddr.withdrawTokens(_to, type(uint256).max);
}
/// @notice Approves the allowance target, executes the aggregator call and revokes the allowance
/// @param _exData The exchange data
/// @param _realCalldata Calldata forwarded to the aggregator
/// @return success Whether the aggregator call was successful
/// @return tokensSwapped Amount of destination tokens received
function _executeOffchainSwap(
DFSExchangeData.ExchangeData memory _exData,
bytes memory _realCalldata
) internal returns (bool success, uint256 tokensSwapped) {
if (!_getExchangeAggregatorRegistry()
.isExchangeAggregatorAddr(_exData.offchainData.exchangeAddr)) {
return (false, 0);
}
IERC20(_exData.srcAddr).safeApprove(_exData.offchainData.allowanceTarget, _exData.srcAmount);
uint256 tokensBefore = _exData.destAddr.getBalance(address(this));
(success,) = _exData.offchainData.exchangeAddr.call(_realCalldata);
if (success) {
// Gets the current balance of the swapped tokens.
tokensSwapped = _exData.destAddr.getBalance(address(this)) - tokensBefore;
if (tokensSwapped == 0) revert ZeroTokensSwapped();
}
_exData.srcAddr.removeAllowance(_exData.offchainData.allowanceTarget);
}
/// @notice Writes a uint256 to a bytes array
/// @param _b Bytes array
/// @param _index Index to write the uint256 to
/// @param _input Uint256 to write
function _writeUint256(bytes memory _b, uint256 _index, uint256 _input) internal pure {
if (_b.length < _index + 32) {
revert InvalidOffchainData();
}
bytes32 input = bytes32(_input);
_index += 32;
assembly {
mstore(add(_b, _index), input)
}
}
}
contracts/exchangeV3/helpers/ExchangeHelper.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
import {
IExchangeAggregatorRegistry
} from "../../interfaces/exchange/IExchangeAggregatorRegistry.sol";
import { IWrapperExchangeRegistry } from "../../interfaces/exchange/IWrapperExchangeRegistry.sol";
import { IDFSRegistry } from "../../interfaces/core/IDFSRegistry.sol";
import { DFSIds } from "../../utils/DFSIds.sol";
import { MainnetExchangeAddresses } from "./MainnetExchangeAddresses.sol";
contract ExchangeHelper is MainnetExchangeAddresses {
function _getExchangeAggregatorRegistry() internal view returns (IExchangeAggregatorRegistry) {
return IExchangeAggregatorRegistry(
IDFSRegistry(DFS_REGISTRY_ADDR).getAddr(DFSIds.EXCHANGE_AGGREGATOR_REGISTRY)
);
}
function _getWrapperExchangeRegistry() internal view returns (IWrapperExchangeRegistry) {
return IWrapperExchangeRegistry(
IDFSRegistry(DFS_REGISTRY_ADDR).getAddr(DFSIds.WRAPPER_EXCHANGE_REGISTRY)
);
}
}
contracts/exchangeV3/helpers/MainnetExchangeAddresses.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
contract MainnetExchangeAddresses {
address internal constant FEE_RECIPIENT_ADDRESS = 0x39C4a92Dc506300c3Ea4c67ca4CA611102ee6F2A;
address internal constant DISCOUNT_ADDRESS = 0x84fE6D4aaD0CA1ce3af7153eecd11729fa7a74f0;
address internal constant DFS_REGISTRY_ADDR = 0x287778F121F134C66212FB16c9b53eC991D32f5b;
address internal constant TOKEN_GROUP_REGISTRY = 0xcA49e64FE1FE8be40ED30F682edA1b27a6c8611c;
}
contracts/interfaces/auth/IAdminVault.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
interface IAdminVault {
function owner() external view returns (address);
function admin() external view returns (address);
function changeOwner(address _owner) external;
function changeAdmin(address _admin) external;
}
contracts/interfaces/core/IDFSRegistry.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
interface IDFSRegistry {
function getAddr(bytes4 _id) external view returns (address);
function addNewContract(bytes4 _id, address _contractAddr, uint256 _waitPeriod) external;
function startContractChange(bytes4 _id, address _newContractAddr) external;
function approveContractChange(bytes4 _id) external;
function cancelContractChange(bytes4 _id) external;
}
contracts/interfaces/exchange/IExchangeAggregatorRegistry.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
interface IExchangeAggregatorRegistry {
function isExchangeAggregatorAddr(address _exchangeAddr) external view returns (bool);
function setExchangeTargetAddr(address _exchangeAddr, bool _state) external;
}
contracts/interfaces/exchange/IOffchainWrapper.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
import { DFSExchangeData } from "../../exchangeV3/DFSExchangeData.sol";
/// @title IOffchainWrapper
/// @notice Shared interface for off-chain exchange wrapper contracts.
/// @dev Important security assumptions, which apply to every implementation:
/// 1. Exchange wrapper contracts are not intended to be used standalone, but as part of a DFS sell action,
/// which performs additional checks before forwarding the order and a final slippage check after the swap.
/// See DFSExchangeCore for more details.
/// 2. Wrapper contracts are designed to be stateless, meaning they do not hold funds,
/// and any token balances can be cleared by anyone.
interface IOffchainWrapper {
/// @notice Takes an order on the off-chain exchange and returns bool indicating if it is successful
/// @dev Implementations must approve exactly `_exData.srcAmount` to `offchainData.allowanceTarget`,
/// revoke that allowance before returning, and refund any leftover funds to the caller.
/// @param _exData The exchange data
/// @return success Whether the swap was successful
/// @return tokensSwapped Amount of destination tokens received
function takeOrder(DFSExchangeData.ExchangeData memory _exData)
external
payable
returns (bool success, uint256 tokensSwapped);
}
contracts/interfaces/exchange/IWrapperExchangeRegistry.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
interface IWrapperExchangeRegistry {
function isWrapper(address _wrapper) external view returns (bool);
function addWrapper(address _wrapper) external;
function removeWrapper(address _wrapper) external;
}
contracts/interfaces/token/IERC20.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
interface IERC20 {
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint256 digits);
function totalSupply() external view returns (uint256 supply);
function balanceOf(address _owner) external view returns (uint256 balance);
function transfer(address _to, uint256 _value) external returns (bool success);
function transferFrom(address _from, address _to, uint256 _value)
external
returns (bool success);
function approve(address _spender, uint256 _value) external returns (bool success);
function allowance(address _owner, address _spender) external view returns (uint256 remaining);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
contracts/interfaces/token/IWETH.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
interface IWETH {
function allowance(address, address) external view returns (uint256);
function balanceOf(address) external view returns (uint256);
function approve(address, uint256) external;
function transfer(address, uint256) external returns (bool);
function transferFrom(address, address, uint256) external returns (bool);
function deposit() external payable;
function withdraw(uint256) external;
}
contracts/utils/DFSIds.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
library DFSIds {
bytes4 internal constant DFS_PROXY_REGISTRY_V2 = bytes4(keccak256("DFSProxyRegistryV2"));
bytes4 internal constant RECIPE_EXECUTOR = bytes4(keccak256("RecipeExecutor"));
bytes4 internal constant STRATEGY_EXECUTOR = bytes4(keccak256("StrategyExecutorID")); // Abstract, so support both L1 and L2
bytes4 internal constant TX_SAVER_EXECUTOR = bytes4(keccak256("TxSaverExecutor"));
bytes4 internal constant BOT_AUTH = bytes4(keccak256("BotAuth"));
bytes4 internal constant BOT_AUTH_FOR_TX_SAVER = bytes4(keccak256("BotAuthForTxSaver"));
bytes4 internal constant KYBER_SCALING_HELPER = bytes4(keccak256("KyberInputScalingHelper"));
bytes4 internal constant WRAPPER_EXCHANGE_REGISTRY =
bytes4(keccak256("WrapperExchangeRegistry"));
bytes4 internal constant EXCHANGE_AGGREGATOR_REGISTRY =
bytes4(keccak256("ExchangeAggregatorRegistry"));
bytes4 internal constant LLAMALEND_SWAPPER = bytes4(keccak256("LlamaLendSwapper"));
bytes4 internal constant CURVE_TRANSIENT_SWAPPER =
bytes4(keccak256("CurveUsdSwapperTransient"));
bytes4 internal constant SFPROXY_ENTRY_POINT = bytes4(keccak256("SFProxyEntryPoint"));
}
contracts/utils/token/TokenUtils.sol
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
import { IWETH } from "../../interfaces/token/IWETH.sol";
import { IERC20 } from "../../interfaces/token/IERC20.sol";
import { SafeERC20 } from "../../_vendor/openzeppelin/SafeERC20.sol";
library TokenUtils {
using SafeERC20 for IERC20;
address public constant WETH_ADDR = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address public constant ETH_ADDR = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
/// @dev Only approves the amount if allowance is lower than amount, does not decrease allowance
function approveToken(address _tokenAddr, address _to, uint256 _amount) internal {
if (_tokenAddr == ETH_ADDR) return;
if (IERC20(_tokenAddr).allowance(address(this), _to) < _amount) {
IERC20(_tokenAddr).safeApprove(_to, _amount);
}
}
function removeAllowance(address _tokenAddr, address _to) internal {
if (_tokenAddr == ETH_ADDR || IERC20(_tokenAddr).allowance(address(this), _to) == 0) {
return;
}
IERC20(_tokenAddr).safeApprove(_to, 0);
}
function pullTokensIfNeeded(address _token, address _from, uint256 _amount)
internal
returns (uint256)
{
// handle max uint amount
if (_amount == type(uint256).max) {
_amount = getBalance(_token, _from);
}
if (_from != address(0) && _from != address(this) && _token != ETH_ADDR && _amount != 0) {
IERC20(_token).safeTransferFrom(_from, address(this), _amount);
}
return _amount;
}
function withdrawTokens(address _token, address _to, uint256 _amount)
internal
returns (uint256)
{
if (_amount == type(uint256).max) {
_amount = getBalance(_token, address(this));
}
if (_to != address(0) && _to != address(this) && _amount != 0) {
if (_token != ETH_ADDR) {
IERC20(_token).safeTransfer(_to, _amount);
} else {
(bool success,) = _to.call{ value: _amount }("");
require(success, "Eth send fail");
}
}
return _amount;
}
function depositWeth(uint256 _amount) internal {
IWETH(WETH_ADDR).deposit{ value: _amount }();
}
function withdrawWeth(uint256 _amount) internal {
IWETH(WETH_ADDR).withdraw(_amount);
}
function getBalance(address _tokenAddr, address _acc) internal view returns (uint256) {
if (_tokenAddr == ETH_ADDR) {
return _acc.balance;
} else {
return IERC20(_tokenAddr).balanceOf(_acc);
}
}
function getTokenDecimals(address _token) internal view returns (uint256) {
if (_token == ETH_ADDR) return 18;
return IERC20(_token).decimals();
}
}
Compiler settingsShow
{
"optimizer": {
"runs": 1000,
"enabled": true
},
"evmVersion": "cancun"
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.