HasNoEther.test.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const { expectThrow } = require('../helpers/expectThrow');
  2. const { ethSendTransaction, ethGetBalance } = require('../helpers/web3');
  3. const HasNoEtherTest = artifacts.require('HasNoEtherTest');
  4. const ForceEther = artifacts.require('ForceEther');
  5. const BigNumber = web3.BigNumber;
  6. require('chai')
  7. .use(require('chai-bignumber')(BigNumber))
  8. .should();
  9. contract('HasNoEther', function ([_, owner, anyone]) {
  10. const amount = web3.toWei('1', 'ether');
  11. beforeEach(async function () {
  12. this.hasNoEther = await HasNoEtherTest.new({ from: owner });
  13. });
  14. it('should not accept ether in constructor', async function () {
  15. await expectThrow(HasNoEtherTest.new({ value: amount }));
  16. });
  17. it('should not accept ether', async function () {
  18. await expectThrow(
  19. ethSendTransaction({
  20. from: owner,
  21. to: this.hasNoEther.address,
  22. value: amount,
  23. }),
  24. );
  25. });
  26. it('should allow owner to reclaim ether', async function () {
  27. const startBalance = await ethGetBalance(this.hasNoEther.address);
  28. startBalance.should.be.bignumber.equal(0);
  29. // Force ether into it
  30. const forceEther = await ForceEther.new({ value: amount });
  31. await forceEther.destroyAndSend(this.hasNoEther.address);
  32. (await ethGetBalance(this.hasNoEther.address)).should.be.bignumber.equal(amount);
  33. // Reclaim
  34. const ownerStartBalance = await ethGetBalance(owner);
  35. await this.hasNoEther.reclaimEther({ from: owner });
  36. const ownerFinalBalance = await ethGetBalance(owner);
  37. ownerFinalBalance.should.be.bignumber.gt(ownerStartBalance);
  38. (await ethGetBalance(this.hasNoEther.address)).should.be.bignumber.equal(0);
  39. });
  40. it('should allow only owner to reclaim ether', async function () {
  41. // Force ether into it
  42. const forceEther = await ForceEther.new({ value: amount });
  43. await forceEther.destroyAndSend(this.hasNoEther.address);
  44. (await ethGetBalance(this.hasNoEther.address)).should.be.bignumber.equal(amount);
  45. // Reclaim
  46. await expectThrow(this.hasNoEther.reclaimEther({ from: anyone }));
  47. });
  48. });