GSNRecipientERC20Fee.test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const { accounts, contract, web3 } = require('@openzeppelin/test-environment');
  2. const { ether, expectEvent } = require('@openzeppelin/test-helpers');
  3. const gsn = require('@openzeppelin/gsn-helpers');
  4. const { expect } = require('chai');
  5. const GSNRecipientERC20FeeMock = contract.fromArtifact('GSNRecipientERC20FeeMock');
  6. const ERC20 = contract.fromArtifact('ERC20');
  7. const IRelayHub = contract.fromArtifact('IRelayHub');
  8. describe('GSNRecipientERC20Fee', function () {
  9. const [ sender ] = accounts;
  10. const name = 'FeeToken';
  11. const symbol = 'FTKN';
  12. beforeEach(async function () {
  13. this.recipient = await GSNRecipientERC20FeeMock.new(name, symbol);
  14. this.token = await ERC20.at(await this.recipient.token());
  15. });
  16. describe('token', function () {
  17. it('has a name', async function () {
  18. expect(await this.token.name()).to.equal(name);
  19. });
  20. it('has a symbol', async function () {
  21. expect(await this.token.symbol()).to.equal(symbol);
  22. });
  23. it('has 18 decimals', async function () {
  24. expect(await this.token.decimals()).to.be.bignumber.equal('18');
  25. });
  26. });
  27. context('when called directly', function () {
  28. it('mock function can be called', async function () {
  29. const { logs } = await this.recipient.mockFunction();
  30. expectEvent.inLogs(logs, 'MockFunctionCalled');
  31. });
  32. });
  33. context('when relay-called', function () {
  34. beforeEach(async function () {
  35. await gsn.fundRecipient(web3, { recipient: this.recipient.address });
  36. this.relayHub = await IRelayHub.at('0xD216153c06E857cD7f72665E0aF1d7D82172F494');
  37. });
  38. it('charges the sender for GSN fees in tokens', async function () {
  39. // The recipient will be charged from its RelayHub balance, and in turn charge the sender from its sender balance.
  40. // Both amounts should be roughly equal.
  41. // The sender has a balance in tokens, not ether, but since the exchange rate is 1:1, this works fine.
  42. const senderPreBalance = ether('2');
  43. await this.recipient.mint(sender, senderPreBalance);
  44. const recipientPreBalance = await this.relayHub.balanceOf(this.recipient.address);
  45. const { tx } = await this.recipient.mockFunction({ from: sender, useGSN: true });
  46. await expectEvent.inTransaction(tx, IRelayHub, 'TransactionRelayed', { status: '0' });
  47. const senderPostBalance = await this.token.balanceOf(sender);
  48. const recipientPostBalance = await this.relayHub.balanceOf(this.recipient.address);
  49. const senderCharge = senderPreBalance.sub(senderPostBalance);
  50. const recipientCharge = recipientPreBalance.sub(recipientPostBalance);
  51. expect(senderCharge).to.be.bignumber.closeTo(recipientCharge, recipientCharge.divn(10));
  52. });
  53. });
  54. });