The Truffle documentation says that the new()
contract method deploys a new instance to the network.
I have the following code:
const Web3 = require('web3');
const provider = new Web3.providers.HttpProvider("http://localhost:8545");
const contract = require('truffle-contract');
const MyContractJSON = require('./contracts/MyContract.json');
const MyContract = contract(MyContractJSON);
MyContract.setProvider(provider);
MyContract.new().then(instance => {
console.log('it worked');
}).catch(err => {
console.log('error', err);
});
Why does the promise returned by new()
throw an error that says "Invalid address"? It does this regardless of whether or not I've deployed a contract to the network using truffle deploy
. Am I using new()
incorrectly?
EDIT: several days later I still haven't figured it out, but I've observed that any methods having to do with addresses throw an "Invalid address" error. I'm using truffle-contract@2.0.5 and web3@0.20.0.
If I have a contract that looks like this
import "./OtherContract.sol";
contract MyContract {
function createNewInstance() returns (OtherContract) {
return new OtherContract();
}
}
and in the JS I do something like the JS above except with instance
I call
instance.createNewInstance()
I get an "invalid address" error.
Best Answer
Well, after a lot of digging and hacking, I found my answer.
It turns out that with truffle-contract you must provide addresses with each transaction call. So the above fails because there's no
from
address specified.An example solution would be something like:
Or by setting the defaults for
MyContract
, like so: