HasNoEther.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. let forceEther = await ForceEther.new({value: amount});
  32. await forceEther.destroyAndSend(hasNoEther.address);
  33. const forcedBalance = await web3.eth.getBalance(hasNoEther.address);
  34. assert.equal(forcedBalance, amount);
  35. // Reclaim
  36. const ownerStartBalance = await web3.eth.getBalance(accounts[0]);
  37. await hasNoEther.reclaimEther();
  38. const ownerFinalBalance = await web3.eth.getBalance(accounts[0]);
  39. const finalBalance = await web3.eth.getBalance(hasNoEther.address);
  40. assert.equal(finalBalance, 0);
  41. assert.isAbove(ownerFinalBalance, ownerStartBalance);
  42. });
  43. it('should allow only owner to reclaim ether', async function() {
  44. // Create contract
  45. let hasNoEther = await HasNoEtherTest.new({from: accounts[0]});
  46. // Force ether into it
  47. let forceEther = await ForceEther.new({value: amount});
  48. await forceEther.destroyAndSend(hasNoEther.address);
  49. const forcedBalance = await web3.eth.getBalance(hasNoEther.address);
  50. assert.equal(forcedBalance, amount);
  51. // Reclaim
  52. await expectThrow(hasNoEther.reclaimEther({from: accounts[1]}));
  53. });
  54. });