GSNRecipient.test.js 4.5 KB

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