4

I am trying to use Mix to test multiple instances of the same contract, therefore I am trying to select a contract by address, but currently without success. Is this a limitation within Mix or my poor understanding?

I have tried to use the web3.js API for example as described in Contract Methods but I only get an error.

Here is the code snippet that causes an error...

    var contractAddress = "0xc069cb45291acafdd701e9341e7bf730255abbe1";
var abi = '[{\"constant\":false,\"inputs\":[],\"name\":\"kill\",\"outputs\":[],\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_greeting\",\"type\":\"string\"}],\"name\":\"setgreeting\",\"outputs\":[],\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"greet\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"owned\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"type\":\"function\"},{\"inputs\":[{\"name\":\"_greeting\",\"type\":\"string\"}],\"type\":\"constructor\"}]';
var queryContract = web3.eth.contract(abi);

/*
The next line throws "web3.js - line 2838 - Uncaught TypeError: undefined is not a function"
*/
var LocalContract = queryContract.at(contractAddress);

Is this related to Mix? I believe that the ABI is ok.

Here are the full details. I have borrowed the greeter contract for the purposes of chasing my error. The contract..

contract mortal {
    /* Define variable owner of the type address*/
    address owner;

    /* this function is executed at initialization and sets the owner of the contract */
    function mortal() { owner = msg.sender; }

    /* Function to recover the funds on the contract */
    function kill() { if (msg.sender == owner) suicide(owner); }
}

contract greeter is mortal {
    /* define variable greeting of the type string */
    string greeting;

    /* this runs when the contract is executed */
    function greeter(string _greeting) public {
        greeting = _greeting;
    }

    /* main function */
    function greet() constant returns (string) {
        return greeting;
    }
    /* set function */
    function setgreeting(string _greeting)  {
        greeting = _greeting;
    }
    /* other function */
    function owned() constant returns (address) {
        return owner;
    }
}

And now the code...

    <html>
  <head>
    <title>Multiple Contract Test</title>
<script type='text/javascript'>

/*
 Test Set Functions
 */

function getGreetingB() {
    var contractAddress = "0xc069cb45291acafdd701e9341e7bf730255abbe1";
    var abi = '[{\"constant\":false,\"inputs\":[],\"name\":\"kill\",\"outputs\":[],\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_greeting\",\"type\":\"string\"}],\"name\":\"setgreeting\",\"outputs\":[],\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"greet\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"owned\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"type\":\"function\"},{\"inputs\":[{\"name\":\"_greeting\",\"type\":\"string\"}],\"type\":\"constructor\"}]';
    var queryContract = web3.eth.contract(abi);

    /*
    feeble attempts at querying the contract object
    */
    console.log("Logging start");
    //console.log(Object.keys(queryContract));
    console.log(Object.getOwnPropertyNames(queryContract));   // Yields "eth,abi,new"   and NOT "at"
    console.log(Object.getOwnPropertyNames(queryContract.eth));
    console.log("Logging End");

    /*
      Some access from the contract that does work.
      */
    document.getElementById('QueryContractABIReturned').innerText = queryContract.abi;

    /*
    Now lets try to specify exactly which contract to reference.

    The next line throws "web3.js - line 2838 - Uncaught TypeError: undefined is not a function"
    */

    var LocalContract = queryContract.at(contractAddress);




    document.getElementById('QueryContractAddressB').innerText = LocalContract.address;
    document.getElementById('QueryGreetingB').innerText = LocalContract.contract.greet();
    document.getElementById('QueryOwnerB').innerText = LocalContract.contract.owned();

}

function setGreetingB() {
/*
 nothing yet
 */
}


</script>
  </head>
  <body>
    <h3>Duplicate Contract Access Test B</h3>

<div>
    <input value='Fetch Greeting' type='button' onclick='getGreetingB()' />
<table>
    <tr><td>ABI:</td><td><div id='QueryContractABIReturned'></div></td></tr>
    <tr><td>Greeting:</td><td><div id='QueryGreetingB'></div></td></tr>
    <tr><td>Owner Address:</td><td><div id='QueryOwnerB'></div></td></tr>
    <tr><td>Contract Address:</td><td><div id='QueryContractAddressB'></div></td></tr>
</table>
</div>  
<hr>
    <div>
        New Greeting: <input type="string" id="newgreetingB">
        <button onclick="setGreetingB()">Save</button>
    </div>
    </body>
</html>
1

To get a contract instance by its address you can do:

IN SOLIDITY

ContractClass contractInstance = ContractClass(contractAddress);

IN JS WITH WEB3

var Web3 = require('web3');
var web3 = new Web3();
var contractClass = web3.eth.contract(contractClassAbi);
var contractInstance = contractClass.at(contractAddress);

IN YOUR CASE

You have to install and initialize web3.js correctly, the error comes from the web3 object not from the functions you've used.

2
  • Thanks, I guess i suspected as much. But when i add that code snippet, I get the error "Uncaught ReferenceError: require is not defined"
    – Slender
    Apr 12 '16 at 6:01
  • With the error "Uncaught ReferenceError: require is not defined" It won't go past first base. "require" is a node.js feature is it not? I am stumped as to how to get it referenced. If this is running in Mix in the index.html do i need something like browserify? running Ubuntu 15.10 in VirtualBox VM. I installed ethereum binaries (including mix) as per ethdocs.org/en/latest/ethereum-clients/cpp-ethereum/… I then installed node.js, npm, ethereum-console, web3
    – Slender
    Apr 12 '16 at 6:09

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.