-
Notifications
You must be signed in to change notification settings - Fork 516
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
Checkout plugin and proxy #598
Open
kumaryash90
wants to merge
10
commits into
main
Choose a base branch
from
yash/plugin-checkout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ccc4f30
Checkout plugin and proxy
kumaryash90 5aa972a
tests
kumaryash90 1c4cfab
more tests
kumaryash90 ef7f6cd
update packages, fix slither
kumaryash90 6933b23
correct methods
kumaryash90 955c131
test swapAndExecute
kumaryash90 c24e576
magic address permissions
kumaryash90 53ff9ae
reorder
kumaryash90 7a0c3b6
recompile
kumaryash90 786154c
bubble up revert message
kumaryash90 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.11; | ||
|
||
// $$\ $$\ $$\ $$\ $$\ | ||
// $$ | $$ | \__| $$ | $$ | | ||
// $$$$$$\ $$$$$$$\ $$\ $$$$$$\ $$$$$$$ |$$\ $$\ $$\ $$$$$$\ $$$$$$$\ | ||
// \_$$ _| $$ __$$\ $$ |$$ __$$\ $$ __$$ |$$ | $$ | $$ |$$ __$$\ $$ __$$\ | ||
// $$ | $$ | $$ |$$ |$$ | \__|$$ / $$ |$$ | $$ | $$ |$$$$$$$$ |$$ | $$ | | ||
// $$ |$$\ $$ | $$ |$$ |$$ | $$ | $$ |$$ | $$ | $$ |$$ ____|$$ | $$ | | ||
// \$$$$ |$$ | $$ |$$ |$$ | \$$$$$$$ |\$$$$$\$$$$ |\$$$$$$$\ $$$$$$$ | | ||
// \____/ \__| \__|\__|\__| \_______| \_____\____/ \_______|\_______/ | ||
|
||
import { IPRBProxyPlugin } from "@prb/proxy/src/interfaces/IPRBProxyPlugin.sol"; | ||
|
||
import "./TargetCheckout.sol"; | ||
|
||
contract PluginCheckout is IPRBProxyPlugin, TargetCheckout { | ||
function getMethods() external pure override returns (bytes4[] memory) { | ||
bytes4[] memory methods = new bytes4[](4); | ||
methods[0] = this.withdraw.selector; | ||
methods[1] = this.execute.selector; | ||
methods[2] = this.swapAndExecute.selector; | ||
methods[3] = this.approveSwapRouter.selector; | ||
return methods; | ||
} | ||
} |
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,82 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.11; | ||
|
||
import "../../../lib/CurrencyTransferLib.sol"; | ||
import "../../../eip/interface/IERC20.sol"; | ||
|
||
import { IPRBProxy } from "@prb/proxy/src/interfaces/IPRBProxy.sol"; | ||
import "./interface/IPluginCheckout.sol"; | ||
|
||
// $$\ $$\ $$\ $$\ $$\ | ||
// $$ | $$ | \__| $$ | $$ | | ||
// $$$$$$\ $$$$$$$\ $$\ $$$$$$\ $$$$$$$ |$$\ $$\ $$\ $$$$$$\ $$$$$$$\ | ||
// \_$$ _| $$ __$$\ $$ |$$ __$$\ $$ __$$ |$$ | $$ | $$ |$$ __$$\ $$ __$$\ | ||
// $$ | $$ | $$ |$$ |$$ | \__|$$ / $$ |$$ | $$ | $$ |$$$$$$$$ |$$ | $$ | | ||
// $$ |$$\ $$ | $$ |$$ |$$ | $$ | $$ |$$ | $$ | $$ |$$ ____|$$ | $$ | | ||
// \$$$$ |$$ | $$ |$$ |$$ | \$$$$$$$ |\$$$$$\$$$$ |\$$$$$$$\ $$$$$$$ | | ||
// \____/ \__| \__|\__|\__| \_______| \_____\____/ \_______|\_______/ | ||
|
||
contract TargetCheckout is IPluginCheckout { | ||
mapping(address => bool) public isApprovedRouter; | ||
|
||
function withdraw(address _token, uint256 _amount) external { | ||
require(msg.sender == IPRBProxy(address(this)).owner(), "Not authorized"); | ||
|
||
CurrencyTransferLib.transferCurrency(_token, address(this), msg.sender, _amount); | ||
} | ||
|
||
function approveSwapRouter(address _swapRouter, bool _toApprove) external { | ||
require(msg.sender == IPRBProxy(address(this)).owner(), "Not authorized"); | ||
require(_swapRouter != address(0), "Zero address"); | ||
|
||
isApprovedRouter[_swapRouter] = _toApprove; | ||
} | ||
|
||
function execute(UserOp memory op) external { | ||
require(_canExecute(op, msg.sender), "Not authorized"); | ||
|
||
_execute(op); | ||
} | ||
|
||
function swapAndExecute(UserOp memory op, UserOp memory swapOp) external { | ||
require(isApprovedRouter[swapOp.target], "Invalid router address"); | ||
require(_canExecute(op, msg.sender), "Not authorized"); | ||
|
||
_execute(swapOp); | ||
_execute(op); | ||
} | ||
|
||
// ================================================= | ||
// =============== Internal functions ============== | ||
// ================================================= | ||
|
||
function _execute(UserOp memory op) internal { | ||
bool success; | ||
if (op.currency == CurrencyTransferLib.NATIVE_TOKEN) { | ||
(success, ) = op.target.call{ value: op.valueToSend }(op.data); | ||
} else { | ||
if (op.valueToSend != 0 && op.approvalRequired) { | ||
IERC20(op.currency).approve(op.target, op.valueToSend); | ||
} | ||
|
||
(success, ) = op.target.call(op.data); | ||
} | ||
|
||
require(success, "Execution failed"); | ||
} | ||
Check warning Code scanning / Slither Unused return Medium
TargetCheckout._execute(IPluginCheckout.UserOp) ignores return value by IERC20(op.currency).approve(op.target,op.valueToSend)
Check warning Code scanning / Slither Low-level calls Warning |
||
|
||
function _canExecute(UserOp memory op, address caller) internal view returns (bool) { | ||
address owner = IPRBProxy(address(this)).owner(); | ||
if (owner != caller) { | ||
bool permission = IPRBProxy(address(this)).registry().getPermissionByOwner({ | ||
owner: owner, | ||
envoy: caller, | ||
target: op.target | ||
}); | ||
|
||
return permission; | ||
} | ||
|
||
return true; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
contracts/prebuilts/unaudited/checkout/interface/IPluginCheckout.sol
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,29 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.11; | ||
|
||
interface IPluginCheckout { | ||
/** | ||
* @notice Details of the transaction to execute on target contract. | ||
* | ||
* @param target Address to send the transaction to | ||
* | ||
* @param currency Represents both native token and erc20 token | ||
* | ||
* @param approvalRequired If need to approve erc20 to the target contract | ||
* | ||
* @param valueToSend Transaction value to send - both native and erc20 | ||
* | ||
* @param data Transaction calldata | ||
*/ | ||
struct UserOp { | ||
address target; | ||
address currency; | ||
bool approvalRequired; | ||
uint256 valueToSend; | ||
bytes data; | ||
} | ||
|
||
function execute(UserOp calldata op) external; | ||
|
||
function swapAndExecute(UserOp calldata op, UserOp memory swapOp) external; | ||
} |
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.
Check failure
Code scanning / Slither
Functions that send Ether to arbitrary destinations High