Address.test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const { accounts, contract } = require('@openzeppelin/test-environment');
  2. const { balance, ether, expectRevert, send } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const AddressImpl = contract.fromArtifact('AddressImpl');
  5. const EtherReceiver = contract.fromArtifact('EtherReceiverMock');
  6. describe('Address', function () {
  7. const [ recipient, other ] = accounts;
  8. beforeEach(async function () {
  9. this.mock = await AddressImpl.new();
  10. });
  11. describe('isContract', function () {
  12. it('should return false for account address', async function () {
  13. expect(await this.mock.isContract(other)).to.equal(false);
  14. });
  15. it('should return true for contract address', async function () {
  16. const contract = await AddressImpl.new();
  17. expect(await this.mock.isContract(contract.address)).to.equal(true);
  18. });
  19. });
  20. describe('sendValue', function () {
  21. beforeEach(async function () {
  22. this.recipientTracker = await balance.tracker(recipient);
  23. });
  24. context('when sender contract has no funds', function () {
  25. it('sends 0 wei', async function () {
  26. await this.mock.sendValue(other, 0);
  27. expect(await this.recipientTracker.delta()).to.be.bignumber.equal('0');
  28. });
  29. it('reverts when sending non-zero amounts', async function () {
  30. await expectRevert(this.mock.sendValue(other, 1), 'Address: insufficient balance');
  31. });
  32. });
  33. context('when sender contract has funds', function () {
  34. const funds = ether('1');
  35. beforeEach(async function () {
  36. await send.ether(other, this.mock.address, funds);
  37. });
  38. it('sends 0 wei', async function () {
  39. await this.mock.sendValue(recipient, 0);
  40. expect(await this.recipientTracker.delta()).to.be.bignumber.equal('0');
  41. });
  42. it('sends non-zero amounts', async function () {
  43. await this.mock.sendValue(recipient, funds.subn(1));
  44. expect(await this.recipientTracker.delta()).to.be.bignumber.equal(funds.subn(1));
  45. });
  46. it('sends the whole balance', async function () {
  47. await this.mock.sendValue(recipient, funds);
  48. expect(await this.recipientTracker.delta()).to.be.bignumber.equal(funds);
  49. expect(await balance.current(this.mock.address)).to.be.bignumber.equal('0');
  50. });
  51. it('reverts when sending more than the balance', async function () {
  52. await expectRevert(this.mock.sendValue(recipient, funds.addn(1)), 'Address: insufficient balance');
  53. });
  54. context('with contract recipient', function () {
  55. beforeEach(async function () {
  56. this.contractRecipient = await EtherReceiver.new();
  57. });
  58. it('sends funds', async function () {
  59. const tracker = await balance.tracker(this.contractRecipient.address);
  60. await this.contractRecipient.setAcceptEther(true);
  61. await this.mock.sendValue(this.contractRecipient.address, funds);
  62. expect(await tracker.delta()).to.be.bignumber.equal(funds);
  63. });
  64. it('reverts on recipient revert', async function () {
  65. await this.contractRecipient.setAcceptEther(false);
  66. await expectRevert(
  67. this.mock.sendValue(this.contractRecipient.address, funds),
  68. 'Address: unable to send value, recipient may have reverted'
  69. );
  70. });
  71. });
  72. });
  73. });
  74. });