Foundry 또는 Hardhat으로 컨트랙트 배포하기

Foundry 또는 Hardhat 2를 사용하여 EVM 네트워크에 Hello.sol을 배포하고, Chain ID를 확인하며, 트랜잭션 영수증과 컨트랙트 응답을 확인하세요.

네트워크 선택

대상 체인의 연동 가이드(지원 체인을 통해 접근)에 따라 network.mjs를 생성하고 RPC_URL과 CHAIN_ID를 내보냅니다. 체인 가이드는 GET /v1/chains에서 Chain ID와 메서드 정책을 읽어오며, 가스 토큰, 별도의 브로드캐스트 RPC가 필요한지 여부, 체인별 배포 설정을 설명합니다.

아래 도구 중 하나를 선택하세요. 둘 다 생성자 인수가 없는 동일한 소형 컨트랙트를 배포합니다.

선택한 네트워크의 네이티브 가스 토큰으로 배포자 계정에 자금을 조달하세요. 로컬 환경에서 DEPLOYER_PRIVATE_KEY를 안전하게 설정하고 절대 커밋하지 마세요. 다음 명령은 트랜잭션을 브로드캐스트하고 가스를 소비합니다.

생성자 인수가 없는 Hello.sol을 생성합니다:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Hello {
    event Ping(address indexed sender);
    function ping() external { emit Ping(msg.sender); }
    function hello() external pure returns (string memory) {
        return "hello";
    }
}

Foundry

초기화된 Foundry 프로젝트에서 컨트랙트를 src/Hello.sol에 배치하고 다음 foundry.toml을 사용하세요:

[profile.default]
src = "src"
out = "out"
libs = ["lib"]
solc_version = "0.8.24"
evm_version = "paris"

[rpc_endpoints]
target = "${RPC_URL}"
: "${RPC_URL:?Set RPC_URL}" "${CHAIN_ID:?Set CHAIN_ID}" "${DEPLOYER_PRIVATE_KEY:?Set deployer key}"
test "$(cast chain-id --rpc-url "$RPC_URL")" = "$CHAIN_ID" || exit 1
forge build
forge create src/Hello.sol:Hello --rpc-url target \
  --chain "$CHAIN_ID" --private-key "$DEPLOYER_PRIVATE_KEY" --broadcast

트랜잭션 해시와 배포된 주소를 보관하세요. cast receipt "$TX_HASH" --rpc-url "$RPC_URL"을 사용하여 블록 포함 여부와 상태를 확인한 다음, cast call "$CONTRACT_ADDRESS" "hello()(string)" --rpc-url "$RPC_URL"을 사용하여 컨트랙트를 읽습니다. 배포 출력에서 이 두 변수를 설정하세요.

Hardhat 2 및 ethers 6

별도의 CommonJS 프로젝트(npm init -y)에서 npm install --save-dev hardhat@2 @nomicfoundation/hardhat-ethers@3 ethers@6을 설치합니다. 동일한 컨트랙트를 contracts/Hello.sol에 배치합니다. 이를 hardhat.config.cjs로 저장하세요:

require('@nomicfoundation/hardhat-ethers');
const { RPC_URL, CHAIN_ID, DEPLOYER_PRIVATE_KEY } = process.env;
if (!RPC_URL || !/^[1-9][0-9]*$/.test(CHAIN_ID ?? '') || !DEPLOYER_PRIVATE_KEY) {
  throw new Error('Set RPC_URL, CHAIN_ID and DEPLOYER_PRIVATE_KEY');
}
module.exports = {
  solidity: { version: '0.8.24', settings: { evmVersion: 'paris' } },
  networks: {
    target: { url: RPC_URL, chainId: Number(CHAIN_ID), accounts: [DEPLOYER_PRIVATE_KEY] },
  },
};

scripts 디렉터리를 생성하고 scripts/deploy.cjs로 저장합니다:

const { ethers } = require('hardhat');
async function main() {
  const contract = await ethers.deployContract('Hello');
  console.log(contract.deploymentTransaction().hash);
  await contract.waitForDeployment();
  console.log(await contract.getAddress());
}
main().catch(error => { console.error(error); process.exitCode = 1; });
npx hardhat compile
npx hardhat run scripts/deploy.cjs --network target

네트워크의 chainId는 RPC에 대해 검증됩니다. Hardhat 2 네트워크 설정 및 Foundry 설정(접근 일자: 2026-10-07)을 참조하세요.

이벤트 읽기

LOG_ADDRESS를 배포된 주소로 설정하세요. ping()을 호출한 후 대상 체인 가이드의 이벤트 읽기 방법(/v1/chains의 체인 ws 필드에 따라 WebSocket 구독 또는 HTTP 청크 폴링)에 따라 Ping 이벤트를 읽습니다.

최종 수정일:

이 페이지의 내용