2

I have 2 contracts that deployed with truffle. Now I extended it and my contracts require some bootstrap actions like "set some address" or "set in contract1 address of contract2".

I will not change contracts to make it dependable or mention one contract inside another. I want just to call function contract1.setX(address_of_contract2) and contract2.setY(address_of_contract1).

How it could be done using truffle migration engine (not manually)?

8

With help of Karthikeyan Thangavel and Truffle docs I finally made it works:

module.exports = async function(deployer) {
  let aInst, bInst;

  await Promise.all([
    deployer.deploy(A),
    deployer.deploy(B)
  ]);

  instances = await Promise.all([
    A.deployed(),
    B.deployed()
  ])

  aInst = instances[0];
  bInst = instances[1];

  results = await Promise.all([
    aInst.setX(bInst.address),
    bInst.setY(aInst.address)
  ]);

  const xCheck = await aInst.x.call();
  const yCheck = await bInst.y.call();

  console.log('X: ', xCheck, bInst.address);
  console.log('Y: ', yCheck, aInst.address);
};
  • Not sure if web3 syntax has changed, but if you encounter the error value.forEach is not a function you need to put the parameters in the function call into an array, such as aInst.setX([bInst.address]) – James Cameron Oct 8 '18 at 20:01
2

I have a similar requirement (my contracts are named NppToken and CrowdSale), and here's how I'm handling it (note that CrowdSale uses the NppToken's address as a constructor argument):

module.exports = async function(deployer) {
  deployer.deploy(NppToken).then(() => {
    return deployer.deploy(CrowdSale, NppToken.address);
  }).then(async () => {
    var token = await NppToken.deployed();
    await token.transferOwnership(CrowdSale.address);       
  });      
};
  • Did this work for you? I get a syntax error: Unexpected token function – K Kolluru Apr 19 '18 at 12:42
  • Yes, this is my actual deployment script, but yours might be different depending on your design (e.g. if you don't have transferOwnership in your token). – ulu Apr 19 '18 at 15:13
  • ok. I have problem with async function() step. it says unexpected token 'function' – K Kolluru Apr 19 '18 at 19:27
  • That's because it doesn't support the "async" keyword. Which Node version do you use? Anyway, you should be able to rewrite this as a chain of then-s, without async. The first async is not needed, you may start with removing it and see how the rest works. – ulu Apr 23 '18 at 15:20
1

You can achieve that using the promise function.

deployer.deploy(A).then(function() {
   deployer.deploy(B).then(funtion() {
       return A.setX(B.address);
     });
     return B.setX(A.address);
   });
}

Using the promise function you can do functionalities after deploying the contract. For more details refer truffle docs.

  • 1
    Thank you for help, I already reviewed documentatop you've pointed. Unfortunately it simply does not works. In your example I got error TypeError: A.setX is not a function. – Alex G.P. Nov 13 '17 at 8:10

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.