【求解答!remix部署智能合约到本地ganche失败】

部署智能合约的时候,环境选择remixVM(shanghai)能够成功部署

image.png

【求解答】但是我想连接到本地的ganche环境部署失败

image.png 【求解答】报错如下:

image.png

// SPDX-License-Identifier: GPL-3.0-or-later pragma solidity ^0.8.7;

import "@openzeppelin/contracts/utils/math/SafeMath.sol"; contract MusicChain { // 音乐作品结构体 struct Music{ uint id; // 作品id uint[] parents; // 父作品id uint[] children; // 子作品id bytes cid; // 作品内容的aliyun对象存储地址 }

这是sol文件

// 字符串的转换
mapping(uint => address[]) ownerMap;    // Music的版权持有人
mapping(uint => address[]) authMap;     // Music的授权人
mapping(uint => address[]) nftMap;      // Nft的所有者
Music[] musics;
function toString(address _value) public pure returns(string memory) {
    return toString(abi.encodePacked(_value));
}

function toString(uint256 _value) public pure returns(string memory) {
    return toString(abi.encodePacked(_value));
}

function toString(bytes32 _value) public pure returns(string memory) {
    return toString(abi.encodePacked(_value));
}

function toString(bytes memory _data) public pure returns(string memory) {
    bytes memory alphabet = "0123456789abcdef";

    bytes memory str = new bytes(2 + _data.length * 2);
    str[0] = "0";
    str[1] = "x";
    for (uint i = 0; i < _data.length; i++) {
        str[2+i*2] = alphabet[uint(uint8(_data[i] >> 4))];
        str[3+i*2] = alphabet[uint(uint8(_data[i] & 0x0f))];
    }
    return string(str);
}

function bytesToBytes46(bytes memory _bs) pure public returns (bytes1[46] memory) {
    bytes1[46] memory result;
    for (uint i = 0; i < 46; i++) {
        result[i] = _bs[i];
    }
    return result;
}

function bytes46ToBytes(bytes1[46] memory _bs) pure public returns (bytes memory) {
    bytes memory result = new bytes(46);
    for (uint i = 0; i < 46; i++) {
        result[i] = _bs[i];
    }
    return result;
}
// 改变作品归属
function changeOwn(bytes calldata _cid) public{
    uint id = getIdByCid(_cid);
    require(id != 2**256 - 1, "this cid is invalid");
    address[] storage temp = ownerMap[id];
    temp.push(msg.sender);
}
// 音乐版权写入区块链
function createMusic(bytes calldata _file_hash) public  returns(uint){
    require(getIdByCid(_file_hash) == 2 ** 256 - 1, "already exists");
    Music storage curMusic = musics.push();
    uint curId = musics.length-1;
    curMusic.id = curId;
    curMusic.cid = new bytes(46);
    curMusic.cid = _file_hash;
    address[] storage temp =  ownerMap[curId];
    temp.push(msg.sender);
    return curId;
}

//改编关系写入区块链
function createAdapt(bytes calldata _cid,bytes calldata _cidParent) public{
    uint parentId = getIdByCid(_cidParent);
    require(parentId != 2**256-1,"this cid is invalid");
    uint curId = createMusic(_cid);
    // 父类音乐作品中添加子类
    musics[parentId].children.push(curId);
    // 子类音乐作品中添加父类
    musics[curId].parents.push(parentId);
}

// 获得一个音乐作品当前的归属
function getMusicAccount(bytes calldata _cid) view public returns(address){
    uint id = getIdByCid(_cid);
    require(id != 2**256 - 1, "this cid is invalid");
    address[] storage temp = ownerMap[id];
    return temp[temp.length-1];
}

// 授权一个音乐
function authMusic(bytes calldata _cid) public{
    uint id = getIdByCid(_cid);
    require(id != 2**256 - 1, "this cid is invalid");
    address[] storage temp = authMap[id];
    temp.push(msg.sender);
}

// 指定一个NFT
function createNft(bytes calldata _cid) public{
    uint id = getIdByCid(_cid);
    require(id != 2**256-1, "this cid is invalid");
    address[] storage temp = nftMap[id];
    temp.push(msg.sender);
}
// 获得一个NFT的归属
function getNftAccount(bytes calldata _cid) view public returns(address){
    uint id = getIdByCid(_cid);
    require(id != 2**256-1, "this cid is invalid");
    address[] memory temp = nftMap[id];
    return temp[temp.length-1];
}
// 通过cid获得音乐作品id
function getIdByCid(bytes memory _cid) view public returns(uint){
    for(uint i=0;i<musics.length;i++){
        if(keccak256(_cid) == keccak256(musics[i].cid)){
            return musics[i].id;
        }
    }
    return 2**256-1;
} 

// 测试接口
function getAllMusic() view public returns(Music[] memory){
    return musics;
}
function getAllOwnerMap(uint id) view public returns(address[] memory){
    return ownerMap[id];
}
function getAllAuthMap(uint id) view public returns(address[] memory){
    return authMap[id];
}
function getAllNftMap(uint id) view public returns(address[] memory){
    return nftMap[id];
}

}

请先 登录 后评论

最佳答案 2024-03-28 16:24

找到问题了,大家用remix编译器编译一定要和使用的slidity版本一样

请先 登录 后评论

其它 1 个回答

YGCool
请先 登录 后评论
  • 1 关注
  • 0 收藏,944 浏览
  • 程1 提出于 2024-03-28 09:41