在 Remix IDE 中部署 Solidity 智能合约时出现了抽象合约错误。

huangapple go评论67阅读模式
英文:

Abstract Contract Error in deploying smart contract in Remix IDE using solidity

问题

I am new to solidity and trying to run the below smart contract to trade carbon credits:

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

interface IERC20 {
    function transfer(address recipient, uint256 amount) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
}

/**
 * @title Storage
 * @dev Store & retrieve value in a variable
 * @custom:dev-run-script ./scripts/deploy_with_ethers.ts
 */
contract CarbonCreditTrade {

    address public buyer;
    address public seller;
    uint public carbonCreditsAvailable;
    uint public creditPointsAwarded;

    IERC20 public token; //Token contract interface
    uint256 public creditPrice; //Price per carbon credit in token units

    constructor(address payable _tokenAddress, uint256 _creditPrice){
        seller = msg.sender;
        carbonCreditsAvailable = 10000; //set the initial number of carbon credits available

        token = IERC20(_tokenAddress);
        creditPrice = _creditPrice;
    }

    function buyCarbonCredits(uint _creditsToBuy) external {
        require(msg.sender != seller, "seller is not buying CC");
        require(_creditsToBuy <= carbonCreditsAvailable, "insufficient carbon credits available");

        uint256 totalCost = _creditsToBuy * creditPrice; //set the cost of each carbon credit

        require(token.balanceOf(msg.sender) >= totalCost,"Insufficient token for desired number of credits");

        buyer = msg.sender;
        carbonCreditsAvailable -= _creditsToBuy;
        creditPointsAwarded += _creditsToBuy / 10;

        //Transfer the purchased carbon credits to the buyer
        require(token.transfer(buyer, _creditsToBuy), "Token transfer failed");
    }

    function getCreditPointsAwarded() external view returns (uint){
        return creditPointsAwarded;
    }

}

However when trying to deploy the IERC20 contract in the remix contract dropdown I get below error:
This contract may be abstract, not implement an abstract parent's methods completely or not invoke an inherited contract's constructor correctly

Can anyone please help? The answers on google suggest the correct contract may not be selected to be deployed. However I am selecting the correct contract.

英文:

I am new to solidity and trying to run the below smart contract to trade carbon credits:
'''

// SPDX-License-Identifier: GPL-3.0

pragma solidity &gt;=0.8.2 &lt;0.9.0;

interface IERC20 {
    function transfer(address recipient, uint256 amount) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
}

/**
 * @title Storage
 * @dev Store &amp; retrieve value in a variable
 * @custom:dev-run-script ./scripts/deploy_with_ethers.ts
 */
contract CarbonCreditTrade {

    address public buyer;
    address public seller;
    uint public carbonCreditsAvailable;
    uint public creditPointsAwarded;

    IERC20 public token; //Token contract interface
    uint256 public creditPrice; //Price per carbon credit in token units

    constructor(address payable _tokenAddress, uint256 _creditPrice){
        seller = msg.sender;
        carbonCreditsAvailable = 10000; //set the initial number of carbon credits available

        token = IERC20(_tokenAddress);
        creditPrice = _creditPrice;
    }

    function buyCarbonCredits(uint _creditsToBuy) external {
        require(msg.sender != seller, &quot;seller is not buying CC&quot;);
        require(_creditsToBuy &lt;= carbonCreditsAvailable, &quot;insufficient carbon credits available&quot;);

        uint256 totalCost = _creditsToBuy * creditPrice; //set the cost of each carboncredit

        require(token.balanceOf(msg.sender) &gt;= totalCost,&quot;Insufficient token for desired number of credits&quot;);

        buyer = msg.sender;
        carbonCreditsAvailable -= _creditsToBuy;
        creditPointsAwarded += _creditsToBuy / 10;

        //Transfer the purchased carbon credits to the buyer
        require(token.transfer(buyer, _creditsToBuy), &quot;Token transfer failed&quot;);
    }

    function getCreditPointsAwarded() external view returns (uint){
        return creditPointsAwarded;
    }

}

'''

However when trying to deploy the IERC20 contract in the remix contract dropdown I get below error:
This contract may be abstract, not implement an abstract parent's methods completely or not invoke an inherited contract's constructor correctly

Can anyone please help? The answers on google suggest the correct contract may not be selected to be deployed. However I am selecting the correct contract.

答案1

得分: 1

当您编译文件时,您将会得到以下内容:

当您部署时,您必须确保选择了CarbonCreditTrade下拉菜单。您正在尝试部署IERC20,但您不能将接口部署到EVM,因为它没有代码实现。接口只包含函数签名。

英文:

when you compile the file, you will have those

在 Remix IDE 中部署 Solidity 智能合约时出现了抽象合约错误。

when you deploy, you have to make sure CarbonCreditTrade dropdown is selected. you are trying to deploy IERC20 but you cannot deploy interface in to EVM because there is no code implementation. interface just have function signatures

huangapple
  • 本文由 发表于 2023年7月6日 21:50:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定