0

i want to return owner of contract address :

i write this contract :

// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;

import "./Token.sol";

contract KIA is Token {
    Token private token;

    constructor(
        address owner,
        uint256 initSupply,
        string memory tokenName,
        string memory tokenSymbol,
        uint8 decimalUnits
    ) Token(owner, initSupply, tokenName, tokenSymbol, decimalUnits) {}

    function ownerToken() external view returns (address) {
        return token.owner();
    }

    function BuyToken(uint256 rate) external payable {
        uint256 tokenAmount = msg.value * rate;
        token.transfer(msg.sender, tokenAmount);
    }
}

and this contract inheritance from Token :

// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;

import "./SafeMath.sol";

contract Token is SafeMath {
    uint256 public totalToken;
    string public name;
    string public symbol;
    uint256 public decimals;
    address tokenOwner;

    mapping(address => uint256) public balance;
    mapping(address => mapping(address => uint256)) public allowed;

    event Transfer(address indexed from, address indexed to, uint256 value);

    constructor(
        address owner,
        uint256 initSupply,
        string memory tokenName,
        string memory tokenSymbol,
        uint8 decimalUnits
    ) {
        balance[owner] = initSupply;
        totalToken = initSupply;
        name = tokenName;
        symbol = tokenSymbol;
        decimals = decimalUnits;
        tokenOwner = owner;
    }

    //
    modifier transferFromModifire(
        address _to,
        address _from,
        uint256 _value
    ) {
        require(_value <= 0, " value can not be 0  ");
        require(balance[_from] < _value, " not have enough money ");
        require(balance[_to] + _value < balance[_to], " overflow Error ");
        require(
            _value > allowed[_from][msg.sender],
            " i dont know whats error "
        );
        _;
    }

    modifier transferModifire(address _to, uint256 _value) {
        require(_value <= 0);
        require(balance[_to] + _value < balance[_to]);
        _;
    }

    // Send Owner Address
    function owner() external view returns(address) {
        return tokenOwner;
    }

    // Send Token From Owner To Address
    function transfer(address _to, uint256 _value)
        external
        transferModifire(_to, _value)
        returns (bool)
    {
        balance[msg.sender] = SafeMath.safeSub(balance[msg.sender], _value);
        balance[_to] = SafeMath.safeAdd(balance[_to], _value);
        emit Transfer(msg.sender, _to, _value);
        return true;
    }

    // Send Token From Address To Address
    function transferFrom(
        address _from,
        address _to,
        uint256 _value
    ) external transferFromModifire(_to, _from, _value) returns (bool) {
        balance[_from] = SafeMath.safeSub(balance[msg.sender], _value);
        balance[_to] = SafeMath.safeAdd(balance[_to], _value);
        emit Transfer(_from, _to, _value);
        return true;
    }
}

i want to test and deploy contract :

import assert from 'assert';
import Web3 from 'web3';
import { AbiItem } from 'web3-utils';
import * as KiaToken from '../build/KIA.json';


const web3 = new Web3('https://ropsten.infura.io/v3/afa51bf9aa97452d90cbc4713d14888d');


describe("Kiaswap Contract", () => {

    let rapstoneAddress = '0xB643992c9fBcb1Cb06b6C9eb278b2ac35e6a2711';
    let initSupply = 99999999999999999999999999999999999999999;
    let TokenName = 'KIA';
    let tokenSymbol = 'LION';
    let decimalUnits = 8;
    let token: any;

    beforeEach(async () => {

        token = await new web3.eth.Contract(KiaToken.abi as AbiItem[])
            .deploy({
                data: KiaToken.evm.bytecode.object,
                arguments: [rapstoneAddress, initSupply, TokenName, tokenSymbol, decimalUnits]
            });
    })


    it('Owner ', async () => {
      let owner =   await token.methods.ownerToken().call();
      assert.equal(rapstoneAddress,owner);
    })


})

but after run this code :

 it('Owner ', async () => {
      let owner =   await token.methods.ownerToken().call();
      assert.equal(rapstoneAddress,owner);
    })

it show me error :

TypeError: Cannot read property 'ownerToken' of undefined

now whats the problem ????

3
  • The Kia contract inherits from Token and it also has a token. That is confusing, are you sure you want it that way? I'd guess you don't want Token token;, just remove that line and replace token.funcXX() with funcXX().
    – Ismael
    Aug 29 '21 at 1:19
  • @Ismael , thnaks but without the Token token i can not access to transfer in Token
    – Kianoush
    Aug 29 '21 at 9:10
  • KIA inherits from Token, so it can call transfer(). Perhaps the issue is that it is declared as external, try declaring it as public so it could be called from the inherited contract KIA. Also apply to function owner.
    – Ismael
    Aug 29 '21 at 17:12
0

If you want to call a function in Token contract with "token.function()" you have to first assign token to an inheritance of Token contract. Maybe you can change your constructor like this:

constructor(
    address owner,
    uint256 initSupply,
    string memory tokenName,
    string memory tokenSymbol,
    uint8 decimalUnits
) {
token = new Token(owner, initSupply, tokenName, tokenSymbol, decimalUnits); }
1
  • no , still show me that error
    – Kianoush
    Aug 27 '21 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.