generated from PaulRBerg/foundry-template
-
Notifications
You must be signed in to change notification settings - Fork 3
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
WIP: Code tweaks #5
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
93349af
Use a struct fro the meta params in the aave wrapper
ultrasecreth 616cd9b
Use a struct fro the meta params in the balancer wrapper
ultrasecreth 03b6099
Use a struct fro the meta params in the uniswap wrapper
ultrasecreth 58ab224
Move test code to the test folder
ultrasecreth 99c39d2
Kill dydx wrapper
ultrasecreth 4c3850c
Kill template code
ultrasecreth a2fa691
Use a struct fro the meta params in the erc3156 wrapper
ultrasecreth da4e4d7
Remove FunctionCodec
ultrasecreth d038e7e
Extract common behaviour to parent contract
ultrasecreth 4d7a0a1
Tune up Uniswap wrapper
ultrasecreth 8cc5bda
Test BaseWrapper
ultrasecreth 4643d17
fmt
ultrasecreth 7ecb3ea
Address PR comments
ultrasecreth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity >=0.8.19 <=0.9.0; | ||
|
||
import { Foo } from "../src/Foo.sol"; | ||
|
||
import { BaseScript } from "./Base.s.sol"; | ||
|
||
/// @dev See the Solidity Scripting tutorial: https://book.getfoundry.sh/tutorials/solidity-scripting | ||
contract Deploy is BaseScript { | ||
function run() public broadcast returns (Foo foo) { | ||
foo = new Foo(); | ||
} | ||
function run() public broadcast { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// SPDX-License-Identifier: MIT | ||
// 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 { TransferHelper } from "./utils/TransferHelper.sol"; | ||
|
||
abstract contract BaseWrapper is IERC3156PPFlashLender { | ||
using TransferHelper for IERC20; | ||
|
||
struct Data { | ||
address loanReceiver; | ||
address initiator; | ||
function(address, address, IERC20, uint256, uint256, bytes memory) external returns (bytes memory) callback; | ||
bytes initiatorData; | ||
} | ||
|
||
bytes internal _callbackResult; | ||
|
||
/// @inheritdoc IERC3156PPFlashLender | ||
function flashLoan( | ||
address loanReceiver, | ||
IERC20 asset, | ||
uint256 amount, | ||
bytes calldata initiatorData, | ||
function(address, address, IERC20, uint256, uint256, bytes memory) external returns (bytes memory) callback | ||
) | ||
external | ||
returns (bytes memory result) | ||
{ | ||
Data memory data = Data({ | ||
loanReceiver: loanReceiver, | ||
initiator: msg.sender, | ||
callback: callback, | ||
initiatorData: initiatorData | ||
}); | ||
|
||
_flashLoan(asset, amount, abi.encode(data)); | ||
|
||
result = _callbackResult; | ||
// Avoid storage write if not needed | ||
if (result.length > 0) { | ||
delete _callbackResult; | ||
} | ||
return result; | ||
} | ||
|
||
/// @dev Call the flashloan function in the child contract | ||
function _flashLoan(IERC20 asset, uint256 amount, bytes memory params) internal virtual; | ||
|
||
/// @dev Handle the common parts of bridging the callback | ||
function bridgeToCallback(IERC20 asset, uint256 amount, uint256 fee, bytes memory params) internal { | ||
Data memory data = abi.decode(params, (Data)); | ||
_transferAssets(asset, amount, data.loanReceiver); | ||
|
||
// call the callback and tell the callback receiver to repay the loan to this contract | ||
bytes memory result = data.callback(data.initiator, _repayTo(), IERC20(asset), amount, fee, data.initiatorData); | ||
|
||
_approveRepayment(asset, amount, fee); | ||
|
||
if (result.length > 0) { | ||
// if there's any result, it is kept in a storage variable to be retrieved later in this tx | ||
_callbackResult = result; | ||
} | ||
} | ||
|
||
/// @dev Transfer the assets to the loan receiver. | ||
/// Override it if the provider can send the funds directly | ||
function _transferAssets(IERC20 asset, uint256 amount, address loanReceiver) internal virtual { | ||
asset.safeTransfer(loanReceiver, amount); | ||
} | ||
|
||
/// @dev Approve the repayment of the loan to the provider if needed. | ||
/// Override it if the provider can receive the funds directly and you want to avoid the if condition | ||
function _approveRepayment(IERC20 asset, uint256 amount, uint256 fee) internal virtual { | ||
if (_repayTo() == address(this)) { | ||
asset.approve(msg.sender, amount + fee); | ||
} | ||
} | ||
|
||
/// @dev Where should the end client send the funds to repay the loan | ||
/// Override it if the provider can receive the funds directly | ||
function _repayTo() internal view virtual returns (address) { | ||
return address(this); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should @inheritdoc from the ERC7399 interface when possible, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does that interface lives? I can't see it in the deps