This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 679
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cbc697c
commit aaba45b
Showing
3 changed files
with
1,279 additions
and
0 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
src/chains/ethereum/ethereum/tests/api/eth/contracts/gas-estimation/EstimateGas.sol
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,118 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.11; | ||
|
||
// From https://github.com/ethereumjs/testrpc/issues/58 | ||
contract EstimateGas { | ||
event Add(bytes32 name, bytes32 description, uint value, address owner); | ||
|
||
struct Test { | ||
bytes32 name; | ||
bytes32 description; | ||
uint[] balances; | ||
mapping(address => uint) owners; | ||
} | ||
|
||
uint256 public x; | ||
uint256 public y; | ||
function reset() public { | ||
x = 0; | ||
y = 1; | ||
} | ||
function initialSettingOfX() public { | ||
x = 1; | ||
} | ||
function triggerRsclearRefund() public { | ||
x = gasleft(); | ||
reset(); | ||
} | ||
function triggerRsclearRefundForX() public { | ||
reset(); | ||
x = gasleft(); | ||
} | ||
function triggerRsclearRefundForY() public { | ||
y = gasleft(); | ||
reset(); | ||
} | ||
function triggerRselfdestructRefund() public { | ||
selfdestruct(payable(msg.sender)); | ||
} | ||
function triggerAllRefunds() public { | ||
triggerRsclearRefund(); | ||
triggerRselfdestructRefund(); | ||
} | ||
|
||
// https://github.com/trufflesuite/ganache-cli/issues/294 | ||
mapping (uint => uint) public uints; | ||
// Sets the uints[1] slot to a value; | ||
function store(uint _uint) public { uints[1] = _uint;} | ||
function clear() public { delete uints[1]; } | ||
|
||
mapping(bytes32 => uint) index; | ||
Test[] tests; | ||
|
||
constructor() { | ||
tests.push(); | ||
} | ||
|
||
function add(bytes32 _name, bytes32 _description, uint _value) public returns(bool) { | ||
if (index[_name] != 0) { | ||
return false; | ||
} | ||
uint pos = tests.length; | ||
tests.push(); | ||
tests[pos].name = _name; | ||
tests[pos].description = _description; | ||
tests[pos].balances.push(); | ||
tests[pos].balances.push(_value); | ||
tests[pos].owners[msg.sender] = 1; | ||
index[_name] = pos; | ||
emit Add(_name, _description, _value, msg.sender); | ||
return true; | ||
} | ||
|
||
function transfer(address _to, uint _value, bytes32 _name) public returns(bool) { | ||
uint pos = index[_name]; | ||
if (pos == 0) { | ||
return false; | ||
} | ||
|
||
uint posFrom = tests[pos].owners[msg.sender]; | ||
if (posFrom == 0) { | ||
return false; | ||
} | ||
|
||
if (tests[pos].balances[posFrom] < _value) { | ||
return false; | ||
} | ||
|
||
uint posTo = tests[pos].owners[_to]; | ||
if (posTo == 0) { | ||
uint posBal = tests[pos].balances.length; | ||
tests[pos].balances.push(); | ||
tests[pos].owners[_to] = posBal; | ||
posTo = posBal; | ||
} | ||
|
||
if (tests[pos].balances[posTo] + _value < tests[pos].balances[posTo]) { | ||
return false; | ||
} | ||
tests[pos].balances[posFrom] -= _value; | ||
tests[pos].balances[posTo] += _value; | ||
|
||
return true; | ||
} | ||
|
||
function currentBlock() public view returns (uint) { | ||
return block.number; | ||
} | ||
|
||
uint public counter; | ||
function runsOutOfGas() public { | ||
consumesGas(); | ||
} | ||
function consumesGas() public { | ||
for(uint i = 0; i < 100000; i++){ | ||
counter = i; | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/chains/ethereum/ethereum/tests/api/eth/contracts/gas-estimation/Fib.sol
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,23 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.11; | ||
|
||
contract Fib { | ||
constructor() {} | ||
uint public value = 0; | ||
|
||
fallback() external payable { | ||
Bif bif = new Bif(); | ||
value = bif.calc(5); | ||
} | ||
} | ||
|
||
contract Bif { | ||
constructor() {} | ||
|
||
function calc(uint index) public returns(uint){ | ||
if (index <= 1) { | ||
return 1; | ||
} | ||
return calc(index - 1) + calc(index - 2); | ||
} | ||
} |
Oops, something went wrong.