ERC721GSNRecipientMock.test.js 1.9 KB

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