-
Notifications
You must be signed in to change notification settings - Fork 1
/
UsageExample.sol
44 lines (37 loc) · 1.09 KB
/
UsageExample.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
/**
* @title NiceGuy
* @dev Implements `freeTx` modifier to add a full transaction cost rebate to any function
*/
contract NiceGuy {
/**
* @dev Transfer all ether use in the contract function back to the sender
*/
modifier freeTx() {
uint256 startGas = gasleft();
_;
uint256 spentGas = startGas - gasleft() + 21000 + 16 * msg.data.length;
payable(msg.sender).transfer(spentGas * tx.gasprice);
}
}
/**
* @title Button created by Marto
* @dev Usage example of NiceGuy "freeTx" modifier to add a full transaction cost rebate to any function
*/
contract Button is NiceGuy {
uint256 public counter = 0;
constructor() payable {}
/**
* @dev Transfer all ether use in the contract function back to the sender
*/
function push() public freeTx {
// Make some expensive computation
for (uint i=0; i < 500; i++) {
counter = counter + 1;
}
}
function getBalance() public view returns (uint256) {
return address(this).balance;
}
}