Home > Enterprise >  Tell wether or not a mint call to a contract succeeded
Tell wether or not a mint call to a contract succeeded

Time:12-06

I'm working on an NFT site in NextJS and trying to implement a redirect for the user after they successfully mint a token. Here's my mint code:

const mintToken = () => {
   safeMint?.();
   router.push('/success');
};

As you can see, after safeMint is called, I try to redirect to /success which is what happens. However, it redirects regardless of a successful mint, I want it to only redirect after the call to the smart contract succeeds. I've tried using callbacks and timeouts but nothing seems to work the way I've laid out above. Is there some way of getting or waiting for a success response before redirecting that I'm missing? Thanks!

CodePudding user response:

Function return value is not available outside of EVM if you execute the function with a transaction.

You can wait for the transaction receipt. It contains the transaction status (success / revert), as well as event logs. Tx receipt is available only after the tx is included in a block.

Depending on your safeMint() implementation, it might mint tokens each time the transaction succeeds. But if your implementation allows for the function to succeed even without minting tokens, you might need to check the event logs to make sure that the NFT was really minted.

// transaction reverted
function safeMint() external {
    require(failedCondition);
    _mint(msg.sender, tokenId);
}
// transaction succeeded but no token was minted
function safeMint() external {
    if (failedCondition) {
        _mint(msg.sender, tokenId);
    }
}

How to wait for the receipt with ethers:

const tx = await myContract.safeMint();
const txReceipt = await transaction.wait();

if (txReceipt.status) {
    // not reverted
}

Docs:

CodePudding user response:

in safeMint function inside contract, you can return the tokenId (or you could return true)

const mintToken =async () => {
   const result=await safeMint?();
   if(result){
      router.push('/success');
   }
};
  • Related