Solidity – How to Access a Token Instance Created by Another Smart Contract Constructor

contract-developmentsoliditytokenstruffle

I am using a smart contract which creates a token instance in its constructor:

contract Exemple{

       Token public myToken

       function Exemple(){
               myToken=Token(this);
       }
}

How can i access this token instance from outside of the contract (using truffle console)? For instance to make a command such as: myToken.approve(address,value, {from: other_account})

Hope my question makes sense. Just a beginner here…

Best Answer

You can get contract instance as below:

Using Web3:

    // ------- Get Web3 instance --------------------------------------------------------------
    var web3Client = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

    // ------- Get ABI of the contract ---------------------------------------------------------
    // #################### REPLACE it with your ABI #############################
    var myContractABI = <contract ABI>;

    // ------- Get address of the contract -----------------------------------------------------
    // #################### REPLACE it with your Address #############################
    var myContractAddress = '<address of the contract from which it is deployed>';

    // ------- Create contract client ----------------------------------------------------------
    var myContract = web3Client.eth.contract(myContractABI);

    // ------- Create contract instance --------------------------------------------------------
    var myContractInstance = myContract.at(myContractAddress);

    // ------- Invoke any method of the instance ------------------------------------------------
    myContractInstance.myContractMethod();

From Truffle console:

    myContract.deployed().then(function(instance) {
        myContractInstance = instance;
    })

    myContractInstance.myMethod()
Related Topic