0
pragma solidity ^0.5.0;
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol';

contract SubToken{
    using SafeMath for uint;

    struct token{ 
        string  name; 
        string   symbol; 
        uint8  decimals; 
        uint  totalSupply;  
    }

    token public mytokens;

    mapping(address => token) Tokens;
    mapping(address => uint) balances;

    constructor(string memory _name,string memory _symbol,uint8 _decimals,uint256 _totalSupply)  public{  
        mytokens = token(_name,_symbol,_decimals,_totalSupply);
        balances[msg.sender]=_totalSupply;  
    }

    function getInformations(address contract_address) public view returns(string memory name,string memory symbol,uint8 decimals,uint totalSupply){
        return (Tokens[contract_address].name,Tokens[contract_address].symbol,Tokens[contract_address].decimals,Tokens[contract_address].totalSupply);
    }

    //function transferFrom(address from, address to, uint tokens) public returns (bool success);
    //function allowance(address tokenOwner, address spender) public view 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 MainContract {
    mapping(address => mapping(address=>uint)) balances;

    mapping(address => mapping(address => uint)) allowed;

    address[] public contracts;

    using SafeMath for uint;

    address contract_address;

    struct token{ 
        string  name; 
        string   symbol; 
        uint8  decimals; 
        uint  totalSupply;  
    } 
    mapping(address => token) public tokens;

    function createNewContract(string memory _name,string memory _symbol,uint8 _decimals,uint256 _totalSupply) public returns(address youraddress){ 
        SubToken st = new SubToken(_name,_symbol,_decimals,_totalSupply);
        tokens[address(st)] = token(_name,_symbol,_decimals,_totalSupply);
        return(address(st));
    } 


    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);

}

Hey guys I am a begginer of solidity so my question about contact address and get some information.

so i have 2 contract first one is sub second one is main. Inside my main contract i have create contract function for create for sub contract and its work well.

but in my sub contract i have get information function. this function take a contract address parameter and return informations. and problem at there this function doesn’t work .

I check my code but i dont what I suppose to do
Anyone can help me please :)

0

When you create a SubToken from the MainContract, the SubToken constructor doesn't add the newly created token to the SubToken Tokens mapping and it is this mapping that is used in getInformations.

Both your factory (MainContract) and token (SubToken) have mappings for tokens.
I don't know your requirements for having this data captured in both places. I would have suspected this to only be needed in the MainContract.

A factory creating ERC20 tokens (using the OpenZeppelin ERC20 implementation) could look as follows:

pragma solidity ^0.5.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.3.0/contracts/token/ERC20/ERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.3.0/contracts/token/ERC20/ERC20Detailed.sol";


contract Token is ERC20, ERC20Detailed {

    constructor (string memory name, string memory symbol, uint8 decimals, uint256 totalSupply) 
        public 
        ERC20Detailed(name, symbol, decimals) 
    {
        _mint(msg.sender, totalSupply);
    }
}

contract Factory {
    struct tokenData { 
        string name; 
        string symbol; 
        uint8 decimals; 
        uint256 totalSupply;  
    } 

    event TokenCreated(address indexed tokenAddress, string name, string symbol, uint8 decimals, uint256 totalSupply);

    mapping(address => tokenData) private _tokens;

    function createToken(string memory name, string memory symbol, uint8 decimals, uint256 totalSupply) public returns(address tokenAddress){ 
        Token token = new Token(name, symbol, decimals, totalSupply);
        _tokens[address(token)] = tokenData(name, symbol, decimals, totalSupply);

        emit TokenCreated(address(token), name, symbol, decimals, totalSupply);

        return(address(token));
    } 

    function getTokenData(address tokenAddress) public view returns(string memory name, string memory symbol, uint8 decimals, uint256 totalSupply){
        return (_tokens[tokenAddress].name, _tokens[tokenAddress].symbol, _tokens[tokenAddress].decimals, _tokens[tokenAddress].totalSupply);
    }
}

If you are creating ERC20 tokens you may want to look at the OpenZeppelin Contracts implementation to see if this meets your needs.
See the documentation for details: https://docs.openzeppelin.com/contracts/2.x/tokens#ERC20

If you have questions on using OpenZeppelin you can ask in the Community Forum: https://forum.openzeppelin.com/

Disclosure: I am the Community Manager at OpenZeppelin

  • Hello I read your answer and its make sense to me. "When you create a SubToken from the MainContract, the SubToken constructor doesn't add the newly created token to the SubToken Tokens mapping and it is this mapping that is used in getInformations." but I am still dont know what should i do can you give me advise like you have to chance this ,add this ,remove this or something like that. Thanks for the comment agaion – Deniz Demirel Aug 7 at 11:56
  • I have updated my answer with some sample code which has a factory that creates ERC20 tokens and emits an event when the token is created. – abcoathup Aug 8 at 6:23

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.