Address.test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const { accounts, contract } = require('@openzeppelin/test-environment');
  2. const { balance, constants, 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. const ALL_ONES_ADDRESS = '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF';
  9. beforeEach(async function () {
  10. this.mock = await AddressImpl.new();
  11. });
  12. describe('isContract', function () {
  13. it('should return false for account address', async function () {
  14. expect(await this.mock.isContract(other)).to.equal(false);
  15. });
  16. it('should return true for contract address', async function () {
  17. const contract = await AddressImpl.new();
  18. expect(await this.mock.isContract(contract.address)).to.equal(true);
  19. });
  20. });
  21. describe('toPayable', function () {
  22. it('should return a payable address when the account is the zero address', async function () {
  23. expect(await this.mock.toPayable(constants.ZERO_ADDRESS)).to.equal(constants.ZERO_ADDRESS);
  24. });
  25. it('should return a payable address when the account is an arbitrary address', async function () {
  26. expect(await this.mock.toPayable(other)).to.equal(other);
  27. });
  28. it('should return a payable address when the account is the all ones address', async function () {
  29. expect(await this.mock.toPayable(ALL_ONES_ADDRESS)).to.equal(ALL_ONES_ADDRESS);
  30. });
  31. });
  32. describe('sendValue', function () {
  33. beforeEach(async function () {
  34. this.recipientTracker = await balance.tracker(recipient);
  35. });
  36. context('when sender contract has no funds', function () {
  37. it('sends 0 wei', async function () {
  38. await this.mock.sendValue(other, 0);
  39. expect(await this.recipientTracker.delta()).to.be.bignumber.equal('0');
  40. });
  41. it('reverts when sending non-zero amounts', async function () {
  42. await expectRevert(this.mock.sendValue(other, 1), 'Address: insufficient balance');
  43. });
  44. });
  45. context('when sender contract has funds', function () {
  46. const funds = ether('1');
  47. beforeEach(async function () {
  48. await send.ether(other, this.mock.address, funds);
  49. });
  50. it('sends 0 wei', async function () {
  51. await this.mock.sendValue(recipient, 0);
  52. expect(await this.recipientTracker.delta()).to.be.bignumber.equal('0');
  53. });
  54. it('sends non-zero amounts', async function () {
  55. await this.mock.sendValue(recipient, funds.subn(1));
  56. expect(await this.recipientTracker.delta()).to.be.bignumber.equal(funds.subn(1));
  57. });
  58. it('sends the whole balance', async function () {
  59. await this.mock.sendValue(recipient, funds);
  60. expect(await this.recipientTracker.delta()).to.be.bignumber.equal(funds);
  61. expect(await balance.current(this.mock.address)).to.be.bignumber.equal('0');
  62. });
  63. it('reverts when sending more than the balance', async function () {
  64. await expectRevert(this.mock.sendValue(recipient, funds.addn(1)), 'Address: insufficient balance');
  65. });
  66. context('with contract recipient', function () {
  67. beforeEach(async function () {
  68. this.contractRecipient = await EtherReceiver.new();
  69. });
  70. it('sends funds', async function () {
  71. const tracker = await balance.tracker(this.contractRecipient.address);
  72. await this.contractRecipient.setAcceptEther(true);
  73. await this.mock.sendValue(this.contractRecipient.address, funds);
  74. expect(await tracker.delta()).to.be.bignumber.equal(funds);
  75. });
  76. it('reverts on recipient revert', async function () {
  77. await this.contractRecipient.setAcceptEther(false);
  78. await expectRevert(
  79. this.mock.sendValue(this.contractRecipient.address, funds),
  80. 'Address: unable to send value, recipient may have reverted'
  81. );
  82. });
  83. });
  84. });
  85. });
  86. });