繁体   English   中英

Inheritance 的稳定性问题。 不能使用来自其他智能合约的任何 function

[英]Problem with Inheritance in solidity . can not use any function from other smart contract

我有两个智能合约, DEXUserCoinDEXTransferCoinUserToUser

我想使用 Some Function 从DEXUserCoinDEXTransferCoinUserToUser

DEX用户币:

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

import './DEXCoin.sol';

contract DEXUserCoin {

    struct UserCoin {
        uint coinId;
        uint256 amount; 
    }

    mapping(address => UserCoin) internal userCoins;

    constructor() {

    }

    function ApproveTransferSend(address userAddress , uint coinId , uint256 amount) external view returns(bool) {

        if(userCoins[userAddress].amount >= amount && userCoins[userAddress].coinId == coinId) {
            return true;
        } else {
            return false;
        } 

    }


}

需要使用ApproveTransferSend进入DEXTransferCoinUserToUser我试试这个:

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

import './DEXUserCoin.sol';

contract DEXTransferCoinUserToUser {

    DEXUserCoin private desxUserCoin;

    constructor(){}

    function TransferUserToUser(address from , address to ,uint coinId ,  uint256 amount) 
    public view returns(bool) {

      return  desxUserCoin.ApproveTransferSend(from,coinId,amount);

    }

}

您只是导入但没有继承任何东西:

contract DEXTransferCoinUserToUser is DEXUserCoin {
}

或者您必须在构造函数中设置desxUserCoin

constructor (DEXUserCoin _dex){
    desxUserCoin=_dex;

}

这个构造函数意味着当你创建合约时,你必须传递一个参数来初始化合约。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM