HasNoEther.test.js 2.2 KB

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