Skip to content

Commit

Permalink
Replace payback to use withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
makoto committed Aug 28, 2016
1 parent 523b90f commit 74857ed
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 31 deletions.
41 changes: 26 additions & 15 deletions contracts/Conference.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ contract Conference is Rejector, Ownable, Killable {
string participantName;
address addr;
bool attended;
int payout;
uint256 payout;
bool paid;
}

event Register(string participantName, address addr, uint256 balance, uint256 value);
Expand Down Expand Up @@ -72,7 +73,7 @@ contract Conference is Rejector, Ownable, Killable {
if (isRegistered(msg.sender)) throw;
registered++;
participantsIndex[registered] = msg.sender;
participants[msg.sender] = Participant(_participant, msg.sender, false, 0);
participants[msg.sender] = Participant(_participant, msg.sender, false, 0, false);
totalBalance = totalBalance + (deposit * 1);
}

Expand All @@ -96,27 +97,20 @@ contract Conference is Rejector, Ownable, Killable {
return isRegistered(_addr) && participants[_addr].attended;
}

function isPaid(address _addr) returns (bool){
return isRegistered(_addr) && participants[_addr].paid;
}

function payout() returns(uint256){
return totalBalance / uint(attended);
}

function payback() onlyOwner{
for(uint i=1;i<=registered;i++)
{
for(uint i=1;i<=registered;i++){
if(participants[participantsIndex[i]].attended){
Payback(participantsIndex[i], payout(), participantsIndex[i].balance, true);
participants[participantsIndex[i]].payout = int(payout() - deposit);
participantsIndex[i].send(payout());
}else{
participants[participantsIndex[i]].payout = int(deposit - (deposit * 2));
Payback(participantsIndex[i], payout(), participantsIndex[i].balance, false);
participants[participantsIndex[i]].payout = payout();
}
}
/*
* Actual balance may have some left over if 4 payout is divided among 3 attendees
* eg: 4 / 3 = 1.333
*/
totalBalance = 0;
ended = true;
}

Expand All @@ -135,4 +129,21 @@ contract Conference is Rejector, Ownable, Killable {
attended = 0;
ended = true;
}

modifier onlyPayable {
Participant participant = participants[msg.sender];
if (participant.payout > 0){
_
}
}

function withdraw() onlyPayable {
Participant participant = participants[msg.sender];
participant.paid = true;
totalBalance -= participant.payout;
if (!msg.sender.send(participant.payout)) {
participant.paid = false;
totalBalance += participant.payout;
}
}
}
72 changes: 56 additions & 16 deletions test/conference.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ contract('Conference', function(accounts) {
})

describe('on payback', function(){
it('cannot be paid back if non owner calls', function(done){
it('cannot withdraw if non owner calls', function(done){
var meta
var transaction = web3.toWei(1, "ether");
var gas = 1000000;
Expand All @@ -287,15 +287,50 @@ contract('Conference', function(accounts) {
previousBalances[0] = web3.eth.getBalance(accounts[0]);
return meta.payback.sendTransaction({from:nonOwner, gas:gas})
}).then(function(){
return meta.withdraw.sendTransaction({from:accounts[0], gas:gas})
}).then(function(transaction){
var receipt = web3.eth.getTransactionReceipt(transaction)
// money is still left on contract
assert.equal(web3.eth.getBalance(meta.address).toNumber(), web3.toWei(1, "ether"))
// did not get deposit back
assert.equal(previousBalances[0].toNumber(), web3.eth.getBalance(accounts[0]).toNumber())
assert.equal(previousBalances[0].toNumber(), web3.eth.getBalance(accounts[0]).toNumber() + receipt.gasUsed)
})
.then(done).catch(done);
})

it('receives payout if you attend', function(done){
it('cannot withdraw if you did not attend', function(done){
var meta
var transaction = web3.toWei(1, "ether");
var gas = 1000000;
var previousBalances = [];
var twitterHandle = '@bighero6';
var notAttended = accounts[2];
var attended = accounts[1];
var owner = accounts[0];

Conference.new().then(function(_meta) {
meta = _meta;
return meta.register.sendTransaction(twitterHandle, {from:attended, value:transaction, gas:gas})
}).then(function(){
// contract gets 1 ether
assert.equal( web3.eth.getBalance(meta.address), web3.toWei(1, "ether"))
return meta.attend.sendTransaction(attended, {gas:gas})
}).then(function(){
previousBalances[0] = web3.eth.getBalance(notAttended);
return meta.payback.sendTransaction({from:owner, gas:gas})
}).then(function(){
return meta.withdraw.sendTransaction({from:notAttended, gas:gas})
}).then(function(transaction){
var receipt = web3.eth.getTransactionReceipt(transaction)
// money is still left on contract
assert.equal(web3.eth.getBalance(meta.address).toNumber(), web3.toWei(1, "ether"))
// did not get deposit back
assert.equal(previousBalances[0].toNumber(), web3.eth.getBalance(notAttended).toNumber() + receipt.gasUsed)
})
.then(done).catch(done);
})

it('can withdraw if you attend', function(done){
var meta = Conference.deployed();
var transaction = web3.toWei(1, "ether");
var gas = 1000000;
Expand Down Expand Up @@ -323,34 +358,39 @@ contract('Conference', function(accounts) {
return meta.attend.sendTransaction(accounts[0], {gas:gas})
}).then(function(){
return meta.attend.sendTransaction(accounts[1], {gas:gas})
}).then(function(){
return meta.payback.sendTransaction({from:accounts[0], gas:gas})
}).then(function(){
previousBalances[0] = web3.eth.getBalance(accounts[0]);
previousBalances[1] = web3.eth.getBalance(accounts[1]);
previousBalances[2] = web3.eth.getBalance(accounts[2]);
// payback gets 3 ehter / 2 = 1.5 each
return meta.payback.sendTransaction({from:accounts[0], gas:gas})
}).then(function(){
// no money is left on contract
assert.equal(web3.eth.getBalance(meta.address), web3.toWei(0, "ether"))
// got some money
return meta.withdraw.sendTransaction({from:accounts[0]})
}).then(function(transaction){
assert.equal(balanceDiff(0), 1.5)
return meta.withdraw.sendTransaction({from:accounts[1]})
}).then(function(transaction){
assert.equal(balanceDiff(1), 1.5)
// lost some money
return meta.withdraw.sendTransaction({from:accounts[2]})
}).then(function(transaction){
assert.equal(balanceDiff(2), 0)
// no money is left on contract
assert.equal(web3.eth.getBalance(meta.address).toNumber(), web3.toWei(0, "ether"))
return meta.participants.call(accounts[0]);
}).then(function(participant){
// Got some money
assert.equal(participant[3], web3.toWei(0.5, "ether"))
assert.equal(participant[3], web3.toWei(1.5, "ether"))
assert.equal(participant[4], true)
return meta.participants.call(accounts[1]);
}).then(function(participant){
// Got some money
assert.equal(participant[3], web3.toWei(0.5, "ether"))
assert.equal(participant[3], web3.toWei(1.5, "ether"))
assert.equal(participant[4], true)
return meta.participants.call(accounts[2]);
}).then(function(participant){
// Lost some money
assert.equal(participant[3], web3.toWei(-1, "ether"))
})
.then(done).catch(done);
// Got no money
assert.equal(participant[3], 0)
assert.equal(participant[4], false)
}).then(done).catch(done);
})

it('cannot register any more', function(done){
Expand Down

1 comment on commit 74857ed

@ademidun
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sharing your knowledge @makoto . For anyone interested, here is the blog post @makoto wrote with context on this commit:https://medium.com/noblocknoparty/a-smartcontract-best-practice-push-pull-or-give-b2e8428e032a

Please sign in to comment.