Search code examples
blockchainethereumsoliditysmartcontractsweb3js

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


I Have Two Smart contract , DEXUserCoin and DEXTransferCoinUserToUser.

I want to use Some Function from the DEXUserCoin into the DEXTransferCoinUserToUser.

DEXUserCoin :

// 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;
        } 

    }


}

in need to use ApproveTransferSend into the DEXTransferCoinUserToUser and i try this :

// 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);

    }

}

Solution

  • You are just importing but not inheriting anything:

    contract DEXTransferCoinUserToUser is DEXUserCoin {
    }
    

    or you have to set desxUserCoin value in constructor

    constructor (DEXUserCoin _dex){
        desxUserCoin=_dex;
    
    }
    

    this constructor means when you create the contract, you have to pass a parameter to initalize the contract.