mint() 예시
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
* the total supply.
*
* Emits a {Transfer} event with `from` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
*/
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply += amount;
unchecked {
// Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above.
_balances[account] += amount;
}
emit Transfer(address(0), account, amount);
_afterTokenTransfer(address(0), account, amount);
}
_mint 함수는 ERC20 토큰 스마트 컨트랙트에서 토큰을 생성하고 지정된 계정에 토큰을 할당하는 역할을 한다. 이 함수는 내부(internal) 함수로 해당 컨트랙트 내에서만 호출될 수 있다. 이를 통해 토큰 생성 권한을 특정 로직에 제한할 수 있다.
_mint 함수의 인자와 동작은 다음과 같다.
address account: 토큰을 받을 계정의 주소
uint256 amount: 생성할 토큰의 수량
mint() 주요 로직
_mint 함수의 주요 로직은 다음과 같다.
1. 요구사항 확인
account가 영 주소(0x0)가 아닌지 확인한다. 영 주소에 토큰을 생성하는 것은 금지되어 있다.
2. _beforeTokenTransfer
토큰 전송 전에 호출되는 함수로 필요한 경우 상속을 통해 구현할 수 있다.
3. _totalSupply 증가
총 공급량을 증가시킨다. (총 공급량 = 총 공급량 + 생성할 토큰 수량)
4. unchecked 블록
컴파일러로부터 오버플로(overflow) 검사를 제거한다. 이 경우에 오버플로가 발생하지 않음이 보장되어 있으므로 컴파일러에서 검사를 생략할 수 있다.
5. _balances[account] 증가
지정된 계정의 토큰 잔액을 증가시킵니다. (잔액 = 잔액 + 생성할 토큰 수량)
6. Transfer 이벤트 발생
영 주소에서 account로 토큰이 전송되었음을 알리는 이벤트를 발생시킨다.
7. _afterTokenTransfer
토큰 전송 후에 호출되는 함수로 필요한 경우 상속을 통해 구현할 수 있습니다.
'Develop > Solidity' 카테고리의 다른 글
| [Hardhat] Goerli 테스트넷과 연동하여 내 계정 연결 상태 확인하기 (0) | 2023.07.31 |
|---|---|
| [Solidity] unchecked 문법 이해하기 (0) | 2023.05.08 |
| [Solidity] OpenZeppelin Ownable.sol로 스마트 컨트랙트의 소유권 관리하기 (0) | 2023.05.04 |
| [Solidity] OpenZeppelin ERC20.sol의 approve와 allowance 함수 이해하기 (0) | 2023.05.03 |
| [Solidity] OpenZeppelin의 Context.sol 이해하기 (0) | 2023.04.10 |