Tutorial·10 मिनट का पठन·Solingo द्वारा

Ethereum पर Smart Contract कैसे Deploy करें — 2026 Guide

Smart contract को Ethereum mainnet और testnets पर deploy करने के लिए complete guide। Foundry, Hardhat, verification और best practices।

# Ethereum पर Smart Contract कैसे Deploy करें — 2026 Guide

Smart contract deploy करना development process का critical step है। इस comprehensive guide में, हम testnet और mainnet deployment को cover करेंगे Foundry और Hardhat दोनों के साथ।

Prerequisites

Deployment से पहले, ensure करें:

  • Contract thoroughly tested है
  • - Unit tests (95%+ coverage)

    - Fuzz tests

    - Integration tests

    - Audit (mainnet के लिए)

  • Deployment tools installed हैं
  • # Foundry

    curl -L https://foundry.paradigm.xyz | bash

    foundryup

    # या Hardhat

    npm install --save-dev hardhat

  • Wallet funded है
  • - Testnet: Faucet से free ETH

    - Mainnet: Real ETH required

  • RPC endpoint access
  • - Alchemy, Infura, या QuickNode account

    Step 1: Testnet Deployment

    Testnet पर deploy करना always first step है।

    Popular Testnets (2026):

    | Network | Chain ID | Currency | Faucet | Explorer |

    |---------|----------|----------|--------|----------|

    | Sepolia | 11155111 | SepoliaETH | faucet | etherscan |

    | Holesky | 17000 | HolETH | faucet | explorer |

    Setup Environment Variables:

    # .env file
    

    PRIVATE_KEY=your_private_key_here

    SEPOLIA_RPC_URL=https://eth-sepolia.g.alchemy.com/v2/YOUR_KEY

    ETHERSCAN_API_KEY=your_etherscan_api_key

    ⚠️ Security: Never commit .env to git! Add to .gitignore.

    Foundry Deployment:

    // script/Deploy.s.sol
    

    // SPDX-License-Identifier: MIT

    pragma solidity ^0.8.20;

    import "forge-std/Script.sol";

    import "../src/MyToken.sol";

    contract DeployScript is Script {

    function run() external {

    uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");

    vm.startBroadcast(deployerPrivateKey);

    MyToken token = new MyToken("MyToken", "MTK", 1000000);

    console.log("Token deployed to:", address(token));

    vm.stopBroadcast();

    }

    }

    Deploy करें:

    forge script script/Deploy.s.sol:DeployScript \
    

    --rpc-url $SEPOLIA_RPC_URL \

    --broadcast \

    --verify \

    --etherscan-api-key $ETHERSCAN_API_KEY

    Hardhat Deployment:

    // scripts/deploy.js
    

    const hre = require("hardhat");

    async function main() {

    const MyToken = await hre.ethers.getContractFactory("MyToken");

    const token = await MyToken.deploy("MyToken", "MTK", 1000000);

    await token.waitForDeployment();

    console.log("Token deployed to:", await token.getAddress());

    }

    main().catch((error) => {

    console.error(error);

    process.exitCode = 1;

    });

    npx hardhat run scripts/deploy.js --network sepolia

    Step 2: Verify Contract Source Code

    Verification users को source code read करने देता है Etherscan पर।

    Foundry Verification:

    forge verify-contract \
    

    --chain-id 11155111 \

    --compiler-version v0.8.20 \

    --constructor-args $(cast abi-encode "constructor(string,string,uint256)" "MyToken" "MTK" 1000000) \

    0xYourContractAddress \

    src/MyToken.sol:MyToken \

    --etherscan-api-key $ETHERSCAN_API_KEY

    Hardhat Verification:

    npx hardhat verify --network sepolia \
    

    0xYourContractAddress \

    "MyToken" "MTK" 1000000

    Step 3: Mainnet Deployment

    Mainnet deployment irreversible है और real money involve करता है।

    Pre-Mainnet Checklist:

    • [ ] Contract audited by professionals
    • [ ] All tests passing
    • [ ] Gas optimization done
    • [ ] Testnet deployment successful
    • [ ] Source code verified on testnet
    • [ ] Deployment plan documented
    • [ ] Emergency pause mechanism (if applicable)
    • [ ] Multisig wallet setup for ownership
    • [ ] Sufficient ETH for deployment (+ 20% buffer)

    Gas Price Strategy:

    # Current gas price check करें
    

    cast gas-price --rpc-url $MAINNET_RPC_URL

    # Gas price manually set करें (gwei में)

    forge script script/Deploy.s.sol \

    --rpc-url $MAINNET_RPC_URL \

    --broadcast \

    --gas-price 20000000000 # 20 gwei

    Mainnet Deployment (Foundry):

    # Dry run first (simulate)
    

    forge script script/Deploy.s.sol \

    --rpc-url $MAINNET_RPC_URL

    # Actual deployment

    forge script script/Deploy.s.sol \

    --rpc-url $MAINNET_RPC_URL \

    --broadcast \

    --verify \

    --etherscan-api-key $ETHERSCAN_API_KEY

    Step 4: Post-Deployment

    1. Verify Deployment:

    # Check contract code
    

    cast code 0xYourContractAddress --rpc-url $MAINNET_RPC_URL

    # Call view function

    cast call 0xYourContractAddress "name()" --rpc-url $MAINNET_RPC_URL

    2. Transfer Ownership:

    # Multisig address को transfer करें
    

    cast send 0xYourContractAddress \

    "transferOwnership(address)" \

    0xMultisigAddress \

    --rpc-url $MAINNET_RPC_URL \

    --private-key $PRIVATE_KEY

    3. Initialize Contract:

    अगर initialization required है:

    function initialize(address admin, uint256 initialValue) external {
    

    require(!initialized, "Already initialized");

    owner = admin;

    value = initialValue;

    initialized = true;

    }

    Advanced: Upgradeable Deployment

    OpenZeppelin Upgrades plugin use करें:

    // Hardhat
    

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

    async function main() {

    const MyToken = await ethers.getContractFactory("MyToken");

    const proxy = await upgrades.deployProxy(MyToken, ["MyToken", "MTK"]);

    await proxy.waitForDeployment();

    console.log("Proxy deployed to:", await proxy.getAddress());

    }

    Cost Estimation

    Typical deployment costs (Mainnet, 20 gwei):

    | Contract Type | Gas Used | Cost (20 gwei) | Cost ($ETH @ $3000) |

    |---------------|----------|----------------|---------------------|

    | Simple Token | ~1,000,000 | 0.02 ETH | $60 |

    | NFT Collection | ~2,500,000 | 0.05 ETH | $150 |

    | DeFi Protocol | ~5,000,000 | 0.10 ETH | $300 |

    | Complex DAO | ~8,000,000 | 0.16 ETH | $480 |

    Common Issues और Solutions

    Issue: "Insufficient funds"

    Solution: Wallet में enough ETH ensure करें gas + deployment के लिए।

    Issue: "Nonce too high"

    Solution: Pending transactions clear करें या nonce reset करें।

    Issue: "Contract creation code storage out of gas"

    Solution: Contract size reduce करें या split करें multiple contracts में।

    Issue: "Contract size exceeds 24KB"

    Solution:

    • Libraries use करें
    • Functions को external contracts में move करें
    • Optimizer enable करें higher runs के साथ
    // foundry.toml
    

    [profile.default]

    optimizer = true

    optimizer_runs = 200 # Lower = smaller bytecode

    Security Best Practices

  • Never deploy without audit (mainnet)
  • Use multisig for ownership
  • Implement timelock for upgrades
  • Test on testnet first always
  • Verify source code immediately
  • Monitor contract post-deployment
  • Have emergency plan (pause, upgrade)
  • Deployment to L2s

    Same process, different RPC URLs:

    # Arbitrum
    

    ARBITRUM_RPC_URL=https://arb1.arbitrum.io/rpc

    # Optimism

    OPTIMISM_RPC_URL=https://mainnet.optimism.io

    # Polygon

    POLYGON_RPC_URL=https://polygon-rpc.com

    L2 benefits:

    • 10-100x cheaper gas
    • Faster confirmations
    • Same Solidity code

    निष्कर्ष

    Smart contract deployment एक critical process है जो careful planning require करता है। हमेशा testnet पर thoroughly test करें mainnet पर जाने से पहले।

    Deployment checklist:

  • ✅ Tests pass (95%+ coverage)
  • ✅ Audit complete (mainnet)
  • ✅ Testnet deployment successful
  • ✅ Source verified
  • ✅ Gas price optimal
  • ✅ Sufficient funds
  • ✅ Multisig ready
  • ✅ Emergency plan
  • ✅ Monitoring setup
  • ✅ Documentation complete
  • अगले कदम: Solingo पर deployment process practice करें simulated environments में!

    ---

    अतिरिक्त Resources:

    Practice में लगाने के लिए तैयार हैं?

    Solingo पर interactive exercises के साथ इन concepts को apply करें।

    मुफ्त में शुरू करें