1

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.

3

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:

MyContract.new({from: 0x123...}).then(...);

Or by setting the defaults for MyContract, like so:

MyContract.defaults({from: 0x123...});
1

It seems like we both have same kind of use case where we deploy a new contract instance for every user and not just change the state of single copy of contract everytime.

According to my understanding, truffle saves the contracts abi along with contract address in contract.json file, so it is easy to call the functions of contract easily.

but if I want to have multiple copies of contract then I think the address will be overridden in contract.json file and the previous contracts will be lost. How are you addressing this problem?

  • @Ryan , I am also struggling with this contract.new. I have to make a new copy of my contract and in my case i have to pass some arguments also, but it seems like VM just ignores all my arguments. Any comments on this issue? – Manish Oct 20 '17 at 10:40
  • Sorry I didn't see the notifications for these questions. When you deploy a contract to the network with truffle migrate or something, you aren't deploying a contract instance but the data with which to create a new contract, as I understand it. What it deploys is analogous to a .class file in Java. What Contract.new does would be the same as in Java with new Contract, i.e. it creates a new instance of that contract. The issue you address above isn't really an issue, what it's saving then is the data with which new contract instances can be created, I believe. – Ryan Faulhaber Nov 12 '17 at 16:29
  • 1
    @RyanFaulhaber It is incorrect. In both cases, what you are doing is instantiating a contract. Only a contract instance can live on the chain and hence gets an address. In the case of truffle migrate, truffle creates an instance by deploying it and stores the deployed address in the .json file. With Dapps, any contracts that you want predeployed and available for your application will take this route. Contract.new does the same - just that the trigger is from code. With Dapps, this would be akin to runtime instantiation. – Sara Apr 26 '18 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.