Skip to content
Browse files

SampleOffer: fix withdraw negative amount issue

This fixes the issue #171. Instread of remembering how much was payed out, it is remembered when the last payout was. Type type of withdraw limits has been also changed to uint128 to avoid overflow calculating the amount for the withdraw.
  • Loading branch information...
1 parent 2fbe8f1 commit c7aa3287f0517e878aa86be8de0723822882caf6 @chfast chfast committed May 25, 2016
Showing with 26 additions and 14 deletions.
  1. +2 −2 SampleOffer.sol
  2. +24 −12 SampleOfferWithoutReward.sol
View
4 SampleOffer.sol
@@ -37,7 +37,7 @@ contract SampleOffer is SampleOfferWithoutReward {
bytes32 _IPFSHashOfTheProposalDocument,
uint _totalCosts,
uint _oneTimeCosts,
- uint _minDailyWithdrawLimit
+ uint128 _minDailyWithdrawLimit
) SampleOfferWithoutReward(
_contractor,
_client,
@@ -46,7 +46,7 @@ contract SampleOffer is SampleOfferWithoutReward {
_oneTimeCosts,
_minDailyWithdrawLimit) {
}
-
+
// interface for Ethereum Computer
function payOneTimeReward() returns(bool) {
// client DAO should not be able to pay itself generating
View
36 SampleOfferWithoutReward.sol
@@ -48,21 +48,21 @@ contract SampleOfferWithoutReward {
// The minimal daily withdraw limit that the Contractor accepts.
// Set once by the Offerer.
- uint public minDailyWithdrawLimit;
+ uint128 public minDailyWithdrawLimit;
// The amount of money the Contractor has right to withdraw daily above the
// initial withdraw. The Contractor does not have to do the withdraws every
// day as this amount accumulates.
- uint public dailyWithdrawLimit;
+ uint128 public dailyWithdrawLimit;
// The address of the Contractor.
address public contractor;
// The address of the Proposal/Offer document.
bytes32 public IPFSHashOfTheProposalDocument;
-
- uint public paidOut;
+ // The time of the last withdraw to the Contractor.
+ uint public lastPayment;
uint public dateOfSignature;
DAO public client; // address of DAO
@@ -84,7 +84,7 @@ contract SampleOfferWithoutReward {
bytes32 _IPFSHashOfTheProposalDocument,
uint _totalCosts,
uint _oneTimeCosts,
- uint _minDailyWithdrawLimit
+ uint128 _minDailyWithdrawLimit
) {
contractor = _contractor;
originalClient = DAO(_client);
@@ -105,11 +105,17 @@ contract SampleOfferWithoutReward {
throw;
dateOfSignature = now;
isContractValid = true;
+ lastPayment = now;
}
- function setDailyWithdrawLimit(uint _dailyWithdrawLimit) onlyClient noEther {
- if (_dailyWithdrawLimit >= minDailyWithdrawLimit)
+ function setDailyWithdrawLimit(uint128 _dailyWithdrawLimit) onlyClient noEther {
+ if (_dailyWithdrawLimit >= minDailyWithdrawLimit) {
+ // Before changing the limit withdraw the money the Contractor has
+ // right to. The payment may not be accepted by the Contractor but
+ // it is the Contractor's problem.
+ getDailyPayment();
dailyWithdrawLimit = _dailyWithdrawLimit;
+ }
}
// "fire the contractor"
@@ -118,15 +124,21 @@ contract SampleOfferWithoutReward {
isContractValid = false;
}
- function getDailyPayment() {
- if (msg.sender != contractor)
- throw;
- uint amount = (now - dateOfSignature + 1 days) / (1 days) * dailyWithdrawLimit - paidOut;
+ // Withdraw to the Contractor.
+ //
+ // Withdraw the amount of money the Contractor has right to according to
+ // the current withdraw limit.
+ // Executing this function before the Offer is signed off by the Client
+ // makes no sense as this contract has no money.
+ function getDailyPayment() noEther {
+ uint timeSinceLastPayment = now - lastPayment;
+ // Calculate the amount using 1 second precision.
+ uint amount = (timeSinceLastPayment * dailyWithdrawLimit) / (1 days);
if (amount > this.balance) {
amount = this.balance;
}
if (contractor.send(amount))
- paidOut += amount;
+ lastPayment = now;
}
// Change the client DAO by giving the new DAO's address

0 comments on commit c7aa328

Please sign in to comment.
Something went wrong with that request. Please try again.