3

I'm building a dapp and would like to interact with an already existing contract.

Previously working with Truffle I was writing my own contract, compiling it and deploying it. This process generated a Contract Artifact that I could use with the truffle-contract library and make calls using web3.

How would I go about making calls to a pre existing contract that I never compiled and therefor don't have a Contract Artifact for?

2 Answers 2

5

You would require ABI and the address at which the contract has been deployed.

var abi = <ABI of contract>;                                // Set contract ABI
var newContract = web3.eth.contract(abi);                   // Contract object
var contractInstance = newContract.at(<Contract Address>);  // instance of the contract

contractInstance.functionName.call();                       // Invoke any function of the contract
0
2

In that case, you will have to deploy the contract using web3js and then you can call contract functions without the help of truffle.

Sometime back I also had the similar need of deploying the contract using web3, so I have a sample code for contract deployment using web3 in this gist, you may find worth looking at.

Your Answer

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

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