Skip to content

Commit

Permalink
DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
duncancmt committed Oct 3, 2023
1 parent c38c1f7 commit 51f32e7
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/core/Basic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,24 @@ import {SafeTransferLib} from "../utils/SafeTransferLib.sol";
import {FullMath} from "../utils/FullMath.sol";
import {Panic} from "../utils/Panic.sol";

library Revert {
function _revert(bytes memory reason) internal pure {
assembly ("memory-safe") {
revert(add(reason, 0x20), mload(reason))
}
}

function maybeRevert(bool success, bytes memory reason) internal pure {
if (!success) {
_revert(reason);
}
}
}

abstract contract Basic {
using SafeTransferLib for ERC20;
using FullMath for uint256;
using Revert for bool;

address internal constant ETH_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;

Expand All @@ -35,11 +50,7 @@ abstract contract Basic {
if (data.length == 0) {
require(offset == 0);
(bool success, bytes memory returnData) = payable(pool).call{value: value}("");
if (!success) {
assembly ("memory-safe") {
revert(add(0x20, returnData), mload(returnData))
}
}
success.maybeRevert(returnData);
return;
} else {
if ((offset += 32) > data.length) {
Expand All @@ -66,11 +77,7 @@ abstract contract Basic {
}
}
(bool success, bytes memory returnData) = payable(pool).call{value: value}(data);
if (!success) {
assembly ("memory-safe") {
revert(add(0x20, returnData), mload(returnData))
}
}
success.maybeRevert(returnData);
require(returnData.length > 0 || pool.code.length > 0); // forbid sending data to EOAs
}
}

0 comments on commit 51f32e7

Please sign in to comment.