forked from crytic/not-so-smart-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRaceCondition.sol
50 lines (42 loc) · 1.55 KB
/
RaceCondition.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
45
46
47
48
49
50
pragma solidity ^0.4.16;
// https://github.com/ethereum/EIPs/issues/20
contract ERC20 {
function totalSupply() constant returns (uint totalSupply);
function balanceOf(address _owner) constant returns (uint balance);
function transfer(address _to, uint _value) returns (bool success);
function transferFrom(address _from, address _to, uint _value) returns (bool success);
function approve(address _spender, uint _value) returns (bool success);
function allowance(address _owner, address _spender) constant returns (uint remaining);
event Transfer(address indexed _from, address indexed _to, uint _value);
event Approval(address indexed _owner, address indexed _spender, uint _value);
}
contract RaceCondition{
address private owner;
uint public price;
ERC20 token;
function RaceCondition(uint _price, ERC20 _token)
public
{
owner = msg.sender;
price = _price;
token = _token;
}
// If the owner sees someone calls buy
// he can call changePrice to set a new price
// If his transaction is mined first, he can
// receive more tokens than excepted by the new buyer
function buy(uint new_price) payable
public
{
require(msg.value >= price);
// we assume that the RaceCondition contract
// has enough allowance
token.transferFrom(msg.sender, owner, price);
price = new_price;
owner = msg.sender;
}
function changePrice(uint new_price){
require(msg.sender == owner);
price = new_price;
}
}