0
//token.sol


pragma solidity ^0.4.24;

/*import "./Ownable.sol";
*/
 contract ERC20Interface {
    function totalSupply() public constant returns (uint);
    function balanceOf(address tokenOwner) public constant returns (uint balance);
    function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
    function transfer(address to, uint tokens) public returns (bool success);
    function approve(address spender, uint tokens) public returns (bool success);
    function transferFrom(address from, address to, uint tokens) public returns (bool success);

    event Transfer(address indexed from, address indexed to, uint tokens);
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
contract sofoCoin is ERC20Interface {

 string constant tokenName = "SofoCoin"; //
 string constant symbol = "Sofo";
 mapping (address => uint) coinBalance;
 mapping(address => mapping (address => uint256)) allowed;
 uint  decimal = 8; //decimal of 18th for one unit of crncy
 uint public  totalSupply;
 uint public initialSupply ;
 address public owner;  
 /*  uint public startDate;
 uint public bonusEnds;
 uint public endDate; */

    constructor() public payable{
        totalSupply = 3000000000 * (10 ** decimal);
        initialSupply = 1500000000 * (10 ** decimal);
        owner = msg.sender;
        coinBalance[msg.sender] = initialSupply;
    }

    function initialSupply() public constant returns(uint){
        return initialSupply;   
    } 

    function  totalSupply() public constant returns (uint){
        return totalSupply;
    }

    function balanceOf(address tokenOwner) public constant returns(uint balance)  {
        return coinBalance[tokenOwner];
    }

    function allowance (address tokenOwner,address spender) public constant returns(uint remaining)  {
        return allowed[tokenOwner][spender];
    }

    function approve(address spender, uint tokens) public  returns (bool success){
        uint haveToken = balanceOf(msg.sender);
        require (haveToken>= tokens && tokens>0);
        allowed[msg.sender][spender] = tokens;//address(msg.sender) -> [address(spender)->token(amount of acess)] 
        emit  Approval(msg.sender, spender, tokens);
        return true;
    }

    function transfer(address to, uint tokens) public  returns (bool success){// from any  acc to any addres
        uint haveToken = balanceOf(msg.sender);
        require (haveToken>= tokens && tokens>0);
        coinBalance[msg.sender]= coinBalance[msg.sender] - tokens;
        coinBalance[to]  = coinBalance[to] + tokens;
        emit  Transfer(msg.sender, to, tokens);
        return true;      
    }

    function transferFrom(address from, address to, uint tokens) public  returns (bool success){
        uint allowedToken = allowance(from , msg.sender);
        require (allowedToken>0 && allowedToken>=tokens);
        coinBalance[from]=  coinBalance[from] - tokens ;
        allowed[from][msg.sender] = allowed[from][msg.sender] -tokens;//here msg.sender is the person having acess for acc of person
        coinBalance[to] = coinBalance[to] + tokens;
        emit Transfer(from, to, tokens);
        return true; 
    }          
}












//ico.sol




pragma solidity ^0.4.24;

import "./token.sol";

contract ico {
  /*   mapping (address => uint) coinBalance;
     mapping(address => mapping (address => uint256)) allowed;*/
     uint  decimal = 18; //decimal of 18th for one unit of crncy
     uint public  totalSupply ; 
     uint public initialSupply = address(this).balance;
     address public owner; 
     uint valueOfEther = 1000; // 1000 sofoCoin = 1 ehter  
     **sofoCoin ercObject;**
     event chk(address);
      uint public a;
    constructor (address tokenaddress) public payable{
         **ercObject = sofoCoin(tokenaddress);**
         owner = address(this);
         totalSupply = ercObject.totalSupply();
    }   

    function  burnTokens( uint tokens) public returns(bool res)  {
        // burn token from qwner acc only burn from intitialSupply
        uint haveToken = ercObject.balanceOf(owner);
       emit chk(owner);
        require (msg.sender==owner && haveToken >= tokens);
        **ercObject.coinBalance[owner] = ercObject.coinBalance[owner] - tokens;**

        initialSupply = initialSupply-tokens;
        return true; 
    }

    function tokenDistribution (address to,uint tokens) private returns(bool res)  {
      require(msg.sender== owner);
      calDiscount(tokens);
      return ercObject.transfer(to ,tokens);
    }

    function calcuateRate (uint amount) internal view returns(uint token){
     return amount * (valueOfEther /10^18);
    }

    function buyToken() public payable {
      require(msg.value >= 100000000000000);
      uint tokens = calcuateRate(msg.value);
      tokens  = calDiscount(tokens);
      tokens= tokens * (10 ** decimal);
      ercObject.transfer(msg.sender, tokens);
    }

    function calDiscount (uint tokens) view public returns(uint bonus)  {

      uint coinsDistrubuted = initialSupply - ercObject.coinBalance[owner]; // initially owner have all coins so money distrubuted among the acc is deducted from owner  

      uint percentage =  (100 * coinsDistrubuted) /initialSupply; 

       if( percentage <=10 || percentage == 0){
          bonus = 40;
        }
       else if( percentage <=20){
          bonus = 30;
        }
       else if(percentage <=30){
          bonus = 20;
        }
       else if(percentage <= 40){
          bonus = 10;
       }
       else{
          bonus = 0;
       }
       tokens = tokens + (tokens/100)*bonus;
       tokens = tokens  * (10 ** decimal);
        return tokens;
    }

     function  mintCoin(uint tokens) public returns(bool res){
        uint mintToken = initialSupply+tokens;//initialsupply after minting 
        require ((msg.sender == owner) && (tokens>0) && (totalSupply>=mintToken));
        ercObject.coinBalance[owner] = ercObject.coinBalance[owner] + tokens; 
/*         initialSupply = coinBalance[owner];*/
        initialSupply = initialSupply + tokens;
        return true;
    }

   /* This unnamed function is called whenever someone tries to send ether to it */
    function  () public {
        revert();     // Prevents accidental sending of ether
    }


}

i want to access variable from token contract in ico contract , functions are accessible using obj.functionName() but mapping and variables are showing error.

browser/ico.sol:28:9: TypeError: Indexed expression has to be a type, mapping or array (is function (address) view external returns (uint256)) ercObject.coinBalance[owner] = ercObject.coinBalance[owner] - tokens; ^-------------------^

3
  • This is a compilation error, which has nothing to do with your ultimate goal (as expressed in the title of your question). So instead of pasting your entire code here, just fix it. Commented Sep 26, 2018 at 10:55
  • why this compilation error is occuring? Commented Sep 26, 2018 at 11:03
  • dappsforbeginners.wordpress.com/tutorials/… find some better solutions Commented Sep 26, 2018 at 11:29

1 Answer 1

0

You are trying to access and alter the mapping of an entire other contract. If this were possible, Solidity would be pretty useless as anyone could update every smart contract.

You'll have to create a setter method for the balance in sofoCoin to update the mapping indirectly. This setter checks that it's being called by ico (require(msg.sender == address(ico)) or something)

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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