Skip to content

Commit

Permalink
Unicorn Incorrect change bug fix
Browse files Browse the repository at this point in the history
Bug fix in the change calculation for the Unicorn token with natural number 1
  • Loading branch information
bokkypoobah authored Feb 5, 2017
1 parent 615f489 commit 376cdb1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
17 changes: 12 additions & 5 deletions contracts/TokenSellerFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ pragma solidity ^0.4.4;
// This caters for the Golem Network Token which does not implement the
// ERC20 transferFrom(...), approve(...) and allowance(...) methods
//
// Enjoy. (c) JonnyLatte, Cintix & BokkyPooBah 2016. The MIT licence.
// History:
// Jan 25 2017 - BPB Added makerTransferAsset(...)
// Feb 05 2017 - BPB Bug fix in the change calculation for the Unicorn
// token with natural number 1
//
// Enjoy. (c) JonnyLatte, Cintix & BokkyPooBah 2017. The MIT licence.
// ------------------------------------------------------------------------

// https://github.com/ethereum/EIPs/issues/20
Expand Down Expand Up @@ -68,7 +73,7 @@ contract TokenSeller is Owned {
uint256 _sellPrice,
uint256 _units,
bool _sellsTokens
) internal {
) {
asset = _asset;
sellPrice = _sellPrice;
units = _units;
Expand Down Expand Up @@ -178,13 +183,15 @@ contract TokenSeller is Owned {
// Note that units has already been validated as > 0
uint can_sell = ERC20Partial(asset).balanceOf(address(this)) / units;
uint256 change = 0;
if (order > can_sell) {
change = msg.value - (can_sell * sellPrice);
if (msg.value > (can_sell * sellPrice)) {
change = msg.value - (can_sell * sellPrice);
order = can_sell;
}
if (change > 0) {
if (!msg.sender.send(change)) throw;
}
if (order > 0) {
if(!ERC20Partial(asset).transfer(msg.sender, order * units)) throw;
if (!ERC20Partial(asset).transfer(msg.sender, order * units)) throw;
}
TakerBoughtAsset(msg.sender, msg.value, change, order * units);
}
Expand Down
22 changes: 15 additions & 7 deletions contracts/TokenTraderFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ pragma solidity ^0.4.4;
// directly as the token does not implement the ERC20
// transferFrom(...), approve(...) and allowance(...) methods
//
// Enjoy. (c) JonnyLatte & BokkyPooBah 2016. The MIT licence.
// History:
// Jan 25 2017 - BPB Added makerTransferAsset(...) and
// makerTransferEther(...)
// Feb 05 2017 - BPB Bug fix in the change calculation for the Unicorn
// token with natural number 1
//
// Enjoy. (c) JonnyLatte & BokkyPooBah 2017. The MIT licence.
// ------------------------------------------------------------------------

// https://github.com/ethereum/EIPs/issues/20
Expand Down Expand Up @@ -82,7 +88,7 @@ contract TokenTrader is Owned {
uint256 _units,
bool _buysTokens,
bool _sellsTokens
) internal {
) {
asset = _asset;
buyPrice = _buyPrice;
sellPrice = _sellPrice;
Expand Down Expand Up @@ -255,13 +261,15 @@ contract TokenTrader is Owned {
// Note that units has already been validated as > 0
uint can_sell = ERC20(asset).balanceOf(address(this)) / units;
uint256 change = 0;
if (order > can_sell) {
change = msg.value - (can_sell * sellPrice);
if (msg.value > (can_sell * sellPrice)) {
change = msg.value - (can_sell * sellPrice);
order = can_sell;
}
if (change > 0) {
if (!msg.sender.send(change)) throw;
}
if (order > 0) {
if(!ERC20(asset).transfer(msg.sender, order * units)) throw;
if (!ERC20(asset).transfer(msg.sender, order * units)) throw;
}
TakerBoughtAsset(msg.sender, msg.value, change, order * units);
}
Expand Down Expand Up @@ -298,9 +306,9 @@ contract TokenTrader is Owned {
if (order > can_buy) order = can_buy;
if (order > 0) {
// Extract user tokens
if(!ERC20(asset).transferFrom(msg.sender, address(this), order * units)) throw;
if (!ERC20(asset).transferFrom(msg.sender, address(this), order * units)) throw;
// Pay user
if(!msg.sender.send(order * buyPrice)) throw;
if (!msg.sender.send(order * buyPrice)) throw;
}
TakerSoldAsset(msg.sender, etherValueOfTokensToSell, order * units, order * buyPrice);
}
Expand Down

0 comments on commit 376cdb1

Please sign in to comment.