GSNBouncerERC20Fee.test.js 2.6 KB

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