크립토 좀비에서 제공하는 튜토리얼을 통해 배우는 Solidity 문법을 정리하고 있습니다!
Lesseon 4 에서는 payable 개념에 대해서 조금 더 다룬다.
이전 게시글에서 zombieattack.sol 을 추가해, 좀비가 공격할 수 있도록 만들었다!
이번 장을 정리한 내용은 다음과 같다.
1. payable Modifier
이더리움에는 Ether 를 주고 받을 수 있는, payable 함수라는 녀석이 존재한다.
contract OnlineStore {
function buySomething() external payable {
// Check to make sure 0.001 ether was sent to the function call:
require(msg.value == 0.001 ether);
// If so, some logic to transfer the digital item to the caller of the function:
transferThing(msg.sender);
}
}
msg.value 를 통해 얼마나 많은 Ether 가 해당 contract 로 보내졌는지 알 수 있다(ehter 는 buil-in unit 이다).
위의 함수를 호출하는 web3.js (DApp 의 자바스크립트 front-end) 코드는 다음과 같을 것이다 :
// Assuming `OnlineStore` points to your contract on Ethereum:
OnlineStore.buySomething({from: web3.eth.defaultAccount, value: web3.utils.toWei(0.001)})
2. withdraw 함수
이제 배운 내용을 바탕으로 Ether 를 전달 받은 contract 에서 실제 이더리움 주소로 Ether 를 보내는 코드를 짜 보자.
contract GetPaid is Ownable {
function withdraw() external onlyOwner {
address payable _owner = address(uint160(owner()));
_owner.transfer(address(this).balance);
}
}
// 다음과 같이 응용
uint itemFee = 0.001 ether;
msg.sender.transfer(msg.value - itemFee);
3. Random Numbers
Solidity 에서의 난수 생성은 여러가지 방식이 있다. 예제에서는 간단하게 인자로 들어가는 Nonce 값을 바꾸면서 keccak256 을 이용해 의사 난수를 생성한다.
pragma solidity >=0.5.0 <0.6.0;
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
contract Ownable {
address private _owner;
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
* account.
*/
constructor() internal {
_owner = msg.sender;
emit OwnershipTransferred(address(0), _owner);
}
/**
* @return the address of the owner.
*/
function owner() public view returns(address) {
return _owner;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(isOwner());
_;
}
/**
* @return true if `msg.sender` is the owner of the contract.
*/
function isOwner() public view returns(bool) {
return msg.sender == _owner;
}
/**
* @dev Allows the current owner to relinquish control of the contract.
* @notice Renouncing to ownership will leave the contract without an owner.
* It will not be possible to call the functions with the `onlyOwner`
* modifier anymore.
*/
function renounceOwnership() public onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
*/
function transferOwnership(address newOwner) public onlyOwner {
_transferOwnership(newOwner);
}
/**
* @dev Transfers control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
*/
function _transferOwnership(address newOwner) internal {
require(newOwner != address(0));
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}