I've been trying to store 10000 16byte strings (the string that I used is 'abcdefghijklmnop') in a solidity mapping: mapping(uint256 => string)
I've tried sending all 10000 strings in one transaction and it always exceeds the maximum gas limit and fails.
Divided those 10000 strings into equal 10 arrays and tried to do 10 transactions.. The first transaction, with the first 1000 strings happens successfully but the following transactions fail due to a gas issue
Solidity Code:
uint256 public totalGenomes;
mapping(uint256 => string) public genomesList;
function addGenome(string[] memory genome) public {
for (uint256 index = totalGenomes; index < (1000 + totalGenomes); index++) {
genomesList[index] = genome[index];
}
totalGenomes += 1000;
}
Hardhat script: there are 10 files genomes0.txt, genomes1.txt, genomes2.txt .... each containing 1000 lines of 'abcdefghijklmnop'
for (i = 0; i < 10; i++) {
let filepath = `./img/genomes${i}.txt`;
let genomes1000 = [];
genomes1000 = fs.readFileSync(filepath).toString().split('\n');
console.log(filepath);
await svgNFT.addGenome(genomes1000);
}
Error:
Error: cannot estimate gas; transaction may fail or may require manual gas limit
I also intend to minimise the gas price for the whole process.
Have anyone here got prior experience on such a scenario?
UPDATE: I got past the above problem.
I did this suggested by Miguel_LZPF on Hardhat discord:
contract.function(param1, param2..., lastfunctionparam, {gasLimit: xxxxx})
and define the gasLimit and gasPrice manually. And now, I'm stuck in another problem.
https://rinkeby.etherscan.io/address/0x11740C2367a0F0465d31b3612B3A5464dC7c8Afb
Warning! Error encountered during contract execution [execution
reverted]
Still the first transaction in the loop happens to be successful the rest fails.
Related
I am new to solidity and for this project I am trying to do a reverse lottery drawing, where the losers are drawn and appended until only the winner remains.
I keep getting a gas estimation error:
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
gas required exceeds allowance (29970705)
I have tried upping my gas when I deploy on remix and all that does is up my cost when I run - it currently estimates 75000ethererum for running: drawLoser()
I use fulfullRandomWords which calls Chainlink VRF - which gives me a verified random seed, I then use this seed in drawLoser(). current_supply is hardcoded at 10000 for testing. I then use my random seed to make a random number and check if that number exists in my losers array, if it does not exist, I increment and generate a new number from the same seed and repeat until I find a new number. I am trying to do all of this without storing anything except my uint16 entry in my loser array, but I am guessing I am doing something stupid and storing more than I realize on the blockchain since the gas fee is absurd.
Thank you for any help!
function fulfillRandomWords(
uint256, /* requestId */
uint256[] memory randomWords
) internal override {
s_randomWords = randomWords;
}
function exists1(uint16 num) public view returns (bool) {
for (uint i = 0; i < losers.length; i++) {
if (losers[i] == num) {
return true;
}
}
return false;
}
//Right now I need to fulfill randomwords and then drawLoser - draw loser does not generate a new seed
function drawLoser() public {
//fulfillRandomWords;
uint16 drawing;
uint i = 0;
uint j = 1;
//generate 10% of total entrys as losers
while(i < 10*(current_supply-getCountLosers())/100) { //current_supply is entrys in lotto
drawing = uint16(uint256(keccak256(abi.encode(s_randomWords[0], j)))%(current_supply-losers.length)+1);
if (exists1(drawing) == true){
j++;
}
if (exists1(drawing) == false){
losers.push(drawing);
i++;
}
}
}
error: Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? gas required exceeds allowance (29970705)
This means that your transaction is going to fail to tripped require() or revert.
I have a problem. The problem is that i want a max mint supply for the whitelist sale and public sale. So for example;
In total i have 10.000 NFT's. There will be an whitelist sale and a public sale. For the whitelist sale i have 2.000 wallet addresses, but i only want them to be able to mint 1.500 NFT's. and in the public sale i want the remaining 8.500 NFT's to be sold.
I already tried somethings with the normal maxsupply but can't figure it out. I want a max mint per wallet like 10 and cant seem to limit the supply to 1500 for only the whitelist
Does anyone can explain this to me or have a code example?
I don't recommend limiting your whitelist per-wallet minting amount (you want to mint out, don't you?!), but I understand the reasons why you might. I'll provide both options. All code is abbreviated for brevity sake:
*Note 1: We will be using OpenZeppelin Utilities - Counters for tracking minted progress. You can also consider using totalSupply(), however, if burned tokens are a concern totalSupply() will decrement and throw off your count, whereas Counters will not.
Note 2: This assumes you're whitelist occurs before public and that you're not also juggling a reserve count as well - additional checks and counters would be required for that.
Note 3: This covers ONLY the check for limiting whitelist; you will obviously also need additional checks for valid whitelist account, sufficient payment, etc.
Limit Whitelist Total Supply
...
import "#openzeppelin/contracts/utils/Counters.sol";
...
error ExceededWhitelistSupply();
...
using Counters for Counters.Counter;
uint256 public maxSupply = 10000;
uint256 public maxWhitelistSupply = 1500;
Counters.Counter private totalWhitelistSupply;
...
function mintWhitelist(uint256 _qty) external payable {
if ( totalWhitelistSupply.current() + _qty > maxWhitelistSupply ) revert ExceededWhitelistSupply();
for (uint256 i = 0; i < _qty; i++) {
totalWhitelistSupply.increment();
}
_mint(msg.sender, _qty, '', true);
}
Limit Wallet && Limit Whitelist Total Supply
...
import "#openzeppelin/contracts/utils/Counters.sol";
...
error ExceededWhitelistSupply();
error ExceededMaxPerWallet();
...
using Counters for Counters.Counter;
uint256 public maxSupply = 10000;
uint256 public maxWhitelistSupply = 1500;
uint256 public maxWhitelistPerWallet = 10;
Counters.Counter private totalWhitelistSupply;
mapping(address => uint256) public whitelistMintedAmount;
...
function mintWhitelist(uint256 _qty) external payable {
if ( whitelistMintedAmount[msg.sender] + _qty > maxWhitelistPerWallet ) revert ExceededMaxPerWallet();
if ( totalWhitelistSupply.current() + _qty > maxWhitelistSupply ) revert ExceededWhitelistSupply();
for (uint256 i = 0; i < _qty; i++) {
totalWhitelistSupply.increment();
}
whitelistMintedAmount[msg.sender] += _qty;
_mint(msg.sender, _qty, '', true);
}
Here, we've used mapping - good tutorial here - to track the number of NFTs that have been minted to this wallet (preferred method, as this won't be fooled by the account transferring NFTs out of the wallet and then minting more). If you want to go WAY down the rabbit hole, you can also look at trash-canning this whole approach and learn up on this approach for handling your whitelist.
Keep in mind that there are more checks and balances that you'll need to add (e.g., visualizing this in the front end of your dApp to avoid minting when they shouldn't be able to, additional validation layers in your mint functions, etc.), but this should provide you with the core pieces needed for limiting by a max wallet and max supply. I apologize for any code errors - this is my first StackOverflow answer and the short-handing and readability of the code is a bit difficult to error check.
Have a whitelistMaxSupply
Have a getMaxSupply function
Have a finishWhitelist function
Have a maxSupply
Unless whitelist finished, getMaxSupply will return whitelistMaxSupply, after finished, it will return maxSupply
Profit
There are many ways to solve your problem, this is just the first one that came to my mind
function getMaxSupply() view public returns(uint256){
if(whitelistFinished){
return maxSupply;
}
return whitelistMaxSupply;
}
function finishWhitelist() public{
whitelistFinished = true;
}
I am fairly new to developing smartcontracts and have run into an issue while testing. My intention is to ensure the smartcontract cannot mint more than 13 ERC721 tokens. My understanding is that a require function can have a second string argument that will revert a string as an error if the require condition is not met. Is this correct?
The following is my smart contract code:
contract TheCondemned_Episode is ERC721Enumerable {
string[] public episodes;
constructor() ERC721("TheCondemned_e1", "TCe1") public {
}
function mint(string memory _episode) public {
require(episodes.length <= 13, "Cannot make more than 13 episodes");
episodes.push(_episode);
uint _id= episodes.length;
_mint(msg.sender, _id);
}
}
And the test I am running is as follows:
it('Cannot create more than 13 episodes', async() => {
for(var i=0; i===13; i++){
var episode= `Episode ${i}`
await contract.mint(episode)
}
try {
await contract.mint('Episode 14');
assert(true);
}
catch (err) {
return;
}
assert(false, "The contract did not throw.");
The test fails and returns "The contract did not throw". What is the best practice in regards to catching a revert string from a failed require condition when testing?
My understanding is that a require function can have a second string argument that will revert a string as an error if the require condition is not met. Is this correct?
That's correct. Here's an example of an always failing require() condition that throws an exception with the error message.
require(false, 'Error message');
However, you have a logical error in the Solidity require() condition, as well as in the JS test snippet.
First, let's uncover the Solidity code. For simplicity, let's assume you're allowing to mint only 1 episode.
require(episodes.length <= 1, "Cannot make more than 1 episode");
First iteration (expected to pass)
episodes.length is 0, that's <= 1. Condition passes, you mint the first token, and then push to the episodes array, so its length becomes 1 after the condition.
Second iteration (expected to fail)
episodes.length is 1, that's still <= 1. So the condition passes as well.
Solution: Replace the <= (less than or equal) to just < (less than).
require(episodes.length < 1, "Cannot make more than 1 episode");
First iteration (expected to pass)
episodes.length is 0, that's < 1. Condition passes, you mint the first token, and then push to the episodes array, so its length becomes 1 after the condition.
Second iteration (expected to fail)
episodes.length is 1, which fails the condition 1 < 1, as you expect.
I'm assuming that your intention in the JS snippet is to call the mint() function 13 times in the loop, and then 14th time in the try/catch block.
However, the loop currently doesn't perform any iteration. So in fact, you're only executing the mint() function once (in the try/catch block).
The second parameter in the for loop is a condition stating "this loop will keep iterating for as long as this condition is met". But since you set the value of i to 0 in the first parameter, the loop condition (i===13) is not met, and the loop doesn't perform even the first iteration.
Solution: Check whether the i is "less than 13" instead of just "equals 13".
for(var i = 0; i < 13; i++) {
This way, the loop will iterate 13 times.
I have a state variable in my smart contract called "_ownerAddresses". It is an array of addresses. In one of my functions, I remove addresses from this array depending on a condition. I was wondering what is the best approach to doing this in a fast and memory-efficient way. Should I delete addresses while in the for loop and shrink the array? Should I create a new storage array that just adds the addresses I am keeping and then assign "_ownerAddresses" to this new array? Here is the excerpt of code:
struct Sub {
uint256 amount;
uint256 createdTime;
}
mapping(address => Sub) private _addrBalances;
address[] private _ownerAddresses;
uint MONTH = 30 days;
uint256 private _totalSupply;
function expireAccounts() external onlyOwner(_owner) {
address[] storage _ownersLeft;
for (uint i = 0; i < _ownerAddresses.length; i++) {
if ((_addrBalances[_ownerAddresses[i]].createdTime + MONTH) >= block.timestamp) {
_totalSupply -= _addrBalances[_ownerAddresses[i]].amount;
_addrBalances[_ownerAddresses[i]].amount = 0;
_addrBalances[_ownerAddresses[i]].createdTime = 0;
} else {
_ownersLeft.push(_ownerAddresses[i]);
}
}
_ownerAddresses = _ownersLeft;
}
Any input would be great. Thanks!
Once my team conducted a study of the efficiency of EVM in processing iterative cycles - the results are reflected in the article https://habr.com/ru/company/raiffeisenbank/blog/354122/ (unfortunately, in Russian). In short, with the number of iterations over 1000, the cost of even the simplest operations will reach millions of gas units. Therefore, if the size of the array is several thousand, then the cost of execution expireAccounts() can become very high, and there is even a risk that it will exceed the gas limit per block and will not be executed at all.
Therefore, instead of concentrating the "expire" functionality in a single function for all accounts, I would insert it into the functions for processing the single accounts. Most likely, in this case, you can also refuse the _ownerAddresses array.
i am trying to finish the solidity code in which it includes arithmatic operator '>=' it is throwing an error someone please help me with the error
the error is - 'expected ; but got <='
This is for a Smart Contract to check the draw between 3 people i have tried on different solidity versions but it throwing an error for - int count <=
`
function isDraw() view public returns(bool){
uint256 a = getplayerGuessNum(getPlayers()[0]);
uint256 b = getplayerGuessNum(getPlayers()[1]);
uint256 c = getplayerGuessNum(getPlayers()[2]);
int count <= 3;
if(a==b || a==c || b==c){
return false;
count++;
}
else{
return true;
}
}
`
The code above should check 3 times if there is draw between the a,b,c but i am getting an error # int count <= 3
If you want to check three times you should put the whole block that you want to run three times in a loop (for, while).
If what you want is to initialize count to 3 then change
int count <= 3;
to
int count = 3;
Anyway that line is wrong because after a variable declaration you should only end the line or use an assignment