I want to exec a script in the truffle console.


const artifacts = require('./build/contracts/Message.json')

const contract = require('truffle-contract')
const Message = contract(artifacts);

module.exports = ()=> {
  creator = web3.eth.accounts[0];
  purchaser = web3.eth.accounts[1];
  var messageInstance = null;
  Message.deployed().then(inst => { return messageInstance = inst}).then( () =>{ console.log("Creating Message"); return messageInstance.createMessage(creator, "message.newMessage") }).catch((error)=> { console.error("error", error) }).then( ()=> { return console.log("End");} )


When I manually insert the script (just the part inside module.exports), line by line it works and I get the last console.log.

With exec

When I run exec ./run.js: the script gets stuck at the second Promise without any error.

  • Does it work fine if you set web3.eth.defaultAccount = web3.eth.accounts[0] in truffle develop beforehand? – Paul Razvan Berg Sep 8 '18 at 14:34
  • This is not an ethereum issue. It is that javascript is used incorrectly. You assign a function to module.exports, and it should be used when you want a function to be used from a another file. If you just want to execute the body of the function then you do not need a function or assigning it to module.exports, type the instruction directly in your file. – Ismael Sep 8 '18 at 19:33
  • @ Ismael, module.exportsis fine: truffleframework.com/docs/truffle/getting-started/… – Andi Giga Sep 10 '18 at 8:16
  • @PaulRBerg setting an unlocking an account (beforehand and in the script) doesn't help. Although I don't know what I change but I get an Error: invalid address error when calling the promise chain with exec. Which is usally the error for not having a default acount. I console.log the default account and it shows the right one on both solutions (beforehand & in the script). – Andi Giga Sep 10 '18 at 8:18
  • I figured it out @PaulRBerg thx for giving me the right direction. – Andi Giga Sep 10 '18 at 8:50

This solution worked:

const messageBuild = require('./build/contracts/Message.json')
const contract = require('truffle-contract')
const Message = contract(messageBuild);

module.exports = (callback)=> {

  var creator = web3.eth.accounts[0]
  var purchaser = web3.eth.accounts[1]

  web3.eth.defaultAccount = creator

  Message.defaults({from: creator})

  var messageInstance = null
  Message.deployed().then(inst => { return messageInstance = inst}).then( () =>{ console.log("Creating Message:", typeof messageInstance.createMessage, "creator", creator, "web3.eth.defaultAccount", web3.eth.defaultAccount); return messageInstance.createMessage(creator, "This is a classic warning") }).catch((error)=> { console.error("\n\n\n\n==>error", error) }).then( () =>{ console.log("Reading Message", typeof messageInstance.readMessage); return messageInstance.readMessage(creator).then((msg) => console.log("The result is:", msg)) }).then( ()=> { return console.log("End"); return callback()} )

I think it was the missing from:

Message.defaults({from: creator})

Truffle invalid address

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