Browse Source

examples and more contracts

Manuel Araoz 9 years ago
parent
commit
015708af62

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

+ 15 - 0
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;
+    }
+  }
+}

+ 18 - 0
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;
+	}
+}

+ 11 - 0
contracts/GoodArrayUse.sol

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

+ 16 - 0
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];
+  }
+}

+ 24 - 0
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;
+    }
+  }
+}

+ 16 - 0
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;
+  }
+}

+ 15 - 0
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;
+    }
+  }
+}

+ 3 - 3
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);
 };