How to check values of a variables and functions in solidity like javascript via console.log - solidity

How to check values of a variables and functions in solidity like javascript via console.log? Because i need visibility of results for deep understanding.
I am newbie...
i tried hardhat but need more understand.

I'm a newbie too, but I know this (I think)!
There is no console in solidity, you need to write a function for this, or just put it public in the variable itself!
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Value {
uint public num; //add "public" to see the value
function add(uint _num) external{
num += _num;
}
//this function is for returning value
function viewNum() public view returns(uint){
return(num);
}
}
Also use the Remix editor, it's in the browser. There are plenty of videos on the internet!

If you are using hardhat, then you can use console.sol.
You need to import it into your contract.
import "hardhat/console.sol";
Now, you are able to log your variable while you are working.
console.log(__your_variable__);
If you are still looking to log your variables once you deploy your contract, then you need to use event functionality.

Import hardhat console:
import "hardhat/console.sol";
Use console log in your contract
console.log(variable); //Variable value
console.log(1234); //direct value

Related

How can I get metadata and my image to show up on OpenSea Testnet?

I followed along with Remix's beginner NFT course and successfully deployed a few NFTs using the Goerli testnet and their provided IPFS data. I uploaded my own image and metadata and can see it on IPFS but neither the metadata nor the image is populating on OpenSea.
Here is the code for the contract I am deploying:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;
import "#openzeppelin/contracts#4.4.0/token/ERC721/ERC721.sol";
import "#openzeppelin/contracts#4.4.0/access/Ownable.sol";
contract Donation is ERC721, Ownable {
constructor() ERC721("Donation", "DONO") {}
function _baseURI() internal pure override returns (string memory) {
return "https://ipfs.io/ipfs/QmXZKcU9WDZxvXvxoAL4YdZVR5Ssj97ayEYRPqYBHrRSb2";
}
function safeMint(address to, uint256 tokenId) public onlyOwner {
_safeMint(to, tokenId);
}
}
Please see the URL that I return for my metadata and subsequent link to my image. Is there anything you see that is immediately wrong that would indicate why nothing is populating (in the JSON file, code, or otherwise)?
For ERC721, the _baseURI will essentially be the base to be combined with the NFT's token id, so for example NFT with token ID 0 will have the tokenURI of:
https://ipfs.io/ipfs/QmXZKcU9WDZxvXvxoAL4YdZVR5Ssj97ayEYRPqYBHrRSb2/0
which in this case after checking is invalid as https://ipfs.io/ipfs/QmXZKcU9WDZxvXvxoAL4YdZVR5Ssj97ayEYRPqYBHrRSb2 should be the valid tokenURI itself. OpenZeppelin designed the ERC721 contract this way as it is the most gas-efficient way to create a standard ERC721 NFT. However, the drawback is that it made it difficult to provide modified URI for each token ID.
If you like to set tokenURI with different base for different token ID, then you should instead check ERC721URIStorage in the contract wizard. This way, the ERC721 NFT contract will have the _setTokenURI(tokenId, uri) function, which allows you to modify the tokenURI for different token ID. However, keep in mind that this will be very expensive on the user side as string inputs cost a lot in EVMs.
Hope this helps~

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:

Cannot Compile StorageFactory.sol because "not found contracts/SimpleStorage.sol

I am doing the Solidity, Blockchain, and Smart Contract Course – Beginner to Expert Python Tutorial from freeCodeCamp and Patrick AlphaC
https://github.com/PatrickAlphaC/storage_factory
Below is my code image from my IDE too here
// SPDX-License_Identifier: MIT
pragma solidity ^0.8.0;
import "./SimpleStorage.sol";
contract StorageFactory {
SimpleStorage[] public simpleStorageArray;
function createSimpleStorageContact() public {
SimpleStorage simpleStorage = new SimpleStorage();
simpleStorageArray.push(simpleStorage);
}
}
and I am getting the error:
not found contracts/SimpleStorage.sol
even though all the files are spelt correctly and in the same folder.
Please help as I am trying to follow along with the tutorial and don't want to get left behind.
In your picture your filename is wrong. You write SimpleSorage but it must be SimpleStorage. (Note the missing t in Storage.)

How to correctly import SafeMath.sol into contract

This has been a problem I've been dealing with for a while now. My temporary solution has been to create a SafeMath.sol file in my Contracts directory and directly import it from there. However, I've been looking for a 'clearer solution' to this... Old way seemed to be directly importing it from a GitHub link, as seen in some repos and other stack overflow posts like such
However, the proper way do this seems to be installing the corresponding oz package (#openzeppelin/contracts-ethereum-package) and importing the file directly into the needed contract i.e.
import "#openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol";
However, using VSCode, I still get the error Source "#openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol" not found: File import callback not supported
That said, how can I properly import SafeMath?
EDIT: I am using pragma solidity ^0.6.0;
I looked through the node_modules for the package #openzeppelin/contracts. Here is the current correct import path as of posting:
import "#openzeppelin/contracts/utils/math/SafeMath.sol";
This is no longer needed with Solidity version 8
path is different in v 3.0 documentation
I used this:
import "#openzeppelin/contracts/math/SafeMath.sol";
Instead of this:
import "#openzeppelin/contracts/utils/math/SafeMath.sol";
Can be used in the following way:
pragma solidity ^0.5.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.3.0/contracts/math/SafeMath.sol";
contract contractForSafeMath {
using SafeMath for uint;
uint256 addResult;
uint256 subResult;
uint256 mulResult;
uint256 divResult;
uint256 modResult;
uint public MAX_VALUE = 2**256 -1;
//overflow
uint256 overflowResult;
function getResults(uint256 a, uint256 b) public{
addResult = SafeMath.add(a,b);
subResult = SafeMath.sub(a,b);
mulResult = SafeMath.mul(a,b);
divResult = SafeMath.div(a,b);
modResult = SafeMath.mod(a,b);
overflowResult = SafeMath.add(SafeMath.add ( (a**b), SafeMath.mul (SafeMath.mul (b, a), b)), (b** b));
}
function addResultVal(uint a, uint b) public view returns(uint256){
return addResult;
}
function subResultVal() public view returns(uint256){
return subResult;
}
function mulResultVal() public view returns(uint256){
return mulResult;
}
function divResultVal() public view returns(uint256){
return divResult;
}
function modResultVal() public view returns(uint256){
return modResult;
}
function overflowResultVal() public view returns(uint256){
return overflowResult;
}
}
Go to the github page for any import you'll like to have for openzeppelin-contracts, "SafeMath" in this case, and copy the page url.
Ex:
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol";
Import the library right after pragma solidity. Then include it right after the opening of your contract.
pragma solidity ^x.x.x;
import "openzeppelin-solidity/contracts/utils/math/SafeMath.sol";
contract TheNameOfYourContract {
using SafeMath for uint;
.
.
...
}
creating a project from npx hardhat (2.8.3) selecting advanced project and then having a folder structure like this:
project
|--contracts
|--mycontract.sol
|--node_modules
|--{other files & folders}
The error is gone if I import libraries like this inside mycontract.sol:
pragma solidity ^0.8.4;
import "../node_modules/#openzeppelin/contracts/token/ERC20/ERC20.sol";
import "../node_modules/#openzeppelin/contracts/utils/math/SafeMath.sol";
import "../node_modules/#openzeppelin/contracts/access/Ownable.sol";
... other code
But this will not compile at least with hardhat. So it appears Solhint (.solhint.json) requires to know exactly the import paths but it ignores by default (.solhintignore) the node_modules folder. By removing node_modules from .solhintignore and restarting VS Code it works for me.
No longer needed but import that works for pragma solidity ^0.6.0;
would be:
import "#chainlink/contracts/src/v0.6/vendor/SafeMathChainlink.sol";

Is this a remix,compiler or an openzeplin bug?

I just wrote a simple code to test openzeplin Safemath library. I am using the latest version of remix ide and compiling for ^0.5.0.
Remix is using 0.5.0_commit.1d4f565a compiler
The environment is JavaScript VM
EVM Version is the compiler default
The add function does not seem to be working in the code given below
I have tried x.sub(1) it throws an exception as expected, i have also tried initializing x to different values but still does not work.
pragma solidity ^0.5.0;
import "./SafeMath.sol";
contract SimpleStorage {
using SafeMath for uint;
uint x;
event incremented(uint x);
constructor() public{
x=0;
}
function increment() public {
x.add(1);
emit incremented(x);
}
function get() external view returns (uint) {
return x;
}
}
Expected output is an increment by one on every call to the function but getting the same value every time. Emit also shows the same value.
Well, it's your bug :)
Instead of x.add(1) try x = x.add(1). Add function is not inplace, new value is returned and you need to assign the new value to x.