github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/tests/contracts/evm_tests_sip004.sol (about)

     1  pragma solidity >=0.7.0 <0.9.0;
     2  
     3  // testnet : 0xa44B4ff09825f8c21fc4ad7FAA125a0d6238d0fd
     4  contract evm_tests {
     5  
     6      function test_selfbalance()public view returns (uint256) {
     7          return getThis().balance;
     8      }
     9  
    10      function test_shl() public view returns (bool) {
    11          int256 i = 100;
    12          //i = i << 2;
    13          assembly{
    14              i := shl( 2, i )
    15          }
    16          return i == 400;
    17      }
    18  
    19      function test_shr() public view returns (bool) {
    20          int256 i = 100;
    21          //i = i >> 2;
    22          assembly{
    23              i := shr( 2, i )
    24          }
    25          return i == 25;
    26      }
    27  
    28      function test_sar() public view returns (bool) {
    29          int256 i = -100;
    30          assembly{
    31              i := sar( 2, i )
    32          }
    33          return i == -25;
    34      }
    35  
    36      function test_extcodehash() public view returns (bool) {
    37          address a = getThis();
    38          bytes32 codeHash;
    39          assembly {
    40              codeHash := extcodehash(a)
    41          }
    42          return codeHash != 0x0;
    43      }
    44  
    45      function test_chainid() external view returns (uint256) {
    46          uint256 id;
    47          assembly {
    48              id := chainid()
    49          }
    50          return id;
    51      }
    52  
    53      function test_create2() public payable {
    54          address _owner = getThis();
    55          uint _salt = getNumber();
    56          bytes memory bytecode = abi.encodePacked(type(TestContract).creationCode, abi.encode(_owner));
    57          address addr;
    58          assembly {
    59              addr := create2(
    60                  callvalue(),
    61                  add(bytecode, 0x20),
    62                  mload(bytecode),
    63                  _salt)
    64              if iszero(extcodesize(addr)) {
    65                  revert(0, 0)
    66              }
    67          }
    68          emit Create2_ret( addr, _owner, _salt, addr == getAddress(_owner,_salt));
    69      }
    70  
    71  
    72      constructor() payable {}
    73  
    74      event Create2_ret(address addr, address owner, uint salt, bool ret);
    75  
    76      function getThis() public view returns (address){
    77          return address(this);
    78      }
    79  
    80      function getNumber() public view returns (uint256){
    81          return block.number;
    82      }
    83  
    84      function getAddress(address _owner ,uint _salt) public view returns (address) {
    85          bytes memory bytecode = abi.encodePacked(type(TestContract).creationCode, abi.encode(_owner));
    86          bytes32 hash = keccak256(
    87              abi.encodePacked(bytes1(0xff), address(this), _salt, keccak256(bytecode))
    88          );
    89          return address(uint160(uint(hash)));
    90      }
    91  }
    92  
    93  contract TestContract {
    94      address public owner;
    95  
    96      constructor(address _owner) payable {
    97          owner = _owner;
    98      }
    99  
   100      function getBalance() public view returns (uint) {
   101          return address(this).balance;
   102      }
   103  
   104  }