I'm trying to interact with my smartcontract but get the error: Uncaught TypeError: MyContract.at is not a function

My Code:

var web3 = require('ethereum.js');
web3.setProvider(new web3.providers.HttpProvider());

var MyContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"type":"function"}]);

var result = MyContract.at("MyContractAddress").get();

When I request default Geth functionality I get a correct response:

var version = web3.version.api;
console.log(version);  "0.2.4"
  • var web3 = require('ethereum.js'); are you sure that it shouldn't be var web3 = require('web3.js'); – euri10 May 18 '16 at 11:02

You need to provide the address of your geth node to the HttpProvider constructor so your web3 client is connected to a node. Certain web3 functions like contract.at() only works if you are connected to a node. web3.version.api does not need ethereum connection to function.

web3.setProvider(new web3.providers.HttpProvider('http://geth-node-ip:8545'));

If you did set an address for the HttpProvider, then check that the IP and port # are correct. (i'm using 8545 as an example since that's the default RPC port)

  • I add the node, but still no result. Like my example above I can get the Geth version and I can also get the transactionCount of a specific address: var number = web3.eth.getTransactionCount("MyAddress"); console.log(number); – Jeroen May 18 '16 at 6:16
  • I don't know how you're getting the web3 instance from your "ethereum.js" file, but if i get mine from the following, the rest of your code works for me (provided I have the geth IP and actual contract address filled in). var Web3 = require('web3'); var web3 = new Web3(); – monkey king May 18 '16 at 16:19

You need to create an instance of that contract as described here. You correctly created the contract object (consider placing your ABI into a separate file) but then need to create an instance via

var MyContract = web3.eth.contract(ABI);
var contractInstance = MyContract.at(address);

If you have not deployed your contract, you have to do that first (this will give you the address which you can re-use from then on:

var contractInstance = MyContract.new([contructorParam1] [, contructorParam2], {data: '0x12345...', from: myAccount, gas: 1000000});

I placed a more complete walk-through on my github.

  • 1
    I still have the same result; MyContract.at is not a function. It has problems with the ".at" part, I also tried with another contract. The contract works fine in Geth. – Jeroen May 17 '16 at 20:26
  • 1
    Did you run the code that I suggested in geth or in the browser? Please try in geth first. Also check console.log(JSON.stringify(MyContract)). – SCBuergel May 17 '16 at 20:34
  • I try the code in my browser but it only works in Geth console. I have communication from my browser to my Geth node with; var number = web3.eth.getTransactionCount("MyAddress"); console.log(number); But with MyContract.at I get an error. I use the simple storage contract as an example: solidity.readthedocs.io/en/latest/… – Jeroen May 18 '16 at 6:20
  • And I'm running my Geth node with: geth --unlock 0 --rpc --rpcport "8080". I can also send eth_sendTransaction via RPC JSON, the problem is that I want to request and display my balance. I'm stuck at MyContract.at... – Jeroen May 18 '16 at 6:29
  • 1
    Jeroen, check your ABI is a JS array when passed to web3.eth.contract(ABI) and not stringified. I've been stumped by this same contract.at(addr) for days but have just sorted it now. – o0ragman0o May 19 '16 at 10:33

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.