ERC721GSNRecipientMock.test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 name = 'Non Fungible Token';
  11. const symbol = 'NFT';
  12. const tokenId = '42';
  13. beforeEach(async function () {
  14. this.token = await ERC721GSNRecipientMock.new(name, symbol, signer);
  15. });
  16. async function testMintToken (token, from, tokenId, options = {}) {
  17. const { tx } = await token.mint(tokenId, { from, ...options });
  18. await expectEvent.inTransaction(tx, ERC721GSNRecipientMock, 'Transfer', { from: ZERO_ADDRESS, to: from, tokenId });
  19. }
  20. context('when called directly', function () {
  21. it('sender can mint tokens', async function () {
  22. await testMintToken(this.token, sender, tokenId);
  23. });
  24. });
  25. context('when relay-called', function () {
  26. beforeEach(async function () {
  27. await gsn.fundRecipient(web3, { recipient: this.token.address });
  28. });
  29. it('sender can mint tokens', async function () {
  30. const approveFunction = async (data) =>
  31. fixSignature(
  32. await web3.eth.sign(
  33. web3.utils.soliditySha3(
  34. // eslint-disable-next-line max-len
  35. data.relayerAddress, data.from, data.encodedFunctionCall, toBN(data.txFee), toBN(data.gasPrice), toBN(data.gas), toBN(data.nonce), data.relayHubAddress, this.token.address
  36. ), signer
  37. )
  38. );
  39. await testMintToken(this.token, sender, tokenId, { useGSN: true, approveFunction });
  40. });
  41. });
  42. });