Skip to content

Commit e1417c4

Browse files
maxsam4adamdossa
authored andcommitted
usdtsto optimization (#534)
* Initial optimization * Tests fixed * comments removed * Make some functions external
1 parent 5a02cae commit e1417c4

File tree

3 files changed

+34
-58
lines changed

3 files changed

+34
-58
lines changed

contracts/modules/STO/USDTieredSTO.sol

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ import "../../interfaces/IOracle.sol";
66
import "../../RegistryUpdater.sol";
77
import "../../libraries/DecimalMath.sol";
88
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
9-
import "openzeppelin-solidity/contracts/utils/ReentrancyGuard.sol";
109
import "../../storage/USDTieredSTOStorage.sol";
1110

1211
/**
1312
* @title STO module for standard capped crowdsale
1413
*/
15-
contract USDTieredSTO is USDTieredSTOStorage, STO, ReentrancyGuard {
14+
contract USDTieredSTO is USDTieredSTOStorage, STO {
1615
using SafeMath for uint256;
1716

18-
string public constant POLY_ORACLE = "PolyUsdOracle";
19-
string public constant ETH_ORACLE = "EthUsdOracle";
17+
string internal constant POLY_ORACLE = "PolyUsdOracle";
18+
string internal constant ETH_ORACLE = "EthUsdOracle";
2019

2120
////////////
2221
// Events //
@@ -200,10 +199,12 @@ contract USDTieredSTO is USDTieredSTOStorage, STO, ReentrancyGuard {
200199
)
201200
internal
202201
{
203-
require(_tokensPerTierTotal.length > 0, "No tiers provided");
204202
require(
205-
_ratePerTier.length == _tokensPerTierTotal.length && _ratePerTierDiscountPoly.length == _tokensPerTierTotal.length && _tokensPerTierDiscountPoly.length == _tokensPerTierTotal.length,
206-
"Tier data length mismatch"
203+
_tokensPerTierTotal.length > 0 &&
204+
_ratePerTier.length == _tokensPerTierTotal.length &&
205+
_ratePerTierDiscountPoly.length == _tokensPerTierTotal.length &&
206+
_tokensPerTierDiscountPoly.length == _tokensPerTierTotal.length,
207+
"Invalid Input"
207208
);
208209
delete tiers;
209210
for (uint256 i = 0; i < _ratePerTier.length; i++) {
@@ -253,7 +254,7 @@ contract USDTieredSTO is USDTieredSTOStorage, STO, ReentrancyGuard {
253254
* @notice Reserve address must be whitelisted to successfully finalize
254255
*/
255256
function finalize() public onlyOwner {
256-
require(!isFinalized, "STO is already finalized");
257+
require(!isFinalized, "STO already finalized");
257258
isFinalized = true;
258259
uint256 tempReturned;
259260
uint256 tempSold;
@@ -266,7 +267,7 @@ contract USDTieredSTO is USDTieredSTOStorage, STO, ReentrancyGuard {
266267
tiers[i].mintedTotal = tiers[i].tokenTotal;
267268
}
268269
}
269-
require(ISecurityToken(securityToken).mint(reserveWallet, tempReturned), "Error in minting");
270+
require(ISecurityToken(securityToken).mint(reserveWallet, tempReturned), "Minting Failed");
270271
emit ReserveTokenMint(msg.sender, reserveWallet, tempReturned, currentTier);
271272
finalAmountReturned = tempReturned;
272273
totalTokensSold = tempSold;
@@ -437,7 +438,6 @@ contract USDTieredSTO is USDTieredSTOStorage, STO, ReentrancyGuard {
437438
FundRaiseType _fundRaiseType
438439
)
439440
internal
440-
nonReentrant
441441
whenNotPaused
442442
returns(uint256 spentUSD, uint256 spentValue)
443443
{
@@ -485,10 +485,10 @@ contract USDTieredSTO is USDTieredSTOStorage, STO, ReentrancyGuard {
485485
returns(uint256 netInvestedUSD)
486486
{
487487
require(isOpen(), "STO not open");
488-
require(_investmentValue > 0, "No funds were sent");
488+
require(_investmentValue > 0, "No funds sent");
489489

490490
// Check for minimum investment
491-
require(investedUSD.add(investorInvestedUSD[_beneficiary]) >= minimumInvestmentUSD, "Total investment < minimumInvestmentUSD");
491+
require(investedUSD.add(investorInvestedUSD[_beneficiary]) >= minimumInvestmentUSD, "Investment < min");
492492
netInvestedUSD = investedUSD;
493493
// Check for non-accredited cap
494494
if (investors[_beneficiary].accredited == uint8(0)) {
@@ -565,7 +565,7 @@ contract USDTieredSTO is USDTieredSTOStorage, STO, ReentrancyGuard {
565565
purchasedTokens = maximumTokens;
566566
}
567567
if (purchasedTokens > 0) {
568-
require(ISecurityToken(securityToken).mint(_beneficiary, purchasedTokens), "Error in minting");
568+
require(ISecurityToken(securityToken).mint(_beneficiary, purchasedTokens), "Mint failed");
569569
emit TokenPurchase(msg.sender, _beneficiary, purchasedTokens, spentUSD, _tierPrice, _tier);
570570
}
571571
}
@@ -579,15 +579,8 @@ contract USDTieredSTO is USDTieredSTOStorage, STO, ReentrancyGuard {
579579
* @return bool Whether the STO is accepting investments
580580
*/
581581
function isOpen() public view returns(bool) {
582-
if (isFinalized)
583-
return false;
584-
/*solium-disable-next-line security/no-block-members*/
585-
if (now < startTime)
586-
return false;
587-
/*solium-disable-next-line security/no-block-members*/
588-
if (now >= endTime)
589-
return false;
590-
if (capReached())
582+
/*solium-disable-next-line security/no-block-members*/
583+
if (isFinalized || now < startTime || now >= endTime || capReached())
591584
return false;
592585
return true;
593586
}
@@ -613,9 +606,7 @@ contract USDTieredSTO is USDTieredSTOStorage, STO, ReentrancyGuard {
613606
} else if (_fundRaiseType == FundRaiseType.POLY) {
614607
return IOracle(_getOracle(bytes32("POLY"), bytes32("USD"))).getPrice();
615608
} else if (_fundRaiseType == FundRaiseType.SC) {
616-
return 1 * 10**18;
617-
} else {
618-
revert("Incorrect funding");
609+
return 10**18;
619610
}
620611
}
621612

@@ -626,8 +617,7 @@ contract USDTieredSTO is USDTieredSTOStorage, STO, ReentrancyGuard {
626617
* @return uint256 Value in USD
627618
*/
628619
function convertToUSD(FundRaiseType _fundRaiseType, uint256 _amount) public view returns(uint256) {
629-
uint256 rate = getRate(_fundRaiseType);
630-
return DecimalMath.mul(_amount, rate);
620+
return DecimalMath.mul(_amount, getRate(_fundRaiseType));
631621
}
632622

633623
/**
@@ -637,8 +627,7 @@ contract USDTieredSTO is USDTieredSTOStorage, STO, ReentrancyGuard {
637627
* @return uint256 Value in ETH or POLY
638628
*/
639629
function convertFromUSD(FundRaiseType _fundRaiseType, uint256 _amount) public view returns(uint256) {
640-
uint256 rate = getRate(_fundRaiseType);
641-
return DecimalMath.div(_amount, rate);
630+
return DecimalMath.div(_amount, getRate(_fundRaiseType));
642631
}
643632

644633
/**
@@ -648,42 +637,36 @@ contract USDTieredSTO is USDTieredSTOStorage, STO, ReentrancyGuard {
648637
function getTokensSold() public view returns (uint256) {
649638
if (isFinalized)
650639
return totalTokensSold;
651-
else
652-
return getTokensMinted();
640+
return getTokensMinted();
653641
}
654642

655643
/**
656644
* @notice Return the total no. of tokens minted
657645
* @return uint256 Total number of tokens minted
658646
*/
659-
function getTokensMinted() public view returns (uint256) {
660-
uint256 tokensMinted;
647+
function getTokensMinted() public view returns (uint256 tokensMinted) {
661648
for (uint256 i = 0; i < tiers.length; i++) {
662649
tokensMinted = tokensMinted.add(tiers[i].mintedTotal);
663650
}
664-
return tokensMinted;
665651
}
666652

667653
/**
668654
* @notice Return the total no. of tokens sold for the given fund raise type
669655
* param _fundRaiseType The fund raising currency (e.g. ETH, POLY, SC) to calculate sold tokens for
670656
* @return uint256 Total number of tokens sold for ETH
671657
*/
672-
function getTokensSoldFor(FundRaiseType _fundRaiseType) public view returns (uint256) {
673-
uint256 tokensSold;
658+
function getTokensSoldFor(FundRaiseType _fundRaiseType) external view returns (uint256 tokensSold) {
674659
for (uint256 i = 0; i < tiers.length; i++) {
675660
tokensSold = tokensSold.add(tiers[i].minted[uint8(_fundRaiseType)]);
676661
}
677-
return tokensSold;
678662
}
679663

680664
/**
681665
* @notice Return array of minted tokens in each fund raise type for given tier
682666
* param _tier The tier to return minted tokens for
683667
* @return uint256[] array of minted tokens in each fund raise type
684668
*/
685-
function getTokensMintedByTier(uint256 _tier) public view returns(uint256[] memory) {
686-
require(_tier < tiers.length, "Invalid tier");
669+
function getTokensMintedByTier(uint256 _tier) external view returns(uint256[] memory) {
687670
uint256[] memory tokensMinted = new uint256[](3);
688671
tokensMinted[0] = tiers[_tier].minted[uint8(FundRaiseType.ETH)];
689672
tokensMinted[1] = tiers[_tier].minted[uint8(FundRaiseType.POLY)];
@@ -696,8 +679,7 @@ contract USDTieredSTO is USDTieredSTOStorage, STO, ReentrancyGuard {
696679
* param _tier The tier to calculate sold tokens for
697680
* @return uint256 Total number of tokens sold in the tier
698681
*/
699-
function getTokensSoldByTier(uint256 _tier) public view returns (uint256) {
700-
require(_tier < tiers.length, "Incorrect tier");
682+
function getTokensSoldByTier(uint256 _tier) external view returns (uint256) {
701683
uint256 tokensSold;
702684
tokensSold = tokensSold.add(tiers[_tier].minted[uint8(FundRaiseType.ETH)]);
703685
tokensSold = tokensSold.add(tiers[_tier].minted[uint8(FundRaiseType.POLY)]);
@@ -709,23 +691,22 @@ contract USDTieredSTO is USDTieredSTOStorage, STO, ReentrancyGuard {
709691
* @notice Return the total no. of tiers
710692
* @return uint256 Total number of tiers
711693
*/
712-
function getNumberOfTiers() public view returns (uint256) {
694+
function getNumberOfTiers() external view returns (uint256) {
713695
return tiers.length;
714696
}
715697

716698
/**
717699
* @notice Return the usd tokens accepted by the STO
718700
* @return address[] usd tokens
719701
*/
720-
function getUsdTokens() public view returns (address[] memory) {
702+
function getUsdTokens() external view returns (address[] memory) {
721703
return usdTokens;
722704
}
723705

724706
/**
725707
* @notice Return the permissions flag that are associated with STO
726708
*/
727-
function getPermissions() public view returns(bytes32[] memory) {
728-
bytes32[] memory allPermissions = new bytes32[](0);
709+
function getPermissions() public view returns(bytes32[] memory allPermissions) {
729710
return allPermissions;
730711
}
731712

@@ -741,7 +722,7 @@ contract USDTieredSTO is USDTieredSTOStorage, STO, ReentrancyGuard {
741722
* @return Amount of tokens sold.
742723
* @return Array of bools to show if funding is allowed in ETH, POLY, SC respectively
743724
*/
744-
function getSTODetails() public view returns(uint256, uint256, uint256, uint256[] memory, uint256[] memory, uint256, uint256, uint256, bool[] memory) {
725+
function getSTODetails() external view returns(uint256, uint256, uint256, uint256[] memory, uint256[] memory, uint256, uint256, uint256, bool[] memory) {
745726
uint256[] memory cap = new uint256[](tiers.length);
746727
uint256[] memory rate = new uint256[](tiers.length);
747728
for(uint256 i = 0; i < tiers.length; i++) {

contracts/storage/USDTieredSTOStorage.sol

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,17 @@ contract USDTieredSTOStorage {
3838

3939
mapping(bytes32 => mapping(bytes32 => string)) oracleKeys;
4040

41-
IERC20 public usdToken;
42-
4341
// Determine whether users can invest on behalf of a beneficiary
44-
bool public allowBeneficialInvestments = false;
42+
bool public allowBeneficialInvestments;
4543

4644
// Whether or not the STO has been finalized
4745
bool public isFinalized;
4846

49-
// Address where ETH, POLY & DAI funds are delivered
50-
address payable public wallet;
51-
5247
// Address of issuer reserve wallet for unsold tokens
5348
address public reserveWallet;
5449

5550
// List of stable coin addresses
56-
address[] public usdTokens;
51+
address[] internal usdTokens;
5752

5853
// Current tier
5954
uint256 public currentTier;
@@ -74,7 +69,7 @@ contract USDTieredSTOStorage {
7469
mapping (address => Investor) public investors;
7570

7671
// List of active stable coin addresses
77-
mapping (address => bool) public usdTokenEnabled;
72+
mapping (address => bool) internal usdTokenEnabled;
7873

7974
// List of all addresses that have been added as accredited or non-accredited without
8075
// the default limit

test/p_usd_tiered_sto.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ contract("USDTieredSTO", async (accounts) => {
378378
_reserveWallet[stoId],
379379
"Incorrect _reserveWallet in config"
380380
);
381-
assert.equal(await I_USDTieredSTO_Array[stoId].usdTokens.call(0), _usdToken[stoId][0], "Incorrect _usdToken in config");
381+
assert.equal((await I_USDTieredSTO_Array[stoId].getUsdTokens())[0], _usdToken[stoId][0], "Incorrect _usdToken in config");
382382
assert.equal(
383383
await I_USDTieredSTO_Array[stoId].getNumberOfTiers(),
384384
_tokensPerTierTotal[stoId].length,
@@ -567,7 +567,7 @@ contract("USDTieredSTO", async (accounts) => {
567567
_reserveWallet[stoId],
568568
"Incorrect _reserveWallet in config"
569569
);
570-
assert.equal(await I_USDTieredSTO_Array[stoId].usdTokens.call(0), _usdToken[stoId][0], "Incorrect _usdToken in config");
570+
assert.equal((await I_USDTieredSTO_Array[stoId].getUsdTokens())[0], _usdToken[stoId][0], "Incorrect _usdToken in config");
571571
assert.equal(
572572
await I_USDTieredSTO_Array[stoId].getNumberOfTiers(),
573573
_tokensPerTierTotal[stoId].length,
@@ -965,7 +965,7 @@ contract("USDTieredSTO", async (accounts) => {
965965
"0x0000000000000000000003000000000000000000",
966966
"STO Configuration doesn't set as expected"
967967
);
968-
assert.equal((await I_USDTieredSTO_Array[stoId].usdTokens.call(0)), accounts[3], "STO Configuration doesn't set as expected");
968+
assert.equal((await I_USDTieredSTO_Array[stoId].getUsdTokens())[0], accounts[3], "STO Configuration doesn't set as expected");
969969
});
970970

971971
it("Should fail to change config after endTime", async () => {
@@ -3868,7 +3868,7 @@ contract("USDTieredSTO", async (accounts) => {
38683868
let init_WalletETHBal = new BN(await web3.eth.getBalance(WALLET));
38693869
let init_WalletPOLYBal = await I_PolyToken.balanceOf(WALLET);
38703870

3871-
let tokensToMint = (await I_USDTieredSTO_Array[stoId].buyWithPOLY.call(ACCREDITED1, investment_POLY))[2];
3871+
let tokensToMint = (await I_USDTieredSTO_Array[stoId].buyWithPOLY.call(ACCREDITED1, investment_POLY, {from: ACCREDITED1}))[2];
38723872

38733873
// Buy With POLY
38743874
let tx2 = await I_USDTieredSTO_Array[stoId].buyWithPOLY(ACCREDITED1, investment_POLY, {

0 commit comments

Comments
 (0)