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

Add utility for low level call with return bomb protection #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
72 changes: 72 additions & 0 deletions contracts/utils/Call.sol
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 "@openzeppelin/contracts/utils/math/Math.sol";

/**
* Utility functions helpful when making different kinds of contract calls in Solidity.
*/
library Call {
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)
}
}
}