-
Notifications
You must be signed in to change notification settings - Fork 11.7k
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
Low level call and memory management #5091
Draft
Amxx
wants to merge
5
commits into
OpenZeppelin:master
Choose a base branch
from
Amxx:feature/low-level-call
base: master
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 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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,72 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import {Math} from "./math/Math.sol"; | ||
|
||
/** | ||
* Utility functions helpful when making different kinds of contract calls in Solidity. | ||
*/ | ||
library LowLevelCalls { | ||
function call(address to, uint256 value, bytes memory data) internal returns (bool success) { | ||
return call(to, value, data, gasleft()); | ||
} | ||
|
||
function call(address to, uint256 value, bytes memory data, uint256 txGas) internal returns (bool success) { | ||
assembly ("memory-safe") { | ||
success := call(txGas, to, value, add(data, 0x20), mload(data), 0, 0) | ||
} | ||
} | ||
|
||
function staticcall(address to, bytes memory data) internal view returns (bool success) { | ||
return staticcall(to, data, gasleft()); | ||
} | ||
|
||
function staticcall(address to, bytes memory data, uint256 txGas) internal view returns (bool success) { | ||
assembly ("memory-safe") { | ||
success := staticcall(txGas, to, add(data, 0x20), mload(data), 0, 0) | ||
} | ||
} | ||
|
||
function delegateCall(address to, bytes memory data) internal returns (bool success) { | ||
return delegateCall(to, data, gasleft()); | ||
} | ||
|
||
function delegateCall(address to, bytes memory data, uint256 txGas) internal returns (bool success) { | ||
assembly ("memory-safe") { | ||
success := delegatecall(txGas, to, add(data, 0x20), mload(data), 0, 0) | ||
} | ||
} | ||
|
||
function getReturnDataSize() internal pure returns (uint256 returnDataSize) { | ||
assembly ("memory-safe") { | ||
returnDataSize := returndatasize() | ||
} | ||
} | ||
|
||
function getReturnData(uint256 maxLen) internal pure returns (bytes memory ptr) { | ||
return getReturnDataFixed(Math.min(maxLen, getReturnDataSize())); | ||
} | ||
|
||
function getReturnDataFixed(uint256 len) internal pure returns (bytes memory ptr) { | ||
assembly ("memory-safe") { | ||
ptr := mload(0x40) | ||
mstore(0x40, add(ptr, add(len, 0x20))) | ||
mstore(ptr, len) | ||
returndatacopy(add(ptr, 0x20), 0, len) | ||
} | ||
} | ||
|
||
function revertWithData(bytes memory returnData) internal pure { | ||
assembly ("memory-safe") { | ||
revert(add(returnData, 0x20), mload(returnData)) | ||
} | ||
} | ||
|
||
function revertWithCode(bytes32 code) internal pure { | ||
assembly ("memory-safe") { | ||
mstore(0, code) | ||
revert(0, 0x20) | ||
} | ||
} | ||
} |
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,22 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
/** | ||
* @dev Helper library packing and unpacking multiple values into bytes32 | ||
*/ | ||
library LowLevelMemory { | ||
type FreePtr is bytes32; | ||
|
||
function save() internal pure returns (FreePtr ptr) { | ||
assembly ("memory-safe") { | ||
ptr := mload(0x40) | ||
} | ||
} | ||
|
||
function load(FreePtr ptr) internal pure { | ||
assembly ("memory-safe") { | ||
mstore(0x40, ptr) | ||
} | ||
} | ||
} |
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.
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.
@ernestognw Can you remember why a try catch was not good enough here ? Is it related to truncating the result (and ignoring high bits from the returned value?)
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.
Isn't it because the return value may revert at decoding? A
try IERC20Metadata(address(asset_)).decimals() returns (uint8 value)
would've failed if the token returned anuint256
. Nothing in the ERC20 restricts the return value to uint8