본문 바로가기
솔리디티

delegatecall : immutable, constant 예제

by gun_poo 2024. 2. 3.

이뮤터블, 콘스탄트 들이 어떻게 작동하는지 딜리게이트콜을 아래 테스트를 사용해 알아보자.

DelegateCalled.sol

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

contract DelegateCalled {
    uint256 public constant CONSTANTVALUES = 400;
    uint256 public publicValue1 = 600;
    uint256 public immutable immutableValue = 200;
    uint256 public publicValue2 = 800;
    

    constructor() {
    }

    function getValues() external view returns (uint256, uint256, uint256, uint256) {
        return (immutableValue, constantValue, publicValue1, publicValue2);
    }

    function setPublicValue(uint256 _value, uint256 _value2) external {
        publicValue1 = _value;
        publicValue2 = _value2;
    }
}

DelegateCaller.sol

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

contract DelegateCaller {
    uint256 public immutable immutableValue = 100;
    uint256 public constant CONSTANTVALUES = 300;
    uint256 public publicValue1 = 500;
    uint256 public publicValue2 = 700;
    address public delegateCalledAddress;

    constructor(address _delegateCalledAddress) {
        delegateCalledAddress = _delegateCalledAddress;
    }

    function callGetValues() public returns (uint256, uint256, uint256, uint256) {
        (bool success, bytes memory data) = delegateCalledAddress.delegatecall(
            abi.encodeWithSignature("getValues()")
        );
        require(success, "Delegate call failed.");
        return abi.decode(data, (uint256, uint256, uint256, uint256));
    }

    function callSetPublicValue(uint256 _value, uint256 _value2) public {
        (bool success,) = delegateCalledAddress.delegatecall(
            abi.encodeWithSignature("setPublicValue(uint256)", _value, _value2)
        );
        require(success, "Delegate call to set public value failed.");
    }
}

우선 immutable, constant, public을 각각 컨트렉트에 동일한 변수 위치에 세팅을하고 변수명도 같게해준다 테스트를 위해서

하지만 변수 값은 다르게 해준다.

코드를 살펴보면 caller contract에서

immutable = 100

constant = 300

public1 = 500 

public2 = 600 으로 선언을 해줬다.

 

calledcontract에는 caller와 순서도 변수도 다르게 선언한다.

calleGetValueds를 실행하였을때 값은

 

이 말이 무엇이냐

immutable, constant 값은 called contract

public1, public2는 caller sotrage slot에서 가져온단 말이다. 

 

이유는 immutable, constant 는 called의 contract code에서 불러오고 

public1, public2는 sotrage slot을 참고하여 caller의 변수를 참조하기 때문이다.

 

 

Constant

 

- constant 키워드는 컴파일 시간에 값이 결정되며, 이후에는 변경할 수 없는 변수를 선언하는 데 사용된다

- 주로 고정된 값을 가진 설정에 사용된다. 예를 들어, 수수료율이나 버전 번호 등

- constant 변수는 스마트 컨트랙트의 바이트코드에 직접 하드코딩되어 스토리지를 사용하지 않는다

 

Immutable

 

- immutable 키워드는 컨트랙트가 생성될 때 한 번만 설정되고, 그 후에는 변경할 수 없는 변수를 선언하는 데 사용된다.

- 초기화는 생성자에서 이루어지며, 이후에는 읽기만 가능하다.

- immutable 변수도 스토리지를 사용하지 않고, 컨트랙트의 바이트코드에 삽입되어 가스 비용을 절약한다

 

솔리디티에서는 객체와 같은 복잡한 데이터 구조를 다루는 경우주로 구조체(Structs) 배열(Arrays) 사용한다이러한 데이터 구조는 storage memory 저장될  있으며, storage 저장된 데이터는 변경 가능하거나 불변일  있다그러나 constant immutable 같은 키워드는 이러한 복잡한 데이터 구조에 직접 적용되지 않는다.

 

'솔리디티' 카테고리의 다른 글

openzzeplin 5V => upgrade contract 분석하기  (1) 2024.01.20
업그레이더블 컨트랙트 정리  (0) 2023.07.19
ERC-4337 , Account Abstraction  (0) 2023.04.04
EIP-4337 이해하기  (0) 2023.03.22
call vs delegate call 알아보기  (0) 2023.01.04

댓글