convert uint to string is possible now in solidity? - solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
contract GoTekERC721 is ERC721, Ownable {
constructor() ERC721("Go NFT", "GoN") {}
//Owner to Phone
mapping(address => uint256[]) public phones;
//change
// Phone to Balance
mapping(uint256 => uint256) public balance;
function register(uint256 phone, uint256 Balance) public {
_mint(msg.sender, phone);
phones[msg.sender].push(phone);
balance[phone] = Balance;
}
function details(address owner) public view returns(string memory) {
uint256[] memory ownerPhones = phones[owner];
mapping(uint256 => uint256) storage userBalances;
for (uint256 i = 0; i < ownerPhones.length; i++) {
uint256 phone = ownerPhones[i];
userBalances[phone] = balance[phone];
}
return userBalances;
}
}
in solidity 0.8.9 it gets error.i don't understand what can i do to convert my mapping to string and get o/p like {"phone":"Balance","phone":"Balance"} ...thanks in adanced

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];
}
}

change mapping function method to function() method solidity

can anyone help to change this code:
mapping(address => uint) public balances;
into function
Full code:
pragma solidity ^0.8.0;
contract SmartSHop{
mapping(address => uint) public balances;
function deposit() public payable{
balances[msg.sender] += msg.value;
}
function withdraw(uint _amount) public{
require(balances[msg.sender]>= _amount, "Not enough ether");
balances[msg.sender] -= _amount;
(bool sent,) = msg.sender.call{value: _amount}("Sent");
require(sent, "failed to send ETH");
}
function getBalance(address _owner) public view returns (uint){
return address(_owner).balance;
}
}

Metadata is not showing on opensea

i am trying to develop a nft mint daap. i deploy the erc 721 contract on bsc test net(0x5542bD28FD0D8c22D4AC7D51E62735Ac32d32374) The mint function is working but the metadata is not showing on opensea test net. for metadata and image i am using pinata, here is the link for metadata
https://gateway.pinata.cloud/ipfs/Qmcchbbm2R6CdiwsYbcoLPcY7PSi7yska9HzDCvK977mMU.
the steps i am following: deploy the token, enable public mint , setting the tokenUri.
code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
contract Minter is ERC721, Ownable{
string private baseTokenURI;
uint256 public mintPrice;
uint256 public totalSupply;
uint256 public maxSupply;
uint256 public maxPerWallet;
bool public isPublicMintEnabled;
address public withdrawWallet;
mapping (address=> uint256) public walletMints;
constructor() payable ERC721("NFTTutorial", "NFT") {
mintPrice= 1 ether;
totalSupply = 0;
maxSupply=14400;
maxPerWallet= 25;
withdrawWallet=0x658385eb2Abf80C76DEeF0d99491d41Fd7B638ff;
}
function setIspublicMintEnable( bool isPublicMintEnabled_) external onlyOwner{
isPublicMintEnabled= isPublicMintEnabled_;
}
function mint(uint256 quantity_) public payable{
require(isPublicMintEnabled,"minting not enabled");
//require(msg.value== quantity_* mintPrice, " wrong mint value");
require(totalSupply+quantity_<= maxSupply,"sold out");
require(walletMints[msg.sender]+ quantity_<= maxPerWallet);
for (uint256 i=0; i <quantity_; i++){
uint256 newTokenId=totalSupply+1;
totalSupply++;
walletMints[msg.sender] = walletMints[msg.sender] + quantity_;
_safeMint(msg.sender, newTokenId);
}
}
/// #dev Returns an URI for a given token ID
function _baseURI() internal view virtual override returns (string memory) {
return baseTokenURI;
}
/// #dev Sets the base token URI prefix.
function setBaseTokenURI(string memory _baseTokenURI) public {
baseTokenURI = _baseTokenURI;
}
function withdraw(address payable _to, uint _amount) public {
_to.transfer(_amount);
}
}

I'm getting an error, a declaration error but its pointing to something in quotes which i don't understand

the error I 'm getting reads
DeclarationError: Undeclared identifier.
--> SharedWallet.sol:17:17:
|
17 | require(isOwner() || allowance[msg.sender] >= _amount, "You are not allowed!");
| ^^^^^^^
pragma solidity ^0.8.6;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";
contract Allowance is Ownable {
mapping(address => uint256) public allowance;
function addAllowance(address _who, uint256 _amount) public onlyOwner {
allowance[_who] = _amount;
}
modifier ownerOrAllowed(uint256 _amount) {
require(
isOwner() || allowance[msg.sender] >= _amount,
"You are not allowed!"
);
_;
}
function reduceAllowance(address _who, uint256 _amount)
internal
ownerOrAllowed(_amount)
{
allowance[_who] -= _amount;
}
}
contract SharedWallet is Allowance {
uint256 public balanceReceived;
function sendEther() public payable {
balanceReceived += msg.value;
}
function isOwner() internal view returns (bool) {
return owner() == msg.sender;
}
function withdrawMoney(address payable _to, uint256 _amount)
public
ownerOrAllowed(_amount)
{
require(
_amount <= address(this).balance,
"not enough funds in contract"
);
if (!isOwner()) {
reduceAllowance(msg.sender, _amount);
}
_to.transfer(_amount);
}
fallback() external payable {}
}
pragma solidity ^0.8.6;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";
contract Allowance is Ownable {
mapping(address => uint256) public allowance;
// This will fix compile error, or, need use inheritance to prevent duplicate code.
function isOwner() internal view returns (bool) {
return owner() == msg.sender;
}
function addAllowance(address _who, uint256 _amount) public onlyOwner {
allowance[_who] = _amount;
}
modifier ownerOrAllowed(uint256 _amount) {
require(
isOwner() || allowance[msg.sender] >= _amount,
"You are not allowed!"
);
_;
}
function reduceAllowance(address _who, uint256 _amount)
internal
ownerOrAllowed(_amount)
{
allowance[_who] -= _amount;
}
}
contract SharedWallet is Allowance {
uint256 public balanceReceived;
function sendEther() public payable {
balanceReceived += msg.value;
}
function isOwner() internal view returns (bool) {
return owner() == msg.sender;
}
function withdrawMoney(address payable _to, uint256 _amount)
public
ownerOrAllowed(_amount)
{
require(
_amount <= address(this).balance,
"not enough funds in contract"
);
if (!isOwner()) {
reduceAllowance(msg.sender, _amount);
}
_to.transfer(_amount);
}
fallback() external payable {}
}

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