1

This is a smart contract that I wrote in Solidity. It works 100% if I don´t charge any transaction fees. When I added 2 lines to charge a fee, I am getting this error. What this smart contract does is very simple. It transfers 2 tokens (_tokenId1 and _tokenId2) to the customer and we charge them a fee for the transaction.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";

contract SilverPack {

   event OperatorChanged (address previousOperator, address newOperator);
   event SilverPackTransferCompleted (address offerer, address receiver);

  
  address operator;

  constructor (address _operator) {
        operator = _operator;
    }

  

function saleBronce(address _offerer, address _hostContract, uint _tokenId1, uint _tokenId2, uint256 price) public payable {
    ERC1155 hostContract = ERC1155(_hostContract);
    require(msg.value >= price);     // this line causes the error
    payable(msg.sender).transfer(price); // this line also causes the error
    hostContract.safeTransferFrom(_offerer, msg.sender, _tokenId1, 1,"");
    hostContract.safeTransferFrom(_offerer, msg.sender, _tokenId2, 1,"");  
      
     emit SilverPackTransferCompleted(_offerer, msg.sender);
 }


  function changeOperator(address _newOperator) external {
        require(msg.sender == operator,"only the operator can change the current operator");
        address previousOperator = operator;
        operator = _newOperator;
        emit OperatorChanged(previousOperator, operator);
    }

}

The error that I am getting is.

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
Internal JSON-RPC error. { "code": -32000, "message": "execution reverted" }

During compilation, there is no error. This error only shows up when I try to run the saleBronce function.

My question is, how can I change these two lines so that I can make the transfer and at the same time charge a fee?

    require(msg.value >= price);
    payable(msg.sender).transfer(price);
robsonsf
  • 21
  • 2

1 Answers1

0

If you are sending transaction with price parameter is lower than value. There won't be problem on these two lines. The problem may be depend on the lines after that (safeTransferFroms) but for exact analysis, you can add a error reason to your require statements to see what the problem is. Current information is not really enough to decide the revert reason.

  • Thanks, but as I wrote, if I don´t charge any fees, meaning, if I don´t add those two lines, what comes next (safeTransferFroms) works completely fine and I am able to make the transfers. – robsonsf Aug 26 '22 at 06:48
  • Maybe it can caused by if price param is zero? Can you share the parameters you trying to transact with. It would better to understand the situation – Erim Varış Aug 26 '22 at 09:13