I am using the following contract from Ethernaut and did sudo npm i #openzeppelin/contracts
pragma solidity 0.6.0;
import "#openzeppelin/contracts/math/SafeMath.sol";
contract CoinFlip {
using SafeMath for uint256;
uint256 public consecutiveWins;
uint256 lastHash;
uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968;
constructor() public {
consecutiveWins = 0;
}
function flip(bool _guess) public returns (bool) {
uint256 blockValue = uint256(blockhash(block.number.sub(1)));
if (lastHash == blockValue) {
revert();
}
lastHash = blockValue;
uint256 coinFlip = blockValue.div(FACTOR);
bool side = coinFlip == 1 ? true : false;
if (side == _guess) {
consecutiveWins++;
return true;
} else {
consecutiveWins = 0;
return false;
}
}
}
However, I'm still getting this error regarding SafeMath contract not being found even after installing all the openzeppelin contracts: Error: Could not find #openzeppelin/contracts/math/SafeMath.sol from any sources
My configurations below:
Truffle v5.1.39 (core: 5.1.39)
Solidity - 0.6.0 (solc-js)
Node v16.13.1
Web3.js v1.2.1
The problem is your version selection.
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/solc-0.6/contracts/utils/SafeCast.sol";
This one is for solidity 0.6.0 the version you are currently using.
but you are using
https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol
which is for solidity >= 0.8.0
When you install openzeppelin contracts with npm as you did, you install the version with the current tag. The current tag is right now at Solidity version ^0.8.0. Since version 0.8+, you don't need to use SafeMath anymore as it is implemented on the language level.
As you are using Solidity version 0.6 you should also install the openzeppelin package for this version. This should be 3.4.1. You can install this version with the following command:
npm i #openzeppelin/contracts#3.4.1
The better solution, however, would be to update your contract to solidity version 0.8+, use the current openzeppelin implementation and remove SafeMath library.
Related
I am using Remix to compile and deploy my smart contract to Rinkeby and RSK test networks.
I don't understand why the bytecode of my contract on Rinkeby explorer is different from the metadata.data.deployedBytecode.object in Remix artifacts and also different from evm.deployedBytecode.object coming from solcjs compiler. I also tried this on RSK and I got the same issue.
That's what I do in Remix:
I create a MegaHonk.sol file in contracts folder
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.7;
contract MegaHonk {
uint256 public count;
event LoudSound(address indexed source);
function honk() external {
require(tx.origin != msg.sender, 'EOA not allowed');
count += 1;
emit LoudSound(tx.origin);
}
}
I select the appropriate compiler version 0.8.7, select Environment: Injected provider - Metamask, compile and deploy to Rinkeby. The contract successfully deploys. Then I go to Rinkeby explorer, find my contract bytecode and compare it with the one in remix-file-explorer/contracts/artifacts/MegaHonk.js in the metadata.data.deployedBytecode.object property.
Bytecode from Rinkeby is 1348 symbols long and
Bytecode from Remix is 1350 symbols long.
The exact same thing happens when I am compiling the same smart contract with solcjs compiler. I use the right version 0.8.7 and these input parameters:
const input = {
language: 'Solidity',
settings: {
outputSelection: {
'*': {
'*': ['evm.deployedBytecode', 'evm.bytecode'],
},
},
optimizer: {
enabled: false,
},
},
sources: {
'MegaHonk.sol': {
content: MegaHonk,
},
},
};
Why does this happen? What compiler parameters should I use to make the bytecodes identical in the Remix artifacts, solcjs compiler and in blockchain explorer (Ethereum or RSK testnets)?
I started with v0.6 and connected my node to an operator (called Oracle at the time)
pragma solidity ^0.6.0;
import "#chainlink/contracts/src/v0.6/Oracle.sol";
contract MyOperator is Oracle {
constructor(address _link) Oracle(_link) public {}
}
My client contract using those libs
pragma solidity ^0.6.6;
import "#chainlink/contracts/src/v0.6/ChainlinkClient.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
I had to connect them using setFulfiullmentPermissions at the time:
await operator.setFulfillmentPermission(nodeAddress, true, {
from: owner,
});
=======
I then moved to v0.7 with my Operator being
pragma solidity ^0.7.0;
import "#chainlink/contracts/src/v0.7/dev/Operator.sol";
contract MyOperator is Operator {
constructor(address _link, address owner) Operator(_link, owner) public {}
}
My client contract using those libs
pragma solidity ^0.7.0;
import "#chainlink/contracts/src/v0.7/ChainlinkClient.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
this time the authorization code looked like this
let tx = await operator.setAuthorizedSenders([nodeAddress], { from: owner });
=====
Now trying to migrate to chainlink v0.8
I see an OperatorInterface but not a real contract here in develop: https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/interfaces/OperatorInterface.sol
Why isn't the Operator implementation part of the v0.8 ?
If I am including a contract Operator v0.7 in my project, truffle is all confused with versions mismatch.
I couldn't find a resource online about someone deploying a node with v0.8.
How should I go about migrating to v0.8, what should I deploy as an Operator for my node, please?
The Operator is only implemented in ^0.7. version currently.
You can use Hardhat instead of Truffle, which supports multiple solc versions in a project, and then deploy the Operator using ^0.7 and deploy clients which interact with it in v0.8.
I am a beginner in programming Ethereum smart contracts using solidity and truffle. I made a simple smart contract to deploy that sets a variable equal to 1000000, as shown below:
pragma solidity >=0.4.22 <0.8.0;
contract CelesteToken{
uint256 public totalSupply;
function ClesteToken()public{
totalSupply = 1000000;
}
}
In the truffle console I used the following commands:
CelesteToken.deployed().then(function(i){token =i;})
token.totalSupply().then(function(s){totalSupply =s;})
totalSupply.toNumber()
However instead of returning 1000000 as per the code, it returns 0. I am not sure why this is happening, can anyone help?
You can try replacing
function ClesteToken()public{
totalSupply = 1000000;
}
to
constructor () public {
totalSupply = 1000000;
}
I'm trying to deploy a solidity program to my private ethereum network. However, when i call a method it's not working properly.
This is what i'vd done before the call method.
$ truffle console
truffle(development)> var dApp
undefined
truffle(development)> Hello.deployed().then(function(instance) { dApp = instance; })
undefined
truffle(development)> dApp.message.call()
test env is below
truffle#5.0.28
solc#0.5.10
linux centOS 7
geth#1.8.23
I tried all of the solution in answer about the below error in stack overflow, but it didn't work.
Weird thing is that I installed geth on my macos using same release version, but its version was different from what I've installed on my centOS. It's 1.8.27 on macos and 1.8.23 on centOS 7.
By the way, it was working well when I tried same progress on my macos.
Its return is below.
truffle(development)> dApp.message.call()
'Hello, World : This is a Solidity Smart ' +
'Contract on the Private Ethereum ' +
'Blockchain'
Bammmmmmmmmmmm.
This below is a solidity program I deploied.
pragma solidity >=0.4.15 <0.6.0;
contract Hello {
string public message;
function HelloEth() public {
message = "Hello, World : This is a Solidity Smart Contract on the Private Ethereum Blockchain";
}
}
This is the error returned.
Thrown:
Error: Returned values aren't valid, did it run Out of Gas?
at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:318:1)
at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request.js:208:1)
at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2-cookies/dist/xml-http-request-event-target.js:34:1)
at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-providers-http/src/index.js:96:1)
at /usr/local/lib/node_modules/truffle/build/webpack:/packages/truffle-provider/wrapper.js:112:1
at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3-core-requestmanager/src/index.js:147:1
at sendTxCallback (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth-contract/~/web3-core-method/src/index.js:473:1)
at Method.formatOutput (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth-contract/~/web3-core-method/src/index.js:163:1)
at Method.outputFormatter (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth-contract/src/index.js:818:1)
at Contract._decodeMethodReturn (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth-contract/src/index.js:465:1)
at ABICoder.decodeParameters (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3-eth-abi/src/index.jsa:226:1)
I really want to know because i tried almost 1 months... if you have any idea or any solution please let me know. :(
I also ran into a similar problem, after I posted it in Geth Github, they indicated that the problem may be caused by the latest Solidity compilers which depend on features introduced in Constantinople.
Thus, you might need to add "constantinopleBlock": 0 in your genesis.json to let your private blockchain identify what solidity version you used.
"config": {
"chainId": 1515,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"clique": {
"period": 2,
"epoch": 30000
}
},...
All variables which you defined as public it will always becoming a methods. so, you can access/call your public variable as same as calling a method in smart contract.
ENV:
test-rpc
truffle
code:
let source = "pragma solidity ^0.4.11;contract Calc{ uint count; function add(uint a, uint b) returns(uint){ count++; return a + b; } function getCount() returns (uint){ return count; }}";
let calc = web3.eth.compile.solidity(source);
The terminal tell me:
truffle(development)> Error: Error: Method eth_compileSolidity not supported.
at GethApiDouble.handleRequest (/Users/user/.nvm/versions/node/v6.10.3/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:101870:16)
......
I guess you trying to obtain ABI and/or bytecode of your contract.
Web3 library (which you require in truffle console and then instantiating web3 provider) has no web3.eth.compile.solidity() since version 1.6.0
Here described compiling ways using Remix and solc compiler: https://ethereum.stackexchange.com/questions/15435/how-to-compile-solidity-contracts-with-geth-v1-6
When truffle compiles your code it uses some fixed version of solc (depending on truffle version). For truffle 3.2.5 solc 0.4.11+commit.68ef5810.Emscripten.clang used. Here's the way how to get current version of truffle's solc (it's neccessary to know when you verifying code on Etherscan): https://ethereum.stackexchange.com/questions/18133/how-do-i-find-the-exact-solidity-compiler-version-used-by-truffle