Common Patterns in Contract

Withdrawal from Contract

The recommended method of sending funds after an effect is using the withdrawal pattern.

Although the most intuitive method of sending Ether, as a result of an effect, is a direct send call, this is not recommended as it introduces a potential security risk.

This is an example of the withdrawal pattern in practice in a contract where the goal is to send the most money to the contract in order to become the “richest”.

In the following contract, if you are usurped as the richest, you will receive the funds of the person who has gone on to be the new richest:

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
pragma solidity ^0.4.11;

contract WithdrawalContract {
address public richest;
uint public mostSent;

mapping(address => uint) public pendingWithdrawals;

function WithdrawalContract() payable {
richest = msg.sender;
mostSent = msg.value;
}

function becomeRichest() payable returns (bool) {
if (msg.value > mostSent) {
pendingWithdrawals[msg.sender] += msg.value;
richest = msg.sender;
mostSent = msg.value;
return true;
} else {
return false;
}
}

function withdraw() {
uint amount = pendingWithdrawals[msg.sender];
pendingWithdrawals[msg.sender] = 0;
msg.sender.transfer(amount);
}
}

In the example above, if the Contract is attacked and the withdraw method stuck(for example, msg.sender.transfer(amount) fails), the Contract will keep working.

Restricting Access

Restricting access is a common pattern for contract.

Note that you can never restrict any human or computer from reading the content of your transactions or your contract’s state. You can make it a bit harder by using encryption, but if your contract is supposed to read the data, so will everyone else.

You can restrict read access to your contract’s state by other contract. This is actually the default unless you declare make your state variables public.

Furthermore, you can restrict who can make modifications to your contract’s state or call your contract’s functions.

The use of function modifier makes thse restrictions highly readable.

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
pragma solidity ^0.4.11;

contract AccessRestriction {
address public owner = msg.sender;
uint public creationTime = now;

modifier onlyBy(address _account) {
require(msg.sender == _account);
_;
}

function changeOwner(address _newOwner) onlyBy(owner) {
owner = _newOwner;
}

modifier onlyAfter(uint _time) {
require(now > _time);
_;
}

function disown() onlyBy(owner) onlyAfter(creationTime + 6 weeks) {
delete owner;
}

modifier costs(uint _amount) {
require(msg.value > _amount);
_;
if (msg.value > _amount)
msg.sender.send(msg.value - _amount);
}

function forceOwnerChange(address _newOwner) costs(200 ether) {
owner = _newOwner;
if (uint(owner) & 0 == 1)
return;
}
}

State Machine

Contracts often act as a state machine, which means that they have certain stage in which they behave differently or in which different functions can be called.

A function call often ends a stage and transitions the contract into the next stage.

Function modifiers can be used in this situation to model the states and guard against incorrect usage of the contract.

In the following example, the modifier atStage ensures that the function can only be called at a certain stage, automatic timed transitions are handled by the modifier timeTransitions, which should be used for all functions.

Modifier Order Matters: If atStage is combined with timedTransitions, make sure that you mention it after the latter, so that the new stage is taken into account.

Finally, the modifier transitionNext can be used to automatically go to the next stage when the function finishes.

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
45
46
47
pragma solidity ^0.4.11;

contract StateMachine {
enum Stages {
AcceptingBlindedBids,
RevealBids,
AnotherStage,
AreWeDoneYet,
Finished
}

Stages public stage = Stages.AcceptingBlindedBids;

uint public creationTime = now;

modifier atStage(Stages _stage) {
require(stage == _stage);
_;
}

function nextStage() internal {
stage = Stages(uint(stage) + 1);
}

modifier timedTransitions() {
if (stage == Stages.AcceptingBlindedBids && now >= creationTime + 10 days)
nextStage();
if (stage == Stages.RevealBids && now >= creationTime + 12 days)
nextStage();
_;
}

function bid()
payable
timedTransitions
atStage(Stages.AcceptingBlindedBids)
{
//
}

function reveal()
timedTransitions
atStage(Stages.RevealBids)
{
//
}
}

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×