-
call로 다른 주소에 Ether를 송금 가능.
call로 외부 contract에 함수를 호출 가능.
코드
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 < 0.9.0; contract ContractReceiver { event EvtFallback(string _str); event EvtReceive(string _str); function fun1(uint256 _v1, uint256 _v2) public pure returns(uint256){ return _v1 + _v2; } fallback() external { emit EvtFallback("ContractReciver1.EvtFallback is called"); } receive() external payable { emit EvtReceive("ContractReciver1.EvtReceive is called"); } } contract ContractCaller { event EvtCalledFunction(bool _success, bytes _output); //function transferEther(address payable _to) public payable{ function callV1(address _to) public payable{ (bool success,) = _to.call{value:msg.value}(""); require(success,"failed to callV1"); } function callV2(address _to, uint256 _v1, uint256 _v2) public{ (bool success, bytes memory outputFromCalledFunction) = _to.call( abi.encodeWithSignature("fun1(uint256,uint256)",_v1,_v2) ); require(success,"failed to callV2"); emit EvtCalledFunction(success,outputFromCalledFunction); } function callV3(address _to) public payable { (bool success, bytes memory outputFromCalledFunction) = _to.call{value:msg.value}( "" //abi.encodeWithSignature("fun1(uint256,uint256)",_v1,_v2) ); require(success,"failed to callV3"); emit EvtCalledFunction(success,outputFromCalledFunction); } function callV4(address _to, uint256 _v1, uint256 _v2) public payable { (bool success, bytes memory outputFromCalledFunction) = _to.call{value:msg.value}( abi.encodeWithSignature("fun1(uint256,uint256)",_v1,_v2) ); require(success,"failed to callV4"); emit EvtCalledFunction(success,outputFromCalledFunction); } }
Remix에서 contract등록
ContractReceiver
creation of ContractReceiver pending... [vm]from: 0x5B3...eddC4to: ContractReceiver.(constructor)value: 0 weidata: 0x608...10033logs: 0hash: 0x4b2...083be status true Transaction mined and execution succeed transaction hash 0x4b24e805a7199fc6101f3ec24f19110d12ca6c3cf1f08296068bdab4030083be from 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 to ContractReceiver.(constructor) gas 276617 gas transaction cost 240536 gas execution cost 240536 gas input 0x608...10033 decoded input {} decoded output - logs [] val 0 wei
ContractCaller
creation of ContractCaller pending... [vm]from: 0x5B3...eddC4to: ContractCaller.(constructor)value: 0 weidata: 0x608...10033logs: 0hash: 0xe6e...22fe3 status true Transaction mined and execution succeed transaction hash 0xe6ed15ff0d31301eb932ad7b3e4a9406dabfe764015d096d471e20bc43922fe3 from 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 to ContractCaller.(constructor) gas 696267 gas transaction cost 605449 gas execution cost 605449 gas input 0x608...10033 decoded input {} decoded output - logs [] val 0 wei
ContractReceiver의 주소 할당 됨: 0xd9145CCE52D386f254917e481eB44e9943F39138
callV1으로 1 Ehter전송
value에 1 ether.
함수인수 0xd9145CCE52D386f254917e481eB44e9943F39138
transact to ContractCaller.callV1 pending ... [vm]from: 0x5B3...eddC4to: ContractCaller.callV1(address) 0xd8b...33fa8value: 1000000000000000000 weidata: 0x67a...39138logs: 1hash: 0x689...da253 status true Transaction mined and execution succeed transaction hash 0x689a7dc9d9daf8e547b45cde8d6a6a17b46b399a80563ea60f2fe63f0dfda253 from 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 to ContractCaller.callV1(address) 0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8 gas 38646 gas transaction cost 33605 gas execution cost 33605 gas input 0x67a...39138 decoded input { "address _to": "0xd9145CCE52D386f254917e481eB44e9943F39138" } decoded output {} logs [ { "from": "0xd9145CCE52D386f254917e481eB44e9943F39138", "topic": "0xe7faffa79f66b3c9e37f4a2c9cbbc1071b49ac800e4841a396cfc5fc7ced4b26", "event": "EvtReceive", "args": { "0": "ContractReciver1.EvtReceive is called", "_str": "ContractReciver1.EvtReceive is called" } } ] val 1000000000000000000 wei
EvtReceive()가 호출 됨
callV1으로 0 Ether전송
transact to ContractCaller.callV1 pending ... [vm]from: 0x5B3...eddC4to: ContractCaller.callV1(address) 0xd8b...33fa8value: 0 weidata: 0x67a...39138logs: 1hash: 0xdb2...8450b status true Transaction mined and execution succeed transaction hash 0xdb211f5237184eef391c72e4269f457f48371f5fa01387238d609e286e98450b from 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 to ContractCaller.callV1(address) 0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8 gas 30941 gas transaction cost 26905 gas execution cost 26905 gas input 0x67a...39138 decoded input { "address _to": "0xd9145CCE52D386f254917e481eB44e9943F39138" } decoded output {} logs [ { "from": "0xd9145CCE52D386f254917e481eB44e9943F39138", "topic": "0xe7faffa79f66b3c9e37f4a2c9cbbc1071b49ac800e4841a396cfc5fc7ced4b26", "event": "EvtReceive", "args": { "0": "ContractReciver1.EvtReceive is called", "_str": "ContractReciver1.EvtReceive is called" } } ] val 0 wei
0 Ether에도 EvtReceive()가 호출 됨
callV2함수 호출
fun1에 2, 3 넣어 호출
transact to ContractCaller.callV2 pending ... [vm]from: 0x5B3...eddC4to: ContractCaller.callV2(address,uint256,uint256) 0xd8b...33fa8value: 0 weidata: 0xea4...00003logs: 1hash: 0x1ee...2db1f status true Transaction mined and execution succeed transaction hash 0x1eead82680aa07e7f411295d40d5945d28ebf5617a147bc20879295fffa2db1f from 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 to ContractCaller.callV2(address,uint256,uint256) 0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8 gas 34065 gas transaction cost 29621 gas execution cost 29621 gas input 0xea4...00003 decoded input { "address _to": "0xd9145CCE52D386f254917e481eB44e9943F39138", "uint256 _v1": "2", "uint256 _v2": "3" } decoded output {} logs [ { "from": "0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8", "topic": "0xd06e7294c217d0bf776778aa02ba689f58a1bd8a684572bf38be9de3576a5a8d", "event": "EvtCalledFunction", "args": { "0": true, "1": "0x0000000000000000000000000000000000000000000000000000000000000005", "_success": true, "_output": "0x0000000000000000000000000000000000000000000000000000000000000005" } } ] val 0 wei
_output에 결과값 5
callV3
value: 2 ether
transact to ContractCaller.callV3 pending ... [vm]from: 0x5B3...eddC4to: ContractCaller.callV3(address) 0xd8b...33fa8value: 2000000000000000000 weidata: 0x4f0...39138logs: 2hash: 0xdea...14810 status true Transaction mined and execution succeed transaction hash 0xdea7ad3a2bb0fd80f94ecc0d1ba6b1cbbfe5c7defd76c1188772af68f1a14810 from 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 to ContractCaller.callV3(address) 0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8 gas 41023 gas transaction cost 35672 gas execution cost 35672 gas input 0x4f0...39138 decoded input { "address _to": "0xd9145CCE52D386f254917e481eB44e9943F39138" } decoded output {} logs [ { "from": "0xd9145CCE52D386f254917e481eB44e9943F39138", "topic": "0xe7faffa79f66b3c9e37f4a2c9cbbc1071b49ac800e4841a396cfc5fc7ced4b26", "event": "EvtReceive", "args": { "0": "ContractReciver1.EvtReceive is called", "_str": "ContractReciver1.EvtReceive is called" } }, { "from": "0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8", "topic": "0xd06e7294c217d0bf776778aa02ba689f58a1bd8a684572bf38be9de3576a5a8d", "event": "EvtCalledFunction", "args": { "0": true, "1": "0x", "_success": true, "_output": "0x" } } ] val 2000000000000000000 wei
callV4에 0 Ether
value: 0 Ether
callV4() 인자에 3, 5
transact to ContractCaller.callV4 pending ... [vm]from: 0x5B3...eddC4to: ContractCaller.callV4(address,uint256,uint256) 0xd8b...33fa8value: 0 weidata: 0xb69...00005logs: 1hash: 0xde0...9c236 status true Transaction mined and execution succeed transaction hash 0xde0e0edf99892777d2b0cbf210915007353729f0469eef5ca53b704e6e39c236 from 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 to ContractCaller.callV4(address,uint256,uint256) 0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8 gas 34016 gas transaction cost 29579 gas execution cost 29579 gas input 0xb69...00005 decoded input { "address _to": "0xd9145CCE52D386f254917e481eB44e9943F39138", "uint256 _v1": "3", "uint256 _v2": "5" } decoded output {} logs [ { "from": "0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8", "topic": "0xd06e7294c217d0bf776778aa02ba689f58a1bd8a684572bf38be9de3576a5a8d", "event": "EvtCalledFunction", "args": { "0": true, "1": "0x0000000000000000000000000000000000000000000000000000000000000008", "_success": true, "_output": "0x0000000000000000000000000000000000000000000000000000000000000008" } } ] val 0 wei
0 Ether에도 EvtReceive()가 호출 안 됨
callV4에 3 Ether
value에 3 Ether
callV4() 인자에 3, 5
transact to ContractCaller.callV4 pending ... [vm]from: 0x5B3...eddC4to: ContractCaller.callV4(address,uint256,uint256) 0xd8b...33fa8value: 3000000000000000000 weidata: 0xb69...00005logs: 0hash: 0x345...58722 status false Transaction mined but execution failed transaction hash 0x3451cf82a3489113841bb0554c12c453a74fe288f901d68a854626961a158722 from 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 to ContractCaller.callV4(address,uint256,uint256) 0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8 gas 3000000 gas transaction cost 33224 gas execution cost 33224 gas input 0xb69...00005 decoded input { "address _to": "0xd9145CCE52D386f254917e481eB44e9943F39138", "uint256 _v1": "3", "uint256 _v2": "5" } decoded output {} logs [] val 3000000000000000000 wei transact to ContractCaller.callV4 errored: VM error: revert. revert The transaction has been reverted to the initial state. Reason provided by the contract: "failed to callV4". Debug the transaction to get more information.
처리 실패
'Blockchain' 카테고리의 다른 글
Metamask 계정 다시 만들기(지갑 초기화) (0) 2023.01.08 Metamask Ganache 연결 (0) 2023.01.08 Metamask Network 삭제 (0) 2023.01.08 Solidity Donation Ex (0) 2023.01.08 Solidity What does _ (underscore) (0) 2023.01.08 Geth Ethereum client (0) 2023.01.07 ERC721 관련 (0) 2023.01.07 Truffle 예제 'pet-shop' (0) 2023.01.03