5

When writing tests in truffle (v3.2.1) I can forget about things like a contract's ABI and address and just use ContractName.deployed() to interact with a contract on my testrpc chain. How can I mimic this behavior in my external Javascript code, that is, not by using truffle's built-in console or truffle test? Is there a library I need to import in my script or do I need to manually get the ABI and deployment address of the contract as if I was not using Truffle?

I'd be also fine if I could use those contract abstraction by running my JS code through truffle, but I'm not sure of how would I do that.

Thanks!

4

Not sure whether this is the intended way, but experimenting with bits and pieces of code found around the Internet resulted in something that seems to be working. Here's what I did in my javascript app:

....
const artifacts = require('../build/contracts/MyContract.json')
const contract = require('truffle-contract')
let MyContract = contract(artifacts);
MyContract.setProvider(web3.currentProvider);    
return MyContract.deployed().then(function(instance){      
  return instance.sendTransaction(param1, param2, {from: accounts[0]});
}).then(function(tx_id){
....

The json file is what was produced by truffle when migrating MyContract. Note that I had to install truffle-contract (on top of truffle) for this to work. If anyone has a more elegant way of doing this, please share!

2

Assuming I have a HelloWorld contract with a uint public balance variable and I'd want to get its balance I'd do the following:

let Web3 = require('web3')
var provider = new Web3.providers.HttpProvider("http://localhost:8545")
let contract = require('truffle-contract')
let HelloWorld = contract(require('./build/contracts/HelloWorld.json'))

HelloWorld.setProvider(provider);
HelloWorld.deployed().then(contract => {
   return contract.balance()
}).then(response => {
   console.log(response)
})

Worked for me! Let me know if this works for you!

0

If you where to input it manually you can use something like this in your js

var contractAddressED = 'HERE YOUR CONTRACT ADDRESS';

var byteCodeED = 'HEREYOUR BYTECODE IF YOU WANT TO';

var contractABIED = HEREYOURABI;

contractED = web3.eth.contract(contractABIED).at(contractAddressED);

To interact with your functions you can then do something like:

//to read a state variable eg. owner

contractED.owner(function(err, result) { // document.getElementById('name').innerHTML = 'Hello, ' + result; or jquery $("#owner").html('some text: ' + result); });

//to trigger a function

$("#clearstatus").click(function() { contractED.clearstatusjudging(function(err, result) { console.log(err, result); }) });

hope this helps!

  • Thanks, that will certainly work, but avoiding to do this manually and taking advantage of the contract abstraction from truffle was precisely the point of the question! – manuhalo Apr 25 '17 at 21:43

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.