Address.test.js 4.0 KB

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