GSNRecipient.test.js 4.3 KB

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