为什么开放的齐柏林飞艇安全数学合同不进口?
我正在使用以太纳特的以下合同,并做过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;
}
}
}
但是,即使安装了所有OpenZeppelin合同,我仍然会遇到有关Safemath合同的错误::
错误:找不到 @openzeppelin/Contracts/Math/safemath.sol从任何来源
我的配置下面:
Truffle v5.1.39 (core: 5.1.39)
Solidity - 0.6.0 (solc-js)
Node v16.13.1
Web3.js v1.2.1
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您像以前与NPM安装OpenZeppelin合同时,您将使用当前标签安装版本。当前标签目前为Solidity版本 ^0.8.0。由于版本0.8+,因此您不再需要使用Safemath,因为它可以在语言级别上实现。
当您使用Solidity版本0.6时,您还应该为此版本安装OpenZeppelin软件包。这应该是3.4.1。您可以使用以下命令安装此版本:
更好的解决方案是将您的合同更新为Solidity版本0.8+,使用当前的OpenZeppelin实现并删除Safemath库。
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:
The better solution, however, would be to update your contract to solidity version 0.8+, use the current openzeppelin implementation and remove SafeMath library.
问题是您的版本选择。
这是您当前正在使用的版本0.6.0。
但是您正在使用的
是固体性> = 0.8.0
The problem is your version selection.
This one is for solidity 0.6.0 the version you are currently using.
but you are using
which is for solidity >= 0.8.0