Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ported STO fixes from dev-2.1.0 #591

Merged
merged 5 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 10 additions & 34 deletions contracts/modules/STO/Capped/CappedSTO.sol
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ contract CappedSTO is CappedSTOStorage, STO, ReentrancyGuard {
weiAmount = weiAmount.sub(refund);

_forwardFunds(refund);
_postValidatePurchase(_beneficiary, weiAmount);
}

/**
Expand All @@ -116,7 +115,6 @@ contract CappedSTO is CappedSTOStorage, STO, ReentrancyGuard {
require(fundRaiseTypes[uint8(FundRaiseType.POLY)], "Mode of investment is not POLY");
uint256 refund = _processTx(msg.sender, _investedPOLY);
_forwardPoly(msg.sender, wallet, _investedPOLY.sub(refund));
_postValidatePurchase(msg.sender, _investedPOLY.sub(refund));
}

/**
Expand Down Expand Up @@ -185,7 +183,6 @@ contract CappedSTO is CappedSTOStorage, STO, ReentrancyGuard {
_processPurchase(_beneficiary, tokens);
emit TokenPurchase(msg.sender, _beneficiary, _investedAmount, tokens);

_updatePurchasingState(_beneficiary, _investedAmount);
}

/**
Expand All @@ -197,24 +194,10 @@ contract CappedSTO is CappedSTOStorage, STO, ReentrancyGuard {
function _preValidatePurchase(address _beneficiary, uint256 _investedAmount) internal view {
require(_beneficiary != address(0), "Beneficiary address should not be 0x");
require(_investedAmount != 0, "Amount invested should not be equal to 0");
uint256 tokens;
(tokens, ) = _getTokenAmount(_investedAmount);
require(totalTokensSold.add(tokens) <= cap, "Investment more than cap is not allowed");
/*solium-disable-next-line security/no-block-members*/
require(now >= startTime && now <= endTime, "Offering is closed/Not yet started");
}

/**
* @notice Validation of an executed purchase.
Observe state and use revert statements to undo rollback when valid conditions are not met.
*/
function _postValidatePurchase(
address _beneficiary,
uint256 /*_investedAmount*/
) internal view {
require(_canBuy(_beneficiary), "Unauthorized");
maxsam4 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @notice Source of tokens.
Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
Expand All @@ -239,30 +222,23 @@ contract CappedSTO is CappedSTOStorage, STO, ReentrancyGuard {
_deliverTokens(_beneficiary, _tokenAmount);
}

/**
* @notice Overrides for extensions that require an internal state to check for validity
(current user contributions, etc.)
*/
function _updatePurchasingState(
address, /*_beneficiary*/
uint256 _investedAmount
) internal pure {
_investedAmount = 0; //yolo
}

/**
* @notice Overrides to extend the way in which ether is converted to tokens.
* @param _investedAmount Value in wei to be converted into tokens
* @return Number of tokens that can be purchased with the specified _investedAmount
* @return Remaining amount that should be refunded to the investor
*/
function _getTokenAmount(uint256 _investedAmount) internal view returns(uint256 _tokens, uint256 _refund) {
_tokens = _investedAmount.mul(rate);
_tokens = _tokens.div(uint256(10) ** 18);
function _getTokenAmount(uint256 _investedAmount) internal view returns(uint256 tokens, uint256 refund) {
tokens = _investedAmount.mul(rate);
tokens = tokens.div(uint256(10) ** 18);
if (totalTokensSold.add(tokens) > cap) {
tokens = cap.sub(totalTokensSold);
}
uint256 granularity = ISecurityToken(securityToken).granularity();
_tokens = _tokens.div(granularity);
_tokens = _tokens.mul(granularity);
_refund = _investedAmount.sub((_tokens.mul(uint256(10) ** 18)).div(rate));
tokens = tokens.div(granularity);
tokens = tokens.mul(granularity);
require(tokens > 0, "Cap reached");
refund = _investedAmount.sub((tokens.mul(uint256(10) ** 18)).div(rate));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/modules/STO/STO.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ contract STO is ISTO, STOStorage, Module, Pausable {

function _setFundRaiseType(FundRaiseType[] memory _fundRaiseTypes) internal {
// FundRaiseType[] parameter type ensures only valid values for _fundRaiseTypes
require(_fundRaiseTypes.length > 0, "Raise type is not specified");
require(_fundRaiseTypes.length > 0 && _fundRaiseTypes.length <= 3, "Raise type is not specified");
fundRaiseTypes[uint8(FundRaiseType.ETH)] = false;
fundRaiseTypes[uint8(FundRaiseType.POLY)] = false;
fundRaiseTypes[uint8(FundRaiseType.SC)] = false;
Expand Down
13 changes: 8 additions & 5 deletions contracts/modules/STO/USDTiered/USDTieredSTO.sol
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ contract USDTieredSTO is USDTieredSTOStorage, STO {
}
usdTokens = _usdTokens;
for(i = 0; i < _usdTokens.length; i++) {
require(_usdTokens[i] != address(0), "Invalid USD token");
require(_usdTokens[i] != address(0) && _usdTokens[i] != address(polyToken), "Invalid USD token");
usdTokenEnabled[_usdTokens[i]] = true;
}
emit SetAddresses(wallet, _usdTokens);
}
}

////////////////////
// STO Management //
Expand All @@ -258,7 +258,7 @@ contract USDTieredSTO is USDTieredSTOStorage, STO {
* @notice Finalizes the STO and mint remaining tokens to treasury address
* @notice Treasury wallet address must be whitelisted to successfully finalize
*/
function finalize() public {
function finalize() external {
_onlySecurityTokenOwner();
require(!isFinalized, "STO already finalized");
isFinalized = true;
Expand All @@ -275,6 +275,9 @@ contract USDTieredSTO is USDTieredSTOStorage, STO {
}
address walletAddress = (treasuryWallet == address(0) ? IDataStore(getDataStore()).getAddress(TREASURY) : treasuryWallet);
require(walletAddress != address(0), "Invalid address");
uint256 granularity = ISecurityToken(securityToken).granularity();
tempReturned = tempReturned.div(granularity);
tempReturned = tempReturned.mul(granularity);
ISecurityToken(securityToken).issue(walletAddress, tempReturned, "");
emit ReserveTokenMint(msg.sender, walletAddress, tempReturned, currentTier);
finalAmountReturned = tempReturned;
Expand All @@ -286,7 +289,7 @@ contract USDTieredSTO is USDTieredSTOStorage, STO {
* @param _investors Array of investor addresses to modify
* @param _nonAccreditedLimit Array of uints specifying non-accredited limits
*/
function changeNonAccreditedLimit(address[] memory _investors, uint256[] memory _nonAccreditedLimit) public {
function changeNonAccreditedLimit(address[] calldata _investors, uint256[] calldata _nonAccreditedLimit) external {
_onlySecurityTokenOwner();
//nonAccreditedLimitUSDOverride
require(_investors.length == _nonAccreditedLimit.length, "Length mismatch");
Expand Down Expand Up @@ -317,7 +320,7 @@ contract USDTieredSTO is USDTieredSTOStorage, STO {
* @notice Function to set allowBeneficialInvestments (allow beneficiary to be different to funder)
* @param _allowBeneficialInvestments Boolean to allow or disallow beneficial investments
*/
function changeAllowBeneficialInvestments(bool _allowBeneficialInvestments) public {
function changeAllowBeneficialInvestments(bool _allowBeneficialInvestments) external {
_onlySecurityTokenOwner();
require(_allowBeneficialInvestments != allowBeneficialInvestments, "Value unchanged");
allowBeneficialInvestments = _allowBeneficialInvestments;
Expand Down
3 changes: 2 additions & 1 deletion test/p_usd_tiered_sto.js
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,8 @@ contract("USDTieredSTO", async (accounts) => {
await I_USDTieredSTO_Array[stoId].buyWithUSD(NONACCREDITED1, investment_DAI, I_DaiToken.address, { from: NONACCREDITED1 });

// Change Stable coin address
await I_USDTieredSTO_Array[stoId].modifyAddresses(WALLET, TREASURYWALLET, [I_PolyToken.address], { from: ISSUER });
let I_DaiToken2 = await PolyTokenFaucet.new();
await I_USDTieredSTO_Array[stoId].modifyAddresses(WALLET, TREASURYWALLET, [I_DaiToken2.address], { from: ISSUER });

// NONACCREDITED DAI
await catchRevert(I_USDTieredSTO_Array[stoId].buyWithUSD(NONACCREDITED1, investment_DAI, I_DaiToken.address, { from: NONACCREDITED1 }));
Expand Down