프로젝트 구성에 대해서는 아래 글을 참고.
[이더리움] Hardhat 설치 후 typescript 프로젝트 생성하기
Hardhat 기존 프로젝트에 Hardhat 설치 npm install --save-dev hardhat # npm install --save-dev hardhat npm WARN deprecated request-promise-native@1.0.9: request-promise-native has been deprecated because it extends the now deprecated request package
heun.tistory.com
오픈제플린(OpenZeppelin) 사용하여 솔리디티 작성
오픈제플린 라이브러리를 이용하여 솔리디티를 작성할 수 있는데 기능도 추가할 수 있어 매우 유용한 툴이다. 우측 상단에 Download 버튼을 누르면 Development Pacakge(Hardhat)을 통하여 샘플 프로젝트를 만들 수 있다.
OpenZeppelin | Contracts
OpenZeppelin Contracts helps you minimize risk by using battle-tested libraries of smart contracts for Ethereum and other blockchains. It includes the most used implementations of ERC standards.
www.openzeppelin.com
MyToken.sol 작성
오픈제플린 툴을 이용하여 기본적으로 작성된 소스에 몇 개의 토큰을 발급받을 지에 대해 추가해서 사용해도 된다.
토큰 이름 | 심볼 | 발행량 |
MyToken | MTK | 5000 |
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor() ERC20("MyToken", "MTK") {
_mint(msg.sender, 5000 * 10 ** decimals());
}
}
deploy.ts 작성하기
import { ethers } from "hardhat";
async function main() {
console.log('deploying...');
const ContractFactory = await ethers.getContractFactory("MyToken");
const myToken = await ContractFactory.deploy();
await myToken.deployed();
console.log(`Contract deployed to ${myToken.address}`);
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
async finction main() 함수는 async/await 구문을 사용하여 비동기 함수를 정의한다.
1. ContractFactory 변수를 생성하고 ethers.getContractFactory 함수를 이용하여 MyToken.sol을 가져온다.
2. myToken 변수를 생성하고 ContractFactory.deploy 함수를 사용해서 MyToken.sol을 배포한다.
3. myToken.deployed() 함수를 사용하여 배포가 완료될 때까지 대기한다.
4. myToken.address 변수를 출력하여 컨트랙트 주소 확인
hardhat.config.ts 수정
goerli network를 예시로 추가하였다. ganache나 local node로 테스트를 원할 경우 알맞게 변경해서 사용하면 된다.
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
const config: HardhatUserConfig = {
networks: {
goerli: {
// 원하는 end point 설정
// 개인 mnemonic 키 설정
url: "https://goerli.infura.io/v3/{PROJECT_ID}",
accounts: {
mnemonic: "your testnet wallet mnemonic here",
},
gas: "auto",
gasPrice: "auto",
},
hardhat: {},
local: {
url:'http://127.0.0.1:8545/'
},
},
solidity: {
version: "0.8.9",
settings: {
optimizer: {
enabled: true,
},
},
},
};
export default config;
Terminal에 배포 명령어 실행
배포 성공하게 되면 이더스캔에서 확인할 수 있다.
npx hardhat run --network goerli scripts/deploy.ts
// 배포 정상 처리
deploying...
Contract deployed to 0x0cDC5c8B33367d9b13f2318B276BCfDe1DD1aDdf
Contract Address 0x0cdc5c8b33367d9b13f2318b276bcfde1dd1addf | Etherscan
The Contract Address 0x0cdc5c8b33367d9b13f2318b276bcfde1dd1addf page allows users to view the source code, transactions, balances, and analytics for the contract address. Users can also interact and make transactions to the contract directly on Etherscan.
goerli.etherscan.io
'Blockchain > Ethereum' 카테고리의 다른 글
[이더리움] ZK-Rollups(ZK롤업)이란? (0) | 2023.02.26 |
---|---|
[이더리움] 이더리움 클래식의 기술적인 장점은 뭐가 있을까? (0) | 2023.02.22 |
[이더리움] 오픈제플린 라이브러리 ERC20 토큰 기능 정리 (0) | 2023.02.20 |
[이더리움] The DAO 해킹 사건으로 보는 이더리움과 이더리움 클래식의 차이 (0) | 2023.02.20 |
[이더리움] 이더리움 레이어 2 솔루션 L2 정리 (0) | 2023.02.14 |