Using built-in function in openzeppelin inside my own function

Hi everyone, I am a newbie at writing smart contract. I’ve just started playing around with Solidity and I had this really weird error that I do not know why.
I want to use a function that is already a built-in function in openzeppelin (for example, function safeTransferFrom(address, address, uint256, uint256, bytes) in ERC1155.sol). For specific purposes, I want to use this function (safeTransferFrom()) inside another function (for example, function test()). But as you can see, once I call the safeTransferFrom(), I just cannot write any code after that line (I tried in the hardhat/truffle console and I confirm this). For “after that line”, I mean, just inside the current function I am writing. I can write anything after that function, but not inside that function, after the line I used safeTransferFrom.
I have no idea why I got this “issue” (not sure it’s an issue, maybe I just don’t really understand the concept).
So do you have any idea or suggest about this one?

I got a picture of my code below to represent my problem. Thanks for your support! I really appreciate it!

:1234: Code here

:computer: Environment

  • Solidity: ^0.8.0
  • Yarn: 1.22.10
  • Hardhat: ^2.3.0
  • OpenZeppelin: 4.x

It’s because with those functions they are returning things, so it ends your code execution when calling things like SafeTransfer - hence the name, it’s “safe” because after you call, that’s final. No other things can happen, which could mess with other things in your code.

Read the mint function that you are trying to do and show me what you find!

1 Like

This doesn’t make any sense to me. How do you know you can’t write any code after that line? Are you seeing any errors?

1 Like

Yes, I did do some experiments.
I tried to change some global variables, and if I put those lines (that change the variables) after the safeTransfer function, these variables seemed to remain the same. Everything went right if I put those lines before the safe function one.

This is the source code of _mint function that I used:

I don’t really have many knowledge about programming, so I guess this function is the reason why the function _mint() would terminate after we run it:

As your suggestion, I could understand that there is something that would be return after the _mint(). But literally, I don’t see any “return” here, this function is not marked as it would return anything (the definition don’t have the syntax e.g returns(uint256), there is not keyword return).
Can you please explain more about this?

Can you try this again and show the code you used for testing?

1 Like

It’s pretty weird. I decided to create a whole new project in another folder, and everthing run pretty fine:

  • This is my contract’s source code. The idea in here simply is changing number's value and anotherNumber's value.

  • I deployed TestContract in local. Firstly, I looked on these numbers’ value:

  • Secondly, I changed these values (from: number = 1, anotherNumber = 2; to: number = 2, anotherNumber = 3)

  • And finally, everything seems pretty okay:

But still, in my own project, everything remains the same. I am trying to find out what is the different here :’((

Even more confusing when I put a random variable (e.g num in here, the code now have no warning “Unreachable code” at all D:)

  1. I successfully update num’s value to 4:

  2. Try to update num’s value and failed (got “Unreachable code” error):

After all, I found out that the contract works pretty well when I deployed it to the testnet (just have this current problem when I tried in hardhat/truffle console).
I’m still trying to figure it out, but I’m so confused and totally struggled. :worried: :worried:

I don’t know what version of Solidity you’re using but there is a bug in the latest (0.8.5) that shows false “unreachable code” warnings.


yeah, I think that might be the problem :slightly_smiling_face: Thanks for your help :100: :100: