8

From a truffle develop console, is there a way I can get the contract instance from the contract's address?

I have a remote ec2 with truffle/geth installed and truffle develop instance running. From a local truffle develop console, I have deployed my contracts to that remote network (using the truffle migrate --compile-all --reset --network remote_network_name). The contracts deployed successfully, and I have an address for where the contract is stored.

In a truffle develop console on the remote server, I have tried Contract_Name.deployed().then(function(instance) {app = instance; }) to get the instance, but I get a Contract_Name is undefined error.

From a truffle develop console on the remote server, is there a way I can get the contract instance from the contract's address?

Maybe something like MyContract = new Contract.at("ContractAddress")?

3
  • Do the remote instance have the source code of your contracts? If the source code is available you can try copying the build/ directory with the deployment artifacts to the remote server.
    – Ismael
    Apr 11, 2018 at 3:01
  • Thanks Ismael. The remote instances do not have the source code of my contracts, and I would like to avoid copying my build folder to the server. For instance, if I deployed these contracts to a test network and I do not have file upload access within that server, is there a way I can obtain the contract instance from the contract's address? Is this possible without copying the build folder? Or does Truffle need the build folder to reference the contract ABIs? Apr 11, 2018 at 14:59
  • Inside the build/ directory there's a .json file for each contract, inside that file there's the contract's ABI and the deployment address. That should be enough to get the deployed instance. Perhaps you can use something like github.com/trufflesuite/truffle-artifactor to wrap the artifact in a javascript class that can be used without copying the whole directory.
    – Ismael
    Apr 11, 2018 at 15:38

3 Answers 3

6

There is a much easier way when using truffle.

First include the contract artifact, same as when creating a new contract in truffle.

let MyContract = artifacts.require("./MyContract.sol");

then in script:

let myContract = await MyContract.at('my contract address');

This way no need to take the ABI from another file etc.

1
  • Daniel's answer is the standard way, but this way is needed if the contract you want is spawned by another contract. Thanks.
    – dlsso
    Jan 10, 2022 at 6:05
6

Personally, in Truffle, I favour not having to know or need my deployed contract's address to get its instance. It's possible you don't remember your contract's address or the address has been buried deep within the terminal as new contents push old contents further up.
The way to go to get a deployed contract instance without knowing the contract's address is to use the contract's deployed() method thus:

const deployedContractInstance = await MyContract.deployed()

And that's it!

You now have access to all the attributes and methods your contract exposes, for example, you can get the address by simply doing deployedContractInstance.address.

1
  • 1
    Really great answer! You not only explain the reason behind preferring this approach but with the relevant code and example in a very concise answer. Dec 8, 2019 at 2:30
6

Solved! You need two things to get a contract's instance on a remote network:

  1. Your contract's ABI — after deploying your contract to the remote network by running truffle migrate --compile-all --reset --network remote_network_name in your local truffle develop console, Truffle will create a json object representation of your contract in your local build/ directory. Inside of that json object, you can find your contract's ABI array.
  2. Your contract's address — after deploying to your contract to the remote network, your contract's address will be displayed in your local truffle develop console like so:

Sceenshot of Truffle Develop Console


With those two pieces of information, you can enter your truffle develop console on the remote server and get an instance of your contract through these commands:

  1. var MyContract = new web3.eth.Contract(abiArray);
  2. var contractInstance = await MyContract.at(contractAddress);

NOTE: this is assuming you're using web3.js version 0.2x.x. If you are using web3.js 1.0, refer to this documentation.

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.