상세 컨텐츠

본문 제목

Solidity - Call

Programming Language/Solidity

by Yongari 2023. 1. 17. 22:26

본문

call is a low level function to interact with other contracts.
call은 다른 계약과 상호 작용하기 위핸 저수준의 함수이다. 

This is the recommended method to use when you're just sending Ether via calling the fallback function.
이 방법은 fallback 함수를 호출하여 Ether를 전송할 때 사용하는 권장하는 방법이다.

However it is not the recommend way to call existing functions.
그러나 기존에 존재하는 함수를 호출하는 권장하는 방법은 아닙니다. 

  • Reverts are not bubbled up 
  • Type checks are bypassed 
  • Function existence checks are omitted
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract Receiver {
    event Received(address caller, uint amount, string message);

    fallback() external payable {
        emit Received(msg.sender, msg.value, "Fallback was called");
    }

    function foo(string memory _message, uint _x) public payable returns (uint) {
        emit Received(msg.sender, msg.value, _message);

        return _x + 1;
    }
}

contract Caller {
    event Response(bool success, bytes data);

    // Let's imagine that contract Caller does not have the source code for the
    // contract Receiver, but we do know the address of contract Receiver and the function to call.
    function testCallFoo(address payable _addr) public payable {
        // You can send ether and specify a custom gas amount
        (bool success, bytes memory data) = _addr.call{value: msg.value, gas: 5000}(
            abi.encodeWithSignature("foo(string,uint256)", "call foo", 123)
        );

        emit Response(success, data);
    }

    // Calling a function that does not exist triggers the fallback function.
    function testCallDoesNotExist(address payable _addr) public payable {
        (bool success, bytes memory data) = _addr.call{value: msg.value}(
            abi.encodeWithSignature("doesNotExist()")
        );

        emit Response(success, data);
    }
}

Try on Remix

 

Remix - Ethereum IDE

 

remix.ethereum.org

 

'Programming Language > Solidity' 카테고리의 다른 글

Solidity - Function Selector  (0) 2023.01.19
Solidity - Delegatecall  (0) 2023.01.18
Solidity - Fallback  (0) 2023.01.13
Solidity - Interface  (0) 2023.01.05
Solidity - Visibility  (0) 2023.01.04

관련글 더보기