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.