wighawag / hardhat-deploy

hardhat deployment plugin
MIT License
1.2k stars 296 forks source link

Is there a best practice/convention for deploy scripts variables per network? #174

Closed null-prophet closed 3 years ago

null-prophet commented 3 years ago

Hi, this is more of a general convention question that I couldn't seem to see any general answer for around contract deployment configurations.

Say I have a token contract that depends on specific contract addresses (uniswap for example) in testnet or mainnet.

How best to possibly externalise those addresses/variables so deploy scripts could pick up the testnet variants or mainnet variants when deploying to specific networks?

let configMap = {
  [ChainId.mainnet]: { uniswap: "0x0.......1", weth: "0x0.......11" ... },
  [ChainId.kovan]: { uniswap: "0x0.......2", weth: "0x0.......22" ... }
}

//pseudo code
const chainId = await hre.getChainId();
// at this point I have the specific chains config
const cfg = getConfigMap(chainId); // some kind of finder with a default find or throw or an ES6 Map.

// deploy - specific networks contract variants.
await deploy('xxxx', {
    from: deployer,
    args: [cfg.uniswap, cfg.weth],
    log: true,
  });

Sushiswap has an excellent paradigm of AddressMaps which are maps of addresses keyed on chainIds as a good general solution to things like this. Is there something in hardhat that would achieve this or would I have to roll my own config/chainId config generator?

I can probably put something together quite easily using json config files or similar but hoped someone might have already solved this problem.

Thanks

wighawag commented 3 years ago

What I do is simply add a file for each contract on the respective network's deployment folder

Also if the project expose the deployment file on its npm package you can set it up in the external config : https://github.com/wighawag/hardhat-deploy#importing-deployment-from-other-projects-with-truffle-support

null-prophet commented 3 years ago

ok I kind of get that, this is more for constants.

For instance I might want to deploy to a testnet and talk to a uniswap router or something else like an oracle and the addresses are specific per chainId. The deployments kind of makes sense, but your not deploying anything per se, you just want some specific values per chain.

https://github.com/wighawag/hardhat-deploy#importing-deployment-from-other-projects-with-truffle-support

so this block:

{
    external: {
        contracts: [
          {
            artifacts: "node_modules/@cartesi/arbitration/export/artifacts",
            deploy: "node_modules/@cartesi/arbitration/export/deploy"
          },
          {
            artifacts: "node_modules/someotherpackage/artifacts",
          }
        ],
        deployments: {
          rinkeby: ["node_modules/@cartesi/arbitration/build/contracts"],
        },
    }
}

How could you use the deployments.rinkeby in this case inside a deploy script? Would you get back some kind of files or does it just deploy these for you?

I was hoping more to get back a map or some kind of typed object (I'm using this with TS) to use.

wighawag commented 3 years ago
 deployments: {
          rinkeby: ["node_modules/@cartesi/arbitration/build/contracts"],
        },
    }

this would add the contract contained in node_modules/@cartesi/arbitration/build/contracts to the list of contract tracked by hardhat-deploy on rinkeby

so if you are on rinkeby, you can get them via deployments.get(<name>) or deployments.all() will give you access to all of them

null-prophet commented 3 years ago

Ah ok, I get it now, that makes sense, your relying on that deployed contract info to be available to get your info on addressess etc on rather than hardcoding it. That makes sense.

I just need to confirm I can grab the contract json files for the SC's in question. 😄