Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated to erc7399 #8

Merged
merged 1 commit into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
[submodule "lib/erc3156"]
path = lib/erc3156
url = https://github.com/alcueca/erc3156
[submodule "lib/erc3156pp"]
path = lib/erc3156pp
url = https://github.com/alcueca/erc3156pp
[submodule "lib/erc7399"]
path = lib/erc7399
url = https://github.com/alcueca/erc7399
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Foundry Template [![Open in Gitpod][gitpod-badge]][gitpod] [![Github Actions][gha-badge]][gha] [![Foundry][foundry-badge]][foundry] [![License: MIT][license-badge]][license]

[gitpod]: https://gitpod.io/#https://github.com/alcueca/erc3156pp-wrappers
[gitpod]: https://gitpod.io/#https://github.com/alcueca/erc7399-wrappers
[gitpod-badge]: https://img.shields.io/badge/Gitpod-Open%20in%20Gitpod-FFB45B?logo=gitpod
[gha]: https://github.com/alcueca/erc3156pp-wrappers/actions
[gha-badge]: https://github.com/alcueca/erc3156pp-wrappers/actions/workflows/ci.yml/badge.svg
[gha]: https://github.com/alcueca/erc7399-wrappers/actions
[gha-badge]: https://github.com/alcueca/erc7399-wrappers/actions/workflows/ci.yml/badge.svg
[foundry]: https://getfoundry.sh/
[foundry-badge]: https://img.shields.io/badge/Built%20with-Foundry-FFDB1C.svg
[license]: https://opensource.org/licenses/MIT
Expand Down
1 change: 0 additions & 1 deletion lib/erc3156pp
Submodule erc3156pp deleted from 56ba9d
1 change: 1 addition & 0 deletions lib/erc7399
Submodule erc7399 added at 6f3740
2 changes: 1 addition & 1 deletion lib/prb-test
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@alcueca/erc3156pp-wrappers",
"name": "@alcueca/erc7399-wrappers",
"description": "Wrappers for existing Flash Lenders to be accessible as ERC3156++ Flash Lenders",
"version": "1.0.0",
"author": {
Expand Down
14 changes: 7 additions & 7 deletions src/BaseWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Thanks to ultrasecr.eth
pragma solidity ^0.8.0;

import { IERC3156PPFlashLender } from "lib/erc3156pp/src/interfaces/IERC3156PPFlashLender.sol";
import { IERC20 } from "lib/erc3156pp/src/interfaces/IERC20.sol";
import { IERC7399 } from "lib/erc7399/src/interfaces/IERC7399.sol";
import { IERC20 } from "lib/erc7399/src/interfaces/IERC20.sol";

import { TransferHelper } from "./utils/TransferHelper.sol";

abstract contract BaseWrapper is IERC3156PPFlashLender {
abstract contract BaseWrapper is IERC7399 {
using TransferHelper for IERC20;

event GasUsed(uint256 gasUsed);
Expand All @@ -23,8 +23,8 @@ abstract contract BaseWrapper is IERC3156PPFlashLender {

uint256 public expectedGas;

/// @inheritdoc IERC3156PPFlashLender
function flashLoan(
/// @inheritdoc IERC7399
function flash(
address loanReceiver,
IERC20 asset,
uint256 amount,
Expand Down Expand Up @@ -91,9 +91,9 @@ abstract contract BaseWrapper is IERC3156PPFlashLender {
}

/// @dev Measure and record gas used in flash loans
function setExpectedGas(IERC20 asset) external returns (uint256 gasUsed){
function setExpectedGas(IERC20 asset) external returns (uint256 gasUsed) {
uint256 gasLeftBefore = gasleft();
this.flashLoan(address(this), asset, 10 ** asset.decimals(), "", this.gasCallback);
this.flash(address(this), asset, 10 ** asset.decimals(), "", this.gasCallback);

gasUsed = gasLeftBefore - gasleft();
expectedGas = gasUsed;
Expand Down
2 changes: 1 addition & 1 deletion src/aave/AaveWrapper.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
// Thanks to ultrasecr.eth
pragma solidity ^0.8.0;

Check failure on line 3 in src/aave/AaveWrapper.sol

View workflow job for this annotation

GitHub Actions / lint

Compiler version ^0.8.0 does not satisfy the >=0.8.19 semver requirement

import { IPool } from "./interfaces/IPool.sol";
import { DataTypes } from "./interfaces/DataTypes.sol";
Expand All @@ -8,7 +8,7 @@
import { IPoolAddressesProvider } from "./interfaces/IPoolAddressesProvider.sol";
import { IFlashLoanSimpleReceiver } from "./interfaces/IFlashLoanSimpleReceiver.sol";

import { IERC20 } from "lib/erc3156pp/src/interfaces/IERC20.sol";
import { IERC20 } from "lib/erc7399/src/interfaces/IERC20.sol";
import { FixedPointMathLib } from "lib/solmate/src/utils/FixedPointMathLib.sol";

import { BaseWrapper } from "../BaseWrapper.sol";
Expand All @@ -17,8 +17,8 @@
using FixedPointMathLib for uint256;
using ReserveConfiguration for DataTypes.ReserveConfigurationMap;

IPoolAddressesProvider public immutable ADDRESSES_PROVIDER;

Check warning on line 20 in src/aave/AaveWrapper.sol

View workflow job for this annotation

GitHub Actions / lint

Variable name must be in mixedCase
IPool public POOL;

Check warning on line 21 in src/aave/AaveWrapper.sol

View workflow job for this annotation

GitHub Actions / lint

Variable name must be in mixedCase

constructor(IPoolAddressesProvider provider) {
ADDRESSES_PROVIDER = provider;
Expand Down Expand Up @@ -70,7 +70,7 @@
returns (bool)
{
require(msg.sender == address(POOL), "AaveFlashLoanProvider: not pool");
require(initiator == address(this), "AaveFlashLoanProvider: not initiator");

Check warning on line 73 in src/aave/AaveWrapper.sol

View workflow job for this annotation

GitHub Actions / lint

Error message for require is too long

bridgeToCallback(IERC20(asset), amount, fee, params);

Expand Down
2 changes: 1 addition & 1 deletion src/balancer/BalancerWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { Arrays } from "../utils/Arrays.sol";

import { FixedPointMathLib } from "lib/solmate/src/utils/FixedPointMathLib.sol";
import { IERC20 } from "lib/erc3156pp/src/interfaces/IERC20.sol";
import { IERC20 } from "lib/erc7399/src/interfaces/IERC20.sol";

import { BaseWrapper } from "../BaseWrapper.sol";

Expand Down Expand Up @@ -53,7 +53,7 @@
override
{
require(msg.sender == address(balancer), "BalancerWrapper: not balancer");
require(keccak256(params) == flashLoanDataHash, "BalancerWrapper: params hash mismatch");

Check warning on line 56 in src/balancer/BalancerWrapper.sol

View workflow job for this annotation

GitHub Actions / lint

Error message for require is too long
delete flashLoanDataHash;

bridgeToCallback(IERC20(assets[0]), amounts[0], fees[0], params);
Expand Down
2 changes: 1 addition & 1 deletion src/erc3156/ERC3156Wrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { IERC3156FlashLender } from "lib/erc3156/contracts/interfaces/IERC3156FlashLender.sol";
import { IERC3156FlashBorrower } from "lib/erc3156/contracts/interfaces/IERC3156FlashBorrower.sol";

import { IERC20 } from "lib/erc3156pp/src/interfaces/IERC20.sol";
import { IERC20 } from "lib/erc7399/src/interfaces/IERC20.sol";

import { BaseWrapper } from "../BaseWrapper.sol";

Expand All @@ -15,7 +15,7 @@
contract ERC3156Wrapper is BaseWrapper, IERC3156FlashBorrower {
bytes32 public constant CALLBACK_SUCCESS = keccak256("ERC3156FlashBorrower.onFlashLoan");

mapping(IERC20 => IERC3156FlashLender) public lenders;

Check warning on line 18 in src/erc3156/ERC3156Wrapper.sol

View workflow job for this annotation

GitHub Actions / lint

Main key parameter in mapping lenders is not named

Check warning on line 18 in src/erc3156/ERC3156Wrapper.sol

View workflow job for this annotation

GitHub Actions / lint

Value parameter in mapping lenders is not named

/**
* @param assets_ Asset contracts supported for flash lending.
Expand Down
8 changes: 5 additions & 3 deletions src/uniswapV3/UniswapV3Wrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IUniswapV3FlashCallback } from "./interfaces/callback/IUniswapV3FlashCa
import { IUniswapV3Pool } from "./interfaces/IUniswapV3Pool.sol";
import { PoolAddress } from "./interfaces/PoolAddress.sol";

import { IERC20 } from "lib/erc3156pp/src/interfaces/IERC20.sol";
import { IERC20 } from "lib/erc7399/src/interfaces/IERC20.sol";

import { BaseWrapper } from "../BaseWrapper.sol";

Expand Down Expand Up @@ -112,9 +112,11 @@ contract UniswapV3Wrapper is BaseWrapper, IUniswapV3FlashCallback {
require(msg.sender == address(_activePool), "UniswapV3Wrapper: Only active pool");

uint256 fee = fee0 > 0 ? fee0 : fee1;
IERC20 asset = IERC20(fee0 > 0 ? IUniswapV3Pool(msg.sender).token0() : IUniswapV3Pool(msg.sender).token1()); // TODO: Wouldn't it be cheaper to pass this on `params`?
IERC20 asset = IERC20(fee0 > 0 ? IUniswapV3Pool(msg.sender).token0() : IUniswapV3Pool(msg.sender).token1()); // TODO:
// Wouldn't it be cheaper to pass this on `params`?
(uint256 amount, bytes memory data) = abi.decode(params, (uint256, bytes));
// uint256 amount = asset.balanceOf(address(this)); // TODO: This is not the amount we borrowed, but the amount we have in the contract. We should use the amount in data or calculate a delta
// uint256 amount = asset.balanceOf(address(this)); // TODO: This is not the amount we borrowed, but the amount
// we have in the contract. We should use the amount in data or calculate a delta

bridgeToCallback(asset, amount, fee, data);
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/TransferHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Taken from https://github.com/Uniswap/uniswap-lib/blob/master/src/libraries/TransferHelper.sol
pragma solidity ^0.8.0;

import { IERC20 } from "lib/erc3156pp/src/interfaces/IERC20.sol";
import { IERC20 } from "lib/erc7399/src/interfaces/IERC20.sol";
import "./RevertMsgExtractor.sol";

// helper methods for interacting with ERC20 tokens and sending ETH that do not consistently return true/false
Expand Down
2 changes: 1 addition & 1 deletion test/AaveWrapper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract AaveWrapperTest is PRBTest, StdCheats {

// Test the return values
(bytes32 callbackReturn) = abi.decode(result, (bytes32));
assertEq(uint256(callbackReturn), uint256(borrower.ERC3156PP_CALLBACK_SUCCESS()), "Callback failed");
assertEq(uint256(callbackReturn), uint256(borrower.ERC7399_CALLBACK_SUCCESS()), "Callback failed");

// Test the borrower state
assertEq(borrower.flashInitiator(), address(borrower));
Expand Down
2 changes: 1 addition & 1 deletion test/BalancerWrapper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ contract BalancerWrapperTest is PRBTest, StdCheats {

// Test the return values
(bytes32 callbackReturn) = abi.decode(result, (bytes32));
assertEq(uint256(callbackReturn), uint256(borrower.ERC3156PP_CALLBACK_SUCCESS()), "Callback failed");
assertEq(uint256(callbackReturn), uint256(borrower.ERC7399_CALLBACK_SUCCESS()), "Callback failed");

// Test the borrower state
assertEq(borrower.flashInitiator(), address(borrower));
Expand Down
4 changes: 2 additions & 2 deletions test/BaseWrapper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PRBTest } from "@prb/test/PRBTest.sol";
import { console2 } from "forge-std/console2.sol";
import { StdCheats } from "forge-std/StdCheats.sol";

import { IERC20 } from "lib/erc3156pp/src/interfaces/IERC20.sol";
import { IERC20 } from "lib/erc7399/src/interfaces/IERC20.sol";
import { FlashBorrower } from "./FlashBorrower.sol";

import { BaseWrapper } from "src/BaseWrapper.sol";
Expand Down Expand Up @@ -45,7 +45,7 @@ contract BaseWrapperTest is PRBTest, StdCheats {

// Test the return values
(bytes32 callbackReturn) = abi.decode(result, (bytes32));
assertEq(uint256(callbackReturn), uint256(borrower.ERC3156PP_CALLBACK_SUCCESS()), "Callback failed");
assertEq(uint256(callbackReturn), uint256(borrower.ERC7399_CALLBACK_SUCCESS()), "Callback failed");

// Test the wrapper state (return bytes should be cleaned up)
assertEq(vm.load(address(wrapper), bytes32(uint256(0))), "");
Expand Down
4 changes: 2 additions & 2 deletions test/ERC3156Wrapper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ contract ERC3156WrapperTest is PRBTest, StdCheats {

// Test the return values
(bytes32 callbackReturn) = abi.decode(result, (bytes32));
assertEq(uint256(callbackReturn), uint256(borrower.ERC3156PP_CALLBACK_SUCCESS()), "Callback failed");
assertEq(uint256(callbackReturn), uint256(borrower.ERC7399_CALLBACK_SUCCESS()), "Callback failed");

// Test the borrower state
assertEq(borrower.flashInitiator(), address(borrower));
Expand All @@ -71,7 +71,7 @@ contract ERC3156WrapperTest is PRBTest, StdCheats {
uint256 loan = 1e18;
uint256 fee = wrapper.flashFee(dai, loan);
dai.transfer(address(wrapper), fee);

uint256 expectedGas = wrapper.setExpectedGas(dai);

console2.log(expectedGas, "expectedGas");
Expand Down
24 changes: 12 additions & 12 deletions test/FlashBorrower.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import { IERC3156PPFlashLender } from "lib/erc3156pp/src/interfaces/IERC3156PPFlashLender.sol";
import { IERC20 } from "lib/erc3156pp/src/interfaces/IERC20.sol";
import { IERC7399 } from "lib/erc7399/src/interfaces/IERC7399.sol";
import { IERC20 } from "lib/erc7399/src/interfaces/IERC20.sol";

contract LoanReceiver {
function retrieve(IERC20 asset) external {
Expand All @@ -11,8 +11,8 @@ contract LoanReceiver {
}

contract FlashBorrower {
bytes32 public constant ERC3156PP_CALLBACK_SUCCESS = keccak256("ERC3156PP_CALLBACK_SUCCESS");
IERC3156PPFlashLender lender;
bytes32 public constant ERC7399_CALLBACK_SUCCESS = keccak256("ERC7399_CALLBACK_SUCCESS");
IERC7399 lender;
LoanReceiver loanReceiver;

uint256 public flashBalance;
Expand All @@ -21,7 +21,7 @@ contract FlashBorrower {
uint256 public flashAmount;
uint256 public flashFee;

constructor(IERC3156PPFlashLender lender_) {
constructor(IERC7399 lender_) {
lender = lender_;
loanReceiver = new LoanReceiver();
}
Expand Down Expand Up @@ -49,7 +49,7 @@ contract FlashBorrower {
flashBalance = IERC20(asset).balanceOf(address(this));
asset.transfer(paymentReceiver, amount + fee);

return abi.encode(ERC3156PP_CALLBACK_SUCCESS);
return abi.encode(ERC7399_CALLBACK_SUCCESS);
}

function onSteal(
Expand All @@ -72,7 +72,7 @@ contract FlashBorrower {

// do nothing

return abi.encode(ERC3156PP_CALLBACK_SUCCESS);
return abi.encode(ERC7399_CALLBACK_SUCCESS);
}

function onReenter(
Expand Down Expand Up @@ -100,7 +100,7 @@ contract FlashBorrower {
flashAmount += amount;
flashFee += fee;

return abi.encode(ERC3156PP_CALLBACK_SUCCESS);
return abi.encode(ERC7399_CALLBACK_SUCCESS);
}

function onFlashLoanVoid(
Expand Down Expand Up @@ -129,18 +129,18 @@ contract FlashBorrower {
}

function flashBorrow(IERC20 asset, uint256 amount) public returns (bytes memory) {
return lender.flashLoan(address(loanReceiver), asset, amount, "", this.onFlashLoan);
return lender.flash(address(loanReceiver), asset, amount, "", this.onFlashLoan);
}

function flashBorrowAndSteal(IERC20 asset, uint256 amount) public returns (bytes memory) {
return lender.flashLoan(address(loanReceiver), asset, amount, "", this.onSteal);
return lender.flash(address(loanReceiver), asset, amount, "", this.onSteal);
}

function flashBorrowAndReenter(IERC20 asset, uint256 amount) public returns (bytes memory) {
return lender.flashLoan(address(loanReceiver), asset, amount, "", this.onReenter);
return lender.flash(address(loanReceiver), asset, amount, "", this.onReenter);
}

function flashBorrowVoid(IERC20 asset, uint256 amount) public returns (bytes memory) {
return lender.flashLoan(address(loanReceiver), asset, amount, "", this.onFlashLoanVoid);
return lender.flash(address(loanReceiver), asset, amount, "", this.onFlashLoanVoid);
}
}
2 changes: 1 addition & 1 deletion test/UniswapV3Wrapper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ contract UniswapV3WrapperTest is PRBTest, StdCheats {

// Test the return values
(bytes32 callbackReturn) = abi.decode(result, (bytes32));
assertEq(uint256(callbackReturn), uint256(borrower.ERC3156PP_CALLBACK_SUCCESS()), "Callback failed");
assertEq(uint256(callbackReturn), uint256(borrower.ERC7399_CALLBACK_SUCCESS()), "Callback failed");

// Test the borrower state
assertEq(borrower.flashInitiator(), address(borrower), "flashInitiator");
Expand Down
Loading