Browse Source

Array usage examples

Manuel Araoz 9 years ago
parent
commit
83acbbbb15
3 changed files with 24 additions and 7 deletions
  1. 9 3
      contracts/BadArrayUse.sol
  2. 14 4
      contracts/GoodArrayUse.sol
  3. 1 0
      migrations/2_deploy_contracts.js

+ 9 - 3
contracts/BadArrayUse.sol

@@ -5,10 +5,16 @@ import './PullPaymentCapable.sol';
 contract BadArrayUse is PullPaymentCapable {
 contract BadArrayUse is PullPaymentCapable {
   address[] employees;
   address[] employees;
 
 
-  function payroll() {
+  function payBonus() {
     for (var i = 0; i < employees.length; i++) {
     for (var i = 0; i < employees.length; i++) {
-      
+			address employee = employees[i];
+			uint bonus = calculateBonus(employee);
+    	asyncSend(employee, bonus);
     }
     }
-      
   }
   }
+
+	function calculateBonus(address employee) returns (uint) {
+    // some expensive computation...
+  }
+
 }
 }

+ 14 - 4
contracts/GoodArrayUse.sol

@@ -1,11 +1,21 @@
+import './PullPaymentCapable.sol';
 
 
-contract GoodArrayUse {
+contract GoodArrayUse is PullPaymentCapable {
   address[] employees;
   address[] employees;
+  mapping(address => uint) bonuses;
 
 
-  function payroll() {
+  function payBonus() {
     for (uint i = 0; i < employees.length; i++) {
     for (uint i = 0; i < employees.length; i++) {
-      
+			address employee = employees[i];
+			uint bonus = bonuses[employee];
+    	asyncSend(employee, bonus);
     }
     }
-      
   }
   }
+
+	function calculateBonus(address employee) returns (uint) {
+    uint bonus = 0;
+    // some expensive computation...
+    bonuses[employee] = bonus;
+  }
+
 }
 }

+ 1 - 0
migrations/2_deploy_contracts.js

@@ -2,4 +2,5 @@ module.exports = function(deployer) {
   deployer.deploy(BadFailEarly);
   deployer.deploy(BadFailEarly);
   deployer.deploy(GoodFailEarly);
   deployer.deploy(GoodFailEarly);
   deployer.deploy(PullPaymentBid);
   deployer.deploy(PullPaymentBid);
+  deployer.deploy(BadArrayUse);
 };
 };