Could you help me gas error

Hi, I have a gas error, if I put the gas in it the transaction is never executed, but if I remove the gasLimit or price it stays and never exits the function, I attach the connected contract what it returns, i need tokenId the function return tokenId.

function:

async function mintNFTEmail(metadataUrl: string, tokenAddress:  string) {

      try {

        const ethers = Moralis.web3Library;

        console.log('resTx')

        const royal = 20 * 2000;

        const ethAddress = userRender.attributes?.ethAddress ? userRender.attributes.ethAddress.toString().toLowerCase() : "";

        const privateKey = userRender.attributes?.privateKey

        ? userRender.attributes.privateKey

        : "";

        const mnemonic = userRender.attributes?.mnemonic ? userRender.attributes.mnemonic : "";

       

        const contractTokenKNRT = contracts.token.toString().toLowerCase();

        const collectionAddress = tokenAddress.toString().toLowerCase();

   

        const provider = new ethers.providers.JsonRpcProvider(`${urlProvider}${apiAlchemy}`);

        const signer = new ethers.Wallet(privateKey, provider);

        const sendOptions2 = [

              ethAddress,

              ethAddress,

              royal,

              ethAddress,

              metadataUrl,

              0,

              contractTokenKNRT,

              '0',

              '0',

              '0x0000000000000000000000000000000000000000000000000000000000000000'

        ]

       

        const contract = new ethers.Contract(tokenAddress, tokenErc721, provider);

        const tx = await contract.connect(signer).createItem(sendOptions2, { gasLimit: 9000000, gasPrice: 1000000 });

        const tokenId = await tx.wait(1);

       

        // console.log('resTx', parseInt(tokenId.events[1].args[1]._hex, 16).toString())

   

        return 123 //parseInt(tokenId.events[1].args[1]._hex, 16).toString()

      }catch (e: any) {

        console.log("ErrorMintNFT1", e)

        console.log("ErrorMintNFT2", e.message)

        return "-1";

      }

  }

tx response:

mintNFTEmail ~ tx

  1. {type: 2, chainId: 80001, nonce: 71, maxPriorityFeePerGas: BigNumber, maxFeePerGas: BigNumber, …}

  2. accessList: []

  3. chainId: 80001

  4. confirmations: 0

  5. data: “0x52258eaf0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f7fdd1a239a6fbca88ac8aaf97a174831d91bde3000000000000000000000000f7fdd1a239a6fbca88ac8aaf97a174831d91bde30000000000000000000000000000000000000000000000000000000000009c40000000000000000000000000f7fdd1a239a6fbca88ac8aaf97a174831d91bde30000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011142365ddbc92c3547b8a074289409b5432ca8b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005068747470733a2f2f697066732e6d6f72616c69732e696f3a323035332f697066732f516d62726f50465168395147354d31625157443438326d645a3838784a6b517a6d3657413835654a53444d69763900000000000000000000000000000000”

  6. from: “0xf7fDd1a239A6FBCA88ac8AaF97A174831d91bDe3”

  7. gasLimit: BigNumber

1. _hex: "0x0dbba0"
2. _isBigNumber: true
3. [[Prototype]]: Object
  1. gasPrice: null
  2. hash: “0x53153545def65e90ee6a4fcb1f5e0869ea17fc9904e48a6bf18eb84f61542b64”
  3. maxFeePerGas: BigNumber
1. _hex: "0x0f4240"
2. _isBigNumber: true
3. [[Prototype]]: Object
  1. maxPriorityFeePerGas: BigNumber
1. _hex: "0x0f4240"
2. _isBigNumber: true
3. [[Prototype]]: Object
  1. nonce: 71
  2. r: “0x8307fc3e44c29b9d52a46f6048adec9be9dbe5c813c95c42a309f6d94f708fd5”
  3. s: “0x06c85b4c7193043d604d806f493c4237f3ff30cc9271638f1d83e253d8bce9a6”
  4. to: “0x1C7d04C4DDEE4d13c31E800568039AcEabDCc3C8”
  5. type: 2
  6. v: 1
  7. value: BigNumber
1. _hex: "0x00"
2. _isBigNumber: true
3. [[Prototype]]: Object
  1. wait: (confirmations) => {…}
1. length: 1
2. name: ""
3. arguments: (...)
4. caller: (...)
5. [[FunctionLocation]]: index.js:236
6. [[Prototype]]: ƒ ()
  1. [[Prototype]]: Object

Hey there @davidzuccarini

Can you pls copy the specific error message that you got from this?

Also can you give me the contract script or contract address (if it is verified) so I can check the source code :raised_hands:

Of course, thanks for assisting me, this is the address of the contract: 0x58eE39a5f27dbE99c48F39Aca32C64BFef4d179C the error is that it does not mint, I execute it with the function of moralis executeFunction and it works but with ethers I don’t know what I’m doing wrong if I remove gasPrice from constant tx it gives me an error of gas but if I leave it it never exits the function if I remove this line of code const tokenId = await tx.wait();
I do not get the token id and it returns the aforementioned code

If I just run the function like this:

const tx = await contract.connect(signer).createItem(sendOptions2 );

This answers me but I don’t get the tokenId and the function returns the tokenID

response:

* {type: 2, chainId: 80001, nonce: 74, maxPriorityFeePerGas: BigNumber, maxFeePerGas: BigNumber, …}

1. accessList: []
2. chainId: 80001
3. confirmations: 0
4. data: "0x52258eaf0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f7fdd1a239a6fbca88ac8aaf97a174831d91bde3000000000000000000000000f7fdd1a239a6fbca88ac8aaf97a174831d91bde30000000000000000000000000000000000000000000000000000000000009c40000000000000000000000000f7fdd1a239a6fbca88ac8aaf97a174831d91bde30000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011142365ddbc92c3547b8a074289409b5432ca8b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005068747470733a2f2f697066732e6d6f72616c69732e696f3a323035332f697066732f516d62726f50465168395147354d31625157443438326d645a3838784a6b517a6d3657413835654a53444d69763900000000000000000000000000000000"
5. from: "0xf7fDd1a239A6FBCA88ac8AaF97A174831d91bDe3"
6. gasLimit: BigNumber

  1. _hex: "0x03fe6b"
  2. _isBigNumber: true
  3. [[Prototype]]: Object

7. gasPrice: null
8. hash: "0x59817b618af20a742d381b47fb81c7a72825fb9fdce9ee7ffb66120ac8e930ef"
9. maxFeePerGas: BigNumber

  1. _hex: "0x59682f1e"
  2. _isBigNumber: true
  3. [[Prototype]]: Object

10. maxPriorityFeePerGas: BigNumber

  1. _hex: "0x59682f00"
  2. _isBigNumber: true
  3. [[Prototype]]: Object

11. nonce: 74
12. r: "0x010cea0552c1a2ae3377353d6615acca10a8dc59eb795ee0b4d7b3ff8cfa2b7d"
13. s: "0x74d6cafd3e32844938a538ab2c378e7040a89cf1658b6b435a96cfbfed80afce"
14. to: "0x1C7d04C4DDEE4d13c31E800568039AcEabDCc3C8"
15. type: 2
16. v: 0
17. value: BigNumber

  1. _hex: "0x00"
  2. _isBigNumber: true
  3. [[Prototype]]: Object

18. wait: (confirmations) => {…}

  1. length: 1
  2. name: ""
  3. arguments: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at Function.invokeGetter (<anonymous>:3:28)]
  4. caller: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at Function.invokeGetter (<anonymous>:3:28)]
  5. [[FunctionLocation]]: index.js:236
  6. [[Prototype]]: ƒ ()

19. [[Prototype]]: Object

I see so seems like this is purely gas issue. So to resolve it, should be very simple

You simply just need to set the gasPrice to its estimated value instead of a fixed value as gas price can vary depending on how congested the network is and how big your data input is

To do so:

const gas = await provider.getGasPrice();
const functionGasFees = await contract.estimateGas.createItem(sendOptions2);
const gasPrice = gasPrice * functionGasFees;

Which then you can put your gas inputs to

{
  gasLimit: gasPrice,
  gasPrice
}

in the moralis function I extract the tokenId as follows: tokenId.events[1].args[1]._hex, 16but with the ethers function I have no success.

Ahhh yes write function from smart contract even if it returns something, it will not return anything on ethersjs

It will only return the value of the executed function if it gets called by another function in smart contract

Usually if you would like to fetch the value, you’ll need to log it in events and listen to any events when it is emitted from smart contracts. That is usually the best practice

For listening to smart contract events you best solution will be using our Streams API where you can test it easily through our dashboard and when you’re ready setup a webhook to listen the events real time

ok I understand, but could I do it with .on() ? Could you help me with an example to later migrate it? I’m in the functionality stage. I haven’t created the self-hosted server yet. When I finish the functionality face, I migrate the server, since I’m migrating things that were already done and how Moralis changed, I changed many functions. Could you help me a lot? thank you

What are you referring here to .on()?

And if you need help with migrating the server, feel free to ask questions about it, you are welcome to do so anytime :raised_hands:

I mean you can listen to the event of the contract example:

 contract.on( "createItem" , ( from: any, to: any, value: any, event: any ) => {

           let transferEvent = { from: from, to: to, value: value, eventData: event, }
           console.log ( JSON.stringify( transferEvent) ) }
         );

because I am mining the nft in this case I do not need the stream because it is only to obtain the token id I see the use of stream more to read all the nfts or list the nfts in the front or for another use in this case it is the mining process of the nft to create it, then if I have to migrate several functions to streams to observe the nfts created in the smart contract of the market, I don’t know if I make myself understand. What I need at this moment is to execute the purchase of the nft and the minting are the 2 functions that I am missing to migrate to ethers and then stream everything that happens in the smart contract of the market. if you understand the problem? hehe yes of course I’m going to ask and I use the forum a lot I like this forum more than stackoverflow :slight_smile: they are very attentive and help. And also sorry for bothering you with many questions.

and if you can observe the contract mumbai0x58eE39a5f27dbE99c48F39Aca32C64BFef4d179C in mumbai the transaction is not being created there is nothing

Hey yeah I roughly understand your problem :raised_hands:

Cool if you wanna just use contract.on() for listening to events that is another option, stream is not particularly to read NFTs only, it’s for listening or monitoring a certain wallet or contract events btw

So for migrating to ethers do you still get any particular error now?

And no worries, you’re not bothering, glad to help you out on your issue :grinning_face_with_smiling_eyes:

If the problem is that I can’t mint the nft in the smart contract, it doesn’t do anything, I have had this error for 12 days, it’s frustrating sometimes to develop, hahahaha, I’ve tried everything in the documentation, but I don’t know what else to do, the problem This one does not lie, the code that I placed above is executed but without success in the network, thanks for the help

This is the function that is executed with Moralis.executeFunction, it works correctly now what I have to do is to migrate it to use it with a user who has a private key and memotechnical phrase in the db:

const sendOptions = {

        contractAddress: tokenAddress.toString().toLowerCase(),

        functionName: "createItem",

        abi: tokenErc721,

        params: {

          _req: [

          user.get("ethAddress"),

          user.get("ethAddress"),

          parseFloat(royal) * 2000,

          user.get("ethAddress"),

          metadataUrl,

          0,

          contracts.token,

          '0',

          '0',

          '0x0000000000000000000000000000000000000000000000000000000000000000'

        ],

        },

      };

        const tx: any = await Moralis.executeFunction(sendOptions)

Although since it is a custom wallet, seeing the problem, I should send the transaction, it is with the wallet, right? Not with the smart contract because I don’t use metamask

executing it with the wallet with the signer gives me this error, first I put the modified function and then the error:

async function mintNFTEmail(metadataUrl: string, tokenAddress:  string) {

      try {

        console.log("🚀 ~ file: MintState.tsx:977 ~ mintNFTEmail ~ tokenAddress", tokenAddress)

       

        const ethers = Moralis.web3Library;

        const web = new Web3(Moralis.provider);

        console.log('resTx')

        const royal = 20 * 2000;

        const ethAddress = userRender.attributes?.ethAddress ? userRender.attributes.ethAddress.toString().toLowerCase() : "";

        const privateKey = userRender.attributes?.privateKey

        ? userRender.attributes.privateKey

        : "";

        const mnemonic = userRender.attributes?.mnemonic ? userRender.attributes.mnemonic : "";

       

        const contractTokenKNRT = contracts.token.toString().toLowerCase();

        const collectionAddress = tokenAddress.toString().toLowerCase();

   

        console.log('resTx')

        const provider = new ethers.providers.JsonRpcProvider(`${urlProvider}${apiAlchemy}`);

        // let gasPrice = await provider.getFeeData();

        // console.log("🚀 ~ file: MintState.tsx:1085 ~ mintNFTEmail ~ gasPrice", ethers.formatUnits(gasPrice, "gwei") )

        const signer1 = provider.getSigner(ethAddress)

        const signer = new ethers.Wallet(privateKey, provider);

        const signerConnect = signer.connect(provider)

       

        console.log('resTx')

        const sendOptions = {

          contractAddress: collectionAddress,

          functionName: "createItem",

          abi: tokenErc721,

          params: {

            _req: [

              ethAddress,

              ethAddress,

              royal,

              ethAddress,

              metadataUrl,

              0,

              contractTokenKNRT,

              '0',

              '0',

              '0x0000000000000000000000000000000000000000000000000000000000000000'

          ],

          },

        };

        const sendOptions3 = {

          gasPrice: 1000000 ,

          gasLimit:900000,

          params: {

            _req: [

              ethAddress,

              ethAddress,

              royal,

              ethAddress,

              metadataUrl,

              0,

              contractTokenKNRT,

              '0',

              '0',

              '0x0000000000000000000000000000000000000000000000000000000000000000'

          ],

          },

        };

        const sendOptions2 = [

              ethAddress,

              ethAddress,

              royal,

              ethAddress,

              metadataUrl,

              0,

              contractTokenKNRT,

              '0',

              '0',

              '0x0000000000000000000000000000000000000000000000000000000000000000'

        ]

       

        console.log('resTx')

        const contract = new ethers.Contract(collectionAddress, tokenErc721, provider);

        const tx = await contract.connect(signer).createItem(sendOptions2 );

        console.log("🚀 ~ file: MintState.tsx:1126 ~ mintNFTEmail ~ tx", tx)

        signer1.sendTransaction(tx).then((transaction: any) => {

          console.dir(transaction);

          console.log("Send finished!");

        });

       

        // const tokenId = await tx.wait(1);

        // contract.on( "createItem" , ( from: any, to: any, value: any, event: any ) => {

        //   let transferEvent = { from: from, to: to, value: value, eventData: event, }

        //   console.log ( JSON.stringify( transferEvent) ) }

        // );

        // console.log("🚀 ~ file: MintState.tsx:1129 ~ mintNFTEmail ~ tokenId", tokenId)

       

        // console.log('resTx', parseInt(tokenId.events[1].args[1]._hex, 16).toString())

   

        console.log('resTx')

        return 123 //parseInt(tokenId.events[1].args[1]._hex, 16).toString()

      }catch (e: any) {

        console.log("ErrorMintNFT1", e)

        console.log("ErrorMintNFT2", e.message)

        return "-1";

      }

  }

error:

index.js:224 Uncaught (in promise) Error: invalid object key - hash (argument="transaction:hash", value={"type":2,"chainId":80001,"nonce":82,"maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x59682f00"},"maxFeePerGas":{"type":"BigNumber","hex":"0x59682f1e"},"gasPrice":null,"gasLimit":{"type":"BigNumber","hex":"0x6190"},"to":"0x58eE39a5f27dbE99c48F39Aca32C64BFef4d179C","value":{"type":"BigNumber","hex":"0x00"},"data":"0x52258eaf0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f7fdd1a239a6fbca88ac8aaf97a174831d91bde3000000000000000000000000f7fdd1a239a6fbca88ac8aaf97a174831d91bde30000000000000000000000000000000000000000000000000000000000009c40000000000000000000000000f7fdd1a239a6fbca88ac8aaf97a174831d91bde30000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011142365ddbc92c3547b8a074289409b5432ca8b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005068747470733a2f2f697066732e6d6f72616c69732e696f3a323035332f697066732f516d62726f50465168395147354d31625157443438326d645a3838784a6b517a6d3657413835654a53444d69763900000000000000000000000000000000","accessList":[],"hash":"0x1d6878c5603e3c759c6e7c9ff97374dbd2f822ea2fb43a643a8e61666870ddfc","v":1,"r":"0x8e99749e5b505c14fff3a86bff7e6aa14fad6a71fa383fe86fb73329f2e7d940","s":"0x238a09fc47b7be30d3a0279ddf6ba085fd3798f6d18ad117e4d835be90c62778","from":"0xf7fDd1a239A6FBCA88ac8AaF97A174831d91bDe3","confirmations":0}, code=INVALID_ARGUMENT, version=properties/5.7.0)

While executing transactions with private you will have to use the nonce parameter in the transaction object. Can you try including that too?

I don’t see an example anywhere of how to execute said transaction. Could you help me? please, although in the answer above it has the nonce : 11. nonce: 74

this is the actual code:

 const contract = new ethers.Contract(collectionAddress, tokenErc721, provider);
        const tx = await contract.connect(signer).createItem(sendOptions2 );
        console.log("🚀 ~ file: MintState.tsx:1126 ~ mintNFTEmail ~ tx", tx)

        signer1.sendTransaction(tx).then((transaction: any) => {
          console.dir(transaction);
          console.log("Send finished!");
        });

and this is the error:

invalid object key - hash (argument="transaction:hash", value={"type":2,"chainId":80001,"nonce":83,"maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x59682f00"},"maxFeePerGas":{"type":"BigNumber","hex":"0x59682f20"},"gasPrice":null,"gasLimit":{"type":"BigNumber","hex":"0x6190"},"to":"0x58eE39a5f27dbE99c48F39Aca32C64BFef4d179C","value":{"type":"BigNumber","hex":"0x00"},"data":"0x52258eaf0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f7fdd1a239a6fbca88ac8aaf97a174831d91bde3000000000000000000000000f7fdd1a239a6fbca88ac8aaf97a174831d91bde30000000000000000000000000000000000000000000000000000000000009c40000000000000000000000000f7fdd1a239a6fbca88ac8aaf97a174831d91bde30000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011142365ddbc92c3547b8a074289409b5432ca8b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005068747470733a2f2f697066732e6d6f72616c69732e696f3a323035332f697066732f516d62726f50465168395147354d31625157443438326d645a3838784a6b517a6d3657413835654a53444d69763900000000000000000000000000000000","accessList":[],"hash":"0x37c10b053d761bf70d557a85a9d6a1ead1452ca4fabfd06821eb03b17ea7a813","v":1,"r":"0x77622fff3fd23e15b2aafe62351f737abb31150a11698a125d8e2c4e10ec6032","s":"0x16ac18c71f7188230f00ddd9ae14bd4e5e834f33033c67d3ad36389001cf1ca2","from":"0xf7fDd1a239A6FBCA88ac8AaF97A174831d91bDe3","confirmations":0}, code=INVALID_ARGUMENT, version=properties/5.7.0)

Hey there @davidzuccarini

Looks like it might be just a typing issue on your input, from your error looks like it is on line 224, not sure which line is that but try to have a check and highlight it :raised_hands:

Hello, identify the error, the error is that the wallet got a transaction stuck and for that reason the transaction was not executed, how could I see the transactions generated and cancel the one that is stuck?