为什么开放的齐柏林飞艇安全数学合同不进口?

发布于 2025-02-13 07:07:26 字数 1138 浏览 0 评论 0原文

我正在使用以太纳特的以下合同,并做过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

数理化全能战士 2025-02-20 07:07:26

当您像以前与NPM安装OpenZeppelin合同时,您将使用当前标签安装版本。当前标签目前为Solidity版本 ^0.8.0。由于版本0.8+,因此您不再需要使用Safemath,因为它可以在语言级别上实现。

当您使用Solidity版本0.6时,您还应该为此版本安装OpenZeppelin软件包。这应该是3.4.1。您可以使用以下命令安装此版本:

npm i @openzeppelin/[email protected]

更好的解决方案是将您的合同更新为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:

npm i @openzeppelin/[email protected]

The better solution, however, would be to update your contract to solidity version 0.8+, use the current openzeppelin implementation and remove SafeMath library.

尤怨 2025-02-20 07:07:26

问题是您的版本选择。

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/solc-0.6/contracts/utils/SafeCast.sol";

这是您当前正在使用的版本0.6.0。

但是您正在使用的

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol

是固体性> = 0.8.0

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文