I'm trying to make sure that, when my contract receives any token, send mine to the wallet of the person who sent it to the contract.

event Received(address, uint);
    receive() external payable {
    emit Received(msg.sender, msg.value);
    uint256 numTokens = msg.value * 1000;
    require(numTokens <= balances[msg.sender]);
    balances[address(this)] = balances[address(this)].sub(numTokens);
    balances[msg.sender] = balances[msg.sender].add(numTokens);
    emit Transfer(address(this), msg.sender,  numTokens);

I'm doing this, but an error appears: Warning! Error encountered during contract execution [out of gas]

Transaction Details

What I find strange is that if I remove the line:

balances[msg.sender] = balances[msg.sender].add(numTokens);

The error does not happen, the quantity of the tokens in the contract decreases, but without the line I removed it does not update the wallet of who sent the tokens for the contract.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.