CryptoD 2023-03-22 17:18 采纳率: 85.7%
浏览 42
已结题

Solidity编译成功,部署失败

原本的程式码是可以编译后部署的,但我新增两个function之后,一样可以编译,也能在Remix的VM上成功部署,但如果切换成测试链:Goerli、BSC Testnet,部署就会报错:Failed transaction Transaction 2 failed! [ethjs-query] while formatting outputs from RPC '{"value": {"code": -32603,"data": {"message":"Internal Server Error"}}}'

新增两个function:searchCampaigns、getCampaignsByCategory,部署出现错误,请问到底是哪里有问题?

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

contract CrowdFunding {
    struct Campaign{
        address owner;
        string title;
        string description;
        string[] categories;
        uint256 target;
        uint256 deadline;
        uint256 amountCollected;
        string image;
        string details;
        address[] donators;
        uint256[] donations;
    } 


    mapping(uint256=>Campaign)public campaigns;

    uint256 public numberOfCampaigns = 0;

    function createCampaign(address _owner, string memory _title, string memory _description,string[] memory _category, uint256 _target, uint256 _deadline, string memory _image,string memory _details) public returns (uint256){

        //将类别array push进来
        Campaign storage campaign = campaigns[numberOfCampaigns];

        // is everything okay?
        require(campaign.deadline < block.timestamp, "The deadline should be a date in the future.");

        campaign.owner = _owner;
        campaign.title = _title;
        campaign.description = _description;
        for(uint i = 0; i < _category.length; i++){
            campaign.categories.push(_category[i]);
        }
        campaign.target = _target;
        campaign.deadline = _deadline;
        campaign.amountCollected = 0;
        campaign.image = _image;
        campaign.details = _details;

        numberOfCampaigns++;
        return numberOfCampaigns - 1;

    }

    function donateToCampaign(uint256 _id) public payable {
         uint256 amount = msg.value;
         Campaign storage campaign = campaigns[_id];

         campaign.donators.push(msg.sender);
         campaign.donations.push(amount);

         (bool sent,) = payable(campaign.owner).call{value: amount}("");

         if(sent){
             campaign.amountCollected = campaign.amountCollected + amount;
         }
    }

    function getDonators(uint _id) view public returns (address[] memory, uint256[] memory){
        return (campaigns[_id].donators, campaigns[_id].donations);
    }

    function getCampaigns() public view returns (Campaign[] memory) {
        Campaign[] memory allCampaigns = new Campaign[](numberOfCampaigns);

        for(uint i = 0 ; i < numberOfCampaigns ; i++){
            Campaign storage item = campaigns[i];
            allCampaigns[i] = item;
        }

        return allCampaigns;
    }

//新增以下两个function之后就出现错误了
    
    function searchCampaigns(string memory _searchText) public view returns (Campaign[] memory) {
        Campaign[] memory matchedCampaigns = new Campaign[](numberOfCampaigns);
        uint256 count = 0;

        for(uint i = 0 ; i < numberOfCampaigns ; i++){
            Campaign storage item = campaigns[i];
            bytes memory titleBytes = bytes(item.title);
            bytes memory searchTextBytes = bytes(_searchText);

            if (titleBytes.length >= searchTextBytes.length) {
                bool isMatch = true;

                for (uint j = 0; j < searchTextBytes.length; j++) {
                    if (titleBytes[j] != searchTextBytes[j]) {
                        isMatch = false;
                        break;
                    }
                }

                if (isMatch) {
                    matchedCampaigns[count] = item;
                    count++;
                }
            }
        }

        Campaign[] memory result = new Campaign[](count);
        for(uint i = 0 ; i < count ; i++){
            result[i] = matchedCampaigns[i];
        }

        return result;
    }

    function getCampaignsByCategory(string memory _category) public view returns (Campaign[] memory) {
    Campaign[] memory matchingCampaigns = new Campaign[](numberOfCampaigns);
    uint256 matchingCampaignsCount = 0;

    for(uint i = 0 ; i < numberOfCampaigns ; i++){
        Campaign storage item = campaigns[i];

        // 检查当前 Campaign 是否包含选中的类别
        bool containsCategory = false;
        for(uint j = 0 ; j < item.categories.length ; j++){
            if(keccak256(bytes(item.categories[j])) == keccak256(bytes(_category))){
                containsCategory = true;
                break;
            }
        }

        if(containsCategory){
            matchingCampaigns[matchingCampaignsCount] = item;
            matchingCampaignsCount++;
        }
    }

    // 复制匹配到的 Campaign 到一个新的数组中,长度为匹配到的数量
    Campaign[] memory result = new Campaign[](matchingCampaignsCount);
    for(uint i = 0 ; i < matchingCampaignsCount ; i++){
        result[i] = matchingCampaigns[i];
    }

    return result;
}
} 
  • 写回答

1条回答 默认 最新

  • 海梦在飘扬 2023-03-22 17:56
    关注

    这可能是因为您在部署时没有提供足够的燃气费用,导致交易失败。尝试增加燃气费用并重新部署。此外,还要确保您正在使用正确的合约地址和 ABI。如果问题仍然存在,请检查您的智能合约代码是否与所选测试链的规则相符。
    另外,如果您新增了两个函数而导致部署失败,那么问题可能出在这两个函数的实现上。您可以检查这两个函数的代码是否正确,并且确保它们符合 Solidity 语言的语法和规则。如果您无法解决问题,请提供更多的错误信息和您的智能合约代码,以便我们更好地帮助您解决问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月1日
  • 已采纳回答 3月24日
  • 创建了问题 3月22日

悬赏问题

  • ¥15 对象代号: , 表单: 不存在!
  • ¥15 WebSocket的问题
  • ¥15 WebSocket的问题
  • ¥15 centos上启动kylin后网页报错404
  • ¥20 使用hackrf进行信号收发时接收到的信号幅度太小
  • ¥15 WebSocket的问题
  • ¥15 BDSBAS-B1C和B1C信号有什么不同
  • ¥15 在半圆平面内随机生成点坐标
  • ¥15 系统容量变化的几种多址方式TDMA, CDMA,FDMA,OFDMA 对比,应该给的是一个曲线 图,随着系统容量的增加,几种多址方式性能的对比 图,MATLAB程序仿真折线图
  • ¥15 用visual Studio 写c ++只运行上一个旧代码的运行结果是怎么回事