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.

Andi Giga
  • 439
  • 4
  • 15
  • Does it work fine if you set web3.eth.defaultAccount = web3.eth.accounts[0] in truffle develop beforehand? – Paul Razvan Berg Sep 08 '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 08 '18 at 19:33
  • 1
    @ Ismael, module.exportsis fine: https://truffleframework.com/docs/truffle/getting-started/writing-external-scripts – Andi Giga Sep 10 '18 at 08: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 08:18
  • I figured it out @PaulRBerg thx for giving me the right direction. – Andi Giga Sep 10 '18 at 08:50

1 Answers1

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

Andi Giga
  • 439
  • 4
  • 15
  • For others: the fix isn't the missing "from", its the addition of a callback parameter, and actually calling it.

    Use this as your function template:

        module.exports = async function(callback) {
    
          return callback()
        };```
    
    – AndrewStone Nov 04 '21 at 13:11
  • If you don't call back, the execution never continues, so you get a hang! – AndrewStone Nov 04 '21 at 13:27