GSNRecipientERC20Fee.test.js 2.6 KB

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