Solidity: The transaction has been reverted to the initial state - solidity

I have a separate function openTrading()
function openTrading() external onlyOwner() {
require(!tradingOpen,"trading is already open");
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E);
uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
.createPair(address(this), _uniswapV2Router.WETH());
uniswapV2Router = _uniswapV2Router;
}
This was working fine few days ago. Now it is failing, debugging in JVM reveals it is failing at
_uniswapV2Router.factory()
Error:
The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
I am completely lost, no idea why something that was working earlier now failing. To test this, I copied old deployed contract and it failed exact same place.
Could someone please suggest me how to resolve this issue?

Related

HardHat - Fail - Unable to verify / Etherscan

Here's my contract.
// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.3;
contract terceiroTest {
// We pass and old String, a new string and when this event is
// broadcast everybody is able to see that the even happened.
// and see the strings exposed too.
event UpdatedMessages(string oldStr, string newStr);
string public message;
// When this contract is deployed we require an argument passed called initMessasge
constructor (string memory initMessage) {
message = initMessage;
}
function update(string memory newMessage) public {
string memory oldMsg = message;
message = newMessage;
emit UpdatedMessages(oldMsg, newMessage);
}
}
and it gives me the error:
I've tried to find any kind of description about this error, even changed solidity's version. I'm studying about smartcontracts still, if someone having or had the same error I would apretiate for enlighting me. Thanks.
I have had similar problems and they all resolved by themselves. It is possible that there is no issue in your code and the issue is with etherscan.
Here are a few things I recommend to triage:
Submit again
Give gap between submission and verification
Try a different env like polygonscan.
Manually submit from their UI.
I took your contract and tested it in my hardhat env. Everything worked fine. Maybe there's something else happening in your local setup?
Try again with a fresh hardhat setup. Try using this 'badass hardhat setup' https://github.com/mistersingh179/badass-hardhat-setup
I used that setup as a clean slate, started my localhost chain, and then connected that to my sidekik interface to test the functions. Your code works fine. Example here:

Error: missing revert data in call exception on tokenOfOwnerByIndex and tokenByIndex from openzeppelin ERC721Enumerable

I'm getting a odd error across several calls out to openzeppelin that
Error: missing revert data in call exception; Transaction reverted without a reason string
I didn't change anything and am using out of the box openzeppelin and minimal test harness code
contract MyToken is ERC721Enumerable
This works fine and I successfully call several internal functions from my tests like
const totalSupply = await myTokenContract.totalSupply();
const ownerBalance = await myTokenContract.balanceOf(owner.address);
these both work just fine
However, when calling functions like tokenOfOwnerByIndex or tokenByIndex I get that Error
const ownerToken = await myTokenContract.tokenOfOwnerByIndex(
owner.address, ethers.BigNumber.from(1).toNumber()
);
const ownerToken = await myTokenContract.tokenOfOwnerByIndex(
owner.address, 1
);
const firstToken = await myTokenContract.tokenByIndex(1);
These each produce the same error that
Error: missing revert data in call exception; Transaction reverted without a reason string
I'm assuming it's some environmental configuration issue but I can't quite seem to figure it out?
hardhat 2.9.3, openzeppelin/contracts 4.6.0, solidity 0.8.1

Empty Events Array on UI but works on Test | Need TokenId from Transaction

I'm working on an app that allows the user to create NFTs and list them on a market place. When I try to create the token on the UI using metamask, a createToken function is being called. The resolved promise of createToken is an object for which I expect an events key with 2 events (I'm able to confirm this by running npx hardhat test. However I don't actually see these events emitted to the UI... I need these events to get the tokenId. If someone knows an alternative way to get the tokenId I'm open to that as well.
createToken:
contract NFT is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
address contractAddress;
constructor(address marketplaceAddress) ERC721("Metaverse", "METT") {
contractAddress = marketplaceAddress;
}
function createToken(string memory tokenURI) public returns (uint256) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(msg.sender, newItemId);
_setTokenURI(newItemId, tokenURI);
// Not emitting this event to the UI but it works in the test
setApprovalForAll(contractAddress, true);
return newItemId;
}
}
The function on the UI looks like below where createSale creates a listing on the marketplace:
async function createSale(url) {
const web3Modal = new Web3Modal();
const connection = await web3Modal.connect();
const provider = new ethers.providers.Web3Provider(connection);
const signer = provider.getSigner();
/* next, create the item */
let contract = new ethers.Contract(nftAddress, NFT.abi, signer);
let transaction = await contract.createToken(url);
let tx = await transaction.wait();
// Seeing a successful transaction
console.log("tx ==> ", tx);
let event = tx.events[0];
// Breaks here sine event[0] is `undefined`
let value = event.args[2];
let tokenId = value.toNumber();
const price = ethers.utils.parseUnits(formInput.price, "ether");
/* then list the item for sale on the marketplace */
contract = new ethers.Contract(nftMarketAddress, Market.abi, signer);
let listingPrice = await contract.getListingPrice();
listingPrice = listingPrice.toString();
transaction = await contract.createMarketItem(nftAddress, tokenId, price, {
value: listingPrice,
});
await transaction.wait();
router.push("/");
}
Below is a screenshot of the resolved promise with the empty events array:
The nftAddress is empty - it doesn't hold the NFT contract.
The blockNumber property has value 1, which means this transaction was mined in the first block. Hardhat network automines by default - creates a new block for each transaction. Which makes the attached transaction the first one on this network instance, and rules out any possible previous transactions deploying the contract (to this network instance).
When you send a valid transaction to an empty address, it passes through as there's no contract to revert it - but also there's no contract to emit event logs. So it results in an empty events array - just like on the attached screenshot.
I expect an events key with 2 events (I'm able to confirm this by running npx hardhat test
When you run a hardhat test, it creates a network instance, deploys the contracts, runs the test scripts, and then destroys this network instance.
Same goes for when you run npx hardhat node - it creates a new network instance, and when you stop running the node, it destroys its data (along with deployed contracts).
Hardhat network doesn't seem to have a way to save its state and load it later. (Anyone please correct me if I'm mistaken - I just couldn't find anything related in the docs). So you'll might have to redeploy the contract each time you run npx hardhat node. Or to use a different network that supports this feature (e.g. Ganache and its Workspaces) - you'll still be able to use the Hardhat library for the tests, it will just connect to this other network instead of the default Hardhat network.
I had this exact same issue. I solved mine by using the ropsten test network instead.

Error in creating uniswap pair for smart contract

I am getting the below error on calling function setRouterAddress with router address 0xD99D1c33F9fC3444f8101754aBC46c52416550D1 in testnet.
Gas estimation failed error
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
execution reverted
this is message is coming for this line of code in the below function. IUniswapV2Factory(_uniswapV2Router.factory()).createPair(address(this), _uniswapV2Router.WETH());
declarations:
IUniswapV2Router02 public unswapV2Router;
address public unswapV2Pair;
Function:
function setRouterAddress(address newRouter) external onlyOwner() returns(address, address){
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(newRouter);
// Create a uniswap pair for this new token
uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory()).createPair(address(this), _uniswapV2Router.WETH());
// set the rest of the contract variables
uniswapV2Router = _uniswapV2Router;
return (newRouter, uniswapV2Pair);
}
Could someone guide what is the problem ?

Web3 Revert Message

I am using web3 over the Rinkeby test network for my solidity project. I am trying to get the exact revert message, using the error object.
try{
await contract.methods.acceptBattle(id).send({
from: address,
value:val
});
return '';
}
catch(e){
console.log(e.message);
}
After the code is running in the catch block, I am getting the output in the following format:
execution reverted: This battle isn't exist.
0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001b426174746c65206e7
56d6265722069736e27742065786973742e0a0000000000
Is there a way to get only the message itself (in the first row) and not the address coming after?
As I saw in Do all Ethereum networks return the revert reasons as a “message” field?
and since I am running over the Rinkeby test network, the address supposed to be part of the data field of the error and not part of the message.
This is the relevant code for the revert message:
function acceptBattle(uint256 battle_id) public payable{
Battle storage bate=battleInfo[battle_id];
require(bate.amountBet>0, "Battle number isn't exist.\n");
require(bate.creator!=msg.sender, "Impossible to fight against yourself.");
require(bate.creator==bate.opponent, "This battle is closed, opponent already exist.");
require(msg.value==bate.amountBet, "Betting value isn't as specified for this battle.");
bate.opponent=msg.sender;
}