0

I want to exec a script in the truffle console.

script

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

const contract = require('truffle-contract')
const Message = contract(artifacts);
Message.setProvider(web3.currentProvider);


module.exports = ()=> {
  creator = web3.eth.accounts[0];
  purchaser = web3.eth.accounts[1];
  web3.personal.unlockAccount(creator);
  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");} )
}

Manually

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.

closed as off-topic by Ismael, Achala Dissanayake, Richard Horrocks, Briomkez, mirg Sep 12 '18 at 1:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Ethereum, the decentralized application platform and smart contract enabled blockchain, within the scope defined in the help center." – Ismael, Achala Dissanayake, Richard Horrocks, Briomkez, mirg
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Does it work fine if you set web3.eth.defaultAccount = web3.eth.accounts[0] in truffle develop beforehand? – Paul 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
0

This solution worked:

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

module.exports = (callback)=> {
  Message.setProvider(web3.currentProvider);

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

  web3.eth.defaultAccount = creator
  web3.personal.unlockAccount(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.