2

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...

1

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()
0

Let's say this is how your contract looks:

contract Example {

   Token public myToken;

   //constructor  
  function Example() {
       myToken = new Token();
  }
}
contract Token {
   function getInt() constant returns(uint) {
       return 1;
   }
}

Then you need first to get object of your example smart contract. Code below is using web3js, in truffle things are a bit simpler, since contract calls and transactions returns a promise, so there is no need to provide callbacks and you may use then() function.:

var exampleContractInstance = web3.eth.contract(abiArray).at(contractAddress);

Then you need to get reference to your Token instance address with:

exampleContractInstance.myToken.call(function(error, addressOfToken) {});

Be aware that this function will not return you a smart contract object. It will return address, where your smart contract is e.g. 0xd21b9493802cf961e96efcde89958093d0172b76. Then you need to get object for your Token instance using addressOfToken:

var tokenContractInstance = web3.eth.contract(abiArray).at(addressOfToken);

Now you have object representing your Token instance and you may interact with it e.g.

tokenContractInstance.getInt.call(function(error, integerValue) {});

PS 1 Ouside of truffle console or truffle test, you will need abiArray to interact with your contracts. It should be provided by your IDE (e.g. https://remix.ethereum.org). In truffle console you may skip it since your contract definition is already there (reference here http://truffleframework.com/docs/getting_started/console):

Example.at(addressOfContract)

PS 2 Be aware that return values would be inside callbacks, so don't expect that call function will return you value returned by the contract.

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.