mirror of openzeppelin-contracts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openzeppelin-contracts/test/crowdsale/RefundableCrowdsale.test.js

108 lines
3.7 KiB

const { balance, BN, ether, expectRevert, time } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const RefundableCrowdsaleImpl = artifacts.require('RefundableCrowdsaleImpl');
Crowdsale refactor and add new models (#744) * Basic idea * Fine tuning idea * Add comments / tidy up Crowdsale base class * fixed TimedCrowdsale constructor * added simple crowdsale test * added HODL directory under home to store unused contracts. ugly hack to solve Crowdsale selection in tests, better way? * Capped no longer inherits from Timed, added capReached() method (replacing hasEnded()) * added SafeMath in TimedCrowdsale for safety, CHECK whether it is inherited from Crowdsale * several fixes related to separating Capped from Timed. functions renamed, mocks changed. Capped tests passing * added TimedCrowdsaleImpl.sol, TimedCrowdsale tests, passed * added Whitelisted implementation and test, passed. * removed unnecessary super constructor call in WhitelistedCrowdsale, removed unused dependencies in tests * renamed UserCappedCrowdsale to IndividuallyCappedCrowdsale, implemented IndividuallyCappedCrowdsaleImpl.sol and corresponding tests, passed. * homogeneized use of using SafeMath for uint256 across validation crowdsales. checked that it IS indeed inherited, but leaving it there as per Frans suggestion. * adding questions.md where I track questions, bugs and progress * modified VariablePriceCrowdsale, added Impl. * finished VariablePrice, fixed sign, added test, passing. * changed VariablePrice to IncreasingPrice, added corresponding require() * MintedCrowdsale done, mock implemented, test passing * PremintedCrowdsale done, mocks, tests passing * checked FinalizableCrowdsale * PostDeliveryCrowdsale done, mock, tests passing. * RefundableCrowdsale done. Detached Vault. modified mock and test, passing * renamed crowdsale-refactor to crowdsale in contracts and test * deleted HODL old contracts * polished variable names in tests * fixed typos and removed comments in tests * Renamed 'crowdsale-refactor' to 'crowdsale' in all imports * Fix minor param naming issues in Crowdsale functions and added documentation to Crowdsale.sol * Added documentation to Crowdsale extensions * removed residual comments and progress tracking files * added docs for validation crowdsales * Made user promises in PostDeliveryCrowdsale public so that users can query their promised token balance. * added docs for distribution crowdsales * renamed PremintedCrowdsale to AllowanceCrowdsale * added allowance check function and corresponding test. fixed filename in AllowanceCrowdsale mock. * spilt Crowdsale _postValidatePurchase in _postValidatePurchase and _updatePurchasingState. changed IndividuallyCappedCrowdsale accordingly. * polished tests for linter, salve Travis * polished IncreasingPriceCrowdsale.sol for linter. * renamed and polished for linter WhitelistedCrowdsale test. * fixed indentation in IncreasingPriceCrowdsaleImpl.sol for linter * fixed ignoring token.mint return value in MintedCrowdsale.sol * expanded docs throughout, fixed minor issues * extended test coverage for IndividuallyCappedCrowdsale * Extended WhitelistedCrwodsale test coverage * roll back decoupling of RefundVault in RefundableCrowdsale * moved cap exceedance checks in Capped and IndividuallyCapped crowdsales to _preValidatePurchase to save gas * revert name change, IndividuallyCapped to UserCapped * extended docs. * added crowd whitelisting with tests * added group capping, plus tests * added modifiers in TimedCrowdsale and WhitelistedCrowdsale * polished tests for linter * moved check of whitelisted to modifier, mainly for testing coverage * fixed minor ordering/polishingafter review * modified TimedCrowdsale modifier/constructor ordering * unchanged truffle-config.js * changed indentation of visibility modifier in mocks * changed naming of modifier and function to use Open/Closed for TimedCrowdsale * changed ordering of constructor calls in SampleCrowdsale * changed startTime and endTime to openingTime and closingTime throughout * fixed exceeding line lenght for linter * renamed _emitTokens to _deliverTokens * renamed addCrowdToWhitelist to addManyToWhitelist * renamed UserCappedCrowdsale to IndividuallyCappedCrowdsale
7 years ago
const SimpleToken = artifacts.require('SimpleToken');
contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, other]) {
const rate = new BN(1);
const goal = ether('50');
const lessThanGoal = ether('45');
const tokenSupply = new BN('10').pow(new BN('22'));
before(async function () {
// Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
await time.advanceBlock();
});
beforeEach(async function () {
this.openingTime = (await time.latest()).add(time.duration.weeks(1));
this.closingTime = this.openingTime.add(time.duration.weeks(1));
this.afterClosingTime = this.closingTime.add(time.duration.seconds(1));
this.preWalletBalance = await balance.current(wallet);
Crowdsale refactor and add new models (#744) * Basic idea * Fine tuning idea * Add comments / tidy up Crowdsale base class * fixed TimedCrowdsale constructor * added simple crowdsale test * added HODL directory under home to store unused contracts. ugly hack to solve Crowdsale selection in tests, better way? * Capped no longer inherits from Timed, added capReached() method (replacing hasEnded()) * added SafeMath in TimedCrowdsale for safety, CHECK whether it is inherited from Crowdsale * several fixes related to separating Capped from Timed. functions renamed, mocks changed. Capped tests passing * added TimedCrowdsaleImpl.sol, TimedCrowdsale tests, passed * added Whitelisted implementation and test, passed. * removed unnecessary super constructor call in WhitelistedCrowdsale, removed unused dependencies in tests * renamed UserCappedCrowdsale to IndividuallyCappedCrowdsale, implemented IndividuallyCappedCrowdsaleImpl.sol and corresponding tests, passed. * homogeneized use of using SafeMath for uint256 across validation crowdsales. checked that it IS indeed inherited, but leaving it there as per Frans suggestion. * adding questions.md where I track questions, bugs and progress * modified VariablePriceCrowdsale, added Impl. * finished VariablePrice, fixed sign, added test, passing. * changed VariablePrice to IncreasingPrice, added corresponding require() * MintedCrowdsale done, mock implemented, test passing * PremintedCrowdsale done, mocks, tests passing * checked FinalizableCrowdsale * PostDeliveryCrowdsale done, mock, tests passing. * RefundableCrowdsale done. Detached Vault. modified mock and test, passing * renamed crowdsale-refactor to crowdsale in contracts and test * deleted HODL old contracts * polished variable names in tests * fixed typos and removed comments in tests * Renamed 'crowdsale-refactor' to 'crowdsale' in all imports * Fix minor param naming issues in Crowdsale functions and added documentation to Crowdsale.sol * Added documentation to Crowdsale extensions * removed residual comments and progress tracking files * added docs for validation crowdsales * Made user promises in PostDeliveryCrowdsale public so that users can query their promised token balance. * added docs for distribution crowdsales * renamed PremintedCrowdsale to AllowanceCrowdsale * added allowance check function and corresponding test. fixed filename in AllowanceCrowdsale mock. * spilt Crowdsale _postValidatePurchase in _postValidatePurchase and _updatePurchasingState. changed IndividuallyCappedCrowdsale accordingly. * polished tests for linter, salve Travis * polished IncreasingPriceCrowdsale.sol for linter. * renamed and polished for linter WhitelistedCrowdsale test. * fixed indentation in IncreasingPriceCrowdsaleImpl.sol for linter * fixed ignoring token.mint return value in MintedCrowdsale.sol * expanded docs throughout, fixed minor issues * extended test coverage for IndividuallyCappedCrowdsale * Extended WhitelistedCrwodsale test coverage * roll back decoupling of RefundVault in RefundableCrowdsale * moved cap exceedance checks in Capped and IndividuallyCapped crowdsales to _preValidatePurchase to save gas * revert name change, IndividuallyCapped to UserCapped * extended docs. * added crowd whitelisting with tests * added group capping, plus tests * added modifiers in TimedCrowdsale and WhitelistedCrowdsale * polished tests for linter * moved check of whitelisted to modifier, mainly for testing coverage * fixed minor ordering/polishingafter review * modified TimedCrowdsale modifier/constructor ordering * unchanged truffle-config.js * changed indentation of visibility modifier in mocks * changed naming of modifier and function to use Open/Closed for TimedCrowdsale * changed ordering of constructor calls in SampleCrowdsale * changed startTime and endTime to openingTime and closingTime throughout * fixed exceeding line lenght for linter * renamed _emitTokens to _deliverTokens * renamed addCrowdToWhitelist to addManyToWhitelist * renamed UserCappedCrowdsale to IndividuallyCappedCrowdsale
7 years ago
this.token = await SimpleToken.new();
});
it('rejects a goal of zero', async function () {
await expectRevert(
RefundableCrowdsaleImpl.new(this.openingTime, this.closingTime, rate, wallet, this.token.address, 0),
'RefundableCrowdsale: goal is 0'
);
});
context('with crowdsale', function () {
beforeEach(async function () {
this.crowdsale = await RefundableCrowdsaleImpl.new(
RBAC and Ownable migration towards Roles (#1291) * Role tests (#1228) * Moved RBAC tests to access. * Added Roles.addMany and tests. * Fixed linter error. * Now using uint256 indexes. * Removed RBAC tokens (#1229) * Deleted RBACCappedTokenMock. * Removed RBACMintableToken. * Removed RBACMintableToken from the MintedCrowdsale tests. * Roles can now be transfered. (#1235) * Roles can now be transfered. * Now explicitly checking support for the null address. * Now rejecting transfer to a role-haver. * Added renounce, roles can no longer be transfered to 0. * Fixed linter errors. * Fixed a Roles test. * True Ownership (#1247) * Added barebones Secondary. * Added transferPrimary * Escrow is now Secondary instead of Ownable. * Now reverting on transfers to 0. * The Secondary's primary is now private. * MintableToken using Roles (#1236) * Minor test style improvements (#1219) * Changed .eq to .equal * Changed equal(bool) to .to.be.bool * Changed be.bool to equal(bool), disallowed unused expressions. * Add ERC165Query library (#1086) * Add ERC165Query library * Address PR Comments * Add tests and mocks from #1024 and refactor code slightly * Fix javascript and solidity linting errors * Split supportsInterface into three methods as discussed in #1086 * Change InterfaceId_ERC165 comment to match style in the rest of the repo * Fix max-len lint issue on ERC165Checker.sol * Conditionally ignore the asserts during solidity-coverage test * Switch to abi.encodeWithSelector and add test for account addresses * Switch to supportsInterfaces API as suggested by @frangio * Adding ERC165InterfacesSupported.sol * Fix style issues * Add test for supportsInterfaces returning false * Add ERC165Checker.sol newline * feat: fix coverage implementation * fix: solidity linting error * fix: revert to using boolean tests instead of require statements * fix: make supportsERC165Interface private again * rename SupportsInterfaceWithLookupMock to avoid name clashing * Added mint and burn tests for zero amounts. (#1230) * Changed .eq to .equal. (#1231) * ERC721 pausable token (#1154) * ERC721 pausable token * Reuse of ERC721 Basic behavior for Pausable, split view checks in paused state & style fixes * [~] paused token behavior * Add some detail to releasing steps (#1190) * add note about pulling upstream changes to release branch * add comment about upstream changes in merging section * Increase test coverage (#1237) * Fixed a SplitPayment test * Deleted unnecessary function. * Improved PostDeliveryCrowdsale tests. * Improved RefundableCrowdsale tests. * Improved MintedCrowdsale tests. * Improved IncreasingPriceCrowdsale tests. * Fixed a CappedCrowdsale test. * Improved TimedCrowdsale tests. * Improved descriptions of added tests. * ci: trigger docs update on tag (#1186) * MintableToken now uses Roles. * Fixed FinalizableCrowdsale test. * Roles can now be transfered. * Fixed tests related to MintableToken. * Removed Roles.check. * Renamed transferMintPermission. * Moved MinterRole * Fixed RBAC. * Adressed review comments. * Addressed review comments * Fixed linter errors. * Added Events tests of Pausable contract (#1207) * Fixed roles tests. * Rename events to past-tense (#1181) * fix: refactor sign.js and related tests (#1243) * fix: refactor sign.js and related tests * fix: remove unused dep * fix: update package.json correctly * Added "_" sufix to internal variables (#1171) * Added PublicRole test. * Fixed crowdsale tests. * Rename ERC interfaces to I prefix (#1252) * rename ERC20 to IERC20 * move ERC20.sol to IERC20.sol * rename StandardToken to ERC20 * rename StandardTokenMock to ERC20Mock * move StandardToken.sol to ERC20.sol, likewise test and mock files * rename MintableToken to ERC20Mintable * move MintableToken.sol to ERC20Mintable.sol, likewise test and mock files * rename BurnableToken to ERC20Burnable * move BurnableToken.sol to ERC20Burnable.sol, likewise for related files * rename CappedToken to ERC20Capped * move CappedToken.sol to ERC20Capped.sol, likewise for related files * rename PausableToken to ERC20Pausable * move PausableToken.sol to ERC20Pausable.sol, likewise for related files * rename DetailedERC20 to ERC20Detailed * move DetailedERC20.sol to ERC20Detailed.sol, likewise for related files * rename ERC721 to IERC721, and likewise for other related interfaces * move ERC721.sol to IERC721.sol, likewise for other 721 interfaces * rename ERC721Token to ERC721 * move ERC721Token.sol to ERC721.sol, likewise for related files * rename ERC721BasicToken to ERC721Basic * move ERC721BasicToken.sol to ERC721Basic.sol, likewise for related files * rename ERC721PausableToken to ERC721Pausable * move ERC721PausableToken.sol to ERC721Pausable.sol * rename ERC165 to IERC165 * move ERC165.sol to IERC165.sol * amend comment that ERC20 is based on FirstBlood * fix comments mentioning IERC721Receiver * added explicit visibility (#1261) * Remove underscores from event parameters. (#1258) * Remove underscores from event parameters. Fixes #1175 * Add comment about ERC * Move contracts to subdirectories (#1253) * Move contracts to subdirectories Fixes #1177. This Change also removes the LimitBalance contract. * fix import * move MerkleProof to cryptography * Fix import * Remove HasNoEther, HasNoTokens, HasNoContracts, and NoOwner (#1254) * remove HasNoEther, HasNoTokens, HasNoContracts, and NoOwner * remove unused ERC223TokenMock * remove Contactable * remove TokenDestructible * remove DeprecatedERC721 * inline Destructible#destroy in Bounty * remove Destructible * Functions in interfaces changed to "external" (#1263) * Add a leading underscore to internal and private functions. (#1257) * Add a leading underscore to internal and private functions. Fixes #1176 * Remove super * update the ERC721 changes * add missing underscore after merge * Fix mock * Improve encapsulation on SignatureBouncer, Whitelist and RBAC example (#1265) * Improve encapsulation on Whitelist * remove only * update whitelisted crowdsale test * Improve encapsulation on SignatureBouncer * fix missing test * Improve encapsulation on RBAC example * Improve encapsulation on RBAC example * Remove extra visibility * Improve encapsulation on ERC20 Mintable * Improve encapsulation on Superuser * fix lint * add missing constant * Addressed review comments. * Fixed build error. * Improved Roles API. (#1280) * Improved Roles API. * fix linter error * Added PauserRole. (#1283) * Remove Claimable, DelayedClaimable, Heritable (#1274) * remove Claimable, DelayedClaimable, Heritable * remove SimpleSavingsWallet example which used Heritable (cherry picked from commit 0dc711732a297e70af63f23a9b52e4b3712eac40) * Role behavior tests (#1285) * Added role tests. * Added PauserRole tests to contracts that have that role. * Added MinterRole tests to contracts that have that role. * Fixed linter errors. * Migrate Ownable to Roles (#1287) * Added CapperRole. * RefundEscrow is now Secondary. * FinalizableCrowdsale is no longer Ownable. * Removed Whitelist and WhitelistedCrowdsale, redesign needed. * Fixed linter errors, disabled lbrace due to it being buggy. * Remove RBAC, SignatureBouncer refactor (#1289) * Added CapperRole. * RefundEscrow is now Secondary. * FinalizableCrowdsale is no longer Ownable. * Removed Whitelist and WhitelistedCrowdsale, redesign needed. * Fixed linter errors, disabled lbrace due to it being buggy. * Moved SignatureBouncer tests. * Deleted RBAC and Superuser. * Deleted rbac directory. * Updated readme. * SignatureBouncer now uses SignerRole, renamed bouncer to signer. * feat: implement ERC721Mintable and ERC721Burnable (#1276) * feat: implement ERC721Mintable and ERC721Burnable * fix: linting errors * fix: remove unused mintable mock for ERC721BasicMock * fix: add finishMinting tests * fix: catch MintFinished typo * inline ERC721Full behavior * undo pretty formatting * fix lint errors * rename canMint to onlyBeforeMintingFinished for consistency with ERC20Mintable * Fix the merge with the privatization branch * remove duplicate CapperRole test
6 years ago
this.openingTime, this.closingTime, rate, wallet, this.token.address, goal
);
await this.token.transfer(this.crowdsale.address, tokenSupply);
});
context('before opening time', function () {
it('denies refunds', async function () {
await expectRevert(this.crowdsale.claimRefund(investor),
'RefundableCrowdsale: not finalized'
);
});
});
context('after opening time', function () {
beforeEach(async function () {
await time.increaseTo(this.openingTime);
});
it('denies refunds', async function () {
await expectRevert(this.crowdsale.claimRefund(investor),
'RefundableCrowdsale: not finalized'
);
});
context('with unreached goal', function () {
beforeEach(async function () {
await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor });
});
context('after closing time and finalization', function () {
beforeEach(async function () {
await time.increaseTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: other });
});
it('refunds', async function () {
const balanceTracker = await balance.tracker(investor);
await this.crowdsale.claimRefund(investor, { gasPrice: 0 });
expect(await balanceTracker.delta()).to.be.bignumber.equal(lessThanGoal);
});
});
});
context('with reached goal', function () {
beforeEach(async function () {
await this.crowdsale.sendTransaction({ value: goal, from: investor });
});
context('after closing time and finalization', function () {
beforeEach(async function () {
await time.increaseTo(this.afterClosingTime);
await this.crowdsale.finalize({ from: other });
});
it('denies refunds', async function () {
await expectRevert(this.crowdsale.claimRefund(investor),
'RefundableCrowdsale: goal reached'
);
});
it('forwards funds to wallet', async function () {
const postWalletBalance = await balance.current(wallet);
expect(postWalletBalance.sub(this.preWalletBalance)).to.be.bignumber.equal(goal);
});
});
});
});
});
});