7

I'm following truffle tutorial to interact with a deployed contract and have a naive question. How do I run this block of multiline scripts in truffle(development)> without collapsing it into a single line?

var account_one = "0x1234..."; // an address
var account_two = "0xabcd..."; // another address

var meta;
MetaCoin.deployed().then(function(instance) {
  meta = instance;
  return meta.sendCoin(account_two, 10, {from: account_one});
}).then(function(result) {
  // If this callback is called, the transaction was successfully processed.
  alert("Transaction successful!")
}).catch(function(e) {
  // There was an error! Handle it.
})
  • 1
    I haven't used truffle myself, but in Node.js, you could just paste this in (or type it by hand), and the newlines wouldn't be a problem. Does the truffle console not work the same way? – smarx Jan 17 '18 at 0:14
  • Unfortunately, no. It throws SyntaxError: Unexpected end of input at MetaCoin.deployed().then(function(instance) {. – sinoTrinity Jan 17 '18 at 0:27
  • Disappointing! I'm sure someone will come along with a helpful answer. :-) Good luck! – smarx Jan 17 '18 at 0:31
  • Are you looking for a particular solution with a console or you just need a solution to run this code? Sorry, I just need to clarify. – Roman Frolov Jan 17 '18 at 1:32
  • 1
    If this is your first truffle tutorial, let me suggest this one. Just because it will cover every topic in the development. As for tutorial that you are following you may need to complete the code to actually run it. – Roman Frolov Jan 17 '18 at 2:14
7

You can simply require the file you want to run, just like how you would in a node application:

$ echo "console.log('hello')" > myfile.js
$ truffle console
truffle(development)> require('./myfile.js')
hello

However, the deployed contract won't be available but truffle does offer the exec command which injects a web3 provider. You'd just have to create the contract instance.

Example myfile.js

const artifacts = require('./build/contracts/MyContract.json')
const contract = require('truffle-contract')
const MyContract = contract(artifacts);
MyContract.setProvider(web3.currentProvider);

MyContract.deployed()
.then(function(instance) {
  console.log(instance.address)
})
.catch(function(error) {
  console.error(error)
})

Then run in console:

$ truffle console
truffle(development)> exec ./myfile.js
0x6e153f03845fc132d73cb03c3f1d1b6c13d715fc
  • Your example works. But as soon as I use MetaCoin in myfile.js, it complains ReferenceError: MetaCoin is not defined. – sinoTrinity Jan 17 '18 at 5:57
  • @sinoTrinity are you sure you compiled it and see the artifacts in build/contracts/MetaCoin.json? The paths are relative as well so make sure your file is in the correct directory. – rhlsthrm Jan 17 '18 at 22:48
  • 1
    Works after I install truffle-contract. – sinoTrinity Jan 18 '18 at 0:23
  • When running it with exec the script always gets stuck: ethereum.stackexchange.com/questions/58337/… maybe you can help me, thx? – Andi Giga Sep 8 '18 at 10: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.