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);