examples and more contracts

pull/14/head
Manuel Araoz 9 years ago
parent c9ae7c3f1c
commit 015708af62
  1. 14
      contracts/BadArrayUse.sol
  2. 15
      contracts/BadFailEarly.sol
  3. 18
      contracts/BadPushPayments.sol
  4. 11
      contracts/GoodArrayUse.sol
  5. 16
      contracts/GoodFailEarly.sol
  6. 24
      contracts/GoodPullPayments.sol
  7. 16
      contracts/PullPaymentBid.sol
  8. 15
      contracts/PullPaymentCapable.sol
  9. 6
      migrations/2_deploy_contracts.js

@ -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++) {
}
}
}

@ -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;
}
}
}

@ -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;
}
}

@ -0,0 +1,11 @@
contract GoodArrayUse {
address[] employees;
function payroll() {
for (uint i = 0; i < employees.length; i++) {
}
}
}

@ -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];
}
}

@ -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;
}
}
}

@ -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;
}
}

@ -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;
}
}
}

@ -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);
};

Loading…
Cancel
Save