Tuesday, March 27, 2018

Truffle execute external script call contract function

errors:

 * Error: VM Exception while processing transaction: out of gas
 * Error: Cannot create instance of YourContract; no code at address
 * Error: sender account not recognized
 * Error: invalid address

If you get some of these errors while trying to execute contract function from truffle script, here is proper way to do it:

//First get all accounts on testrpc
web3.eth.getAccounts((err, acc) => {
let accounts = acc;
contract.deployed().then(function(instance){
//you need to specify from and gas parameters
instance.yourFunction(someParam, {from: accounts[0], gas: 500000});
}).then(function(result) {
console.log(result);
});
});

No comments:

Post a Comment