create account

Solidity의 modifier 와 payable by kaipark

View this thread on: hive.blogpeakd.comecency.com
· @kaipark · (edited)
$10.39
Solidity의 modifier 와 payable
Solidity를 공부 할 때 제가 참고하는 것은 크게 [공식 Document](https://solidity.readthedocs.io/en/develop/) 와 [State of the DAPPs](https://dapps.ethercasts.com/), ICO들의 소스 입니다.

공식문서는 Solidity의 기본부터 심화까지 모든 것을 담고 있고, State of the dapps 의 경우 현재 진행되고 있는 거의 모든  dapp들을 볼수 있고, 해당 소스도 참고하면서 공부도 해볼수 있습니다.

ICO 들의 소스의 경우  ERC20 토큰의 제일 충분한 예시입니다.

그리고 해당 소스들을 보면 특이한 점들이 있습니다.

```
pragma solidity 0.4.11;

import "TokenFactory.sol"

contract CrowdSaleSample {

    TokenFactory public token;
    mapping   (address => uint256)  balances;
   address public owner;

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    function FLToken(){
        owner = msg.sender;
    }

    function() payable {
        if (now > endBlock) throw;
        token.distribute(msg.sender);
    }

    function distribute(address depositor) onlyOwner {
        balances[depositor] +=  100;
    }

```

매우 간단한 소스이고, `function` 이라는 것은 어느 언어에서든지 있는 개념입니다.

하지만 `modifier` 라는 것은 조금 생소했고, function 뒤에 리턴값이 아닌 것이 위치해있습니다.

그래서 Document 에서 해당 사항을 찾아봤습니다.

```
Function Modifiers

Modifiers can be used to easily change the behaviour of functions. 
For example, they can automatically check a condition prior to executing the function. 
Modifiers are inheritable properties of contracts and may be overridden by derived contracts.

-> modifier 는 function의 실행(행동)을 쉽게 변경가능하도록하며, function 실행의 조건을 체크 할수 있다

```

위에 예제에서 modifier 는  한번은 정의, 그리고 또 한번은 function 뒤에 두번 적혀있습니다.

간단하게 본다면 `distribute` function 을 실행하기 전에 `onlyOwner` 라는 것을 미리 체크를 해보다는 겁니다.

```
modifier onlyOwner {
        // msg.sender(실행하려는 Address)와 owner(Contract 를 생성한 Address) 같아야 합니다.
        require(msg.sender == owner); 
        _;
    }
```

`Rails` 에서는 [BeforeFilter](https://apidock.com/rails/ActionController/Filters/ClassMethods/before_filter) 라는 개념이 있습니다.

저는 이것과 비슷한 개념이라고 이해를 했습니다.

함수를 실행하기 전에 미리 필터링을 시켜준다는 개념(?)(저만의 정의입니다.)

그러면 다양하게 적용을 할수 있을 겁니다.

```
    modifier  startCheck {
        require(block.number >= startBlock);
        _;
    }

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    modifier  crowdEndingCheck  {
        require(!isCrwoldEnding);
        _;
    }
```   

등등등 중복적으로 실행이 필요하지만 간단하다면 modifier 로 정의해서 사용해보면 될겁니다.

# 그리고 `payable`

[Reddit](https://www.reddit.com/r/ethereum/comments/4yciyz/upcoming_changes_in_solidity_040/) 과[ 공식 문서](http://solidity.readthedocs.io/en/develop/contracts.html)에서 보면 payable 에 대한 설명이 나옵니다.

```
// Reddit 
functions reject ether by default and require the new "payable" modifier if they want to accept it

// Solidity Document
It is important to also provide the
"payable" keyword here, otherwise the function will
automatically reject all Ether sent to it.
```

Default 로 모든 function 에서는  이더 전송에 관한 모든 것을 Reject 시키며, `payable(아마도 내장 modifier 인 것 같네요)` 가 붙지 않으면 모든 전송을 자동으로 Reject 시킨다.

이더 전송에 관련된 function 이라면 무조건 붙여야지만 전송이 가능하도록 해놓은 거 같네요.

modifier 와 payable..

둘 다 생소했지만 Solidity에서는 매우 필요한 요소 인듯 싶네요.

--- 

잘못된 부분은 댓글로 남겨주시면 수정하도록 하겠습니다.
 
---

# Donation

기부는 사랑입니다.

- Ƀ BTC : 16MdVNJgvGYbVuaC6KrjGNy2RCrNsaPaZz
- Ξ ETH : 0x5debb97a6Cc1Fdf686a3C6aA804a623a21deD73c

개발자에게 [Starize](https://github.com/ggomagundan/coinone) 도 사랑입니다.
👍  , , , , , , , , ,
properties (23)
authorkaipark
permlinksolidity-modifier-payable
categorykr
json_metadata{"tags":["kr","coinkorea","solidity","ethereum","kr-dev"],"links":["https://solidity.readthedocs.io/en/develop/","https://dapps.ethercasts.com/","https://apidock.com/rails/ActionController/Filters/ClassMethods/before_filter","https://www.reddit.com/r/ethereum/comments/4yciyz/upcoming_changes_in_solidity_040/","http://solidity.readthedocs.io/en/develop/contracts.html","https://github.com/ggomagundan/coinone"],"app":"steemit/0.1","format":"markdown"}
created2017-06-28 07:50:42
last_update2017-06-28 15:47:54
depth0
children6
last_payout2017-07-05 07:50:42
cashout_time1969-12-31 23:59:59
total_payout_value8.110 HBD
curator_payout_value2.282 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,186
author_reputation848,534,820,110
root_title"Solidity의 modifier 와 payable"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,446,043
net_rshares1,203,442,992,060
author_curate_reward""
vote details (10)
@coinkorea ·
개발 관련태그로 #kr-dev 를 추가해주시면 더 많은 분들이 보실 수 있을것 같습니다 :)
properties (22)
authorcoinkorea
permlinkre-kaipark-solidity-modifier-payable-20170628t084459061z
categorykr
json_metadata{"tags":["kr-dev","kr"],"app":"steemit/0.1"}
created2017-06-28 08:44:57
last_update2017-06-28 08:44:57
depth1
children1
last_payout2017-07-05 08:44:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length51
author_reputation34,890,178,701,093
root_title"Solidity의 modifier 와 payable"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,450,228
net_rshares0
@kaipark ·
새로운 태그 알려주셔서 감사합니다 ㅋㅋ
properties (22)
authorkaipark
permlinkre-coinkorea-re-kaipark-solidity-modifier-payable-20170628t155045809z
categorykr
json_metadata{"tags":["kr"],"app":"steemit/0.1"}
created2017-06-28 15:50:45
last_update2017-06-28 15:50:45
depth2
children0
last_payout2017-07-05 15:50:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length21
author_reputation848,534,820,110
root_title"Solidity의 modifier 와 payable"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,493,956
net_rshares0
@ih5kevin ·
안녕하세요, 한국에서 일하시는 분인가요?? 저는 현재 뉴욕에서 블락체인 연구/컨설팅을 막 시작한 1인입니다. Solidity도 최근에 배워서 기본적인 건 어느 정도 마스터를 한 상태인데요, 한국 내에서는 Solidity나 블락체인에 대한 전반적인 관심이 어느 정도 있는 지 궁금하네요. 더 전반적으로는 가상화폐 모임이나 동호회 같은 구조들이 있나요??
properties (22)
authorih5kevin
permlinkre-kaipark-solidity-modifier-payable-20171121t074048926z
categorykr
json_metadata{"tags":["kr"],"app":"steemit/0.1"}
created2017-11-21 07:41:03
last_update2017-11-21 07:41:03
depth1
children0
last_payout2017-11-28 07:41:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length196
author_reputation17,311,213,551
root_title"Solidity의 modifier 와 payable"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id21,062,194
net_rshares0
@kdj ·
환영합니다.
반갑습니다.
properties (22)
authorkdj
permlinkre-kaipark-solidity-modifier-payable-20170628t161357613z
categorykr
json_metadata{"tags":["kr"],"app":"steemit/0.1"}
created2017-06-28 16:14:00
last_update2017-06-28 16:14:00
depth1
children1
last_payout2017-07-05 16:14:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length13
author_reputation4,188,781,313,300
root_title"Solidity의 modifier 와 payable"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,496,986
net_rshares0
@kaipark ·
@kdj 간간히 써내려가보게습니다 ^^
properties (22)
authorkaipark
permlinkre-kdj-re-kaipark-solidity-modifier-payable-20170629t015828269z
categorykr
json_metadata{"tags":["kr"],"users":["kdj"],"app":"steemit/0.1"}
created2017-06-29 01:58:27
last_update2017-06-29 01:58:27
depth2
children0
last_payout2017-07-06 01:58:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length21
author_reputation848,534,820,110
root_title"Solidity의 modifier 와 payable"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,560,695
net_rshares0
@steemitboard ·
Congratulations @kaipark! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png)](http://steemitboard.com/@kaipark) Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click [here](https://steemit.com/@steemitboard)

If you no longer want to receive notifications, reply to this comment with the word `STOP`

> By upvoting this notification, you can help all Steemit users. Learn how [here](https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png)!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-kaipark-20171201t031402000z
categorykr
json_metadata{"image":["https://steemitboard.com/img/notifications.png"]}
created2017-12-01 03:14:00
last_update2017-12-01 03:14:00
depth1
children0
last_payout2017-12-08 03:14:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length684
author_reputation38,975,615,169,260
root_title"Solidity의 modifier 와 payable"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id22,052,803
net_rshares0