HasNoEther.test.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. const forcedBalance = await ethGetBalance(this.hasNoEther.address);
  33. forcedBalance.should.be.bignumber.equal(amount);
  34. // Reclaim
  35. const ownerStartBalance = await ethGetBalance(owner);
  36. await this.hasNoEther.reclaimEther({ from: owner });
  37. const ownerFinalBalance = await ethGetBalance(owner);
  38. const finalBalance = await ethGetBalance(this.hasNoEther.address);
  39. finalBalance.should.be.bignumber.equal(0);
  40. ownerFinalBalance.should.be.bignumber.gt(ownerStartBalance);
  41. });
  42. it('should allow only owner to reclaim ether', async function () {
  43. // Force ether into it
  44. const forceEther = await ForceEther.new({ value: amount });
  45. await forceEther.destroyAndSend(this.hasNoEther.address);
  46. const forcedBalance = await ethGetBalance(this.hasNoEther.address);
  47. forcedBalance.should.be.bignumber.equal(amount);
  48. // Reclaim
  49. await expectThrow(this.hasNoEther.reclaimEther({ from: anyone }));
  50. });
  51. });