From 83acbbbb15a4c551d4a9b7a63ef15721ce5008d4 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Thu, 11 Aug 2016 13:02:32 -0300 Subject: [PATCH] Array usage examples --- contracts/BadArrayUse.sol | 12 +++++++++--- contracts/GoodArrayUse.sol | 18 ++++++++++++++---- migrations/2_deploy_contracts.js | 1 + 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/contracts/BadArrayUse.sol b/contracts/BadArrayUse.sol index e278ea3a5..b019bb173 100644 --- a/contracts/BadArrayUse.sol +++ b/contracts/BadArrayUse.sol @@ -5,10 +5,16 @@ import './PullPaymentCapable.sol'; contract BadArrayUse is PullPaymentCapable { address[] employees; - function payroll() { + function payBonus() { 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... + } + } diff --git a/contracts/GoodArrayUse.sol b/contracts/GoodArrayUse.sol index abac54195..f0693f35a 100644 --- a/contracts/GoodArrayUse.sol +++ b/contracts/GoodArrayUse.sol @@ -1,11 +1,21 @@ +import './PullPaymentCapable.sol'; -contract GoodArrayUse { +contract GoodArrayUse is PullPaymentCapable { address[] employees; + mapping(address => uint) bonuses; - function payroll() { + function payBonus() { 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; + } + } diff --git a/migrations/2_deploy_contracts.js b/migrations/2_deploy_contracts.js index 0d1c9266d..02476c063 100644 --- a/migrations/2_deploy_contracts.js +++ b/migrations/2_deploy_contracts.js @@ -2,4 +2,5 @@ module.exports = function(deployer) { deployer.deploy(BadFailEarly); deployer.deploy(GoodFailEarly); deployer.deploy(PullPaymentBid); + deployer.deploy(BadArrayUse); };