11

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.
})
sinoTrinity
  • 377
  • 2
  • 8
  • 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? – user19510 Jan 17 '18 at 00:14
  • Unfortunately, no. It throws SyntaxError: Unexpected end of input at MetaCoin.deployed().then(function(instance) {. – sinoTrinity Jan 17 '18 at 00:27
  • Disappointing! I'm sure someone will come along with a helpful answer. :-) Good luck! – user19510 Jan 17 '18 at 00: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 01:32
  • @RomanFrolov I'm looking for a generic solution to run multiline code under truffle console mode. – sinoTrinity Jan 17 '18 at 01:49
  • You need to save your js code in some file and then use exec ./somefile.js from console. Before this you will need to use migrate to deploy your contracts to a blockchain, which you can run with Ganache or truffle develop. – Roman Frolov Jan 17 '18 at 02:05
  • 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 02:14

2 Answers2

10

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
Miguel Mota
  • 5,143
  • 29
  • 47
  • 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 05: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 00:23
  • When running it with exec the script always gets stuck: https://ethereum.stackexchange.com/questions/58337/truffle-exec-gets-stuck maybe you can help me, thx? – Andi Giga Sep 08 '18 at 10:43
3

Truffle-Contract is now deprecated: https://www.npmjs.com/package/truffle-contract

We should be using now: @truffle/contract https://www.npmjs.com/package/@truffle/contract

Also as of today this works on truffle console:

const CakeToken = artifacts.require("./CakeToken.sol")

module.exports = async function(callback) { const cakeToken = await CakeToken.deployed() console.log(cakeToken.address) }