사랑하애오
article thumbnail

이번 포스팅에서는  remix에서 진행하겠습니다.

다음 포스팅에서는 VSCODE와 truffle, openzeppelin을 활용해 진행하겠습니다.

코드만 보시겠다는 분들은 5. 나만의 토큰 만들기 로 넘어가주세요.

간단하게 작성한 컨트랙트입니다. 참고바랍니다.

 

1.  리믹스에 접속

주소: https://remix.ethereum.org/

 

Remix - Ethereum IDE

 

remix.ethereum.org

 

2. 컨트랙트 파일 생성

'나만의_토큰_이름.sol'

 

3. 라이센스 설정 및 컴파일러 버전 설정

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

컨트랙트 작성에 앞서 맨 위에 2줄을 선언합니다.

모든 블록체인 소스는 오픈소스임을 명시해야합니다.

https://docs.soliditylang.org/en/v0.6.8/layout-of-source-files.html

 

Layout of a Solidity Source File — Solidity 0.6.8 documentation

Paths In the above, filename is always treated as a path with / as directory separator, and . as the current and .. as the parent directory. When . or .. is followed by a character except /, it is not considered as the current or the parent directory. All

docs.soliditylang.org

pragma solidity는 컴파일러 버전을 말하는데 컨트랙트의 버전에 따라
보안 이슈등에 대해 문법이나 가스비 등 릴리즈 되는게 많으니 꼭 최신버전을 사용해줍시다.

 

4. 라이브러리 및 인터페이스, 추상클래스 모듈 가져오기

import "@openzeppelin/contracts/utils/Context.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import "@openzeppelin/contracts/access/Ownable.sol";

토큰 컨트랙트 작성에 앞서 필요한 모듈들을 가져옵니다.

이렇게 선언을 해주시면

이렇게 똑똑한 사람들이 만든 똑똑한 remix가 필요한 dependency를 알아서 가져옵니다.

하나하나 짧게 어떤 모듈들을 가져왔는지 살펴보면

4-1. Context.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

추상 클래스로 친절하게 주석으로 설명까지 나와있습니다. 

_msgSender() 메서드는 msg.sender를 반환하고
_msgData() 메서드는 msg.data를 반환합니다.

Context에 선언은 안되있지만 따로 구현을 해본다면

function _msgValue() internal view virtual returns (bytes calldata) {
        return msg.value;
}

이런 메서드를 하나 만들 수 있는데 다 msg.xxx를 반환하는 메서드인데,

이거에 관해서는 따로 포스팅을 하겠지만 급하신 분들은 따로 구글 선생님께 물어보셔서 진행하셔도 됩니다.

 

4-2. SafeMath.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (utils/math/SafeMath.sol)

pragma solidity ^0.8.0;

// CAUTION
// This version of SafeMath should only be used with Solidity 0.8 or later,
// because it relies on the compiler's built in overflow checks.

/**
 * @dev Wrappers over Solidity's arithmetic operations.
 *
 * NOTE: `SafeMath` is generally not needed starting with Solidity 0.8, since the compiler
 * now has built in overflow checking.
 */
library SafeMath {
    /**
     * @dev Returns the addition of two unsigned integers, with an overflow flag.
     *
     * _Available since v3.4._
     */
    function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            uint256 c = a + b;
            if (c < a) return (false, 0);
            return (true, c);
        }
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, with an overflow flag.
     *
     * _Available since v3.4._
     */
    function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b > a) return (false, 0);
            return (true, a - b);
        }
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
     *
     * _Available since v3.4._
     */
    function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
            // benefit is lost if 'b' is also tested.
            // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
            if (a == 0) return (true, 0);
            uint256 c = a * b;
            if (c / a != b) return (false, 0);
            return (true, c);
        }
    }

    /**
     * @dev Returns the division of two unsigned integers, with a division by zero flag.
     *
     * _Available since v3.4._
     */
    function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b == 0) return (false, 0);
            return (true, a / b);
        }
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
     *
     * _Available since v3.4._
     */
    function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b == 0) return (false, 0);
            return (true, a % b);
        }
    }

    /**
     * @dev Returns the addition of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `+` operator.
     *
     * Requirements:
     *
     * - Addition cannot overflow.
     */
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        return a + b;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting on
     * overflow (when the result is negative).
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        return a - b;
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `*` operator.
     *
     * Requirements:
     *
     * - Multiplication cannot overflow.
     */
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        return a * b;
    }

    /**
     * @dev Returns the integer division of two unsigned integers, reverting on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator.
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        return a / b;
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * reverting when dividing by zero.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        return a % b;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
     * overflow (when the result is negative).
     *
     * CAUTION: This function is deprecated because it requires allocating memory for the error
     * message unnecessarily. For custom revert reasons use {trySub}.
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        unchecked {
            require(b <= a, errorMessage);
            return a - b;
        }
    }

    /**
     * @dev Returns the integer division of two unsigned integers, reverting with custom message on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        unchecked {
            require(b > 0, errorMessage);
            return a / b;
        }
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * reverting with custom message when dividing by zero.
     *
     * CAUTION: This function is deprecated because it requires allocating memory for the error
     * message unnecessarily. For custom revert reasons use {tryMod}.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        unchecked {
            require(b > 0, errorMessage);
            return a % b;
        }
    }
}

이것도 친절하게 주석에 설명이 있지만 간단하게 요약하자면
파라미터(매개변수)로 uint256의 타입만을 받는 것을 알 수 있는데
덧셈 뺄셈 곱셋 나눗셈을 메서드로 선언 해놓은 라이브러리입니다.

4-3. IERC20.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) external returns (bool);
}

EIP20 표준 인터페이스로 이더리움계열에서 상용되는 모든 FT들은 다 따릅니다.

인터페이스이므로 우리가 나중에 만들 토큰에 이 인터페이스를 상속받아 사용할 것입니다.

주석에 설명을 잘 읽어보시고 이해 안되시는건 구글선생님께 물어봅시다.

 

4-4. Ownable.sol

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

마지막으로 추상 클래스인 Ownable인데 

컨트랙트를 블록체인에 배포할 때 컨트랙트 오너(주인)가 취할 수 있는 메서드들입니다.

자기는 굳이 탈중앙화인데 이런게 필요하냐 난 안쓰겠다 하시는 분들은 안쓰셔도 됩니다만,
저는 완전한 탈중앙화는 누군가의 관리 및 보수덕에 유지될 수 있다 생각하기에 완전한 탈중앙화는 없다고 생각합니다.

 

5. 나만의 토큰 만들기

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

import "@openzeppelin/contracts/utils/Context.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import "@openzeppelin/contracts/access/Ownable.sol";

contract CoolmarvelToken is IERC20, Context, Ownable {
    using SafeMath for uint256;

    mapping(address => uint256) private _balances;
    mapping(address => mapping(address => uint256)) private _allowances;

    string public _name;
    string public _symbol;
    uint256 public _decimals;
    uint256 private _totalSupply;

    constructor() {
        _name = "CoolmarvelToken";
        _symbol = "CMT";
        _decimals = 18;
        _totalSupply = 100000000 * 10**_decimals;
        _balances[msg.sender] = _totalSupply;
        emit Transfer(address(0), msg.sender, _totalSupply);
    }

    function totalSupply() public view override returns (uint256) {
        return _totalSupply;
    }

    function balanceOf(address _addr) public view override returns (uint256) {
        return _balances[_addr];
    }

    function transfer(address _to, uint256 _amount)
        public
        override
        returns (bool)
    {
        _transfer(_msgSender(), _to, _amount);
        return true;
    }

    function allowance(address _owner, address _to)
        public
        view
        override
        returns (uint256)
    {
        return _allowances[_owner][_to];
    }

    function approve(address _to, uint256 _amount)
        public
        override
        returns (bool)
    {
        _approve(_msgSender(), _to, _amount);
        return true;
    }

    function transferFrom(
        address _from,
        address _to,
        uint256 _amount
    ) public override returns (bool) {
        _transfer(_from, _to, _amount);
        _approve(
            _from,
            _msgSender(),
            _allowances[_from][_msgSender()].sub(
                _amount,
                "ERC20: transfer amount exceeds allowance"
            )
        );
        return true;
    }

    function increaseAllowance(address _to, uint256 _amount)
        public
        returns (bool)
    {
        _approve(
            _msgSender(),
            _to,
            _allowances[_msgSender()][_to].add(_amount)
        );
        return true;
    }

    function decreaseAllowance(address _to, uint256 _amount)
        public
        returns (bool)
    {
        _approve(
            _msgSender(),
            _to,
            _allowances[_msgSender()][_to].sub(
                _amount,
                "ERC20: decreased allowance below zero"
            )
        );
        return true;
    }

    function _transfer(
        address _from,
        address _to,
        uint256 _amount
    ) internal {
        require(_from != address(0), "ERC20: transfer from the zero address");
        require(_to != address(0), "ERC20: transfer to the zero address");

        _balances[_from] = _balances[_from].sub(
            _amount,
            "ERC20: transfer amount exceeds balance"
        );
        _balances[_to] = _balances[_to].add(_amount);
        emit Transfer(_from, _to, _amount);
    }

    function _mint(address _address, uint256 _amount) internal {
        require(_address != address(0), "ERC20: mint to the zero address");

        _totalSupply = _totalSupply.add(_amount);
        _balances[_address] = _balances[_address].add(_amount);
        emit Transfer(address(0), _address, _amount);
    }

    function _burn(address _address, uint256 _amount) internal {
        require(_address != address(0), "ERC20: burn from the zero address");

        _balances[_address] = _balances[_address].sub(
            _amount,
            "ERC20: burn amount exceeds balance"
        );
        _totalSupply = _totalSupply.sub(_amount);
        emit Transfer(_address, address(0), _amount);
    }

    function _approve(
        address _owner,
        address _to,
        uint256 _amount
    ) internal {
        require(_owner != address(0), "ERC20: approve from the zero address");
        require(_to != address(0), "ERC20: approve to the zero address");

        _allowances[_owner][_to] = _amount;
        emit Approval(_owner, _to, _amount);
    }

    function _burnFrom(address _address, uint256 _amount) internal {
        _burn(_address, _amount);
        _approve(
            _address,
            _msgSender(),
            _allowances[_address][_msgSender()].sub(
                _amount,
                "ERC20: burn amount exceeds allowance"
            )
        );
    }
}

이렇게 위에서 가져온 모듈들을 활용해 토큰 컨트랙트를 간단하게 작성해봤습니다.

하나씩 살펴보자면 

using SafeMath for uint256;

이건 SafeMath 라이브러리를 uint256 타입에 사용하겠다는 뜻입니다.

 

mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;

맵핑으로 c언어 계열을 하셨던 분이라면 금방 이해하시겠지만 모르시는 분들을 위해 쉽게 설명하자면,

function balanceOf(address _addr) public view override returns (uint256) {
        return _balances[_addr];
}

balanceOf라는 메서드를 호출을 하게 되면 _balances 맵핑을 return을 하는데
저 _balances라는 튜플에 address 타입을 넣으면 uint256 타입을 반환한다는 뜻입니다.
(address (keyType) => uint256 (valueType)

그렇다면 balanceOf라는 메서드는 어떠한 지갑 주소를 넣으면
그 지갑주소가 얼만큼의 토큰을 가지고 있는지 반환하는 메서드라는걸 알 수 있겠죠? 
 

 

string public _name;
string public _symbol;
uint256 public _decimals;
uint256 private _totalSupply;

constructor() {
   _name = "CoolmarvelToken";
   _symbol = "CMT";
   _decimals = 18;
   _totalSupply = 100000000 * 10**_decimals;
   _balances[msg.sender] = _totalSupply;
   emit Transfer(address(0), msg.sender, _totalSupply);
}

다음으로 살펴볼거는 constructor(생성자) 입니다.

처음 컨트랙트가 배포될 때에 딱 한번만 실행되는 것으로 초기선언을 할 때 사용합니다.

보시면 저희는 constructor(매개변수) {} 에 매개변수를 아무것도 넣지 않고 Deploy할 수 있습니다.

만약 나는 저 컨트랙트를 좀 더 종속되지 않고 자유롭게 원하는 토큰을 찍어내는 boilerplate로 만들고 싶다하시는 분들은

string public _name;
string public _symbol;
uint256 public _decimals;
uint256 private _totalSupply;

constructor(string memory name_, string memory symbol_, uint256 decimals_, uint256 totalSupply_) {
    _name = name_;
    _symbol = symbol_;
    _decimals = decimals_;
    _totalSupply = totalSupply_;
    _balances[_msgSender()] = _totalSupply;
    
    emit Transfer(address(0), _msgSender(), _totalSupply);
}

이렇게 파라미터를 추가해주셔서 선언해주시면

아까 위에랑 다르게 배포할 때 필요한 파라미터들을 입력해 활용하실 수 있습니다.

 

이제 배포를 하고 메서드들을 살펴보면

저희가 작성한 컨트랙트들의 메서드를 살펴 볼 수 있는데 친절한 사람들이 만든 친절한 remix는 가스비용에 대해 색깔별로 표시해줍니다.

빨간색은 메서드에 payable이 붙어 이더가 소모될 수 있다.
주황색은 가스비가 발생한다.
파란색은 가스비가 발생하지 않는다.

이로써 배포까지 했으니 각 메서드들을 테스트해봅시다!

 

이 포스팅은 컨트랙트 문법을 어느 정도 아시는 분들을 위해 간단하게 작성한거라 다소 불친절하다고 느끼실 수 있지만,
정말 세세하게 하나하나 설명한다면 그건 또 그거대로 포스팅의 주제와 맞지 않다 생각합니다.

문법부터 다시 보고자 하신다면 밑에 링크에 들어가셔서 학습 하시는것도 추천드립니다.

https://dayone.tistory.com/

 

Just Blockchain

 

dayone.tistory.com

 

profile

사랑하애오

@사랑하애

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!