solstat/report/report_sections/optimizations/
payable_function.rs1pub fn report_section_content() -> String {
2 String::from(
3 r##"
4## Mark functions as payable (with discretion)
5You can mark public or external functions as payable to save gas. Functions that are not payable have additional logic to check if there was a value sent with a call, however, making a function payable eliminates this check. This optimization should be carefully considered due to potentially unwanted behavior when a function does not need to accept ether.
6
7```js
8contract GasTest is DSTest {
9 Contract0 c0;
10 Contract1 c1;
11
12 function setUp() public {
13 c0 = new Contract0();
14 c1 = new Contract1();
15 }
16
17 function testGas() public {
18 c0.isNotPayable();
19 c1.isPayable();
20 }
21}
22
23contract Contract0 {
24 function isNotPayable() public view {
25 uint256 val = 0;
26 val++;
27 }
28}
29
30contract Contract1 {
31 function isPayable() public payable {
32 uint256 val = 0;
33 val++;
34 }
35}
36```
37
38### Gas Report
39```js
40╭────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
41│ Contract0 contract ┆ ┆ ┆ ┆ ┆ │
42╞════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
43│ Deployment Cost ┆ Deployment Size ┆ ┆ ┆ ┆ │
44├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
45│ 32081 ┆ 190 ┆ ┆ ┆ ┆ │
46├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
47│ Function Name ┆ min ┆ avg ┆ median ┆ max ┆ # calls │
48├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
49│ isNotPayable ┆ 198 ┆ 198 ┆ 198 ┆ 198 ┆ 1 │
50╰────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
51╭────────────────────┬─────────────────┬─────┬────────┬─────┬─────────╮
52│ Contract1 contract ┆ ┆ ┆ ┆ ┆ │
53╞════════════════════╪═════════════════╪═════╪════════╪═════╪═════════╡
54│ Deployment Cost ┆ Deployment Size ┆ ┆ ┆ ┆ │
55├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
56│ 29681 ┆ 178 ┆ ┆ ┆ ┆ │
57├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
58│ Function Name ┆ min ┆ avg ┆ median ┆ max ┆ # calls │
59├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
60│ isPayable ┆ 174 ┆ 174 ┆ 174 ┆ 174 ┆ 1 │
61╰────────────────────┴─────────────────┴─────┴────────┴─────┴─────────╯
62```
63
64
65"##,
66 )
67}