🌈Payable + deposit + receive + fallback+msg.value

RoRAΒ·2023λ…„ 5μ›” 25일
0

Solidity (use_ Remix)

λͺ©λ‘ 보기
9/10

πŸͺ‚ 2023. 05. 12. Friday_ νŒ€ λͺ…단 곡지! 뚜λ‘₯! πŸͺ‚


βœ… Intro

​

μ•ˆλ…•ν•˜μ„Έμš”!😁

λͺ¨λ‘ ν•¨κ»˜ 4κ°œμ›” λ™μ•ˆ ν•˜λ‚˜μ˜ 멋진 ν”„λ‘œμ νŠΈλ₯Ό μ™„μ„±ν•˜λŠ” κ·Έλ‚ κΉŒμ§€ ν™”μ΄νŒ…ν•©μ‹œλ‹€ !πŸ‘πŸ‘πŸ‘
​
* 이 κ³³μ—μ„œλŠ” 당일에 배운 λ‚΄μš©μ„ λ³΅μŠ΅ν•˜λŠ” μš©λ„λ‘œ 글을 μž‘μ„±ν•˜λ €κ³  ν•©λ‹ˆλ‹€.
​
* μ˜€λŠ˜μ€ μ†”λ¦¬λ””ν‹°μ˜ 'PAYABLE' λ‚΄μš©μ„ λ³΅μŠ΅ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€.


​
​

βœ… PAYABLE

​

πŸ‘‰ Payable μ‹€μŠ΅ μ½”λ“œ

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.18;


contract Note {
    address a;
    address payable owner;

    function setOwner() public {
        owner = payable(msg.sender);
    }

    function getOwner() public view returns(address payable) {
        return owner;
    }

    function setA() public {
        a = msg.sender;
    }

    function getA() public view returns(address) {
        return a;
    }
    ```solidity
// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.18;

/*
μ‹€μŠ΅κ°€μ΄λ“œ
1. 1번 지갑 μ€€λΉ„(μ£Όμ†Œ 볡뢙) -> setOwner, 2번 지갑 μ€€λΉ„(μ£Όμ†Œ 볡뢙) -> setA
2. deploy ν›„ 1번 μ§€κ°‘μœΌλ‘œ 10eth -> deposit()
3. contract μž”μ•‘ λ³€ν™” 확인
4. 1번 지갑 λ„£κ³ , 1000000000000000000 transferTo() -> 1λ²ˆμ§€κ°‘, contract μž”μ•‘ λ³€ν™” 확인
5. 2번 지갑, 1000000000000000000, transferTo() -> 2번 지갑, contract μž”μ•‘ λ³€ν™” 확인

0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2
*/

contract Note {
    address a;
    address payable owner;

    function deposit() public payable returns(uint){
        return msg.value;
    }

    function setOwner() public {
        owner = payable(msg.sender);
    }

    function getOwner() public view returns(address payable) {
        return owner;
    }

    function setA() public {
        a = msg.sender;
    }

    function getA() public view returns(address) {
        return a;
    }
    
/////////////λˆμ„ λ³΄λ‚΄λŠ” ν•¨μˆ˜///////////////////////////////////////

    function transferTo(address payable _to, uint _amount) public {
        _to.transfer(_amount); // μ§€κ°‘μ£Όμ†Œ.transfer(규λͺ¨)
    }

    function transferToOwner(uint _amount) public {
        owner.transfer(_amount);
    }

    /*function transferToA(uint _amount) public {
        a.transfer(_amount);
    }*/
    
}

​

πŸ‘‰ μœ„ μ½”λ“œ λœ―μ–΄λ³΄κΈ° !

​
βœ”οΈ address μ„ μ–Έ

β €β €β–ͺ️ address a : address ν˜• λ³€μˆ˜ aλ₯Ό μ„ μ–Έ
β €β €β–ͺ️ address payable owner : λˆμ„ 받을 수 μžˆλŠ” address ν˜• λ³€μˆ˜ ownder μ„ μ–Έ
​

βœ”οΈ setOwner()

β €β €β–ͺ️ ν•¨μˆ˜λ₯Ό λˆ„λ₯Έ μ‚¬λžŒμ΄ ownerκ°€ 될 수 μžˆλ„λ‘ μ„€μ •ν•˜λŠ” ν•¨μˆ˜
β €β €β–ͺ️ msg.sender : 거래λ₯Ό μΌμœΌν‚¨ μ‚¬λžŒμ„ 의미 (== λ²„νŠΌμ„ λˆ„λ₯Έ μ‚¬λžŒ)
β €β €β–ͺ️ this : λ°˜ν™˜ν•  것 == contract address (ν•΄λ‹Ή μ»¨νŠΈλ ‰νŠΈ address λ°˜ν™˜)
​

βœ”οΈ getOwner()

β €β €β–ͺ️ owner의 지갑 μ£Όμ†Œλ₯Ό λ°›λŠ” ν•¨μˆ˜
β €β €β–ͺ️ return κ°’ : owner
​

βœ”οΈ setA()

β €β €β–ͺ️ ν•¨μˆ˜λ₯Ό λˆ„λ₯Έ μ‚¬λžŒμ΄ aκ°€ 될 수 μžˆλ„λ‘ μ„€μ •ν•˜λŠ” ν•¨μˆ˜
​

βœ”οΈλˆμ„ λ³΄λ‚΄λŠ” ν•¨μˆ˜ == μ§€κ°‘μ£Όμ†Œ.transfer(μ–Όλ§ŒνΌ 보낼지 μ–‘)
​

βœ”οΈ transferTo()

β €β €β–ͺ️ ~μ—κ²Œ ~만큼의 λˆμ„ λ³΄λ‚΄λŠ” ν•¨μˆ˜
β €β €β–ͺ️ input κ°’ : _to(λˆ„κ΅¬μ—κ²Œ 보낼 것인지 ) + _amount(μ–Όλ§ˆλ§ŒνΌ 보낼 것인지)
​

βœ”οΈ transferToOwner()

β €β €β–ͺ️ Ownerμ—κ²Œ λˆμ„ λ³΄λ‚΄λŠ” ν•¨μˆ˜
β €β €β–ͺ️ input : Owner둜 νŠΉμ • 지갑 μ£Όμ†Œλ₯Ό μ§€μ •ν–ˆμŒμœΌλ‘œ _amount κ°’λ§Œ ν•„μš”ν•˜λ‹€.
​

βœ”οΈ transferToA()

β €β €β–ͺ️ Aμ—κ²Œ λˆμ„ λ³΄λ‚΄λŠ” ν•¨μˆ˜
β €β €β–ͺ️ aλŠ” payable이 μ—†κΈ° λ•Œλ¬Έμ— μ»¨νŠΈλ ‰νŠΈλ‘œ λΆ€ν„° λˆμ„ 받을 수 μ—†λ‹€.
β €β €β–ͺ️ λ”°λΌμ„œ λ‹€μŒ ν•¨μˆ˜λŠ” 였λ₯˜κ°€ λ°œμƒν•œλ‹€.

​
​
​

πŸ‘‰ payable μžˆμ„ λ•Œ vs 없을 λ•Œ

​
βœ”οΈ payable

β €β €β–ͺ️ λˆμ„ 받을 수 μžˆλŠ”μ§€ 확인해쀀닀.
β €β €β € β €β €πŸ”Έex) μ»¨νŠΈλ ‰νŠΈλ‘œλΆ€ν„° λˆμ„ 받을 경우, payable이 μžˆμ–΄μ•Ό λˆμ„ 받을 수 μžˆλ‹€!

β €β €β–ͺ️ 지갑 μž”κ³ λ₯Ό λ°”κΏ”μ€€λ‹€~!~

β €β €β–ͺ️ μ»¨νŠΈλ ‰νŠΈ μž…μž₯μ—μ„œλŠ” 돈이 λΉ μ Έλ‚˜κ°„λ‹€κ³  ν‘œν˜„ν•  수 μžˆλ‹€.
​
​
​

πŸ‘‰ payable μ—†λŠ”λ° ν•¨μˆ˜κ°€ 싀행될 경우

​
βœ”οΈ function transferTo(address payable _to, uint _amount) public {}

β €β €β–ͺ️ payable이 μ—†μœΌλ©΄, μ»¨νŠΈλ ‰νŠΈλ‘œ λΆ€ν„° λˆμ„ 받을 수 μ—†λ‹€.

β €β €β–ͺ️ BUT!!!
β €β €β–ͺ️ μœ„μ˜ ν•¨μˆ˜μ²˜λŸΌ input 값에 payable을 λΆ™μ—¬μ„œ 값을 받을 κ²½μš°μ—λŠ” λˆμ„ 받을 수 μžˆλ‹€.


​
​

βœ… deposit() + receive() + fallback()

​

πŸ‘‰ deposit() μ‹€μŠ΅ μ½”λ“œ

    function deposit() public payable{}

βœ”οΈ deposit

β €β €β–ͺ️ μ»¨νŠΈλ ‰νŠΈμ— μ’…μ†λ˜μ–΄ μžˆλŠ” ν•¨μˆ˜μ΄λ‹€.
β €β €β–ͺ️ νŠΉμ • μŠ€λ§ˆνŠΈμ»¨νŠΈλ ‰νŠΈκ°€ λˆμ„ 받을 수 있게 ν•΄μ£ΌλŠ” ν•¨μˆ˜μ΄λ‹€!

​

πŸ‘‰ receive() μ‹€μŠ΅ μ½”λ“œ

    receive() external payable{}

βœ”οΈ receive

β €β €β–ͺ️ 이더λ₯Ό λ°›λŠ”λ° νŠΉν™”λœ ν•¨μˆ˜μ΄λ‹€.
β €β €β–ͺ️ μ˜ˆμ•½μ–΄λ‘œ λ˜μ–΄ μžˆλ‹€.

​

πŸ‘‰ fallback() μ‹€μŠ΅ μ½”λ“œ

    fallback() external payable{}

βœ”οΈ fallback

β €β €β–ͺ️ μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  λ•Œ μ‚¬μš©ν•˜λŠ” ν•¨μˆ˜μ΄λ‹€.
β €β €β–ͺ️ μ˜ˆμ™Έμ²˜λ¦¬μ™€ λΉ„μŠ·ν•˜λ‹€κ³  μƒκ°ν•˜λ©΄ λœλ‹€.
​
​
​

πŸ‘‰ deposit( ) ​ ​ vs ​ ​ receive( )

​
βœ”οΈ deposit

β €β €β–ͺ️ ν•¨μˆ˜λ₯Ό μ‹€ν–‰ μ‹œμΌœμ•Ό μ»¨νŠΈλ ‰νŠΈλ‘œλΆ€ν„° λˆμ„ 받을 수 μžˆλ‹€.
​

βœ”οΈ receive

β €β €β–ͺ️ CA - CA κ±°λž˜μ‹œ μ‚¬μš©
β €β €β € β €β €πŸ”ΈCA : μ»¨νŠΈλ ‰νŠΈμ— μ’…μ†λœ 지갑
β €β €β € β €β €πŸ”ΈEOA : μ‚¬λžŒμ΄ μ†Œμœ ν•œ 지갑

β €β €β–ͺ️ λˆμ„ μ „μ†‘ν•˜λŠ” 거래λ₯Ό λ°œμƒν–ˆμ„ 경우 μ•Œμ•„μ„œ ν˜ΈμΆœλœλ‹€.
​

βœ”οΈ λˆμ„ λ³΄λ‚΄λŠ” ν•¨μˆ˜λ₯Ό μ‹€ν–‰μ‹œμΌ°μ„ 경우 !

β €β €β–ͺ️ ν•¨μˆ˜λ₯Ό μ‹€ν–‰μ‹œ, from - to 뢀뢄을 확인할 수 μžˆλŠ”λ°

β €β €β–ͺ️ receive: to에 ν•¨μˆ˜λͺ…이 μ•ˆ λΆ™λŠ”λ‹€.
β €β €β–ͺ️ deposit : to에 λ°œμƒν•œ ν•¨μˆ˜λͺ…이 λΆ™λŠ”λ‹€.


​
​

βœ… msg.value

​

πŸ‘‰ msg.value μ‹€μŠ΅ μ½”λ“œ

    function deposit() public payable returns(uint) {
        return msg.value;
    }

βœ”οΈ msg.value

β €β €β–ͺ️ 보낸 만큼의 돈의 양을 확인할 λ•Œ μ‚¬μš©ν•œλ‹€.
β €β €β–ͺ️ payable()κ°€ λΆ™μ–΄ μžˆμ–΄μ•Ό μž”κ³ λ₯Ό 확인할 수 μžˆλ‹€.
β €β €β–ͺ️ deployν•œ ν›„ decoded ouput μ—μ„œ 넣은 값을 확인할 수 μžˆλ‹€.


​
​

βœ… μ‹€μŠ΅ κ°€μ΄λ“œ

​

πŸ₯° κ°•μ‚¬λ‹˜ κ°μ‚¬ν•©λ‹ˆλ‹€ πŸ₯°

* balance : μ»¨νŠΈλ ‰νŠΈμ˜ balanceλ₯Ό μ˜λ―Έν•œλ‹€.

1. 1번 지갑 μ€€λΉ„(μ£Όμ†Œ 볡뢙) -> setOwner, 2번 지갑 μ€€λΉ„(μ£Όμ†Œ 볡뢙) -> setA
2. deploy ν›„ 1번 μ§€κ°‘μœΌλ‘œ 10eth -> deposit()
3. contract μž”μ•‘ λ³€ν™” 확인
4. 1번 지갑 λ„£κ³ , 1000000000000000000 transferTo() -> 1λ²ˆμ§€κ°‘, contract μž”μ•‘ λ³€ν™” 확인
5. 2번 지갑, 1000000000000000000, transferTo() -> 2번 지갑, contract μž”μ•‘ λ³€ν™” 확인

​

β €β €β €

πŸ“Œ23. 05. 12

벨둜그 밀리면 μ•ˆλ˜λŠ”λ°...μ΄λ―Έγ…œγ…œλ°€λ €λ²„λ €μ„œ κ³ ν†΅λ°›λŠ” 쀑 ..

0개의 λŒ“κΈ€