2

Using the Truffle console, I'm able to obtain the address of a deployed smart contract by issuing the following:

MySmartContract.deployed().address;

In this case, MySmartContract is a smart contract written in Solidity and is compiled and migrated with Truffle to TestRPC.

Now, using the Web3-library, I'm able to obtain a 'reference' to the deployed smart contract by issuing the following statements:

web3.eth.contract(MySmartContract.abi).at("0xf6cd8e7179c88aae52339b5268fd67ea2d3305b9")

...but obviously, I want to treat and access the smart contract like a singleton.

How do I obtain the address for a deployed (singleton) smart contract without having to specify its address?

Update:

While I have seen the term being used in blogs and tutorials, 'singleton' does indeed not cover it exactly; especially considering the fact that, if you upgrade a smart contract, its actually deployed to a different address. (Just trying to verify my understanding of things here; correct me if I'm wrong!)

But how exactly does Truffle know what contract I'm referring to with MySmartContract.deployed()?

Basically, I want to tell web3: "Here is my contract ABI, find me its address on the blockchain!"

3

For start - there's no way that contract can be a singleton (a single instance in the whole blockchain). Anyone with its source code can deploy it multiple times :)

This means that you need to know the address of the contract that you've deployed (or at least the one that you're going to use).

You could try to find the address by scanning the blockchain, however it would require knowing some details (like who created it etc). I'm not sure if it is possible (or enough) to search the blockchain only by contracts compiled data.

  • Hi Radmen, thanks for your response! While I have seen the term being used, 'singleton' does indeed not cover it exactly; especially considering the fact that, if you upgrade a smart contract, its actually deployed to a different address. (Just trying to verify my understanding of things here; correct me if I'm wrong!) But how exactly does Truffle know what contract I'm referring to with MySmartContract.deployed()? Basically, I want to tell web3: "Here is my contract ABI, find me its address on the blockchain!" – Rob Wijkstra Mar 7 '17 at 13:47
  • You're right about contract upgrades :) Truffle generates quite a lot of boilerplate you can check it in build folder. If you want to deploy at your own a contract you need to: 1) compile source code, 2) create contracts interface (using abi data generated by compiler), 3) deploy contracts binary data (it's also generated by compiler) 4) fetch contracts address. Here you can find more details: github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract – radmen Mar 7 '17 at 14:02
  • Radmen, thanks for the link. Currently I am able to perform the 4 steps you just mentioned in my front-end application; however, my desired use-case is not to be found. My approach at trying to work with a 'root contract' is misguided, isn't it? – Rob Wijkstra Mar 7 '17 at 14:11
  • Maybe I don't understand something - you'd like to be sure that users are interacting with one, selected contract? If so I'm affraid that you need to store somewhere address of deployed contract. – radmen Mar 7 '17 at 14:18
  • Yes, that's exactly what I'm going for! For now, I'll stick to doing just that. Thanks! Edit: I must say that I still wonder what the MySmartContract.deployed()-function in Truffle does. – Rob Wijkstra Mar 7 '17 at 14:19

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.