Skip to content

Commit

Permalink
transfer->transferFrom() with explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
LefterisJP authored and CJentzsch committed Nov 14, 2016
1 parent 857ce96 commit 3ddba10
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
9 changes: 8 additions & 1 deletion refund.sol
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,14 @@ contract Refund {
function withdraw(DAO _dao) internal {
uint balance = _dao.balanceOf(msg.sender);

if (!_dao.transfer(this, balance) || !msg.sender.send(balance * totalWeiSupply / totalSupply))
// The msg.sender must call approve(this, balance) beforehand so that
// transferFrom() will work and not throw. We need transferFrom()
// instead of transfer() due to the msg.sender in the latter ending
// up to be the contract
if (!_dao.transferFrom(msg.sender, this, balance)
|| !msg.sender.send(balance * totalWeiSupply / totalSupply)) {

throw;
}
}
}
9 changes: 8 additions & 1 deletion refundBlack.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ contract Refund {
function withdraw(DAO dao) internal {
uint balance = dao.balanceOf(msg.sender);

if (!dao.transfer(this, balance) || !msg.sender.send(balance * totalWeiSupply / totalSupply))
// The msg.sender must call approve(this, balance) beforehand so that
// transferFrom() will work and not throw. We need transferFrom()
// instead of transfer() due to the msg.sender in the latter ending
// up to be the contract
if (!dao.transferFrom(msg.sender, this, balance)
|| !msg.sender.send(balance * totalWeiSupply / totalSupply)) {

throw;
}
}
}
12 changes: 9 additions & 3 deletions simpleRefund.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ contract Refund {
function withdraw(address donateExtraBalanceTo){
uint balance = mainDAO.balanceOf(msg.sender);

if (!mainDAO.transfer(this, balance)
// The msg.sender must call approve(this, balance) beforehand so that
// transferFrom() will work and not throw. We need transferFrom()
// instead of transfer() due to the msg.sender in the latter ending
// up to be the contract
if (!mainDAO.transferFrom(msg.sender, this, balance)
|| !msg.sender.send(balance)
|| !donateExtraBalanceTo.send(balance * totalWeiSupply / totalSupply - balance)
) throw;
|| !donateExtraBalanceTo.send(balance * totalWeiSupply / totalSupply - balance)) {

throw;
}
}
}

0 comments on commit 3ddba10

Please sign in to comment.