HasNoEther.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import expectThrow from './helpers/expectThrow';
  2. import toPromise from './helpers/toPromise';
  3. const HasNoEther = artifacts.require('../contracts/lifecycle/HasNoEther.sol');
  4. const HasNoEtherTest = artifacts.require('../helpers/HasNoEtherTest.sol');
  5. const ForceEther = artifacts.require('../helpers/ForceEther.sol');
  6. contract('HasNoEther', function (accounts) {
  7. const amount = web3.toWei('1', 'ether');
  8. it('should be constructorable', async function () {
  9. let hasNoEther = await HasNoEtherTest.new();
  10. });
  11. it('should not accept ether in constructor', async function () {
  12. await expectThrow(HasNoEtherTest.new({ value: amount }));
  13. });
  14. it('should not accept ether', async function () {
  15. let hasNoEther = await HasNoEtherTest.new();
  16. await expectThrow(
  17. toPromise(web3.eth.sendTransaction)({
  18. from: accounts[1],
  19. to: hasNoEther.address,
  20. value: amount,
  21. }),
  22. );
  23. });
  24. it('should allow owner to reclaim ether', async function () {
  25. // Create contract
  26. let hasNoEther = await HasNoEtherTest.new();
  27. const startBalance = await web3.eth.getBalance(hasNoEther.address);
  28. assert.equal(startBalance, 0);
  29. // Force ether into it
  30. let forceEther = await ForceEther.new({ value: amount });
  31. await forceEther.destroyAndSend(hasNoEther.address);
  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. let forceEther = await ForceEther.new({ value: amount });
  47. await forceEther.destroyAndSend(hasNoEther.address);
  48. const forcedBalance = await web3.eth.getBalance(hasNoEther.address);
  49. assert.equal(forcedBalance, amount);
  50. // Reclaim
  51. await expectThrow(hasNoEther.reclaimEther({ from: accounts[1] }));
  52. });
  53. });