GSNRecipientERC20Fee.test.js 2.8 KB

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