-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docking to l2output oracle and dgf contracts
- Loading branch information
1 parent
d69aa0b
commit c0cc7d1
Showing
8 changed files
with
155 additions
and
13 deletions.
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
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
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,106 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
|
||
library SafeCall { | ||
function send( | ||
address _target, | ||
uint256 _gas, | ||
uint256 _value | ||
) internal returns (bool) { | ||
bool _success; | ||
assembly { | ||
_success := call( | ||
_gas, // gas | ||
_target, // recipient | ||
_value, // ether value | ||
0, // inloc | ||
0, // inlen | ||
0, // outloc | ||
0 // outlen | ||
) | ||
} | ||
return _success; | ||
} | ||
|
||
function call( | ||
address _target, | ||
uint256 _gas, | ||
uint256 _value, | ||
bytes memory _calldata | ||
) internal returns (bool) { | ||
bool _success; | ||
assembly { | ||
_success := call( | ||
_gas, // gas | ||
_target, // recipient | ||
_value, // ether value | ||
add(_calldata, 32), // inloc | ||
mload(_calldata), // inlen | ||
0, // outloc | ||
0 // outlen | ||
) | ||
} | ||
return _success; | ||
} | ||
|
||
function hasMinGas(uint256 _minGas, uint256 _reservedGas) internal view returns (bool) { | ||
bool _hasMinGas; | ||
assembly { | ||
// Equation: gas × 63 ≥ minGas × 64 + 63(40_000 + reservedGas) | ||
_hasMinGas := iszero( | ||
lt(mul(gas(), 63), add(mul(_minGas, 64), mul(add(40000, _reservedGas), 63))) | ||
) | ||
} | ||
return _hasMinGas; | ||
} | ||
|
||
function callWithMinGas( | ||
address _target, | ||
uint256 _minGas, | ||
uint256 _value, | ||
bytes memory _calldata | ||
) internal returns (bool) { | ||
bool _success; | ||
bool _hasMinGas = hasMinGas(_minGas, 0); | ||
assembly { | ||
// Assertion: gasleft() >= (_minGas * 64) / 63 + 40_000 | ||
if iszero(_hasMinGas) { | ||
// Store the "Error(string)" selector in scratch space. | ||
mstore(0, 0x08c379a0) | ||
// Store the pointer to the string length in scratch space. | ||
mstore(32, 32) | ||
// Store the string. | ||
// | ||
// SAFETY: | ||
// - We pad the beginning of the string with two zero bytes as well as the | ||
// length (24) to ensure that we override the free memory pointer at offset | ||
// 0x40. This is necessary because the free memory pointer is likely to | ||
// be greater than 1 byte when this function is called, but it is incredibly | ||
// unlikely that it will be greater than 3 bytes. As for the data within | ||
// 0x60, it is ensured that it is 0 due to 0x60 being the zero offset. | ||
// - It's fine to clobber the free memory pointer, we're reverting. | ||
mstore(88, 0x0000185361666543616c6c3a204e6f7420656e6f75676820676173) | ||
|
||
// Revert with 'Error("SafeCall: Not enough gas")' | ||
revert(28, 100) | ||
} | ||
|
||
// The call will be supplied at least ((_minGas * 64) / 63) gas due to the | ||
// above assertion. This ensures that, in all circumstances (except for when the | ||
// `_minGas` does not account for the `memory_expansion_cost` and `code_execution_cost` | ||
// factors of the dynamic cost of the `CALL` opcode), the call will receive at least | ||
// the minimum amount of gas specified. | ||
_success := call( | ||
gas(), // gas | ||
_target, // recipient | ||
_value, // ether value | ||
add(_calldata, 32), // inloc | ||
mload(_calldata), // inlen | ||
0x00, // outloc | ||
0x00 // outlen | ||
) | ||
} | ||
return _success; | ||
} | ||
} |