2

i've been studying solidity/blockchain for a few months now and recently started development on a personal project. I have a few questions i kept running into, specifically when deploying contracts with truffle/ganache. After successfully compiling my contracts and updating the truffle-config.js file to deploy the contracts to my local blockchain, one of the common issues i ran into was that every time i ran "truffle migrate" on my terminal (after i initially ran 'truffle migrate --reset'), it would re-deploy my contracts despite it already being deployed earlier to the same local blockchain instance. My understanding from reading the truffle suite documentation is that, "truffle migrate --reset", will deploy contracts as if it were the first time, while "truffle migrate" will only deploy non-deployed contracts in my migrations folder however this is not the case. I updated my migrations code to handle this with try/catch statements, but instead the contracts would never deploy when restarting my local blockchain. I'm trying to understand why, despite not getting errors, the contract is not deployed successfully. Below I have a simple version of what my update migration js file looks like (constructor has one address parameter of a different contract). Apologies in advance, if some parts are not clear.

const AnotherContract= artifacts.require("./AnotherContract.sol");

module.exports = async function (deployer) {
    let anotherContractInstance= await AnotherContract.deployed();

    deployer.deploy(Contract, anotherContractInstance.address);
};

Q's: .Does truffle always re-deploy already deployed contracts when running "truffle migrate"? .If so is there a different command or changes that can be made so deployed contracts aren't re-deployed, for example, if we add a new and I need to run the migration file to deploy it? .Should I approach the migration file, as if im only running it once? If so, at least from a testing standpoint, would i need to restart my local blockchain anytime i make changes?

2 Answers 2

0
const AnotherContract = artifacts.require("./AnotherContract.sol");

module.exports = function (deployer) {
  deployer.deploy(AnotherContract).then(function () {
    return deployer.deploy(Contract, AnotherContract.address);
  });
};

Maybe you could try this change.

And you typically run migrations once unless you need to make changes to the deployment scripts or contracts. You don't need to restart your local blockchain for every change. You can continue running truffle migrate to deploy new contracts or update existing ones as needed during development.

1
  • Thanks for the response, i did see this formatting when working with multiple contract deployments on a single migration file in the truffle documentation, however, correct me if im wrong, doesn't deployer.deploy(AnotherContract).then(function () { return deployer.deploy(Contract, AnotherContract.address); deploy AnotherContract, then once successful deploy Contract, which is relying on the AnotherContract to already be deployed? If so, i have already deployed AnotherContract in a separate migration file, wouldn't this re-deploy AnotherContract?
    – pierre96
    Sep 7, 2023 at 4:30
0

truffle has these options, that lets you to run just new migrations (not all of them). Maybe it can help you

--f <number>     Run contracts from a specific migration. The number refers to the prefix of the migration file.
--to <number>    Run contracts to a specific migration. The number refers to the prefix of the migration file.

you can use them like these.

truffle migrate --f 2
truffle migrate --to 2-blah-blah-migration.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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