Address.test.js 4.1 KB

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