PROBLEM WITH REENTRANCY - NEED HELP

I'm creating a function that sells the tokens for BNB.

Sell Function
> 1 - The Sell function receives as a parameter the amount of tokens you want to exchange for BNB.
> 2 - The Sell function checks if the user has the amount of tokens he wants to sell.
> 3 - The Sell function burns the amount of tokens being sold.
> 4 - The Sell function calculates the amount of BNB that must be paid.
> 5 - The Sell function transfers the amount of BNB to the person selling the tokens.

How to sell the tokens and send them to the user in the same role without being vulnerable to reentrant attacks?

csharp
. // Sell your token for BNB:
1 function sell(uint amount) public nonReentrant {
2 require(balanceOf(msg.sender) >= amount);
3 _burn(msg.sender, amount);
4 uint bnbAmount = amount.mul(sellPrice);
5 payable(msg.sender).transfer(bnbAmount); // withdrawBNB
. }

🤔 Would you like to know how to sell and already withdraw directly in the same function, avoiding problems with ReEntrancy?

Feb 20, 2022, 12:17 PM

© 2024 Draquery.com All rights reserved.