Truffle

Truffle Exec 卡住了

  • September 10, 2018

我想在松露控制台中執行一個腳本。

腳本

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");} )
}

手動

當我手動插入腳本(只是 module.exports 中的部分)時,它逐行工作,我得到了最後一個console.log.

與執行

當我執行 exec 時./run.js:腳本卡在第二個 Promise 上,沒有任何錯誤。

該解決方案有效:

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()} )
}

我認為這是缺少的:

Message.defaults({from: creator})

松露無效地址

引用自:https://ethereum.stackexchange.com/questions/58337