상세 컨텐츠

본문 제목

Solidity - Calling Other Contract

Programming Language/Solidity

by Yongari 2023. 1. 25. 23:35

본문

Contract can call other contracts in 2 ways.
계약은 두 가지 방법으로 다른 계약을 호출할 수 있습니다.

The easiest way to is to just call it, like A.foo(x, y, z).
가장 쉬운 방법은 A.foo(x, y, z)처럼 그냥 부르는 것입니다.

Another way to call other contracts is to use the low-level call.
다른 계약을 호출하는 또 다른 방법은 낮은 수준의 호출을 사용하는 것입니다.

This method is not recommended.

이 방법은 권장되지 않습니다.

 

 

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract Callee {
    uint public x;
    uint public value;

    function setX(uint _x) public returns (uint) {
        x = _x;
        return x;
    }

    function setXandSendEther(uint _x) public payable returns (uint, uint) {
        x = _x;
        value = msg.value;

        return (x, value);
    }
}

contract Caller {
    function setX(Callee _callee, uint _x) public {
        uint x = _callee.setX(_x);
    }

    function setXFromAddress(address _addr, uint _x) public {
        Callee callee = Callee(_addr);
        callee.setX(_x);
    }

    function setXandSendEther(Callee _callee, uint _x) public payable {
        (uint x, uint value) = _callee.setXandSendEther{value: msg.value}(_x);
    }
}

Try on Remix

 

Remix - Ethereum IDE

 

remix.ethereum.org

 

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

우분투에 솔리디티 컴파일러 설치  (0) 2023.02.07
스마트 컨트랙트 구조와 Solidity 변수  (0) 2023.02.07
Solidity - Function Selector  (0) 2023.01.19
Solidity - Delegatecall  (0) 2023.01.18
Solidity - Call  (0) 2023.01.17

관련글 더보기