Proxy

Solidity

Définition

Pattern permettant de rendre un smart contract upgradable. Le proxy délègue tous les appels à un contrat d'implémentation via delegatecall. Pour upgrader, il suffit de pointer vers une nouvelle implémentation. Standards : EIP-1967 (Transparent Proxy), UUPS (Universal Upgradeable Proxy Standard), Beacon Proxy. Attention aux risques de storage collision.

Version anglaise

Pattern allowing smart contract upgradeability. Proxy delegates all calls to an implementation contract via delegatecall. To upgrade, simply point to a new implementation. Standards: EIP-1967 (Transparent Proxy), UUPS (Universal Upgradeable Proxy Standard), Beacon Proxy. Beware of storage collision risks.

Exemple de Code

// Transparent Proxy (simplifié)
contract TransparentProxy {
  address public implementation;
  address public admin;

  constructor(address _implementation) {
    implementation = _implementation;
    admin = msg.sender;
  }

  fallback() external payable {
    address impl = implementation;
    assembly {
      calldatacopy(0, 0, calldatasize())
      let result := delegatecall(gas(), impl, 0, calldatasize(), 0, 0)
      returndatacopy(0, 0, returndatasize())
      switch result
      case 0 { revert(0, returndatasize()) }
      default { return(0, returndatasize()) }
    }
  }

  function upgrade(address newImplementation) external {
    require(msg.sender == admin, "Not admin");
    implementation = newImplementation;
  }
}

Pratique ce concept sur Solingo

Maîtrise Proxy avec des exercices interactifs et un IDE intégré.

Commencer gratuitement