GSNRecipient.test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const { accounts, contract, web3 } = require('@openzeppelin/test-environment');
  2. const { balance, BN, constants, ether, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  3. const { ZERO_ADDRESS } = constants;
  4. const gsn = require('@openzeppelin/gsn-helpers');
  5. const { expect } = require('chai');
  6. const GSNRecipientMock = contract.fromArtifact('GSNRecipientMock');
  7. const ContextMockCaller = contract.fromArtifact('ContextMockCaller');
  8. const { shouldBehaveLikeRegularContext } = require('./Context.behavior');
  9. describe('GSNRecipient', function () {
  10. const [ payee, sender, newRelayHub ] = accounts;
  11. beforeEach(async function () {
  12. this.recipient = await GSNRecipientMock.new();
  13. });
  14. it('returns the compatible RelayHub version', async function () {
  15. expect(await this.recipient.relayHubVersion()).to.equal('1.0.0');
  16. });
  17. describe('get/set RelayHub', function () {
  18. const singletonRelayHub = '0xD216153c06E857cD7f72665E0aF1d7D82172F494';
  19. it('initially returns the singleton instance address', async function () {
  20. expect(await this.recipient.getHubAddr()).to.equal(singletonRelayHub);
  21. });
  22. it('can be upgraded to a new RelayHub', async function () {
  23. const { logs } = await this.recipient.upgradeRelayHub(newRelayHub);
  24. expectEvent.inLogs(logs, 'RelayHubChanged', { oldRelayHub: singletonRelayHub, newRelayHub });
  25. });
  26. it('cannot upgrade to the same RelayHub', async function () {
  27. await expectRevert(
  28. this.recipient.upgradeRelayHub(singletonRelayHub),
  29. 'GSNRecipient: new RelayHub is the current one'
  30. );
  31. });
  32. it('cannot upgrade to the zero address', async function () {
  33. await expectRevert(
  34. this.recipient.upgradeRelayHub(ZERO_ADDRESS), 'GSNRecipient: new RelayHub is the zero address'
  35. );
  36. });
  37. context('with new RelayHub', function () {
  38. beforeEach(async function () {
  39. await this.recipient.upgradeRelayHub(newRelayHub);
  40. });
  41. it('returns the new instance address', async function () {
  42. expect(await this.recipient.getHubAddr()).to.equal(newRelayHub);
  43. });
  44. });
  45. });
  46. context('when called directly', function () {
  47. beforeEach(async function () {
  48. this.context = this.recipient; // The Context behavior expects the contract in this.context
  49. this.caller = await ContextMockCaller.new();
  50. });
  51. shouldBehaveLikeRegularContext(sender);
  52. });
  53. context('when receiving a relayed call', function () {
  54. beforeEach(async function () {
  55. await gsn.fundRecipient(web3, { recipient: this.recipient.address });
  56. });
  57. describe('msgSender', function () {
  58. it('returns the relayed transaction original sender', async function () {
  59. const { tx } = await this.recipient.msgSender({ from: sender, useGSN: true });
  60. await expectEvent.inTransaction(tx, GSNRecipientMock, 'Sender', { sender });
  61. });
  62. });
  63. describe('msgData', function () {
  64. it('returns the relayed transaction original data', async function () {
  65. const integerValue = new BN('42');
  66. const stringValue = 'OpenZeppelin';
  67. const callData = this.recipient.contract.methods.msgData(integerValue.toString(), stringValue).encodeABI();
  68. // The provider doesn't properly estimate gas for a relayed call, so we need to manually set a higher value
  69. const { tx } = await this.recipient.msgData(integerValue, stringValue, { gas: 1000000, useGSN: true });
  70. await expectEvent.inTransaction(tx, GSNRecipientMock, 'Data', { data: callData, integerValue, stringValue });
  71. });
  72. });
  73. });
  74. context('with deposited funds', async function () {
  75. const amount = ether('1');
  76. beforeEach(async function () {
  77. await gsn.fundRecipient(web3, { recipient: this.recipient.address, amount });
  78. });
  79. it('funds can be withdrawn', async function () {
  80. const balanceTracker = await balance.tracker(payee);
  81. await this.recipient.withdrawDeposits(amount, payee);
  82. expect(await balanceTracker.delta()).to.be.bignumber.equal(amount);
  83. });
  84. it('partial funds can be withdrawn', async function () {
  85. const balanceTracker = await balance.tracker(payee);
  86. await this.recipient.withdrawDeposits(amount.divn(2), payee);
  87. expect(await balanceTracker.delta()).to.be.bignumber.equal(amount.divn(2));
  88. });
  89. it('reverts on overwithdrawals', async function () {
  90. await expectRevert(this.recipient.withdrawDeposits(amount.addn(1), payee), 'insufficient funds');
  91. });
  92. });
  93. });