Skip to content

Commit 85c7628

Browse files
run format
1 parent 70e3210 commit 85c7628

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

program-analysis/echidna/basic/working-with-eth.md

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,51 @@
1010

1111
## Introduction
1212

13-
We will see how to use ETH during the fuzzing fuzzed. The following smart contract will be used as example:
13+
We will see how to use ETH during the fuzzing fuzzed. The following smart contract will be used as example:
1414

1515
```solidity
1616
contract C {
17-
function pay() payable public {
18-
require(msg.value == 12000);
19-
}
17+
function pay() public payable {
18+
require(msg.value == 12000);
19+
}
2020
21-
function echidna_has_some_value() public returns (bool) {
22-
return (address(this).balance != 12000);
23-
}
21+
function echidna_has_some_value() public returns (bool) {
22+
return (address(this).balance != 12000);
23+
}
2424
}
2525
```
2626

27-
This code forces Echidna to send a particular amount of eth as value in the `pay` function.
27+
This code forces Echidna to send a particular amount of eth as value in the `pay` function.
2828
Echidna will do this for each payable function in the target function (or any contract if `allContracts` is enabled):
2929

3030
```
31-
$ echidna balanceSender.sol
31+
$ echidna balanceSender.sol
3232
...
33-
echidna_has_some_value: failed!💥
33+
echidna_has_some_value: failed!💥
3434
Call sequence:
3535
pay() Value: 0x2ee0
3636
```
3737

38-
Echidna will show the value amount in hexadecimal.
38+
Echidna will show the value amount in hexadecimal.
3939

4040
## Controlling the amount of ether in payable functions
4141

4242
The amount of ether to send in each payable function will be randomly selected, but with a maximum value determined by the `maxValue` value
4343
with a default of 100 eth per transaction:
4444

4545
```yaml
46-
maxValue: 100000000000000000000
46+
maxValue: 100000000000000000000
4747
```
4848
49-
This means that each transaction will contain, at most, 100 eth in value. However, there is no maximum that will be used in total.
50-
The maximum amount to receive will be determined by the number of transactions. If you are using 100 transactions (`--seq-len 100`),
51-
then the maximum amount of ether in transactions will be 100 * 100 eth.
49+
This means that each transaction will contain, at most, 100 eth in value. However, there is no maximum that will be used in total.
50+
The maximum amount to receive will be determined by the number of transactions. If you are using 100 transactions (`--seq-len 100`),
51+
then the maximum amount of ether in transactions will be 100 \* 100 eth.
5252

53-
Keep in mind that the balance of the senders (e.g. `msg.sender.balance`) is a fixed value that will NOT change between transactions.
53+
Keep in mind that the balance of the senders (e.g. `msg.sender.balance`) is a fixed value that will NOT change between transactions.
5454
This value is determined by the following config option:
5555

5656
```yaml
57-
balanceAddr: 0xffffffff
57+
balanceAddr: 0xffffffff
5858
```
5959

6060
## Controlling the amount of ether in contracts
@@ -63,26 +63,27 @@ Another approach to handle ether will be allow the testing contract to receive c
6363

6464
```solidity
6565
contract A {
66-
C internal c;
67-
constructor() payable public {
68-
require(msg.value == 12000);
69-
c = new C();
70-
}
71-
72-
function payToContract(uint256 toPay) public {
73-
toPay = toPay % (address(this).balance + 1);
74-
c.pay{value: toPay}();
75-
}
76-
77-
function echidna_C_has_some_value() public returns (bool) {
78-
return (address(c).balance != 12000);
79-
}
66+
C internal c;
67+
68+
constructor() public payable {
69+
require(msg.value == 12000);
70+
c = new C();
71+
}
72+
73+
function payToContract(uint256 toPay) public {
74+
toPay = toPay % (address(this).balance + 1);
75+
c.pay{ value: toPay }();
76+
}
77+
78+
function echidna_C_has_some_value() public returns (bool) {
79+
return (address(c).balance != 12000);
80+
}
8081
}
8182
8283
contract C {
83-
function pay() payable public {
84-
require(msg.value == 12000);
85-
}
84+
function pay() public payable {
85+
require(msg.value == 12000);
86+
}
8687
}
8788
```
8889

@@ -94,7 +95,7 @@ $ echidna balanceContract.sol
9495
echidna: Deploying the contract 0x00a329c0648769A73afAc7F9381E08FB43dBEA72 failed (revert, out-of-gas, sending ether to an non-payable constructor, etc.):
9596
```
9697

97-
We need to define the amount to send during the contract creation:
98+
We need to define the amount to send during the contract creation:
9899

99100
```yaml
100101
balanceContract: 12000
@@ -103,16 +104,16 @@ balanceContract: 12000
103104
We can re-run echidna, using that config file, to obtain the expected result:
104105

105106
```
106-
$ echidna balanceContract.sol --config balanceContract.yaml
107+
$ echidna balanceContract.sol --config balanceContract.yaml
107108
...
108-
echidna_C_has_some_value: failed!💥
109+
echidna_C_has_some_value: failed!💥
109110
Call sequence:
110111
payToContract(12000)
111112
```
112113

113114
## Summary: Working with ether
114115

115-
Echidna has two options for using ether during a fuzzing campaign.
116+
Echidna has two options for using ether during a fuzzing campaign.
116117

117-
* `maxValue` to set the max amount of ether per transaction
118-
* `contractBalance` to set the initial amount of ether that the testing contract receives in the constructor.
118+
- `maxValue` to set the max amount of ether per transaction
119+
- `contractBalance` to set the initial amount of ether that the testing contract receives in the constructor.

0 commit comments

Comments
 (0)