-
Notifications
You must be signed in to change notification settings - Fork 7
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
feat: (WIP) Simplified PropellerRouter Sketch #127
Draft
tamaralipows
wants to merge
6
commits into
main
Choose a base branch
from
tnl/propeller-router-sketch
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.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dc163d0
feat: (WIP) Simplified PropellerRouter Sketch
eb767e0
docs: Remove unnecessary/outdated comments/docstrings
5b7fb0e
feat: remove batchExecute, put back _singleSwap
4e75f98
refactor: no more mention of "checked" - check by default
1091591
refactor: collapse internal and external router contract
db80fd6
fix: put back buildRuntime.sh
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
|
||
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
import "@src/libraries/EfficientERC20.sol"; | ||
import "@interfaces/batch-swap-router/IBatchSwapRouterV1Structs.sol"; | ||
|
||
/** | ||
* @title ApprovalManagement for contracts | ||
* @author PropellerHeads Devs | ||
* @dev Allows a contract to tokens for trading on third party contracts. | ||
*/ | ||
contract ApprovalManagement is IBatchSwapRouterV1Structs { | ||
using EfficientERC20 for IERC20; | ||
|
||
/** | ||
* @dev Set allowance to several addresses per token for transfer on behalf of this contract. | ||
*/ | ||
function _setApprovals(ExternalApproval[] calldata approvals) internal { | ||
for (uint256 i = 0; i < approvals.length; i++) { | ||
IERC20 token = approvals[i].token; | ||
uint256 allowance = approvals[i].allowance; | ||
for (uint256 j = 0; j < approvals[i].addresses.length; j++) { | ||
address beneficiary = approvals[i].addresses[j]; | ||
token.safeApprove(beneficiary, allowance); | ||
} | ||
} | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
evm/src/propeller-router/CallbackVerificationDispatcher.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,94 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
|
||
import "@interfaces/batch-swap-router/IBatchSwapRouterV1Structs.sol"; | ||
import "@interfaces/ICallbackVerifier.sol"; | ||
|
||
/** | ||
* @title Dispatch callback verification to external contracts | ||
* @author PropellerHeads Devs | ||
* @dev Provides the ability to delegate callback verification to external | ||
* contracts. This allows dynamically adding new supported protocols | ||
* without needing to upgrade any contracts. External contracts will | ||
* be called using delegatecall so they can share state with the main | ||
* contract if needed. | ||
* | ||
* Note Verifier contracts need to implement the ICallbackVerifier interface | ||
*/ | ||
contract CallbackVerificationDispatcher is PropellerRouterStructs { | ||
mapping(bytes4 => address) public callbackVerifiers; | ||
|
||
/** | ||
* @dev Registers a new callback verifier contract. | ||
*/ | ||
function _setCallbackVerifier(bytes4 selector, address target) internal { | ||
callbackVerifiers[selector] = target; | ||
} | ||
|
||
/** | ||
* @dev Set multiple callback verifier with a single call. | ||
*/ | ||
function _setCallbackVerifierBatch(CallbackVerifierEntry[] calldata batch) | ||
internal | ||
{ | ||
for (uint8 i = 0; i < (batch.length); i++) { | ||
callbackVerifiers[batch[i].selector] = batch[i].verifier; | ||
} | ||
} | ||
|
||
/** | ||
* @dev Calls a callback verifier. This should revert if the callback verification fails. | ||
* TODO check if we even still need the GenericCallbackHeader if we are hard-coding | ||
* the callbacks for USV3 (which we can do since we don't care about gas and are | ||
* prioritizing simplicity) | ||
* This function returns the offset of the GenericCallbackHeader in data. | ||
* This offset depends on the protocol and is required to parse the callback data in the fallback function. | ||
*/ | ||
function _callVerifyCallback(bytes calldata data) | ||
internal | ||
returns ( | ||
uint256 amountOwed, | ||
uint256 amountReceived, | ||
address tokenOwed, | ||
uint16 dataOffset | ||
) | ||
{ | ||
bytes4 verifySelector = ICallbackVerifier.verifyCallback.selector; | ||
address sender = msg.sender; | ||
address verifier = callbackVerifiers[bytes4(data[:4])]; | ||
if (verifier != address(0)) { | ||
assembly { | ||
let ptr := mload(0x40) | ||
let inpSize := add(100, data.length) | ||
|
||
// function selector | ||
mstore(ptr, verifySelector) | ||
// sender | ||
mstore(add(ptr, 4), sender) | ||
// offset for dynamic data | ||
mstore(add(ptr, 36), 64) | ||
// dynamic part | ||
// length of byte array | ||
mstore(add(ptr, 68), data.length) | ||
// byte array contents | ||
calldatacopy(add(ptr, 100), add(data.offset, 4), data.length) | ||
|
||
//if delegate call successed | ||
if iszero(delegatecall(gas(), verifier, ptr, inpSize, ptr, 128)) | ||
{ | ||
// forward revert reason | ||
let retSize := returndatasize() | ||
returndatacopy(ptr, 0, retSize) | ||
revert(ptr, retSize) | ||
} | ||
// load returned data in res | ||
amountOwed := mload(ptr) | ||
amountReceived := mload(add(ptr, 32)) | ||
tokenOwed := mload(add(ptr, 64)) | ||
dataOffset := and(mload(add(ptr, 96)), 0xFFFF) | ||
} | ||
} else { | ||
revert UnknownSelector(bytes4(data[:4])); | ||
} | ||
} | ||
} |
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.
let's leave this for now until we get feedback from the users. then we can check how to use permit2