solidity

Solidity

이더리움에서 가장 잘 사용되는 언어

  • 주석: //로 시작

  • contract: contract 객체를 선언

  • require: 인자가 false이면 exception을 터트림

  • address payable: address 중에 ETH를 보낼 수 있는 address

  • msg 객체: 모든 컨트랙트에서 접근할 수 있는 입력 (컨트랙트를 호출한 Transaction을 의미)

  • payable: 트랜잭션에 함수를 지정하지 않았을 때 호출 됨

    • fallback(): msg.data가 비어있지 않을 때
    • receive(): msg.data가 비었을 때
  • Example

pragma solidity 0.8.26;
// SPDX-License-Identifier: GPL-3.0

// Our first contract is a faucet!
contract Faucet {

    // Give out ether to anyone who asks
    function withdraw(uint256 _withdrawAmount, address payable _to) public {

        // Limit withdrawal amount
        require(_withdrawAmount <= 1000000000000);

        // Send the amount to the address that requested it
        _to.transfer(_withdrawAmount);
    }

    // Function to receive Ether. msg.data must be empty
    receive() external payable {}

    // Fallback function is called when msg.data is not empty
    fallback() external payable {}
}