Developers Forum for XinFin XDC Network

Discussion on: [Closed]How to Deploy proxy contract ?

Collapse
menezesphill profile image
Phill Menezes • Edited on

Hi @deepan_chakkravarthy_bfbe I believe the best environment to deploy proxy contracts is Hardhat. You can start a new Hardhat project (See this tutorial for more details on how to do that). Then install the following dependencies:

npm install @nomicfoundation/hardhat-toolbox
npm install @openzeppelin/contracts-upgradeable
npm install @openzeppelin/hardhat-upgrades
Enter fullscreen mode Exit fullscreen mode

You can change your hardhat.config.js file to include the dependencies:

require("@nomicfoundation/hardhat-toolbox");
require("@nomiclabs/hardhat-ethers");
require("@openzeppelin/hardhat-upgrades");
require('dotenv').config();

module.exports = {
  solidity: "0.8.16",
  networks: {
    xinfin: {
      url: process.env.XINFIN_NETWORK_URL,
      accounts: [process.env.PRIVATE_KEY],
    },
     apothem: {
      url: process.env.APOTHEM_NETWORK_URL,
      accounts: [process.env.PRIVATE_KEY]
    }
  },
};
Enter fullscreen mode Exit fullscreen mode

And make your Smart Contract Initializable. The code below is based on the Pizza.sol contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

// Open Zeppelin libraries for controlling upgradability and access.
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

contract Pizza is Initializable, UUPSUpgradeable, OwnableUpgradeable {
   uint256 public slices;

      ///@dev no constructor in upgradable contracts. Instead we have initializers
    ///@param _sliceCount initial number of slices for the pizza
   function initialize(uint256 _sliceCount) public initializer {
       slices = _sliceCount;

      ///@dev as there is no constructor, we need to initialise the OwnableUpgradeable explicitly
       __Ownable_init();
   }

   ///@dev required by the OZ UUPS module
   function _authorizeUpgrade(address) internal override onlyOwner {}

   ///@dev decrements the slices when called
   function eatSlice() external {
       require(slices > 1, "no slices left");
       slices -= 1;
   }
}
Enter fullscreen mode Exit fullscreen mode

To deploy this contract along with its upgradable proxy, create a deployment script deploy.js:

const { ethers, upgrades } = require("hardhat");

const SLICES = 8;

async function main() {
 const Pizza = await ethers.getContractFactory("Pizza");

 console.log("Deploying Pizza...");

 const pizza = await upgrades.deployProxy(Pizza, [SLICES], {
   initializer: "initialize",
 });
 await pizza.deployed();

 console.log("Pizza deployed to:", pizza.address);
}

main();
Enter fullscreen mode Exit fullscreen mode

And proceed to deploy the contract:

npx hardhat run scripts/deploy.js --network xinfin
Enter fullscreen mode Exit fullscreen mode