Skip to content

Instantly share code, notes, and snippets.

@ecmendenhall
Created May 4, 2022 05:37
Show Gist options
  • Save ecmendenhall/45104a6ead7d9bdf6dc1306e7d590578 to your computer and use it in GitHub Desktop.
Save ecmendenhall/45104a6ead7d9bdf6dc1306e7d590578 to your computer and use it in GitHub Desktop.
Alignments
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.10;
import "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
import "openzeppelin-contracts/contracts/utils/Address.sol";
interface IWETH {
function deposit() external payable;
function withdraw(uint wad) external;
function balanceOf(address guy) external returns (uint);
}
contract SelfDestruct {
constructor(address account) payable {
selfdestruct(payable(account));
}
}
contract Alignments {
using SafeERC20 for IERC20;
address internal constant account = address(42);
uint256 internal constant amount = 100;
address internal constant weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
IWETH public constant WETH = IWETH(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
function lawfulGood() public {
payable(account).transfer(amount);
}
function neutralGood() public {
Address.sendValue(
payable(account),
amount
);
}
function chaoticGood() public {
IWETH(weth).deposit{ value: amount }();
IERC20(weth).safeTransfer(account, amount);
}
function lawfulNeutral() public {
require(
payable(account).send(amount),
"Transfer failed"
);
}
function trueNeutral() public {
(bool success, ) = account.call{ value: amount }("");
require(success, "Transfer failed");
}
function chaoticNeutral() public {
new SelfDestruct{ value: amount }(account);
}
function lawfulEvil() public {
payable(account).send(amount);
}
function neutralEvil() public {
address _account = account;
assembly {
let x := mload(0x40)
let ret := call(gas(), _account, amount, 0, 0, x, 0)
mstore(0x40, add(x, 0x20))
}
}
function chaoticEvil() public {
payable(address(0)).transfer(address(this).balance - amount);
selfdestruct(payable(account));
}
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.10;
import "forge-std/Test.sol";
import "../Alignments.sol";
contract AlignmentsTest is Test {
Alignments internal a;
address internal account = address(42);
uint256 internal balanceBefore = account.balance;
function setUp() public {
a = new Alignments();
deal(address(this), 1 ether);
}
function testLawfulGood() public {
a.lawfulGood();
assertEq(account.balance - balanceBefore, 100);
}
function testNeutralGood() public {
a.neutralGood();
assertEq(account.balance - balanceBefore, 100);
}
function testChaoticGood() public {
a.chaoticGood();
assertEq(a.WETH().balanceOf(account), 100);
}
function testLawfulNeutral() public {
a.lawfulNeutral();
assertEq(account.balance - balanceBefore, 100);
}
function testTrueNeutral() public {
a.trueNeutral();
assertEq(account.balance - balanceBefore, 100);
}
function testChaoticNeutral() public {
a.chaoticNeutral();
assertEq(account.balance - balanceBefore, 100);
}
function testLawfulEvil() public {
a.lawfulEvil();
assertEq(account.balance - balanceBefore, 100);
}
function testNeutralEvil() public {
a.neutralEvil();
assertEq(account.balance - balanceBefore, 100);
}
function testChaoticEvil() public {
a.chaoticEvil();
assertEq(account.balance - balanceBefore, 100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment