ERC721GSNRecipientMock.test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const { accounts, contract, web3 } = require('@openzeppelin/test-environment');
  2. const { constants, expectEvent } = require('@openzeppelin/test-helpers');
  3. const { ZERO_ADDRESS } = constants;
  4. const gsn = require('@openzeppelin/gsn-helpers');
  5. const { fixSignature } = require('../helpers/sign');
  6. const { utils: { toBN } } = require('web3');
  7. const ERC721GSNRecipientMock = contract.fromArtifact('ERC721GSNRecipientMock');
  8. describe('ERC721GSNRecipient (integration)', function () {
  9. const [ signer, sender ] = accounts;
  10. const tokenId = '42';
  11. beforeEach(async function () {
  12. this.token = await ERC721GSNRecipientMock.new(signer);
  13. });
  14. async function testMintToken (token, from, tokenId, options = {}) {
  15. const { tx } = await token.mint(tokenId, { from, ...options });
  16. await expectEvent.inTransaction(tx, ERC721GSNRecipientMock, 'Transfer', { from: ZERO_ADDRESS, to: from, tokenId });
  17. }
  18. context('when called directly', function () {
  19. it('sender can mint tokens', async function () {
  20. await testMintToken(this.token, sender, tokenId);
  21. });
  22. });
  23. context('when relay-called', function () {
  24. beforeEach(async function () {
  25. await gsn.fundRecipient(web3, { recipient: this.token.address });
  26. });
  27. it('sender can mint tokens', async function () {
  28. const approveFunction = async (data) =>
  29. fixSignature(
  30. await web3.eth.sign(
  31. web3.utils.soliditySha3(
  32. // eslint-disable-next-line max-len
  33. data.relayerAddress, data.from, data.encodedFunctionCall, toBN(data.txFee), toBN(data.gasPrice), toBN(data.gas), toBN(data.nonce), data.relayHubAddress, this.token.address
  34. ), signer
  35. )
  36. );
  37. await testMintToken(this.token, sender, tokenId, { useGSN: true, approveFunction });
  38. });
  39. });
  40. });