One way to achieve this through the Truffle Migration files would be to modify contract Two
such that its constructor takes an address as input and stores it as oneAdd
, then deploy the contracts with the following migration file:
var One = artifacts.require("./One.sol");
var Two = artifacts.require("./Two.sol");
module.exports = (deployer, network) => {
deployer.deploy(One).then(function() {
return deployer.deploy(Two, One.address)
});
};
Alternatively, you could deploy One
and Two
first, then call a function in Two
to set the address of One
with an async migration as follows:
var One = artifacts.require('./One.sol');
var Two = artifacts.require('./Two.sol');
module.exports = async(deployer) => {
let deployOne = await deployer.deploy(One);
let deployTwo = await deployer.deploy(Two);
contractTwo = await Two.deployed()
let setAddress = await contractTwo.setAddress(
One.address,
{ gas: 200000 }
);
};
Note that if you choose this option you need to think carefully about who can call the setAddress
function to prevent malicious actors from breaking your contract; ideally only transactions from authorised addresses would be allowed to call this function using some sort of onlyOwner
or onlyAdmin
modifier