3

With truffle-contract, all you needed to do to get the contract instance was something like this:

const getContract = require('truffle-contract')
const contractJSON = require('./MyContract.json')

const contract = getContract(contractJSON)
contract.setProvider(web3.currentProvider)

const instance = await contract.deployed()
return instance
  • Please note that the MyContract.json file is the build artifact from Truffle.

But since the instance created by truffle-contract returns promises instead of the PromiEvent which came with version 1.0 of Web3, I decided that I wanted to use regular v1.0 Web3 in order to get the contract instance. In other words, I no longer wanted to rely on truffle-contract.

Unfortunately, I am not that familiar with the contract API in the new 1.0 Web3. This is my attempt:

const contract = new web3.eth.Contract(contractJSON.abi)
contract.methods.get().call()

The first line is okay, but the second line throws the following error:

This contract object doesn't have address set yet, please set an address first.

I see that this makes sense because the documentation seems to state that you can set an address so that it will know where to find the deployed instance.

Does this mean that I'd have to go to my Truffle Console (aka testrpc) to find the contract address and copy and paste this in every time? I sure hope not, because with truffle-contract, all I needed to do was just include the JSON file and it figured it out for me.

How am I supposed to get this address and pass it in?

6
  • 1
    Yes, you need to provide the address. I assume that MyContract.json must include the address, so you could just read it from there if you want.
    – user19510
    Jul 7, 2018 at 8:21
  • @smarx but MyContract.json (i.e. the Truffle build artifact) does not contain the address. This is because it's a build artifact of the contract definition, not an artifact of a deployed instance. Yet truffle-contract seems to be able to figure it out.
    – adrianmcli
    Jul 7, 2018 at 15:56
  • Digging into the source code for truffle-contract, it looks like it does something weird like trying to check which network it's on and then somehow comparing to see whether or not that contract is deployed and then what address it is. I can't figure out how it's doing that though.
    – adrianmcli
    Jul 7, 2018 at 15:57
  • 1
    Look at the JSON in build/contracts. From a quick search, it looks to me like that's where you should find the address. I don't use Truffle myself, so I don't have anything handy to poke around in, sorry.
    – user19510
    Jul 7, 2018 at 17:06
  • That's the JSON I am talking about. It does not have the address.
    – adrianmcli
    Jul 7, 2018 at 20:26

1 Answer 1

7

I couldn't find where the address was stored because the JSON file I took was an out-dated version. It was one that was generated before I migrated on Truffle. It turns out that the JSON file is updated every time Truffle runs its migrations. And with that, it will add the address of the deployed contract instance.

I had to dig deep into their docs to find it here: https://github.com/trufflesuite/truffle/tree/develop/packages/truffle-contract-schema#networks

From there, it was easy to find the pre-deployed address. The final code looks something like this:

const networkId = await web3.eth.net.getId()
const deployedAddress = contractJSON.networks[networkId].address
const contract = new web3.eth.Contract(contractJSON.abi, deployedAddress)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.