diff --git a/contracts/BadArrayUse.sol b/contracts/BadArrayUse.sol new file mode 100644 index 000000000..e278ea3a5 --- /dev/null +++ b/contracts/BadArrayUse.sol @@ -0,0 +1,14 @@ +import './PullPaymentCapable.sol'; + +// UNSAFE CODE, DO NOT USE! + +contract BadArrayUse is PullPaymentCapable { + address[] employees; + + function payroll() { + for (var i = 0; i < employees.length; i++) { + + } + + } +} diff --git a/contracts/BadFailEarly.sol b/contracts/BadFailEarly.sol new file mode 100644 index 000000000..809d91166 --- /dev/null +++ b/contracts/BadFailEarly.sol @@ -0,0 +1,15 @@ +// UNSAFE CODE, DO NOT USE! + +contract BadFailEarly { + + uint constant DEFAULT_SALARY = 50000; + mapping(string => uint) nameToSalary; + + function getSalary(string name) constant returns (uint) { + if (bytes(name).length != 0 && nameToSalary[name] != 0) { + return nameToSalary[name]; + } else { + return DEFAULT_SALARY; + } + } +} diff --git a/contracts/BadPushPayments.sol b/contracts/BadPushPayments.sol new file mode 100644 index 000000000..4c1741ce6 --- /dev/null +++ b/contracts/BadPushPayments.sol @@ -0,0 +1,18 @@ +// UNSAFE CODE, DO NOT USE! + +contract BadPushPayments { + + address highestBidder; + uint highestBid; + + function bid() { + if (msg.value < highestBid) throw; + + if (highestBidder != 0) { + highestBidder.send(highestBid); + } + + highestBidder = msg.sender; + highestBid = msg.value; + } +} diff --git a/contracts/GoodArrayUse.sol b/contracts/GoodArrayUse.sol new file mode 100644 index 000000000..abac54195 --- /dev/null +++ b/contracts/GoodArrayUse.sol @@ -0,0 +1,11 @@ + +contract GoodArrayUse { + address[] employees; + + function payroll() { + for (uint i = 0; i < employees.length; i++) { + + } + + } +} diff --git a/contracts/GoodFailEarly.sol b/contracts/GoodFailEarly.sol new file mode 100644 index 000000000..f76cb0b68 --- /dev/null +++ b/contracts/GoodFailEarly.sol @@ -0,0 +1,16 @@ +contract GoodFailEarly { + + uint constant DEFAULT_SALARY = 50000; + mapping(string => uint) nameToSalary; + + function getSalary(string name) constant returns (uint) { + if (bytes(name).length == 0) { + throw; + } + if (nameToSalary[name] == 0) { + throw; + } + + return nameToSalary[name]; + } +} diff --git a/contracts/GoodPullPayments.sol b/contracts/GoodPullPayments.sol new file mode 100644 index 000000000..e8ac86b13 --- /dev/null +++ b/contracts/GoodPullPayments.sol @@ -0,0 +1,24 @@ +contract GoodPullPayments { + address highestBidder; + uint highestBid; + mapping(address => uint) refunds; + + function bid() external { + if (msg.value < highestBid) throw; + + if (highestBidder != 0) { + refunds[highestBidder] += highestBid; + } + + highestBidder = msg.sender; + highestBid = msg.value; + } + + function withdrawRefund() external { + uint refund = refunds[msg.sender]; + refunds[msg.sender] = 0; + if (!msg.sender.send(refund)) { + refunds[msg.sender] = refund; + } + } +} diff --git a/contracts/PullPaymentBid.sol b/contracts/PullPaymentBid.sol new file mode 100644 index 000000000..247105eab --- /dev/null +++ b/contracts/PullPaymentBid.sol @@ -0,0 +1,16 @@ +import './PullPaymentCapable.sol'; + +contract PullPaymentBid is PullPaymentCapable { + address public highestBidder; + uint public highestBid; + + function bid() external { + if (msg.value <= highestBid) throw; + + if (highestBidder != 0) { + asyncSend(highestBidder, highestBid); + } + highestBidder = msg.sender; + highestBid = msg.value; + } +} diff --git a/contracts/PullPaymentCapable.sol b/contracts/PullPaymentCapable.sol new file mode 100644 index 000000000..8bdfc79bf --- /dev/null +++ b/contracts/PullPaymentCapable.sol @@ -0,0 +1,15 @@ +contract PullPaymentCapable { + mapping(address => uint) refunds; + + function asyncSend(address dest, uint amount) { + refunds[dest] += amount; + } + + function withdrawRefund() external { + uint refund = refunds[msg.sender]; + refunds[msg.sender] = 0; + if (!msg.sender.send(refund)) { + refunds[msg.sender] = refund; + } + } +} diff --git a/migrations/2_deploy_contracts.js b/migrations/2_deploy_contracts.js index ebb3a480d..0d1c9266d 100644 --- a/migrations/2_deploy_contracts.js +++ b/migrations/2_deploy_contracts.js @@ -1,5 +1,5 @@ module.exports = function(deployer) { - deployer.deploy(ConvertLib); - deployer.autolink(); - deployer.deploy(MetaCoin); + deployer.deploy(BadFailEarly); + deployer.deploy(GoodFailEarly); + deployer.deploy(PullPaymentBid); };