How can I reveal NFTs as they are minted? - solidity

My goal is to get my reveal function to only reveal the NFT as it gets bought/minted and not reveal the whole collection so that people don't have to wait, however I just keep mucking it up.
Here is what I've got
string baseURI;
string public baseExtension = ".json";
uint256 public cost = 0.05 ether;
uint256 public maxSupply = 10000;
uint256 public maxMintAmount = 20;
bool public paused = true;
bool public revealed = false;
string public notRevealedUri;
bool public onlyWhitelisted = true;
address[] public whitelistedAddresses;
mapping(address => uint256) public addressMintedBalance;
uint256 public nftPerAddressLimit = 100;
mapping(uint256 => string) private _tokenURI;
constructor(
string memory _name,
string memory _symbol,
string memory _initBaseURI,
string memory _initNotRevealedUri
) ERC721(_name, _symbol) {
setBaseURI(_initBaseURI);
setNotRevealedURI(_initNotRevealedUri);
}
now for the reveal
function reveal() public onlyOwner {
revealed = false;
}
I can't seem to think of a way that would change the bool for specific tokens as they get minted.
My thought was that it should be at the end of the mint function itself, though I am not at all confident in that.
for (uint256 i = 1; i <= _mintAmount; i++) {
addressMintedBalance[msg.sender]++;
_safeMint(msg.sender, supply + i);
_tokenURI[supply].revealed = true; <-- *I know this wont work if anyone ever mints more than one at a time.*
}

You can include a mapping to hold the reveal states of your nfts. I am not sure whether you intended only admin to call this reveal function every time, in this case this would do!
....
mapping(address => mapping(uint256 => bool)) private reveals;
constructor(
string memory _name,
string memory _symbol,
string memory _initBaseURI,
string memory _initNotRevealedUri
) ERC721(_name, _symbol) {
setBaseURI(_initBaseURI);
setNotRevealedURI(_initNotRevealedUri);
}
function reveal(address _nftContract, uint256 tokenId, bool _reveal) external onlyOwner {
bool revealed = reveals[_nftContract][tokenId];
require(!revealed, 'Token Already revealed');
reveals[_nftContract][tokenId] = true;
}

The best method of doing what I asked, is in fact to not mint any yourself, and have a D-app on a website so that total supply is only that of what has been minted, thus can only point to the baseURI then

Related

Error in the implementation of Solidity code

Hi I ran the following code but unfortunately I got this error at run time
ParserError: Expected ';' but got reserved keyword 'in'
--> Reputation.sol:90:30:
|
90 | for (address rater in reputation[seller]){
| ^^
Please help, thank you
`
function rateSeller(address seller, address rater, uint rating) public {
reputation[seller][rater] = rating;
uint reputationSum = 0;
uint numRatings = 0;
uint reputationAverage=0;
for (address rater in reputation[seller]){
reputationSum += reputation[seller][rater];
numRatings++;
}
reputationAverage=reputationSum / numRatings;
return reputationAverage;
}`
I want to calculate the reputation given to each seller for a specific rater. In fact, the collection of all sellers evaluated by a particular evaluator. Please guide. Thank you
Your for() loop syntax is wrong. To edit properly i need more source code. But it should be something like this:
for(uint index; index < arrayName.length; index++) {
your code
}
First of all i added productId variable, that counter product ids. Also changed mapping reputation, now it just sum up sellers reputation. With this approach its way easier to check sellers reputation.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Marketplace {
address payable public owner;
mapping(address => bool) public sellers;
mapping(uint => Product) public products;
mapping(address => mapping(uint => bool)) public purchased;
mapping(address => mapping(uint => bool)) public disputes;
mapping(address => mapping(uint => bytes32)) public reviews;
mapping(address => uint) public reputation;
uint public productId;
struct Product {
address payable seller;
uint price;
string name;
string description;
}
event ProductListed(uint productId);
event ProductPurchased(uint productId);
event DisputeRaised(uint productId);
event DisputeResolved(uint productId);
constructor() {
owner = payable(msg.sender);
}
function addSeller(address seller) public {
require(msg.sender == owner);
sellers[seller] = true;
}
function removeSeller(address seller) public {
require(msg.sender == owner);
sellers[seller] = false;
}
function listProduct(
address payable seller,
uint price,
string memory name,
string memory description) public {
(sellers[seller]);
products[productId] = Product(seller, price, name, description);
productId++;
emit ProductListed(productId);
}
function purchaseProduct(uint _productId, address buyer) public payable {
require(products[_productId].price <= msg.value);
require(!purchased[buyer][_productId]);
products[_productId].seller.transfer(products[_productId].price);
purchased[buyer][_productId] = true;
emit ProductPurchased(_productId);
}
function raiseDispute(uint _productId, address buyer) public {
require(purchased[buyer][_productId]);
disputes[buyer][_productId] = true;
emit DisputeRaised(_productId);
}
function resolveDispute(uint _productId, address payable buyer, bool refund)
public {
require(msg.sender == owner);
require(disputes[buyer][productId]);
if (refund) {
buyer.transfer(products[_productId].price);
}
disputes[buyer][_productId] = false;
emit DisputeResolved(productId);
}
function leaveReview(uint _productId, address buyer, bytes32 review) public {
require(purchased[buyer][_productId]);
reviews[buyer][_productId] = review;
}
function rateSeller(address seller, uint rating) public {
reputation[seller] += rating;
// reputation[seller][rater] = rating;
}
function getProduct(uint _productId) public view returns (address payable, uint,
string memory, string memory) {
return (products[_productId].seller, products[_productId].price,
products[_productId].name, products[_productId].description);
}
function getSellerReputation(address seller) public view returns (uint) {
return reputation[seller];
}
}

Withdraw functions reverts

I'm practicing with smart contracts and still new to them. I have a instance of a contract that i'm practicing making calls too. I can get the deposit function to work when calling it from my contract but cannot get the withdraw function to work. It keeps reverting.
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
import "hardhat/console.sol";
interface Matic {
function deposit() external payable;
function withdraw(uint wad) external;
function balanceOf(address _customerAddress) external view returns(uint256);
}
contract Funds {
Matic public depositFunds;
address public owner;
constructor(address _depositFundsAddress) {
owner = msg.sender;
depositFunds = Matic(_depositFundsAddress);
}
// Fallback is called when DepositFunds sends Ether to this contract.
receive() external payable {
payable(owner).transfer(address(this).balance);
}
function deposits() external payable {
depositFunds.deposit{value: msg.value}();
}
function withDras(uint wad) external {
depositFunds.withdraw(wad);
}
function getBalance(address _owner) external view returns (uint256) {
return depositFunds.balanceOf(_owner);
}
}
here is the contract instance
/**
*Submitted for verification at polygonscan.com on 2021-06-09
*/
// https://firebird.finance DeFi multi-chain yield farms deployer & DEXs aggregator.
// Copyright (C) 2015, 2016, 2017 Dapphub
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
pragma solidity ^0.4.18;
contract WMATIC {
string public name = "Wrapped Matic";
string public symbol = "WMATIC";
uint8 public decimals = 18;
event Approval(address indexed src, address indexed guy, uint wad);
event Transfer(address indexed src, address indexed dst, uint wad);
event Deposit(address indexed dst, uint wad);
event Withdrawal(address indexed src, uint wad);
mapping (address => uint) public balanceOf;
mapping (address => mapping (address => uint)) public allowance;
function() public payable {
deposit();
}
function deposit() public payable {
balanceOf[msg.sender] += msg.value;
Deposit(msg.sender, msg.value);
}
function withdraw(uint wad) public {
require(balanceOf[msg.sender] >= wad);
balanceOf[msg.sender] -= wad;
msg.sender.transfer(wad);
Withdrawal(msg.sender, wad);
}
function totalSupply() public view returns (uint) {
return this.balance;
}
function approve(address guy, uint wad) public returns (bool) {
allowance[msg.sender][guy] = wad;
Approval(msg.sender, guy, wad);
return true;
}
function transfer(address dst, uint wad) public returns (bool) {
return transferFrom(msg.sender, dst, wad);
}
function transferFrom(address src, address dst, uint wad)
public
returns (bool)
{
require(balanceOf[src] >= wad);
if (src != msg.sender && allowance[src][msg.sender] != uint(-1)) {
require(allowance[src][msg.sender] >= wad);
allowance[src][msg.sender] -= wad;
}
balanceOf[src] -= wad;
balanceOf[dst] += wad;
Transfer(src, dst, wad);
return true;
}
}
I'm not sure exactly what it is i'm doing wrong. on the instance itself the withdraw function works fine but on my contract it keeps reverting

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?

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" }
I have implemented the safemoon contract but when I deployed the code in the BSC testnet it's given me an error.
contract SafemoonDemo is Context, IERC20, Ownable {
using SafeMath for uint256;
using Address for address;
mapping (address => uint256) private _rOwned;
mapping (address => uint256) private _tOwned;
mapping (address => mapping (address => uint256)) private _allowances;
mapping (address => bool) private _isExcludedFromFee;
mapping (address => bool) private _isExcluded;
address[] private _excluded;
uint256 private constant MAX = ~uint256(0);
uint256 private _tTotal = 1000 * 10**6 * 10**9;
uint256 private _rTotal = (MAX - (MAX % _tTotal));
uint256 private _tFeeTotal;
string private _name = "SafeMoonDemo";
string private _symbol = "SFD";
uint8 private _decimals = 9;
uint256 public _taxFee = 5;
uint256 private _previousTaxFee = _taxFee;
uint256 public _liquidityFee = 5;
uint256 private _previousLiquidityFee = _liquidityFee;
IUniswapV2Router02 public immutable uniswapV2Router;
address public immutable uniswapV2Pair;
bool inSwapAndLiquify;
bool public swapAndLiquifyEnabled = true;
uint256 public _maxTxAmount = 5000 * 10**6 * 10**9;
uint256 private numTokensSellToAddToLiquidity = 5000 * 10**6 * 10**9;
event MinTokensBeforeSwapUpdated(uint256 minTokensBeforeSwap);
event SwapAndLiquifyEnabledUpdated(bool enabled);
event SwapAndLiquify(
uint256 tokensSwapped,
uint256 ethReceived,
uint256 tokensIntoLiqudity
);
modifier lockTheSwap {
inSwapAndLiquify = true;
_;
inSwapAndLiquify = false;
}
constructor () public {
_rOwned[_msgSender()] = _rTotal;
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E);
// 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;
//exclude owner and this contract from fee
_isExcludedFromFee[owner()] = true;
_isExcludedFromFee[address(this)] = true;
emit Transfer(address(0), _msgSender(), _tTotal);
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns (uint8) {
return _decimals;
}
function totalSupply() public view override returns (uint256) {
return _tTotal;
}
function balanceOf(address account) public view override returns (uint256) {
if (_isExcluded[account]) return _tOwned[account];
return tokenFromReflection(_rOwned[account]);
}
function transfer(address recipient, uint256 amount) public override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}
function allowance(address owner, address spender) public view override returns (uint256) {
return _allowances[owner][spender];
}
function approve(address spender, uint256 amount) public override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
return true;
}
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
return true;
}
Your constructor is trying to call the factory() function on the 0x10ED43C718714eb63d5aA57B78B54704E256024E address, expecting a 20-byte response that it would convert to an address type.
Even though there is the PancakeSwap router contract deployed on this address on the BSC mainnet, the BSC testnet doesn't hold any contract on this address.
So your constructor fails as it doesn't get the 20-byte response (from the non-contract address) that it expects.
The Pancakeswap docs page doesn't list any testnet addresses, so it's possible that there are no official Pancakeswap contracts on the testnet.

Error on BSCSCAN : Unable to generate Contract ByteCode and ABI

I'm really new to coding and crypto creation but i'm trying to verify my contract source since 2 hours but i always get the error :
" Error! Unable to generate Contract ByteCode and ABI
Found the following ContractName(s) in source code : Token
But we were unable to locate a matching bytecode (err_code_2)"
Here is my code :
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.2;
contract Token {
mapping(address => uint) public balances;
mapping(address => mapping(address => uint)) public allowance;
uint public totalSupply = 100000000000000000 * 10 ** 18;
string public name = "No Marketing Coin";
string public symbol = "NOMK";
uint public decimals = 18;
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
constructor() {
balances[msg.sender] = totalSupply;
}
function balanceOf(address owner) public returns(uint) {
return balances[owner];
}
function transfer(address to, uint value) public returns(bool) {
require(balanceOf(msg.sender) >= value, 'balance too low');
balances[to] += value;
balances[msg.sender] -= value;
emit Transfer(msg.sender, to, value);
return true;
}
function transferFrom(address from, address to, uint value) public returns(bool) {
require(balanceOf(from) >= value, 'balance too low');
require(allowance[from][msg.sender] >= value, 'allowance too low');
balances[to] += value;
balances[from] -= value;
emit Transfer(from, to, value);
return true;
}
function approve(address spender, uint value) public returns (bool) {
allowance[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
}
Thanks to everyone if you help me !
I got the same error message when I was verifying my smart contract on the rinkeby testnet, I happened to use Remix for deployment.
The Solution:
When I switched to Hardhat, copied my contract codes from my code editor. Then Boom! It worked, my contract was verified.
Don't know why pasting from remix directly wasn't working.
I'm a bit late but I suggest you to make sure that the compiler version is exactly the same as in Remix and that the license field is set to the same as in the source code.
My problem solved by setting optimization to 200.

DeclarationError: Undeclared identifier. Did you mean "balanceOf" or "blances"?

I am trying to create my own token using Solidity on Remix Ethereum. I use MetaMask and logged in Binance Smart Chain Testnet . But when I use Solidity Compiler to compile, it keeps showing this error on lines 15, 19, 24, 25, 33, 34:
DeclarationError: Undeclared identifier. Did you mean "balanceOf" or "blances"?
Here the code:
pragma solidity ^0.8.2;
contract MyTestToken {
mapping(address => uint) public blances;
mapping(address => mapping(address => uint)) public allowance;
uint public totalSupply = 1000000000000 * 10 ** 18;
string public name = "MyToken Test";
string public symbol = "TKNT";
uint public decimals = 18;
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
constructor () {
balances[msg.sender] = totalSupply;
}
function balanceOf(address owner) public view returns(uint) {
return balances[owner];
}
function transfer(address to, uint value) public returns(bool) {
require(balanceOf(msg.sender) >= value, 'balance too low');
balances[to] += value;
balances[msg.sender] -= value;
emit Transfer(msg.sender, to, value);
return true;
}
function transferFrom(address from, address to, uint value) public returns(bool) {
require(balanceOf(from) >= value, 'balance too low');
require(allowance[from][msg.sender] >= value, 'allowance too low');
balances[to] += value;
balances[from] -= value;
emit Transfer(from, to, value);
return true;
}
function approve(address spender, uint value) public returns(bool) {
allowance[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
}
Thank you in advance.
It's caused by a typo in your code. Rename the mapping blances to balances.
mapping(address => uint) public blances; // original code
mapping(address => uint) public balances; // corrected