Deploy Contract

Deploying and compiling the contract is different for the Polygon CDK, here we discuss the steps to deploy your first contract in the Polygon CDK network.

Step 1: Get the RPC URL - follow the instructions on the RPC Access page to get the RPC endpoints.

Step 2: Get ABI and bytecode - Deploying a contract in polygon CDK is the same as deploying a contract in the Ethereum network. You can compile the contract code in bytecode and abi normally as you would do for Ethereum and then can use it in the next step.

Step 3: Replace Values - Replace RPC_URL, contractAbi, contractBytecode and privateKey. once you replace those values with valid values, you can run the file with ts-node filename.ts

import { ethers } from "ethers";

async function deployContract() {
    // Replace with your Ethereum node's RPC endpoint
    const RPC_URL = "https://demouser:demopass@rpcnode.demo-vali-02aa6b.zeeve.net/rpc";
    // Connect to your Ethereum node
    const provider = new ethers.JsonRpcProvider(RPC_URL);

    // Replace these values with your contract's ABI and bytecode
    const contractAbi = []; // Your contract's ABI as an array
    const contractBytecode = ""; // Your contract's bytecode as a hex string

    // Set the account that will deploy the contract
    const privateKey = ""; // Replace with the private key of the account deploying the contract
    const wallet = new ethers.Wallet(privateKey, provider);
    console.log(`wallet address: ${wallet.address} bal: ${await provider.getBalance(wallet.address)}`);

    // Create a contract factory
    const MyContractFactory = new ethers.ContractFactory(contractAbi, contractBytecode, wallet);
    console.log("contract ready to deploy");

    // Deploy the contract
    const MyContract = await MyContractFactory.deploy(10);

    // Wait for the contract to be mined
    await MyContract.waitForDeployment();
    console.log("contract deployed");

    console.log(`Contract deployed successfully at address: ${await MyContract.getAddress()}`);
}

deployContract();

Step 4: Contract Address - Once it runs successfully you will get a contract address.

Last updated