HasNoEther.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict';
  2. import expectThrow from './helpers/expectThrow';
  3. import toPromise from './helpers/toPromise';
  4. const HasNoEther = artifacts.require('../contracts/lifecycle/HasNoEther.sol');
  5. const HasNoEtherTest = artifacts.require('../helpers/HasNoEtherTest.sol');
  6. const ForceEther = artifacts.require('../helpers/ForceEther.sol');
  7. contract('HasNoEther', function(accounts) {
  8. const amount = web3.toWei('1', 'ether');
  9. it('should be constructorable', async function() {
  10. let hasNoEther = await HasNoEtherTest.new();
  11. });
  12. it('should not accept ether in constructor', async function() {
  13. await expectThrow(HasNoEtherTest.new({value: amount}));
  14. });
  15. it('should not accept ether', async function() {
  16. let hasNoEther = await HasNoEtherTest.new();
  17. await expectThrow(
  18. toPromise(web3.eth.sendTransaction)({
  19. from: accounts[1],
  20. to: hasNoEther.address,
  21. value: amount,
  22. }),
  23. );
  24. });
  25. it('should allow owner to reclaim ether', async function() {
  26. // Create contract
  27. let hasNoEther = await HasNoEtherTest.new();
  28. const startBalance = await web3.eth.getBalance(hasNoEther.address);
  29. assert.equal(startBalance, 0);
  30. // Force ether into it
  31. await ForceEther.new(hasNoEther.address, {value: amount});
  32. const forcedBalance = await web3.eth.getBalance(hasNoEther.address);
  33. assert.equal(forcedBalance, amount);
  34. // Reclaim
  35. const ownerStartBalance = await web3.eth.getBalance(accounts[0]);
  36. await hasNoEther.reclaimEther();
  37. const ownerFinalBalance = await web3.eth.getBalance(accounts[0]);
  38. const finalBalance = await web3.eth.getBalance(hasNoEther.address);
  39. assert.equal(finalBalance, 0);
  40. assert.isAbove(ownerFinalBalance, ownerStartBalance);
  41. });
  42. it('should allow only owner to reclaim ether', async function() {
  43. // Create contract
  44. let hasNoEther = await HasNoEtherTest.new({from: accounts[0]});
  45. // Force ether into it
  46. await ForceEther.new(hasNoEther.address, {value: amount});
  47. const forcedBalance = await web3.eth.getBalance(hasNoEther.address);
  48. assert.equal(forcedBalance, amount);
  49. // Reclaim
  50. await expectThrow(hasNoEther.reclaimEther({from: accounts[1]}));
  51. });
  52. });