Warning: Function state mutability can be restricted to pure function - ethereum

I am new to solidity and I have been trying to print out simple messages using functions in solidity, but I have failed to deploy successfully, and there is an error that I can not figure out what's wrong.
This is what I have tried so far:
pragma solidity ^0.6.0;
contract test {
string public _feedback;
function reply(string memory feedback) public
{
feedback = "Well done!";
}
}
The error I am receiving is "Warning: Function state mutability can be restricted to pure function"

The compiler simply WARNS that the result of the execution of the function reply is fixed and "canonically" this can be indicated by adding a pure specifier to it:
function reply(string memory feedback) public pure
{
feedback = "Well done!";
}
But even without this change, the contract will be compiled and created correctly. Only in its current form, you will not be able to understand in any way what it is working out :-)

Related

What is Interface signature in solidity?

I'm going through the source code for the ERC-1155 token standard and I came across this block of code
bytes4 constant private INTERFACE_SIGNATURE_ERC165 = 0x01ffc9a7;
bytes4 constant private INTERFACE_SIGNATURE_ERC1155 = 0xd9b67a26;
function supportsInterface(bytes4 _interfaceID) override external view returns (bool) {
if (_interfaceID == INTERFACE_SIGNATURE_ERC165 ||
_interfaceID == INTERFACE_SIGNATURE_ERC1155) {
return true;
}
return false;
}
I don't quite understand what the benefit of this function is used for.
Also, where do you get the INTERFACE_SIGNATURE constants from?
Could someone please explain?
I believe there is a good explanation for this on openzeppelin docs.
but just to give a short answer, suppose you sent ERC20 tokens to a contract that lacks the ability to transfer it so these tokens are forever locked in the contract. To avoid this kind of things, when you do a safetranser it will first check whether the receiver is capable of receiving it or not and in that case this function is called.
As for INTERFACE_SIGNATURE you can give it a read here.

Solidity - OpenZeppeling/utils/Counters question

When we use the Counters library, we init it usually as such
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
so far all good. Using Counters library methods for Counters.Counter (the struct in the library) and assigning _tokenIds to point to that struct. (+-? cool.)
What confuses me is the function definitions inside Counters; i.e
function current(Counter storage counter) internal view returns (uint256) {
return counter._value;
}
function increment(Counter storage counter) internal {
unchecked {
counter._value += 1;
}
}
The function takes in a varaible called counter ? is it not expecting an argument ?
Where is the link between our defined _tokenIds to the smaller-case counter ?
I don't know why I find this so confusing but it seems like something's missing to me (even tho I know its not missing, just failing to understand).
Thanks in advance.
The using <library> for <type> expression allows you to use functions of the library on variables of this type. And it automatically passes the variable as the first argument of the function when you're calling it as a member function.
So in your case, Counters.current(_tokenIds) (library function) is the same as _tokenIds.current() (member function).
Docs: https://docs.soliditylang.org/en/v0.8.14/contracts.html#using-for

Error of Big number while using constructer in solidity

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
struct account{
string _name;
uint _acc_id;
uint balance;
}
contract My_acc{
account public person;
constructor(string memory name, uint acc_id, uint _balance){
person._name = name;
person._acc_id = acc_id;
person.balance = _balance;
}
}
I am trying to pass values to struct variable through constructor But I am getting this following error.
creation of My_acc errored: Error encoding arguments: Error: invalid BigNumber string (argument="value", value="", code=INVALID_ARGUMENT, version=bignumber/5.5.0)
I tried running the same code and it worked just fine.
Constructor runs only once and that is at the time of deployment. Since you are passing the values to constructor, you should pass the values at the time of deployment.
My reputation does not seem to allow me to upload the image. Please go through the link below.
https://i.stack.imgur.com/AFBWd.png
So guys, I resolved the error. It wasn't actually an error. It was just a silly mistake that I made as a beginner.
SOLUTION: -
I wasn't actually passing values to constructor initially. Actually when we add constructor in our contract we basically get a input section with our deploy button automatically (even before actually deploying). which have to be used to pass values before deploying our contract.

Solidity Internal exception in StandardCompiler

This is the source code
pragma solidity ^0.5.1;
contract myContract{
string value;
constructor() public {
value = "myValue";
}
function get() public view returns(string memory){
return value;
}
function set(string memory _value) public{
value = _value;
}
}
Selected Compiler on Remix IDE is 0.5.1+commit.c8a2cb62
Selected Environment is Javascript VM
When I compile the command I get this error
Internal exception in StandardCompiler::compileInternal: /root/project/libevmasm/ExpressionClasses.cpp(187): Throw in function ExpressionClasses::Id dev::eth::ExpressionClasses::tryToSimplify(const dev::eth::ExpressionClasses::Expression &)
Dynamic exception type: boost::exception_detail::clone_impl<dev::eth::OptimizerException>
std::exception::what: Rule list not properly initialized.
[dev::tag_comment*] = Rule list not properly initial
However this error does not pop up for same source code if I select the compiler as 0.5.11
Other users are saying that they are getting this error on pre 0.5.3 compilers.
This is a bug in a certain version of the compiler - it does not work properly if the javascript runtime environment does not provide enough stack space or memory. It should work with the most recent compilers.
Check this thread for more info

Ethereum Transaction Error while calling a contract function from another contract

Following smart contract works fine in Remix and Ganache. However doesn't work on private ethereum blockchains like Kaleido or Azure. What am I missing. When I call setA it consumes all gas and then fails.
pragma solidity ^0.4.24;
contract TestA {
uint public someValue;
function setValue(uint a) public returns (bool){
someValue = a;
return true;
}
}
contract TestB {
address public recentA;
function createA() public returns (address) {
recentA = new TestA();
return recentA;
}
function setA() public returns (bool) {
TestA(recentA).setValue(6);
return true;
}
}
I tried your contract in Kaleido, and found even calling eth_estimateGas with very large numbers was resulting in "out of gas".
I changed the setValue cross-contract call to set a gas value, and I was then able to call setA, and estimating the gas for setA showed just 31663.
recentA.setValue.gas(10000)(6);
I suspect this EVM behavior is related to permissioned chains with a gasprice of zero. However, that is speculation as I haven't investigated the internals.
I've also added eth_estimateGas, and support for multiple contracts in a Solidity file, to kaleido-go here in case it's helpful:
https://github.com/kaleido-io/kaleido-go
Another possibility for others encountering "out of gas" calling across contracts - In Geth if a require call fails in a called contract, the error is reported as "out of gas" (rather than "execution reverted", or a detailed reason for the require failing).
You are hitting the limit of gas allowed to be spent per block. Information about gas limit is included into every block, so you can check what's this value is right now in your blockchain. Currently on Ethereum MainNet, GasLimit (per block) is about 8 millions (see here https://etherscan.io/blocks)
To fix this, you can start your blockchain with modified genesis file. Try to increase value of gasLimit parameter in your genesis file, which specifies the maximum amount of gas processed per block. Try "gasLimit": "8000000".
Try to discard the return statement of setValue method in contract TestA.
pragma solidity ^0.4.24;
contract TestA {
uint public someValue;
function setValue(uint a) public {
someValue = a;
}
}