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.
web3.eth.defaultAccount = web3.eth.accounts[0]intruffle developbeforehand? – Paul Razvan Berg Sep 08 '18 at 14:34module.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 tomodule.exports, type the instruction directly in your file. – Ismael Sep 08 '18 at 19:33module.exportsis fine: https://truffleframework.com/docs/truffle/getting-started/writing-external-scripts – Andi Giga Sep 10 '18 at 08:16Error: invalid addresserror when calling the promise chain with exec. Which is usally the error for not having a default acount. Iconsole.logthe default account and it shows the right one on both solutions (beforehand & in the script). – Andi Giga Sep 10 '18 at 08:18