Skip to content

Instantly share code, notes, and snippets.

@noaaizental
Created October 17, 2017 08:36
Show Gist options
  • Save noaaizental/73ef6c494e313b1520e99886770967a5 to your computer and use it in GitHub Desktop.
Save noaaizental/73ef6c494e313b1520e99886770967a5 to your computer and use it in GitHub Desktop.
function buy(address _reserveToken, uint256 _depositAmount, uint256 _minimumValue) public returns (uint256 value) {
uint8 reserveRatio = reserveRatioOf[_reserveToken];
if (reserveRatio == 0 || _depositAmount == 0) // validate input
throw;
if (stage != Stage.Traded) // validate state
throw;
ReserveToken reserveToken = ReserveToken(_reserveToken);
uint256 reserveBalance = reserveToken.balanceOf(this);
BancorFormula formulaContract = BancorFormula(formula);
value = formulaContract.calculatePurchaseValue(totalSupply, reserveBalance, reserveRatio, _depositAmount);
if (value == 0 || value < _minimumValue) // trade gave nothing in return or didn't return a value that meets the minimum requested value
throw;
if (totalSupply + value < totalSupply) // supply overflow protection
throw;
if (!reserveToken.transferFrom(msg.sender, this, _depositAmount)) // can't withdraw funds from the reserve token
throw;
uint256 startSupply = totalSupply;
totalSupply += value;
balanceOf[msg.sender] += value;
dispatchConversion(_reserveToken, msg.sender, true, startSupply, reserveBalance, value, _depositAmount);
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment