概要

AbstractV2Upgraderコントラクトは、FiatTokenProxyをv2系実装に安全にアップグレードする際の共通ロジックを提供する抽象コントラクトです。 オーナー権限の管理、Proxy・Logicアドレスの参照、残高引き出し、アップグレード中止・後始末機能をまとめています。

コード

text
/**
 * Copyright 2023 Circle Internet Group, Inc. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

pragma solidity 0.6.12;

import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { Ownable } from "../../v1/Ownable.sol";
import { FiatTokenProxy } from "../../v1/FiatTokenProxy.sol";
import { AbstractUpgraderHelper } from "./helpers/AbstractUpgraderHelper.sol";

/**
 * @dev An abstract contract to encapsulate any common logic
 * for any V2+ Upgrader contracts.
 */
abstract contract AbstractV2Upgrader is Ownable {
    using SafeMath for uint256;

    FiatTokenProxy internal _proxy;
    address internal _implementation;
    address internal _newProxyAdmin;
    AbstractUpgraderHelper internal _helper;

    /**
     * @notice Constructor
     * @param proxy             FiatTokenProxy contract
     * @param implementation    Address of the implementation contract
     * @param newProxyAdmin     Grantee of proxy admin role after upgrade
     */
    constructor(
        FiatTokenProxy proxy,
        address implementation,
        address newProxyAdmin
    ) public Ownable() {
        _proxy = proxy;
        _implementation = implementation;
        _newProxyAdmin = newProxyAdmin;
    }

    /**
     * @notice The address of the FiatTokenProxy contract
     * @return Contract address
     */
    function proxy() external view returns (address) {
        return address(_proxy);
    }

    /**
     * @notice The address of the FiatTokenV2 implementation contract
     * @return Contract address
     */
    function implementation() external view returns (address) {
        return _implementation;
    }

    /**
     * @notice The address of the V2UpgraderHelper contract
     * @return Contract address
     */
    function helper() external view returns (address) {
        return address(_helper);
    }

    /**
     * @notice The address to which the proxy admin role will be transferred
     * after the upgrade is completed
     * @return Address
     */
    function newProxyAdmin() external view returns (address) {
        return _newProxyAdmin;
    }

    /**
     * @notice Withdraw any FiatToken in the contract
     */
    function withdrawFiatToken() public onlyOwner {
        IERC20 fiatToken = IERC20(address(_proxy));
        uint256 balance = fiatToken.balanceOf(address(this));
        if (balance > 0) {
            require(
                fiatToken.transfer(msg.sender, balance),
                "Failed to withdraw FiatToken"
            );
        }
    }

    /**
     * @notice Transfer proxy admin role to newProxyAdmin, and self-destruct
     */
    function abortUpgrade() external onlyOwner {
        // Transfer proxy admin role
        _proxy.changeAdmin(_newProxyAdmin);

        // Tear down
        tearDown();
    }

    /**
     * @dev Tears down the helper contract followed by this contract.
     */
    function tearDown() internal {
        _helper.tearDown();
        selfdestruct(msg.sender);
    }
}

変数

solidity
FiatTokenProxy internal _proxy

アップグレード対象のプロキシコントラクトインスタンス。

solidity
address internal _implementation

新しい実装コントラクトのアドレス。

solidity
address internal _newProxyAdmin

アップグレード後にプロキシ管理者権限を与えるアドレス。

solidity
AbstractUpgraderHelper internal _helper

Proxyコントラクトから情報を取得する補助するヘルパーコントラクト。

関数

constructor

solidity
constructor(
    FiatTokenProxy proxy,
    address implementation,
    address newProxyAdmin
) public Ownable() {
    _proxy = proxy;
    _implementation = implementation;
    _newProxyAdmin = newProxyAdmin;
}

コントラクトの初期化を行い、アップグレード対象Proxy・Logicアドレス・新しい管理者を設定します。

引数

  • proxy
    • アップグレード対象のFiatTokenProxyインスタンス。
  • implementation
    • 新しいLogicコントラクト(FiatTokenV2)のアドレス。
  • newProxyAdmin
    • アップグレード後にProxy管理者権限を付与するアドレス。

proxy

solidity
function proxy() external view returns (address) {
    return address(_proxy);
}

Proxyコントラクトのアドレスを返す関数。

詳細

  • 内部変数 _proxy をアドレス型にキャストして返却します。

戻り値

  • address
    • Proxyコントラクトのアドレス。

implementation

solidity
function implementation() external view returns (address) {
    return _implementation;
}

Logicコントラクトのアドレスを返す関数。

戻り値

  • address
    • 新しいLogicコントラクトのアドレス。

helper

solidity
function helper() external view returns (address) {
    return address(_helper);
}

Proxyコントラクト内の情報を取得するヘルパーコントラクトのアドレスを返す関数。

戻り値

  • address
    • アップグレード中に使用されるヘルパーコントラクトのアドレス。

newProxyAdmin

solidity
function newProxyAdmin() external view returns (address) {
    return _newProxyAdmin;
}

アップグレード後に付与予定のProxy管理者アドレスを返す関数。

戻り値

  • address
    • 付与先のアドレス。

withdrawFiatToken

solidity
function withdrawFiatToken() public onlyOwner {
    IERC20 fiatToken = IERC20(address(_proxy));
    uint256 balance = fiatToken.balanceOf(address(this));
    if (balance > 0) {
        require(
            fiatToken.transfer(msg.sender, balance),
            "Failed to withdraw FiatToken"
        );
    }
}

コントラクトに残ったFiatTokenをオーナーに送る関数。

  • onlyOwner によりオーナーのみ実行可能です。
  • ProxyのERC20インターフェースを使い、自身の残高を取得します。
  • 残高があれば transfer でオーナーへ送付し、失敗時は revert します。

abortUpgrade

solidity
function abortUpgrade() external onlyOwner {
    _proxy.changeAdmin(_newProxyAdmin);
    tearDown();
}

アップグレードを中止し、Proxy管理者を移譲してコントラクトを破棄する関数。

  • onlyOwner のみ実行可能です。
  • changeAdmin を呼び出してプロキシの管理者権限を設定済みアドレスへ移譲します。
  • その後、tearDown 関数を呼び、ヘルパーコントラクトとコントラクト自信を破棄します。

tearDown

solidity
function tearDown() internal {
    _helper.tearDown();
    selfdestruct(msg.sender);
}

ヘルパーコントラクトの破棄と、コントラクト自体の破棄を順に実行する関数。

  • まずヘルパーコントラクトの tearDown() を呼び、ヘルパーを破棄します。
  • その後、 selfdestruct によりコントラクトを破棄し、コントラクト内のETHを呼び出し元(オーナー)に送金します。