5

I am trying to get a contract code from javascript. However, when I run this:

web3.eth.getCode("0x9bA082240DBa3F9ef90038b9357649Fa569fd763")

on my browser console, I get an Invalid address error which is unexpected. The above address is a valid contract address and the same function on my geth console returns the correct code. My geth client always syncs up with the blockchain. So my question: why is this problem happening? and how to fix it?

  • what is your eth --version ? on mine (1.2.0) is returns a too long stuff to paste in the comment :) – euri10 Mar 11 '16 at 8:55
  • I check with other contract addresses, the problem still shows up. Seems like getCode only works with ...normal address (like personal address). But getCode is just to fetch the code of a contract?.. – Loi.Luu Mar 11 '16 at 8:56
  • @euri10 mine is 1.2.1 – Loi.Luu Mar 11 '16 at 9:04
  • Have you tried lower-casing all of the letters? The Case-checksum was only added recently and web3 may not be compatible with it yet – Tjaden Hess Mar 13 '16 at 22:00
  • i am also facing the some problem, i deployed the contract using truffle/pudding and getting the address as well but when i do > web3.eth.getCode("0x48007968c0bd1cac4aef962754eab8c2d806b4aa") in geth it will give "0x", what does it mean?. when i use metaCoin deploy ,the address is giving the code, but tokenRecipient contract is not working for me .. please help me – Priyanka D L Jul 23 '16 at 10:35
4

Just figure out why. IDK whether its a bug in the implementation, but the function isAddress(address) in web3.js is implemented like this

var isAddress = function (address) {
    return /^(0x)?[0-9a-f]{40}$/.test(address);
};

similarly for other functions like isStrictAddress(address). When I changed the regex rule to accept A-F characters as below

var isAddress = function (address) {
    return /^(0x)?[0-9a-fA-F]{40}$/.test(address);
};

then things work fine.

1

on eth 1.2.0 it returns, maybe your eth version is too old

> web3.eth.getCode("0x9bA082240DBa3F9ef90038b9357649Fa569fd763")
'0x6060604052361561007f576000357c010000000000000000000000000000000000000000000000000000000090048063228cb733146102cc57806341c0e1b5146102ef5780634fb60251146102fe57806367e404ce146103795780638da5cb5b146103b2578063a0d7afb7146103eb578063cf3090121461040e5761007f565b6102ca5b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561015c57600160149054906101000a900460ff16156100f357610002565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166000600260005054604051809050600060405180830381858888f1935050505050346002600050819055506102c7565b6000600036905011156102c657600160149054906101000a900460ff161561018357610002565b6003600050546002600036604051808383808284378201915050925050506020604051808303816000866161da5a03f1156100025750506040518051906020015010156102c5573373ffffffffffffffffffffffffffffffffffffffff166000600260005054604051809050600060405180830381858888f193505050505060003660046000509190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061024d57803560ff191683800117855561027e565b8280016001018555821561027e579182015b8281111561027d57823582600050559160200191906001019061025f565b5b5090506102a9919061028b565b808211156102a5576000818150600090555060010161028b565b5090565b50506001600160146101000a81548160ff021916908302179055505b5b5b5b565b005b6102d96004805050610490565b6040518082815260200191505060405180910390f35b6102fc6004805050610543565b005b61030b60048050506104a2565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f16801561036b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6103866004805050610431565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6103bf6004805050610457565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6103f86004805050610499565b6040518082815260200191505060405180910390f35b61041b600480505061047d565b6040518082815260200191505060405180910390f35b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600160149054906101000a900460ff1681565b60026000505481565b60036000505481565b60046000508054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561053b5780601f106105105761010080835404028352916020019161053b565b820191906000526020600020905b81548152906001019060200180831161051e57829003601f168201915b505050505081565b600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156105d457600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b5b56'
  • Did you try it on javascript console? The web3.js module? – Loi.Luu Mar 11 '16 at 9:04
  • I tried inside eth, misread you were trying in a browser, sorry – euri10 Mar 11 '16 at 9:07
  • Yeah, i also got the result inside eth/ geth, but it doesn't work with the web3.js currently – Loi.Luu Mar 11 '16 at 9: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.