|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.11; |
| 3 | + |
| 4 | +// From https://github.com/ethereumjs/testrpc/issues/58 |
| 5 | +contract EstimateGas { |
| 6 | + event Add(bytes32 name, bytes32 description, uint value, address owner); |
| 7 | + |
| 8 | + struct Test { |
| 9 | + bytes32 name; |
| 10 | + bytes32 description; |
| 11 | + uint[] balances; |
| 12 | + mapping(address => uint) owners; |
| 13 | + } |
| 14 | + |
| 15 | + uint256 public x; |
| 16 | + uint256 public y; |
| 17 | + function reset() public { |
| 18 | + x = 0; |
| 19 | + y = 1; |
| 20 | + } |
| 21 | + function initialSettingOfX() public { |
| 22 | + x = 1; |
| 23 | + } |
| 24 | + function triggerRsclearRefund() public { |
| 25 | + x = gasleft(); |
| 26 | + reset(); |
| 27 | + } |
| 28 | + function triggerRsclearRefundForX() public { |
| 29 | + reset(); |
| 30 | + x = gasleft(); |
| 31 | + } |
| 32 | + function triggerRsclearRefundForY() public { |
| 33 | + y = gasleft(); |
| 34 | + reset(); |
| 35 | + } |
| 36 | + function triggerRselfdestructRefund() public { |
| 37 | + selfdestruct(payable(msg.sender)); |
| 38 | + } |
| 39 | + function triggerAllRefunds() public { |
| 40 | + triggerRsclearRefund(); |
| 41 | + triggerRselfdestructRefund(); |
| 42 | + } |
| 43 | + |
| 44 | + // https://github.com/trufflesuite/ganache-cli/issues/294 |
| 45 | + mapping (uint => uint) public uints; |
| 46 | + // Sets the uints[1] slot to a value; |
| 47 | + function store(uint _uint) public { uints[1] = _uint;} |
| 48 | + function clear() public { delete uints[1]; } |
| 49 | + |
| 50 | + mapping(bytes32 => uint) index; |
| 51 | + Test[] tests; |
| 52 | + |
| 53 | + constructor() { |
| 54 | + tests.push(); |
| 55 | + } |
| 56 | + |
| 57 | + function add(bytes32 _name, bytes32 _description, uint _value) public returns(bool) { |
| 58 | + if (index[_name] != 0) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + uint pos = tests.length; |
| 62 | + tests.push(); |
| 63 | + tests[pos].name = _name; |
| 64 | + tests[pos].description = _description; |
| 65 | + tests[pos].balances.push(); |
| 66 | + tests[pos].balances.push(_value); |
| 67 | + tests[pos].owners[msg.sender] = 1; |
| 68 | + index[_name] = pos; |
| 69 | + emit Add(_name, _description, _value, msg.sender); |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + function transfer(address _to, uint _value, bytes32 _name) public returns(bool) { |
| 74 | + uint pos = index[_name]; |
| 75 | + if (pos == 0) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + uint posFrom = tests[pos].owners[msg.sender]; |
| 80 | + if (posFrom == 0) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + if (tests[pos].balances[posFrom] < _value) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + uint posTo = tests[pos].owners[_to]; |
| 89 | + if (posTo == 0) { |
| 90 | + uint posBal = tests[pos].balances.length; |
| 91 | + tests[pos].balances.push(); |
| 92 | + tests[pos].owners[_to] = posBal; |
| 93 | + posTo = posBal; |
| 94 | + } |
| 95 | + |
| 96 | + if (tests[pos].balances[posTo] + _value < tests[pos].balances[posTo]) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + tests[pos].balances[posFrom] -= _value; |
| 100 | + tests[pos].balances[posTo] += _value; |
| 101 | + |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
| 105 | + function currentBlock() public view returns (uint) { |
| 106 | + return block.number; |
| 107 | + } |
| 108 | + |
| 109 | + uint public counter; |
| 110 | + function runsOutOfGas() public { |
| 111 | + consumesGas(); |
| 112 | + } |
| 113 | + function consumesGas() public { |
| 114 | + for(uint i = 0; i < 100000; i++){ |
| 115 | + counter = i; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments