1

I am trying to follow this flow to access a contract that has been mined previously:

var MyContract = new web3.eth.Contract(abiJSON);

// instantiate by address
var contractInstance = MyContract.at(address);

Unfortunatly the contractInstance does not work and I get a contract.at is not a function error. I suspect it has something to do with web3js newest version but I am not sure what to use instead.

I have just tried:

var deployed = new web3.eth.Contract(jsonAbi, address)

But then when I try to call my public function on the contract I get an error:

TypeError: Cannot read property 'call' of undefined

If I look into deployed every thing from the abi to the address is there, so it seems like the fetching of the contract actually worked.

2

at method is deprecated since web3.js v1.0.x.

Instead you can use this format:

var myContract = new web3.eth.Contract(abiInterface, contractAddress);
1

Given a deployed contract at address, with ABI defined as abiJSON, using web3@1.0.0-beta.35, the corresponding code for calling a function specified in that contract is as follows:

Instancing MyContract as originally mentioned is correct. However, setting the contract address has changed in Web3 1.0 [1]: var MyContract = new web3.eth.Contract(abiJSON); MyContract.options.address = address;

Alternatively, setting the address during creation is also valid, as correctly done in the provided code: var deployed = new web3.eth.Contract(abiJSON, address);

Other than the at() function not being available, the function call has also changed in Web3 1.0 to the following [2]: MyContract.methods.myPublicFunction(<parameters>).call(<callback>);

Example using the callback: MyContract.methods.myPublicFunction().call(function(error, result){ console.error(error); console.log(result);
}

It is also possible to use the returned Promise a way to read back the results.

[1] https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#options-address
[2] https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-call

Stay super!

/javi

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.