Imagine that a player bought 100 tokens inside a DEX. They deposited tokens inside a rewards contract in order to play the game. After 15 days, player could withdraw 300 because he earned 200 tokens inside the game. I stored this 200 tokens inside my database (offchain).

So, the player starting from the frontend requesting to withdraw 250. My backend could check with the database if this operation could be done and send a transaction to a function that will receive the amount and the receiver address (player).

function (uint256 amount, address player)

But what if the player interacts directly with the smart contract? The player can withdraw more than your account has. How can I avoid this?

I wanna build a function or a flow that manage tokens offchain, instead holding the state inside the contract and the player pays for the gas fee.

  • How could they withdraw 250 from the contract if the 200 was in the off-chain database? If the 200 were already in the contract why do you need the off-chain database? In general the contract should be the source of truth, and use the off-chain database to improve user experience. If you really need to handle accounting off-chain ensure changes to the contract are carried with your authorization.
    – Ismael
    Feb 5, 2022 at 6:06

1 Answer 1


What do you mean by withdraw? There is no withdraw function in the ERC20 token. What I think you should do is control his tokens offchain and when he clicks a withdraw button in the game, you transfer what he is entitled to to his onchain account. Then he can swap or whatever he wants with his tokens.

If you want to prevent him from wasting gas during the game, you can increase his allowance during the game. Then, when he clicks withdraw, you will call the transferFrom function, transferring from some game account to this/her account. I think this is a good solution, but maybe someone has a better solution.

  • you I mean withdraw i´m using the game term. I know that ERC20 doesn´t have this kind of function and we only use transfer and transferFrom. What i´m trying to avoid is the possibility to interact with smart control directly and get more tokens that he can. I´m also trying to avoid to deployer pays any gas. In your awnser the game wallet will pay gas for that.
    – gustavomr
    Feb 4, 2022 at 23:12
  • Yes, but you will pay only once, when the player decides to withdraw. Then the game will pay for the Allowance and the player will pay the transferFrom. It's the best solution I can think of. I've also thought about making a game and I thought it would be the best solution, but if anyone has a better one, I am all listening. Feb 5, 2022 at 0:11
  • if the owner will pay for that, you could implement a onlyOnwer function with transfer. You don´t need two steps (allowance and transferFrom). I´m trying to think different and giving the gas payment to the user. I don´t know if this is possible using offchain tokens. If you use onchain tokens it would be possible because user sync earnings with the chain.
    – gustavomr
    Feb 5, 2022 at 0:40
  • The transfer function transfer from the msg.sender to another person. You don't want that. You want to transfer from the game to the player. I don't think that you can do it with only one transaction, but I will wait for the opinion of others. Feb 5, 2022 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.