ETH Price: $2,405.96 (+6.78%)

Transaction Decoder

Block:
21497359 at Dec-28-2024 12:24:47 AM +UTC
Transaction Fee:
0.000205868495621124 ETH $0.50
Gas Used:
55,818 Gas / 3.688209818 Gwei

Emitted Events:

473 TransparentUpgradeableProxy.0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925( 0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925, 0x000000000000000000000000bb78cdddd4c3e2436d8631891ccb57b9271706d6, 0x0000000000000000000000000000000000001ff3684f28c67538d4d072c22734, 0000000000000000000000000000000000000000000000027a34e7e0719bec00 )

Account State Difference:

  Address   Before After State Difference Code
0x16631e53...2929b285C
(Titan Builder)
9.973738660863960681 Eth9.973744242663960681 Eth0.0000055818
0xbB78cdDD...9271706D6
0.009151384180480812 Eth
Nonce: 13
0.008945515684859688 Eth
Nonce: 14
0.000205868495621124

Execution Trace

TransparentUpgradeableProxy.095ea7b3( )
  • PToken.approve( spender=0x0000000000001fF3684f28c67538d4D072C22734, value=45699406270000000000 ) => ( True )
    File 1 of 2: TransparentUpgradeableProxy
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
    import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
    import "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol";
    // Kept for backwards compatibility with older versions of Hardhat and Truffle plugins.
    contract AdminUpgradeabilityProxy is TransparentUpgradeableProxy {
        constructor(address logic, address admin, bytes memory data) payable TransparentUpgradeableProxy(logic, admin, data) {}
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    import "../Proxy.sol";
    import "./ERC1967Upgrade.sol";
    /**
     * @dev This contract implements an upgradeable proxy. It is upgradeable because calls are delegated to an
     * implementation address that can be changed. This address is stored in storage in the location specified by
     * https://eips.ethereum.org/EIPS/eip-1967[EIP1967], so that it doesn't conflict with the storage layout of the
     * implementation behind the proxy.
     */
    contract ERC1967Proxy is Proxy, ERC1967Upgrade {
        /**
         * @dev Initializes the upgradeable proxy with an initial implementation specified by `_logic`.
         *
         * If `_data` is nonempty, it's used as data in a delegate call to `_logic`. This will typically be an encoded
         * function call, and allows initializating the storage of the proxy like a Solidity constructor.
         */
        constructor(address _logic, bytes memory _data) payable {
            assert(_IMPLEMENTATION_SLOT == bytes32(uint256(keccak256("eip1967.proxy.implementation")) - 1));
            _upgradeToAndCall(_logic, _data, false);
        }
        /**
         * @dev Returns the current implementation address.
         */
        function _implementation() internal view virtual override returns (address impl) {
            return ERC1967Upgrade._getImplementation();
        }
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    import "../ERC1967/ERC1967Proxy.sol";
    /**
     * @dev This contract implements a proxy that is upgradeable by an admin.
     *
     * To avoid https://medium.com/nomic-labs-blog/malicious-backdoors-in-ethereum-proxies-62629adf3357[proxy selector
     * clashing], which can potentially be used in an attack, this contract uses the
     * https://blog.openzeppelin.com/the-transparent-proxy-pattern/[transparent proxy pattern]. This pattern implies two
     * things that go hand in hand:
     *
     * 1. If any account other than the admin calls the proxy, the call will be forwarded to the implementation, even if
     * that call matches one of the admin functions exposed by the proxy itself.
     * 2. If the admin calls the proxy, it can access the admin functions, but its calls will never be forwarded to the
     * implementation. If the admin tries to call a function on the implementation it will fail with an error that says
     * "admin cannot fallback to proxy target".
     *
     * These properties mean that the admin account can only be used for admin actions like upgrading the proxy or changing
     * the admin, so it's best if it's a dedicated account that is not used for anything else. This will avoid headaches due
     * to sudden errors when trying to call a function from the proxy implementation.
     *
     * Our recommendation is for the dedicated account to be an instance of the {ProxyAdmin} contract. If set up this way,
     * you should think of the `ProxyAdmin` instance as the real administrative interface of your proxy.
     */
    contract TransparentUpgradeableProxy is ERC1967Proxy {
        /**
         * @dev Initializes an upgradeable proxy managed by `_admin`, backed by the implementation at `_logic`, and
         * optionally initialized with `_data` as explained in {ERC1967Proxy-constructor}.
         */
        constructor(address _logic, address admin_, bytes memory _data) payable ERC1967Proxy(_logic, _data) {
            assert(_ADMIN_SLOT == bytes32(uint256(keccak256("eip1967.proxy.admin")) - 1));
            _changeAdmin(admin_);
        }
        /**
         * @dev Modifier used internally that will delegate the call to the implementation unless the sender is the admin.
         */
        modifier ifAdmin() {
            if (msg.sender == _getAdmin()) {
                _;
            } else {
                _fallback();
            }
        }
        /**
         * @dev Returns the current admin.
         *
         * NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyAdmin}.
         *
         * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
         * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
         * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
         */
        function admin() external ifAdmin returns (address admin_) {
            admin_ = _getAdmin();
        }
        /**
         * @dev Returns the current implementation.
         *
         * NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyImplementation}.
         *
         * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
         * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
         * `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`
         */
        function implementation() external ifAdmin returns (address implementation_) {
            implementation_ = _implementation();
        }
        /**
         * @dev Changes the admin of the proxy.
         *
         * Emits an {AdminChanged} event.
         *
         * NOTE: Only the admin can call this function. See {ProxyAdmin-changeProxyAdmin}.
         */
        function changeAdmin(address newAdmin) external virtual ifAdmin {
            _changeAdmin(newAdmin);
        }
        /**
         * @dev Upgrade the implementation of the proxy.
         *
         * NOTE: Only the admin can call this function. See {ProxyAdmin-upgrade}.
         */
        function upgradeTo(address newImplementation) external ifAdmin {
            _upgradeToAndCall(newImplementation, bytes(""), false);
        }
        /**
         * @dev Upgrade the implementation of the proxy, and then call a function from the new implementation as specified
         * by `data`, which should be an encoded function call. This is useful to initialize new storage variables in the
         * proxied contract.
         *
         * NOTE: Only the admin can call this function. See {ProxyAdmin-upgradeAndCall}.
         */
        function upgradeToAndCall(address newImplementation, bytes calldata data) external payable ifAdmin {
            _upgradeToAndCall(newImplementation, data, true);
        }
        /**
         * @dev Returns the current admin.
         */
        function _admin() internal view virtual returns (address) {
            return _getAdmin();
        }
        /**
         * @dev Makes sure the admin cannot access the fallback function. See {Proxy-_beforeFallback}.
         */
        function _beforeFallback() internal virtual override {
            require(msg.sender != _getAdmin(), "TransparentUpgradeableProxy: admin cannot fallback to proxy target");
            super._beforeFallback();
        }
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    import "./TransparentUpgradeableProxy.sol";
    import "../../access/Ownable.sol";
    /**
     * @dev This is an auxiliary contract meant to be assigned as the admin of a {TransparentUpgradeableProxy}. For an
     * explanation of why you would want to use this see the documentation for {TransparentUpgradeableProxy}.
     */
    contract ProxyAdmin is Ownable {
        /**
         * @dev Returns the current implementation of `proxy`.
         *
         * Requirements:
         *
         * - This contract must be the admin of `proxy`.
         */
        function getProxyImplementation(TransparentUpgradeableProxy proxy) public view virtual returns (address) {
            // We need to manually run the static call since the getter cannot be flagged as view
            // bytes4(keccak256("implementation()")) == 0x5c60da1b
            (bool success, bytes memory returndata) = address(proxy).staticcall(hex"5c60da1b");
            require(success);
            return abi.decode(returndata, (address));
        }
        /**
         * @dev Returns the current admin of `proxy`.
         *
         * Requirements:
         *
         * - This contract must be the admin of `proxy`.
         */
        function getProxyAdmin(TransparentUpgradeableProxy proxy) public view virtual returns (address) {
            // We need to manually run the static call since the getter cannot be flagged as view
            // bytes4(keccak256("admin()")) == 0xf851a440
            (bool success, bytes memory returndata) = address(proxy).staticcall(hex"f851a440");
            require(success);
            return abi.decode(returndata, (address));
        }
        /**
         * @dev Changes the admin of `proxy` to `newAdmin`.
         *
         * Requirements:
         *
         * - This contract must be the current admin of `proxy`.
         */
        function changeProxyAdmin(TransparentUpgradeableProxy proxy, address newAdmin) public virtual onlyOwner {
            proxy.changeAdmin(newAdmin);
        }
        /**
         * @dev Upgrades `proxy` to `implementation`. See {TransparentUpgradeableProxy-upgradeTo}.
         *
         * Requirements:
         *
         * - This contract must be the admin of `proxy`.
         */
        function upgrade(TransparentUpgradeableProxy proxy, address implementation) public virtual onlyOwner {
            proxy.upgradeTo(implementation);
        }
        /**
         * @dev Upgrades `proxy` to `implementation` and calls a function on the new implementation. See
         * {TransparentUpgradeableProxy-upgradeToAndCall}.
         *
         * Requirements:
         *
         * - This contract must be the admin of `proxy`.
         */
        function upgradeAndCall(TransparentUpgradeableProxy proxy, address implementation, bytes memory data) public payable virtual onlyOwner {
            proxy.upgradeToAndCall{value: msg.value}(implementation, data);
        }
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    /**
     * @dev This abstract contract provides a fallback function that delegates all calls to another contract using the EVM
     * instruction `delegatecall`. We refer to the second contract as the _implementation_ behind the proxy, and it has to
     * be specified by overriding the virtual {_implementation} function.
     *
     * Additionally, delegation to the implementation can be triggered manually through the {_fallback} function, or to a
     * different contract through the {_delegate} function.
     *
     * The success and return data of the delegated call will be returned back to the caller of the proxy.
     */
    abstract contract Proxy {
        /**
         * @dev Delegates the current call to `implementation`.
         *
         * This function does not return to its internall call site, it will return directly to the external caller.
         */
        function _delegate(address implementation) internal virtual {
            // solhint-disable-next-line no-inline-assembly
            assembly {
                // Copy msg.data. We take full control of memory in this inline assembly
                // block because it will not return to Solidity code. We overwrite the
                // Solidity scratch pad at memory position 0.
                calldatacopy(0, 0, calldatasize())
                // Call the implementation.
                // out and outsize are 0 because we don't know the size yet.
                let result := delegatecall(gas(), implementation, 0, calldatasize(), 0, 0)
                // Copy the returned data.
                returndatacopy(0, 0, returndatasize())
                switch result
                // delegatecall returns 0 on error.
                case 0 { revert(0, returndatasize()) }
                default { return(0, returndatasize()) }
            }
        }
        /**
         * @dev This is a virtual function that should be overriden so it returns the address to which the fallback function
         * and {_fallback} should delegate.
         */
        function _implementation() internal view virtual returns (address);
        /**
         * @dev Delegates the current call to the address returned by `_implementation()`.
         *
         * This function does not return to its internall call site, it will return directly to the external caller.
         */
        function _fallback() internal virtual {
            _beforeFallback();
            _delegate(_implementation());
        }
        /**
         * @dev Fallback function that delegates calls to the address returned by `_implementation()`. Will run if no other
         * function in the contract matches the call data.
         */
        fallback () external payable virtual {
            _fallback();
        }
        /**
         * @dev Fallback function that delegates calls to the address returned by `_implementation()`. Will run if call data
         * is empty.
         */
        receive () external payable virtual {
            _fallback();
        }
        /**
         * @dev Hook that is called before falling back to the implementation. Can happen as part of a manual `_fallback`
         * call, or as part of the Solidity `fallback` or `receive` functions.
         *
         * If overriden should call `super._beforeFallback()`.
         */
        function _beforeFallback() internal virtual {
        }
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.2;
    import "../beacon/IBeacon.sol";
    import "../../utils/Address.sol";
    import "../../utils/StorageSlot.sol";
    /**
     * @dev This abstract contract provides getters and event emitting update functions for
     * https://eips.ethereum.org/EIPS/eip-1967[EIP1967] slots.
     *
     * _Available since v4.1._
     *
     * @custom:oz-upgrades-unsafe-allow delegatecall
     */
    abstract contract ERC1967Upgrade {
        // This is the keccak-256 hash of "eip1967.proxy.rollback" subtracted by 1
        bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;
        /**
         * @dev Storage slot with the address of the current implementation.
         * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
         * validated in the constructor.
         */
        bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
        /**
         * @dev Emitted when the implementation is upgraded.
         */
        event Upgraded(address indexed implementation);
        /**
         * @dev Returns the current implementation address.
         */
        function _getImplementation() internal view returns (address) {
            return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
        }
        /**
         * @dev Stores a new address in the EIP1967 implementation slot.
         */
        function _setImplementation(address newImplementation) private {
            require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
            StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
        }
        /**
         * @dev Perform implementation upgrade
         *
         * Emits an {Upgraded} event.
         */
        function _upgradeTo(address newImplementation) internal {
            _setImplementation(newImplementation);
            emit Upgraded(newImplementation);
        }
        /**
         * @dev Perform implementation upgrade with additional setup call.
         *
         * Emits an {Upgraded} event.
         */
        function _upgradeToAndCall(address newImplementation, bytes memory data, bool forceCall) internal {
            _setImplementation(newImplementation);
            emit Upgraded(newImplementation);
            if (data.length > 0 || forceCall) {
                Address.functionDelegateCall(newImplementation, data);
            }
        }
        /**
         * @dev Perform implementation upgrade with security checks for UUPS proxies, and additional setup call.
         *
         * Emits an {Upgraded} event.
         */
        function _upgradeToAndCallSecure(address newImplementation, bytes memory data, bool forceCall) internal {
            address oldImplementation = _getImplementation();
            // Initial upgrade and setup call
            _setImplementation(newImplementation);
            if (data.length > 0 || forceCall) {
                Address.functionDelegateCall(newImplementation, data);
            }
            // Perform rollback test if not already in progress
            StorageSlot.BooleanSlot storage rollbackTesting = StorageSlot.getBooleanSlot(_ROLLBACK_SLOT);
            if (!rollbackTesting.value) {
                // Trigger rollback using upgradeTo from the new implementation
                rollbackTesting.value = true;
                Address.functionDelegateCall(
                    newImplementation,
                    abi.encodeWithSignature(
                        "upgradeTo(address)",
                        oldImplementation
                    )
                );
                rollbackTesting.value = false;
                // Check rollback was effective
                require(oldImplementation == _getImplementation(), "ERC1967Upgrade: upgrade breaks further upgrades");
                // Finally reset to the new implementation and log the upgrade
                _setImplementation(newImplementation);
                emit Upgraded(newImplementation);
            }
        }
        /**
         * @dev Perform beacon upgrade with additional setup call. Note: This upgrades the address of the beacon, it does
         * not upgrade the implementation contained in the beacon (see {UpgradeableBeacon-_setImplementation} for that).
         *
         * Emits a {BeaconUpgraded} event.
         */
        function _upgradeBeaconToAndCall(address newBeacon, bytes memory data, bool forceCall) internal {
            _setBeacon(newBeacon);
            emit BeaconUpgraded(newBeacon);
            if (data.length > 0 || forceCall) {
                Address.functionDelegateCall(IBeacon(newBeacon).implementation(), data);
            }
        }
        /**
         * @dev Storage slot with the admin of the contract.
         * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
         * validated in the constructor.
         */
        bytes32 internal constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
        /**
         * @dev Emitted when the admin account has changed.
         */
        event AdminChanged(address previousAdmin, address newAdmin);
        /**
         * @dev Returns the current admin.
         */
        function _getAdmin() internal view returns (address) {
            return StorageSlot.getAddressSlot(_ADMIN_SLOT).value;
        }
        /**
         * @dev Stores a new address in the EIP1967 admin slot.
         */
        function _setAdmin(address newAdmin) private {
            require(newAdmin != address(0), "ERC1967: new admin is the zero address");
            StorageSlot.getAddressSlot(_ADMIN_SLOT).value = newAdmin;
        }
        /**
         * @dev Changes the admin of the proxy.
         *
         * Emits an {AdminChanged} event.
         */
        function _changeAdmin(address newAdmin) internal {
            emit AdminChanged(_getAdmin(), newAdmin);
            _setAdmin(newAdmin);
        }
        /**
         * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
         * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)) and is validated in the constructor.
         */
        bytes32 internal constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;
        /**
         * @dev Emitted when the beacon is upgraded.
         */
        event BeaconUpgraded(address indexed beacon);
        /**
         * @dev Returns the current beacon.
         */
        function _getBeacon() internal view returns (address) {
            return StorageSlot.getAddressSlot(_BEACON_SLOT).value;
        }
        /**
         * @dev Stores a new beacon in the EIP1967 beacon slot.
         */
        function _setBeacon(address newBeacon) private {
            require(
                Address.isContract(newBeacon),
                "ERC1967: new beacon is not a contract"
            );
            require(
                Address.isContract(IBeacon(newBeacon).implementation()),
                "ERC1967: beacon implementation is not a contract"
            );
            StorageSlot.getAddressSlot(_BEACON_SLOT).value = newBeacon;
        }
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    /**
     * @dev This is the interface that {BeaconProxy} expects of its beacon.
     */
    interface IBeacon {
        /**
         * @dev Must return an address that can be used as a delegate call target.
         *
         * {BeaconProxy} will check that this address is a contract.
         */
        function implementation() external view returns (address);
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    /**
     * @dev Collection of functions related to the address type
     */
    library Address {
        /**
         * @dev Returns true if `account` is a contract.
         *
         * [IMPORTANT]
         * ====
         * It is unsafe to assume that an address for which this function returns
         * false is an externally-owned account (EOA) and not a contract.
         *
         * Among others, `isContract` will return false for the following
         * types of addresses:
         *
         *  - an externally-owned account
         *  - a contract in construction
         *  - an address where a contract will be created
         *  - an address where a contract lived, but was destroyed
         * ====
         */
        function isContract(address account) internal view returns (bool) {
            // This method relies on extcodesize, which returns 0 for contracts in
            // construction, since the code is only stored at the end of the
            // constructor execution.
            uint256 size;
            // solhint-disable-next-line no-inline-assembly
            assembly { size := extcodesize(account) }
            return size > 0;
        }
        /**
         * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
         * `recipient`, forwarding all available gas and reverting on errors.
         *
         * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
         * of certain opcodes, possibly making contracts go over the 2300 gas limit
         * imposed by `transfer`, making them unable to receive funds via
         * `transfer`. {sendValue} removes this limitation.
         *
         * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
         *
         * IMPORTANT: because control is transferred to `recipient`, care must be
         * taken to not create reentrancy vulnerabilities. Consider using
         * {ReentrancyGuard} or the
         * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
         */
        function sendValue(address payable recipient, uint256 amount) internal {
            require(address(this).balance >= amount, "Address: insufficient balance");
            // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
            (bool success, ) = recipient.call{ value: amount }("");
            require(success, "Address: unable to send value, recipient may have reverted");
        }
        /**
         * @dev Performs a Solidity function call using a low level `call`. A
         * plain`call` is an unsafe replacement for a function call: use this
         * function instead.
         *
         * If `target` reverts with a revert reason, it is bubbled up by this
         * function (like regular Solidity function calls).
         *
         * Returns the raw returned data. To convert to the expected return value,
         * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
         *
         * Requirements:
         *
         * - `target` must be a contract.
         * - calling `target` with `data` must not revert.
         *
         * _Available since v3.1._
         */
        function functionCall(address target, bytes memory data) internal returns (bytes memory) {
          return functionCall(target, data, "Address: low-level call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
         * `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
            return functionCallWithValue(target, data, 0, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but also transferring `value` wei to `target`.
         *
         * Requirements:
         *
         * - the calling contract must have an ETH balance of at least `value`.
         * - the called Solidity function must be `payable`.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
            return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
        }
        /**
         * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
         * with `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
            require(address(this).balance >= value, "Address: insufficient balance for call");
            require(isContract(target), "Address: call to non-contract");
            // solhint-disable-next-line avoid-low-level-calls
            (bool success, bytes memory returndata) = target.call{ value: value }(data);
            return _verifyCallResult(success, returndata, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
            return functionStaticCall(target, data, "Address: low-level static call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(address target, bytes memory data, string memory errorMessage) internal view returns (bytes memory) {
            require(isContract(target), "Address: static call to non-contract");
            // solhint-disable-next-line avoid-low-level-calls
            (bool success, bytes memory returndata) = target.staticcall(data);
            return _verifyCallResult(success, returndata, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but performing a delegate call.
         *
         * _Available since v3.4._
         */
        function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
            return functionDelegateCall(target, data, "Address: low-level delegate call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
         * but performing a delegate call.
         *
         * _Available since v3.4._
         */
        function functionDelegateCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
            require(isContract(target), "Address: delegate call to non-contract");
            // solhint-disable-next-line avoid-low-level-calls
            (bool success, bytes memory returndata) = target.delegatecall(data);
            return _verifyCallResult(success, returndata, errorMessage);
        }
        function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private pure returns(bytes memory) {
            if (success) {
                return returndata;
            } else {
                // Look for revert reason and bubble it up if present
                if (returndata.length > 0) {
                    // The easiest way to bubble the revert reason is using memory via assembly
                    // solhint-disable-next-line no-inline-assembly
                    assembly {
                        let returndata_size := mload(returndata)
                        revert(add(32, returndata), returndata_size)
                    }
                } else {
                    revert(errorMessage);
                }
            }
        }
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    /**
     * @dev Library for reading and writing primitive types to specific storage slots.
     *
     * Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
     * This library helps with reading and writing to such slots without the need for inline assembly.
     *
     * The functions in this library return Slot structs that contain a `value` member that can be used to read or write.
     *
     * Example usage to set ERC1967 implementation slot:
     * ```
     * contract ERC1967 {
     *     bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
     *
     *     function _getImplementation() internal view returns (address) {
     *         return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
     *     }
     *
     *     function _setImplementation(address newImplementation) internal {
     *         require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
     *         StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
     *     }
     * }
     * ```
     *
     * _Available since v4.1 for `address`, `bool`, `bytes32`, and `uint256`._
     */
    library StorageSlot {
        struct AddressSlot {
            address value;
        }
        struct BooleanSlot {
            bool value;
        }
        struct Bytes32Slot {
            bytes32 value;
        }
        struct Uint256Slot {
            uint256 value;
        }
        /**
         * @dev Returns an `AddressSlot` with member `value` located at `slot`.
         */
        function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
            assembly {
                r.slot := slot
            }
        }
        /**
         * @dev Returns an `BooleanSlot` with member `value` located at `slot`.
         */
        function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
            assembly {
                r.slot := slot
            }
        }
        /**
         * @dev Returns an `Bytes32Slot` with member `value` located at `slot`.
         */
        function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
            assembly {
                r.slot := slot
            }
        }
        /**
         * @dev Returns an `Uint256Slot` with member `value` located at `slot`.
         */
        function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
            assembly {
                r.slot := slot
            }
        }
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    import "../utils/Context.sol";
    /**
     * @dev Contract module which provides a basic access control mechanism, where
     * there is an account (an owner) that can be granted exclusive access to
     * specific functions.
     *
     * By default, the owner account will be the one that deploys the contract. This
     * can later be changed with {transferOwnership}.
     *
     * This module is used through inheritance. It will make available the modifier
     * `onlyOwner`, which can be applied to your functions to restrict their use to
     * the owner.
     */
    abstract contract Ownable is Context {
        address private _owner;
        event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
        /**
         * @dev Initializes the contract setting the deployer as the initial owner.
         */
        constructor () {
            address msgSender = _msgSender();
            _owner = msgSender;
            emit OwnershipTransferred(address(0), msgSender);
        }
        /**
         * @dev Returns the address of the current owner.
         */
        function owner() public view virtual returns (address) {
            return _owner;
        }
        /**
         * @dev Throws if called by any account other than the owner.
         */
        modifier onlyOwner() {
            require(owner() == _msgSender(), "Ownable: caller is not the owner");
            _;
        }
        /**
         * @dev Leaves the contract without owner. It will not be possible to call
         * `onlyOwner` functions anymore. Can only be called by the current owner.
         *
         * NOTE: Renouncing ownership will leave the contract without an owner,
         * thereby removing any functionality that is only available to the owner.
         */
        function renounceOwnership() public virtual onlyOwner {
            emit OwnershipTransferred(_owner, address(0));
            _owner = address(0);
        }
        /**
         * @dev Transfers ownership of the contract to a new account (`newOwner`).
         * Can only be called by the current owner.
         */
        function transferOwnership(address newOwner) public virtual onlyOwner {
            require(newOwner != address(0), "Ownable: new owner is the zero address");
            emit OwnershipTransferred(_owner, newOwner);
            _owner = newOwner;
        }
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    /*
     * @dev Provides information about the current execution context, including the
     * sender of the transaction and its data. While these are generally available
     * via msg.sender and msg.data, they should not be accessed in such a direct
     * manner, since when dealing with meta-transactions the account sending and
     * paying for execution may not be the actual sender (as far as an application
     * is concerned).
     *
     * This contract is only required for intermediate, library-like contracts.
     */
    abstract contract Context {
        function _msgSender() internal view virtual returns (address) {
            return msg.sender;
        }
        function _msgData() internal view virtual returns (bytes calldata) {
            this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
            return msg.data;
        }
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    import "../ERC1967/ERC1967Upgrade.sol";
    /**
     * @dev Base contract for building openzeppelin-upgrades compatible implementations for the {ERC1967Proxy}. It includes
     * publicly available upgrade functions that are called by the plugin and by the secure upgrade mechanism to verify
     * continuation of the upgradability.
     *
     * The {_authorizeUpgrade} function MUST be overridden to include access restriction to the upgrade mechanism.
     *
     * _Available since v4.1._
     */
    abstract contract UUPSUpgradeable is ERC1967Upgrade {
        function upgradeTo(address newImplementation) external virtual {
            _authorizeUpgrade(newImplementation);
            _upgradeToAndCallSecure(newImplementation, bytes(""), false);
        }
        function upgradeToAndCall(address newImplementation, bytes memory data) external payable virtual {
            _authorizeUpgrade(newImplementation);
            _upgradeToAndCallSecure(newImplementation, data, true);
        }
        function _authorizeUpgrade(address newImplementation) internal virtual;
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.2;
    import "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";
    abstract contract Proxiable is UUPSUpgradeable {
        function _authorizeUpgrade(address newImplementation) internal override {
            _beforeUpgrade(newImplementation);
        }
        function _beforeUpgrade(address newImplementation) internal virtual;
    }
    contract ChildOfProxiable is Proxiable {
        function _beforeUpgrade(address newImplementation) internal virtual override {}
    }
    

    File 2 of 2: PToken
    // File: @openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.2 <0.8.0;
    
    /**
     * @dev Collection of functions related to the address type
     */
    library AddressUpgradeable {
        /**
         * @dev Returns true if `account` is a contract.
         *
         * [IMPORTANT]
         * ====
         * It is unsafe to assume that an address for which this function returns
         * false is an externally-owned account (EOA) and not a contract.
         *
         * Among others, `isContract` will return false for the following
         * types of addresses:
         *
         *  - an externally-owned account
         *  - a contract in construction
         *  - an address where a contract will be created
         *  - an address where a contract lived, but was destroyed
         * ====
         */
        function isContract(address account) internal view returns (bool) {
            // This method relies on extcodesize, which returns 0 for contracts in
            // construction, since the code is only stored at the end of the
            // constructor execution.
    
            uint256 size;
            // solhint-disable-next-line no-inline-assembly
            assembly { size := extcodesize(account) }
            return size > 0;
        }
    
        /**
         * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
         * `recipient`, forwarding all available gas and reverting on errors.
         *
         * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
         * of certain opcodes, possibly making contracts go over the 2300 gas limit
         * imposed by `transfer`, making them unable to receive funds via
         * `transfer`. {sendValue} removes this limitation.
         *
         * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
         *
         * IMPORTANT: because control is transferred to `recipient`, care must be
         * taken to not create reentrancy vulnerabilities. Consider using
         * {ReentrancyGuard} or the
         * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
         */
        function sendValue(address payable recipient, uint256 amount) internal {
            require(address(this).balance >= amount, "Address: insufficient balance");
    
            // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
            (bool success, ) = recipient.call{ value: amount }("");
            require(success, "Address: unable to send value, recipient may have reverted");
        }
    
        /**
         * @dev Performs a Solidity function call using a low level `call`. A
         * plain`call` is an unsafe replacement for a function call: use this
         * function instead.
         *
         * If `target` reverts with a revert reason, it is bubbled up by this
         * function (like regular Solidity function calls).
         *
         * Returns the raw returned data. To convert to the expected return value,
         * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
         *
         * Requirements:
         *
         * - `target` must be a contract.
         * - calling `target` with `data` must not revert.
         *
         * _Available since v3.1._
         */
        function functionCall(address target, bytes memory data) internal returns (bytes memory) {
          return functionCall(target, data, "Address: low-level call failed");
        }
    
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
         * `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
            return functionCallWithValue(target, data, 0, errorMessage);
        }
    
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but also transferring `value` wei to `target`.
         *
         * Requirements:
         *
         * - the calling contract must have an ETH balance of at least `value`.
         * - the called Solidity function must be `payable`.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
            return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
        }
    
        /**
         * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
         * with `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
            require(address(this).balance >= value, "Address: insufficient balance for call");
            require(isContract(target), "Address: call to non-contract");
    
            // solhint-disable-next-line avoid-low-level-calls
            (bool success, bytes memory returndata) = target.call{ value: value }(data);
            return _verifyCallResult(success, returndata, errorMessage);
        }
    
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
            return functionStaticCall(target, data, "Address: low-level static call failed");
        }
    
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(address target, bytes memory data, string memory errorMessage) internal view returns (bytes memory) {
            require(isContract(target), "Address: static call to non-contract");
    
            // solhint-disable-next-line avoid-low-level-calls
            (bool success, bytes memory returndata) = target.staticcall(data);
            return _verifyCallResult(success, returndata, errorMessage);
        }
    
        function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private pure returns(bytes memory) {
            if (success) {
                return returndata;
            } else {
                // Look for revert reason and bubble it up if present
                if (returndata.length > 0) {
                    // The easiest way to bubble the revert reason is using memory via assembly
    
                    // solhint-disable-next-line no-inline-assembly
                    assembly {
                        let returndata_size := mload(returndata)
                        revert(add(32, returndata), returndata_size)
                    }
                } else {
                    revert(errorMessage);
                }
            }
        }
    }
    
    // File: @openzeppelin/contracts-upgradeable/proxy/Initializable.sol
    
    // SPDX-License-Identifier: MIT
    
    // solhint-disable-next-line compiler-version
    pragma solidity >=0.4.24 <0.8.0;
    
    
    /**
     * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
     * behind a proxy. Since a proxied contract can't have a constructor, it's common to move constructor logic to an
     * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
     * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
     *
     * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
     * possible by providing the encoded function call as the `_data` argument to {UpgradeableProxy-constructor}.
     *
     * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
     * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
     */
    abstract contract Initializable {
    
        /**
         * @dev Indicates that the contract has been initialized.
         */
        bool private _initialized;
    
        /**
         * @dev Indicates that the contract is in the process of being initialized.
         */
        bool private _initializing;
    
        /**
         * @dev Modifier to protect an initializer function from being invoked twice.
         */
        modifier initializer() {
            require(_initializing || _isConstructor() || !_initialized, "Initializable: contract is already initialized");
    
            bool isTopLevelCall = !_initializing;
            if (isTopLevelCall) {
                _initializing = true;
                _initialized = true;
            }
    
            _;
    
            if (isTopLevelCall) {
                _initializing = false;
            }
        }
    
        /// @dev Returns true if and only if the function is running in the constructor
        function _isConstructor() private view returns (bool) {
            return !AddressUpgradeable.isContract(address(this));
        }
    }
    
    // File: @openzeppelin/contracts-upgradeable/utils/EnumerableSetUpgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.0 <0.8.0;
    
    /**
     * @dev Library for managing
     * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
     * types.
     *
     * Sets have the following properties:
     *
     * - Elements are added, removed, and checked for existence in constant time
     * (O(1)).
     * - Elements are enumerated in O(n). No guarantees are made on the ordering.
     *
     * ```
     * contract Example {
     *     // Add the library methods
     *     using EnumerableSet for EnumerableSet.AddressSet;
     *
     *     // Declare a set state variable
     *     EnumerableSet.AddressSet private mySet;
     * }
     * ```
     *
     * As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)
     * and `uint256` (`UintSet`) are supported.
     */
    library EnumerableSetUpgradeable {
        // To implement this library for multiple types with as little code
        // repetition as possible, we write it in terms of a generic Set type with
        // bytes32 values.
        // The Set implementation uses private functions, and user-facing
        // implementations (such as AddressSet) are just wrappers around the
        // underlying Set.
        // This means that we can only create new EnumerableSets for types that fit
        // in bytes32.
    
        struct Set {
            // Storage of set values
            bytes32[] _values;
    
            // Position of the value in the `values` array, plus 1 because index 0
            // means a value is not in the set.
            mapping (bytes32 => uint256) _indexes;
        }
    
        /**
         * @dev Add a value to a set. O(1).
         *
         * Returns true if the value was added to the set, that is if it was not
         * already present.
         */
        function _add(Set storage set, bytes32 value) private returns (bool) {
            if (!_contains(set, value)) {
                set._values.push(value);
                // The value is stored at length-1, but we add 1 to all indexes
                // and use 0 as a sentinel value
                set._indexes[value] = set._values.length;
                return true;
            } else {
                return false;
            }
        }
    
        /**
         * @dev Removes a value from a set. O(1).
         *
         * Returns true if the value was removed from the set, that is if it was
         * present.
         */
        function _remove(Set storage set, bytes32 value) private returns (bool) {
            // We read and store the value's index to prevent multiple reads from the same storage slot
            uint256 valueIndex = set._indexes[value];
    
            if (valueIndex != 0) { // Equivalent to contains(set, value)
                // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
                // the array, and then remove the last element (sometimes called as 'swap and pop').
                // This modifies the order of the array, as noted in {at}.
    
                uint256 toDeleteIndex = valueIndex - 1;
                uint256 lastIndex = set._values.length - 1;
    
                // When the value to delete is the last one, the swap operation is unnecessary. However, since this occurs
                // so rarely, we still do the swap anyway to avoid the gas cost of adding an 'if' statement.
    
                bytes32 lastvalue = set._values[lastIndex];
    
                // Move the last value to the index where the value to delete is
                set._values[toDeleteIndex] = lastvalue;
                // Update the index for the moved value
                set._indexes[lastvalue] = toDeleteIndex + 1; // All indexes are 1-based
    
                // Delete the slot where the moved value was stored
                set._values.pop();
    
                // Delete the index for the deleted slot
                delete set._indexes[value];
    
                return true;
            } else {
                return false;
            }
        }
    
        /**
         * @dev Returns true if the value is in the set. O(1).
         */
        function _contains(Set storage set, bytes32 value) private view returns (bool) {
            return set._indexes[value] != 0;
        }
    
        /**
         * @dev Returns the number of values on the set. O(1).
         */
        function _length(Set storage set) private view returns (uint256) {
            return set._values.length;
        }
    
       /**
        * @dev Returns the value stored at position `index` in the set. O(1).
        *
        * Note that there are no guarantees on the ordering of values inside the
        * array, and it may change when more values are added or removed.
        *
        * Requirements:
        *
        * - `index` must be strictly less than {length}.
        */
        function _at(Set storage set, uint256 index) private view returns (bytes32) {
            require(set._values.length > index, "EnumerableSet: index out of bounds");
            return set._values[index];
        }
    
        // Bytes32Set
    
        struct Bytes32Set {
            Set _inner;
        }
    
        /**
         * @dev Add a value to a set. O(1).
         *
         * Returns true if the value was added to the set, that is if it was not
         * already present.
         */
        function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
            return _add(set._inner, value);
        }
    
        /**
         * @dev Removes a value from a set. O(1).
         *
         * Returns true if the value was removed from the set, that is if it was
         * present.
         */
        function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
            return _remove(set._inner, value);
        }
    
        /**
         * @dev Returns true if the value is in the set. O(1).
         */
        function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
            return _contains(set._inner, value);
        }
    
        /**
         * @dev Returns the number of values in the set. O(1).
         */
        function length(Bytes32Set storage set) internal view returns (uint256) {
            return _length(set._inner);
        }
    
       /**
        * @dev Returns the value stored at position `index` in the set. O(1).
        *
        * Note that there are no guarantees on the ordering of values inside the
        * array, and it may change when more values are added or removed.
        *
        * Requirements:
        *
        * - `index` must be strictly less than {length}.
        */
        function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
            return _at(set._inner, index);
        }
    
        // AddressSet
    
        struct AddressSet {
            Set _inner;
        }
    
        /**
         * @dev Add a value to a set. O(1).
         *
         * Returns true if the value was added to the set, that is if it was not
         * already present.
         */
        function add(AddressSet storage set, address value) internal returns (bool) {
            return _add(set._inner, bytes32(uint256(uint160(value))));
        }
    
        /**
         * @dev Removes a value from a set. O(1).
         *
         * Returns true if the value was removed from the set, that is if it was
         * present.
         */
        function remove(AddressSet storage set, address value) internal returns (bool) {
            return _remove(set._inner, bytes32(uint256(uint160(value))));
        }
    
        /**
         * @dev Returns true if the value is in the set. O(1).
         */
        function contains(AddressSet storage set, address value) internal view returns (bool) {
            return _contains(set._inner, bytes32(uint256(uint160(value))));
        }
    
        /**
         * @dev Returns the number of values in the set. O(1).
         */
        function length(AddressSet storage set) internal view returns (uint256) {
            return _length(set._inner);
        }
    
       /**
        * @dev Returns the value stored at position `index` in the set. O(1).
        *
        * Note that there are no guarantees on the ordering of values inside the
        * array, and it may change when more values are added or removed.
        *
        * Requirements:
        *
        * - `index` must be strictly less than {length}.
        */
        function at(AddressSet storage set, uint256 index) internal view returns (address) {
            return address(uint160(uint256(_at(set._inner, index))));
        }
    
    
        // UintSet
    
        struct UintSet {
            Set _inner;
        }
    
        /**
         * @dev Add a value to a set. O(1).
         *
         * Returns true if the value was added to the set, that is if it was not
         * already present.
         */
        function add(UintSet storage set, uint256 value) internal returns (bool) {
            return _add(set._inner, bytes32(value));
        }
    
        /**
         * @dev Removes a value from a set. O(1).
         *
         * Returns true if the value was removed from the set, that is if it was
         * present.
         */
        function remove(UintSet storage set, uint256 value) internal returns (bool) {
            return _remove(set._inner, bytes32(value));
        }
    
        /**
         * @dev Returns true if the value is in the set. O(1).
         */
        function contains(UintSet storage set, uint256 value) internal view returns (bool) {
            return _contains(set._inner, bytes32(value));
        }
    
        /**
         * @dev Returns the number of values on the set. O(1).
         */
        function length(UintSet storage set) internal view returns (uint256) {
            return _length(set._inner);
        }
    
       /**
        * @dev Returns the value stored at position `index` in the set. O(1).
        *
        * Note that there are no guarantees on the ordering of values inside the
        * array, and it may change when more values are added or removed.
        *
        * Requirements:
        *
        * - `index` must be strictly less than {length}.
        */
        function at(UintSet storage set, uint256 index) internal view returns (uint256) {
            return uint256(_at(set._inner, index));
        }
    }
    
    // File: @openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.0 <0.8.0;
    
    
    /*
     * @dev Provides information about the current execution context, including the
     * sender of the transaction and its data. While these are generally available
     * via msg.sender and msg.data, they should not be accessed in such a direct
     * manner, since when dealing with GSN meta-transactions the account sending and
     * paying for execution may not be the actual sender (as far as an application
     * is concerned).
     *
     * This contract is only required for intermediate, library-like contracts.
     */
    abstract contract ContextUpgradeable is Initializable {
        function __Context_init() internal initializer {
            __Context_init_unchained();
        }
    
        function __Context_init_unchained() internal initializer {
        }
        function _msgSender() internal view virtual returns (address payable) {
            return msg.sender;
        }
    
        function _msgData() internal view virtual returns (bytes memory) {
            this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
            return msg.data;
        }
        uint256[50] private __gap;
    }
    
    // File: @openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.0 <0.8.0;
    
    
    
    
    
    /**
     * @dev Contract module that allows children to implement role-based access
     * control mechanisms.
     *
     * Roles are referred to by their `bytes32` identifier. These should be exposed
     * in the external API and be unique. The best way to achieve this is by
     * using `public constant` hash digests:
     *
     * ```
     * bytes32 public constant MY_ROLE = keccak256("MY_ROLE");
     * ```
     *
     * Roles can be used to represent a set of permissions. To restrict access to a
     * function call, use {hasRole}:
     *
     * ```
     * function foo() public {
     *     require(hasRole(MY_ROLE, msg.sender));
     *     ...
     * }
     * ```
     *
     * Roles can be granted and revoked dynamically via the {grantRole} and
     * {revokeRole} functions. Each role has an associated admin role, and only
     * accounts that have a role's admin role can call {grantRole} and {revokeRole}.
     *
     * By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means
     * that only accounts with this role will be able to grant or revoke other
     * roles. More complex role relationships can be created by using
     * {_setRoleAdmin}.
     *
     * WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to
     * grant and revoke this role. Extra precautions should be taken to secure
     * accounts that have been granted it.
     */
    abstract contract AccessControlUpgradeable is Initializable, ContextUpgradeable {
        function __AccessControl_init() internal initializer {
            __Context_init_unchained();
            __AccessControl_init_unchained();
        }
    
        function __AccessControl_init_unchained() internal initializer {
        }
        using EnumerableSetUpgradeable for EnumerableSetUpgradeable.AddressSet;
        using AddressUpgradeable for address;
    
        struct RoleData {
            EnumerableSetUpgradeable.AddressSet members;
            bytes32 adminRole;
        }
    
        mapping (bytes32 => RoleData) private _roles;
    
        bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
    
        /**
         * @dev Emitted when `newAdminRole` is set as ``role``'s admin role, replacing `previousAdminRole`
         *
         * `DEFAULT_ADMIN_ROLE` is the starting admin for all roles, despite
         * {RoleAdminChanged} not being emitted signaling this.
         *
         * _Available since v3.1._
         */
        event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole);
    
        /**
         * @dev Emitted when `account` is granted `role`.
         *
         * `sender` is the account that originated the contract call, an admin role
         * bearer except when using {_setupRole}.
         */
        event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender);
    
        /**
         * @dev Emitted when `account` is revoked `role`.
         *
         * `sender` is the account that originated the contract call:
         *   - if using `revokeRole`, it is the admin role bearer
         *   - if using `renounceRole`, it is the role bearer (i.e. `account`)
         */
        event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender);
    
        /**
         * @dev Returns `true` if `account` has been granted `role`.
         */
        function hasRole(bytes32 role, address account) public view returns (bool) {
            return _roles[role].members.contains(account);
        }
    
        /**
         * @dev Returns the number of accounts that have `role`. Can be used
         * together with {getRoleMember} to enumerate all bearers of a role.
         */
        function getRoleMemberCount(bytes32 role) public view returns (uint256) {
            return _roles[role].members.length();
        }
    
        /**
         * @dev Returns one of the accounts that have `role`. `index` must be a
         * value between 0 and {getRoleMemberCount}, non-inclusive.
         *
         * Role bearers are not sorted in any particular way, and their ordering may
         * change at any point.
         *
         * WARNING: When using {getRoleMember} and {getRoleMemberCount}, make sure
         * you perform all queries on the same block. See the following
         * https://forum.openzeppelin.com/t/iterating-over-elements-on-enumerableset-in-openzeppelin-contracts/2296[forum post]
         * for more information.
         */
        function getRoleMember(bytes32 role, uint256 index) public view returns (address) {
            return _roles[role].members.at(index);
        }
    
        /**
         * @dev Returns the admin role that controls `role`. See {grantRole} and
         * {revokeRole}.
         *
         * To change a role's admin, use {_setRoleAdmin}.
         */
        function getRoleAdmin(bytes32 role) public view returns (bytes32) {
            return _roles[role].adminRole;
        }
    
        /**
         * @dev Grants `role` to `account`.
         *
         * If `account` had not been already granted `role`, emits a {RoleGranted}
         * event.
         *
         * Requirements:
         *
         * - the caller must have ``role``'s admin role.
         */
        function grantRole(bytes32 role, address account) public virtual {
            require(hasRole(_roles[role].adminRole, _msgSender()), "AccessControl: sender must be an admin to grant");
    
            _grantRole(role, account);
        }
    
        /**
         * @dev Revokes `role` from `account`.
         *
         * If `account` had been granted `role`, emits a {RoleRevoked} event.
         *
         * Requirements:
         *
         * - the caller must have ``role``'s admin role.
         */
        function revokeRole(bytes32 role, address account) public virtual {
            require(hasRole(_roles[role].adminRole, _msgSender()), "AccessControl: sender must be an admin to revoke");
    
            _revokeRole(role, account);
        }
    
        /**
         * @dev Revokes `role` from the calling account.
         *
         * Roles are often managed via {grantRole} and {revokeRole}: this function's
         * purpose is to provide a mechanism for accounts to lose their privileges
         * if they are compromised (such as when a trusted device is misplaced).
         *
         * If the calling account had been granted `role`, emits a {RoleRevoked}
         * event.
         *
         * Requirements:
         *
         * - the caller must be `account`.
         */
        function renounceRole(bytes32 role, address account) public virtual {
            require(account == _msgSender(), "AccessControl: can only renounce roles for self");
    
            _revokeRole(role, account);
        }
    
        /**
         * @dev Grants `role` to `account`.
         *
         * If `account` had not been already granted `role`, emits a {RoleGranted}
         * event. Note that unlike {grantRole}, this function doesn't perform any
         * checks on the calling account.
         *
         * [WARNING]
         * ====
         * This function should only be called from the constructor when setting
         * up the initial roles for the system.
         *
         * Using this function in any other way is effectively circumventing the admin
         * system imposed by {AccessControl}.
         * ====
         */
        function _setupRole(bytes32 role, address account) internal virtual {
            _grantRole(role, account);
        }
    
        /**
         * @dev Sets `adminRole` as ``role``'s admin role.
         *
         * Emits a {RoleAdminChanged} event.
         */
        function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
            emit RoleAdminChanged(role, _roles[role].adminRole, adminRole);
            _roles[role].adminRole = adminRole;
        }
    
        function _grantRole(bytes32 role, address account) private {
            if (_roles[role].members.add(account)) {
                emit RoleGranted(role, account, _msgSender());
            }
        }
    
        function _revokeRole(bytes32 role, address account) private {
            if (_roles[role].members.remove(account)) {
                emit RoleRevoked(role, account, _msgSender());
            }
        }
        uint256[49] private __gap;
    }
    
    // File: @openzeppelin/contracts-upgradeable/cryptography/ECDSAUpgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.0 <0.8.0;
    
    /**
     * @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations.
     *
     * These functions can be used to verify that a message was signed by the holder
     * of the private keys of a given address.
     */
    library ECDSAUpgradeable {
        /**
         * @dev Returns the address that signed a hashed message (`hash`) with
         * `signature`. This address can then be used for verification purposes.
         *
         * The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
         * this function rejects them by requiring the `s` value to be in the lower
         * half order, and the `v` value to be either 27 or 28.
         *
         * IMPORTANT: `hash` _must_ be the result of a hash operation for the
         * verification to be secure: it is possible to craft signatures that
         * recover to arbitrary addresses for non-hashed data. A safe way to ensure
         * this is by receiving a hash of the original message (which may otherwise
         * be too long), and then calling {toEthSignedMessageHash} on it.
         */
        function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
            // Check the signature length
            if (signature.length != 65) {
                revert("ECDSA: invalid signature length");
            }
    
            // Divide the signature in r, s and v variables
            bytes32 r;
            bytes32 s;
            uint8 v;
    
            // ecrecover takes the signature parameters, and the only way to get them
            // currently is to use assembly.
            // solhint-disable-next-line no-inline-assembly
            assembly {
                r := mload(add(signature, 0x20))
                s := mload(add(signature, 0x40))
                v := byte(0, mload(add(signature, 0x60)))
            }
    
            return recover(hash, v, r, s);
        }
    
        /**
         * @dev Overload of {ECDSA-recover-bytes32-bytes-} that receives the `v`,
         * `r` and `s` signature fields separately.
         */
        function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal pure returns (address) {
            // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
            // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
            // the valid range for s in (281): 0 < s < secp256k1n ÷ 2 + 1, and for v in (282): v ∈ {27, 28}. Most
            // signatures from current libraries generate a unique signature with an s-value in the lower half order.
            //
            // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
            // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
            // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
            // these malleable signatures as well.
            require(uint256(s) <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0, "ECDSA: invalid signature 's' value");
            require(v == 27 || v == 28, "ECDSA: invalid signature 'v' value");
    
            // If the signature is valid (and not malleable), return the signer address
            address signer = ecrecover(hash, v, r, s);
            require(signer != address(0), "ECDSA: invalid signature");
    
            return signer;
        }
    
        /**
         * @dev Returns an Ethereum Signed Message, created from a `hash`. This
         * replicates the behavior of the
         * https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign[`eth_sign`]
         * JSON-RPC method.
         *
         * See {recover}.
         */
        function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) {
            // 32 is the length in bytes of hash,
            // enforced by the type signature above
            return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
        }
    }
    
    // File: @openzeppelin/contracts-upgradeable/GSN/IRelayRecipientUpgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.0 <0.8.0;
    
    /**
     * @dev Base interface for a contract that will be called via the GSN from {IRelayHub}.
     *
     * TIP: You don't need to write an implementation yourself! Inherit from {GSNRecipient} instead.
     */
    interface IRelayRecipientUpgradeable {
        /**
         * @dev Returns the address of the {IRelayHub} instance this recipient interacts with.
         */
        function getHubAddr() external view returns (address);
    
        /**
         * @dev Called by {IRelayHub} to validate if this recipient accepts being charged for a relayed call. Note that the
         * recipient will be charged regardless of the execution result of the relayed call (i.e. if it reverts or not).
         *
         * The relay request was originated by `from` and will be served by `relay`. `encodedFunction` is the relayed call
         * calldata, so its first four bytes are the function selector. The relayed call will be forwarded `gasLimit` gas,
         * and the transaction executed with a gas price of at least `gasPrice`. ``relay``'s fee is `transactionFee`, and the
         * recipient will be charged at most `maxPossibleCharge` (in wei). `nonce` is the sender's (`from`) nonce for
         * replay attack protection in {IRelayHub}, and `approvalData` is a optional parameter that can be used to hold a signature
         * over all or some of the previous values.
         *
         * Returns a tuple, where the first value is used to indicate approval (0) or rejection (custom non-zero error code,
         * values 1 to 10 are reserved) and the second one is data to be passed to the other {IRelayRecipient} functions.
         *
         * {acceptRelayedCall} is called with 50k gas: if it runs out during execution, the request will be considered
         * rejected. A regular revert will also trigger a rejection.
         */
        function acceptRelayedCall(
            address relay,
            address from,
            bytes calldata encodedFunction,
            uint256 transactionFee,
            uint256 gasPrice,
            uint256 gasLimit,
            uint256 nonce,
            bytes calldata approvalData,
            uint256 maxPossibleCharge
        )
            external
            view
            returns (uint256, bytes memory);
    
        /**
         * @dev Called by {IRelayHub} on approved relay call requests, before the relayed call is executed. This allows to e.g.
         * pre-charge the sender of the transaction.
         *
         * `context` is the second value returned in the tuple by {acceptRelayedCall}.
         *
         * Returns a value to be passed to {postRelayedCall}.
         *
         * {preRelayedCall} is called with 100k gas: if it runs out during execution or otherwise reverts, the relayed call
         * will not be executed, but the recipient will still be charged for the transaction's cost.
         */
        function preRelayedCall(bytes calldata context) external returns (bytes32);
    
        /**
         * @dev Called by {IRelayHub} on approved relay call requests, after the relayed call is executed. This allows to e.g.
         * charge the user for the relayed call costs, return any overcharges from {preRelayedCall}, or perform
         * contract-specific bookkeeping.
         *
         * `context` is the second value returned in the tuple by {acceptRelayedCall}. `success` is the execution status of
         * the relayed call. `actualCharge` is an estimate of how much the recipient will be charged for the transaction,
         * not including any gas used by {postRelayedCall} itself. `preRetVal` is {preRelayedCall}'s return value.
         *
         *
         * {postRelayedCall} is called with 100k gas: if it runs out during execution or otherwise reverts, the relayed call
         * and the call to {preRelayedCall} will be reverted retroactively, but the recipient will still be charged for the
         * transaction's cost.
         */
        function postRelayedCall(bytes calldata context, bool success, uint256 actualCharge, bytes32 preRetVal) external;
    }
    
    // File: @openzeppelin/contracts-upgradeable/GSN/IRelayHubUpgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.0 <0.8.0;
    
    /**
     * @dev Interface for `RelayHub`, the core contract of the GSN. Users should not need to interact with this contract
     * directly.
     *
     * See the https://github.com/OpenZeppelin/openzeppelin-gsn-helpers[OpenZeppelin GSN helpers] for more information on
     * how to deploy an instance of `RelayHub` on your local test network.
     */
    interface IRelayHubUpgradeable {
        // Relay management
    
        /**
         * @dev Adds stake to a relay and sets its `unstakeDelay`. If the relay does not exist, it is created, and the caller
         * of this function becomes its owner. If the relay already exists, only the owner can call this function. A relay
         * cannot be its own owner.
         *
         * All Ether in this function call will be added to the relay's stake.
         * Its unstake delay will be assigned to `unstakeDelay`, but the new value must be greater or equal to the current one.
         *
         * Emits a {Staked} event.
         */
        function stake(address relayaddr, uint256 unstakeDelay) external payable;
    
        /**
         * @dev Emitted when a relay's stake or unstakeDelay are increased
         */
        event Staked(address indexed relay, uint256 stake, uint256 unstakeDelay);
    
        /**
         * @dev Registers the caller as a relay.
         * The relay must be staked for, and not be a contract (i.e. this function must be called directly from an EOA).
         *
         * This function can be called multiple times, emitting new {RelayAdded} events. Note that the received
         * `transactionFee` is not enforced by {relayCall}.
         *
         * Emits a {RelayAdded} event.
         */
        function registerRelay(uint256 transactionFee, string calldata url) external;
    
        /**
         * @dev Emitted when a relay is registered or re-registered. Looking at these events (and filtering out
         * {RelayRemoved} events) lets a client discover the list of available relays.
         */
        event RelayAdded(address indexed relay, address indexed owner, uint256 transactionFee, uint256 stake, uint256 unstakeDelay, string url);
    
        /**
         * @dev Removes (deregisters) a relay. Unregistered (but staked for) relays can also be removed.
         *
         * Can only be called by the owner of the relay. After the relay's `unstakeDelay` has elapsed, {unstake} will be
         * callable.
         *
         * Emits a {RelayRemoved} event.
         */
        function removeRelayByOwner(address relay) external;
    
        /**
         * @dev Emitted when a relay is removed (deregistered). `unstakeTime` is the time when unstake will be callable.
         */
        event RelayRemoved(address indexed relay, uint256 unstakeTime);
    
        /** Deletes the relay from the system, and gives back its stake to the owner.
         *
         * Can only be called by the relay owner, after `unstakeDelay` has elapsed since {removeRelayByOwner} was called.
         *
         * Emits an {Unstaked} event.
         */
        function unstake(address relay) external;
    
        /**
         * @dev Emitted when a relay is unstaked for, including the returned stake.
         */
        event Unstaked(address indexed relay, uint256 stake);
    
        // States a relay can be in
        enum RelayState {
            Unknown, // The relay is unknown to the system: it has never been staked for
            Staked, // The relay has been staked for, but it is not yet active
            Registered, // The relay has registered itself, and is active (can relay calls)
            Removed    // The relay has been removed by its owner and can no longer relay calls. It must wait for its unstakeDelay to elapse before it can unstake
        }
    
        /**
         * @dev Returns a relay's status. Note that relays can be deleted when unstaked or penalized, causing this function
         * to return an empty entry.
         */
        function getRelay(address relay) external view returns (uint256 totalStake, uint256 unstakeDelay, uint256 unstakeTime, address payable owner, RelayState state);
    
        // Balance management
    
        /**
         * @dev Deposits Ether for a contract, so that it can receive (and pay for) relayed transactions.
         *
         * Unused balance can only be withdrawn by the contract itself, by calling {withdraw}.
         *
         * Emits a {Deposited} event.
         */
        function depositFor(address target) external payable;
    
        /**
         * @dev Emitted when {depositFor} is called, including the amount and account that was funded.
         */
        event Deposited(address indexed recipient, address indexed from, uint256 amount);
    
        /**
         * @dev Returns an account's deposits. These can be either a contract's funds, or a relay owner's revenue.
         */
        function balanceOf(address target) external view returns (uint256);
    
        /**
         * Withdraws from an account's balance, sending it back to it. Relay owners call this to retrieve their revenue, and
         * contracts can use it to reduce their funding.
         *
         * Emits a {Withdrawn} event.
         */
        function withdraw(uint256 amount, address payable dest) external;
    
        /**
         * @dev Emitted when an account withdraws funds from `RelayHub`.
         */
        event Withdrawn(address indexed account, address indexed dest, uint256 amount);
    
        // Relaying
    
        /**
         * @dev Checks if the `RelayHub` will accept a relayed operation.
         * Multiple things must be true for this to happen:
         *  - all arguments must be signed for by the sender (`from`)
         *  - the sender's nonce must be the current one
         *  - the recipient must accept this transaction (via {acceptRelayedCall})
         *
         * Returns a `PreconditionCheck` value (`OK` when the transaction can be relayed), or a recipient-specific error
         * code if it returns one in {acceptRelayedCall}.
         */
        function canRelay(
            address relay,
            address from,
            address to,
            bytes calldata encodedFunction,
            uint256 transactionFee,
            uint256 gasPrice,
            uint256 gasLimit,
            uint256 nonce,
            bytes calldata signature,
            bytes calldata approvalData
        ) external view returns (uint256 status, bytes memory recipientContext);
    
        // Preconditions for relaying, checked by canRelay and returned as the corresponding numeric values.
        enum PreconditionCheck {
            OK,                         // All checks passed, the call can be relayed
            WrongSignature,             // The transaction to relay is not signed by requested sender
            WrongNonce,                 // The provided nonce has already been used by the sender
            AcceptRelayedCallReverted,  // The recipient rejected this call via acceptRelayedCall
            InvalidRecipientStatusCode  // The recipient returned an invalid (reserved) status code
        }
    
        /**
         * @dev Relays a transaction.
         *
         * For this to succeed, multiple conditions must be met:
         *  - {canRelay} must `return PreconditionCheck.OK`
         *  - the sender must be a registered relay
         *  - the transaction's gas price must be larger or equal to the one that was requested by the sender
         *  - the transaction must have enough gas to not run out of gas if all internal transactions (calls to the
         * recipient) use all gas available to them
         *  - the recipient must have enough balance to pay the relay for the worst-case scenario (i.e. when all gas is
         * spent)
         *
         * If all conditions are met, the call will be relayed and the recipient charged. {preRelayedCall}, the encoded
         * function and {postRelayedCall} will be called in that order.
         *
         * Parameters:
         *  - `from`: the client originating the request
         *  - `to`: the target {IRelayRecipient} contract
         *  - `encodedFunction`: the function call to relay, including data
         *  - `transactionFee`: fee (%) the relay takes over actual gas cost
         *  - `gasPrice`: gas price the client is willing to pay
         *  - `gasLimit`: gas to forward when calling the encoded function
         *  - `nonce`: client's nonce
         *  - `signature`: client's signature over all previous params, plus the relay and RelayHub addresses
         *  - `approvalData`: dapp-specific data forwarded to {acceptRelayedCall}. This value is *not* verified by the
         * `RelayHub`, but it still can be used for e.g. a signature.
         *
         * Emits a {TransactionRelayed} event.
         */
        function relayCall(
            address from,
            address to,
            bytes calldata encodedFunction,
            uint256 transactionFee,
            uint256 gasPrice,
            uint256 gasLimit,
            uint256 nonce,
            bytes calldata signature,
            bytes calldata approvalData
        ) external;
    
        /**
         * @dev Emitted when an attempt to relay a call failed.
         *
         * This can happen due to incorrect {relayCall} arguments, or the recipient not accepting the relayed call. The
         * actual relayed call was not executed, and the recipient not charged.
         *
         * The `reason` parameter contains an error code: values 1-10 correspond to `PreconditionCheck` entries, and values
         * over 10 are custom recipient error codes returned from {acceptRelayedCall}.
         */
        event CanRelayFailed(address indexed relay, address indexed from, address indexed to, bytes4 selector, uint256 reason);
    
        /**
         * @dev Emitted when a transaction is relayed.
         * Useful when monitoring a relay's operation and relayed calls to a contract
         *
         * Note that the actual encoded function might be reverted: this is indicated in the `status` parameter.
         *
         * `charge` is the Ether value deducted from the recipient's balance, paid to the relay's owner.
         */
        event TransactionRelayed(address indexed relay, address indexed from, address indexed to, bytes4 selector, RelayCallStatus status, uint256 charge);
    
        // Reason error codes for the TransactionRelayed event
        enum RelayCallStatus {
            OK,                      // The transaction was successfully relayed and execution successful - never included in the event
            RelayedCallFailed,       // The transaction was relayed, but the relayed call failed
            PreRelayedFailed,        // The transaction was not relayed due to preRelatedCall reverting
            PostRelayedFailed,       // The transaction was relayed and reverted due to postRelatedCall reverting
            RecipientBalanceChanged  // The transaction was relayed and reverted due to the recipient's balance changing
        }
    
        /**
         * @dev Returns how much gas should be forwarded to a call to {relayCall}, in order to relay a transaction that will
         * spend up to `relayedCallStipend` gas.
         */
        function requiredGas(uint256 relayedCallStipend) external view returns (uint256);
    
        /**
         * @dev Returns the maximum recipient charge, given the amount of gas forwarded, gas price and relay fee.
         */
        function maxPossibleCharge(uint256 relayedCallStipend, uint256 gasPrice, uint256 transactionFee) external view returns (uint256);
    
         // Relay penalization.
         // Any account can penalize relays, removing them from the system immediately, and rewarding the
        // reporter with half of the relay's stake. The other half is burned so that, even if the relay penalizes itself, it
        // still loses half of its stake.
    
        /**
         * @dev Penalize a relay that signed two transactions using the same nonce (making only the first one valid) and
         * different data (gas price, gas limit, etc. may be different).
         *
         * The (unsigned) transaction data and signature for both transactions must be provided.
         */
        function penalizeRepeatedNonce(bytes calldata unsignedTx1, bytes calldata signature1, bytes calldata unsignedTx2, bytes calldata signature2) external;
    
        /**
         * @dev Penalize a relay that sent a transaction that didn't target ``RelayHub``'s {registerRelay} or {relayCall}.
         */
        function penalizeIllegalTransaction(bytes calldata unsignedTx, bytes calldata signature) external;
    
        /**
         * @dev Emitted when a relay is penalized.
         */
        event Penalized(address indexed relay, address sender, uint256 amount);
    
        /**
         * @dev Returns an account's nonce in `RelayHub`.
         */
        function getNonce(address from) external view returns (uint256);
    }
    
    // File: @openzeppelin/contracts-upgradeable/GSN/GSNRecipientUpgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.0 <0.8.0;
    
    
    
    
    
    /**
     * @dev Base GSN recipient contract: includes the {IRelayRecipient} interface
     * and enables GSN support on all contracts in the inheritance tree.
     *
     * TIP: This contract is abstract. The functions {IRelayRecipient-acceptRelayedCall},
     *  {_preRelayedCall}, and {_postRelayedCall} are not implemented and must be
     * provided by derived contracts. See the
     * xref:ROOT:gsn-strategies.adoc#gsn-strategies[GSN strategies] for more
     * information on how to use the pre-built {GSNRecipientSignature} and
     * {GSNRecipientERC20Fee}, or how to write your own.
     */
    abstract contract GSNRecipientUpgradeable is Initializable, IRelayRecipientUpgradeable, ContextUpgradeable {
        function __GSNRecipient_init() internal initializer {
            __Context_init_unchained();
            __GSNRecipient_init_unchained();
        }
    
        function __GSNRecipient_init_unchained() internal initializer {
            _relayHub = 0xD216153c06E857cD7f72665E0aF1d7D82172F494;
        }
        // Default RelayHub address, deployed on mainnet and all testnets at the same address
        address private _relayHub;
    
        uint256 constant private _RELAYED_CALL_ACCEPTED = 0;
        uint256 constant private _RELAYED_CALL_REJECTED = 11;
    
        // How much gas is forwarded to postRelayedCall
        uint256 constant internal _POST_RELAYED_CALL_MAX_GAS = 100000;
    
        /**
         * @dev Emitted when a contract changes its {IRelayHub} contract to a new one.
         */
        event RelayHubChanged(address indexed oldRelayHub, address indexed newRelayHub);
    
        /**
         * @dev Returns the address of the {IRelayHub} contract for this recipient.
         */
        function getHubAddr() public view virtual override returns (address) {
            return _relayHub;
        }
    
        /**
         * @dev Switches to a new {IRelayHub} instance. This method is added for future-proofing: there's no reason to not
         * use the default instance.
         *
         * IMPORTANT: After upgrading, the {GSNRecipient} will no longer be able to receive relayed calls from the old
         * {IRelayHub} instance. Additionally, all funds should be previously withdrawn via {_withdrawDeposits}.
         */
        function _upgradeRelayHub(address newRelayHub) internal virtual {
            address currentRelayHub = _relayHub;
            require(newRelayHub != address(0), "GSNRecipient: new RelayHub is the zero address");
            require(newRelayHub != currentRelayHub, "GSNRecipient: new RelayHub is the current one");
    
            emit RelayHubChanged(currentRelayHub, newRelayHub);
    
            _relayHub = newRelayHub;
        }
    
        /**
         * @dev Returns the version string of the {IRelayHub} for which this recipient implementation was built. If
         * {_upgradeRelayHub} is used, the new {IRelayHub} instance should be compatible with this version.
         */
        // This function is view for future-proofing, it may require reading from
        // storage in the future.
        function relayHubVersion() public view virtual returns (string memory) {
            this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
            return "1.0.0";
        }
    
        /**
         * @dev Withdraws the recipient's deposits in `RelayHub`.
         *
         * Derived contracts should expose this in an external interface with proper access control.
         */
        function _withdrawDeposits(uint256 amount, address payable payee) internal virtual {
            IRelayHubUpgradeable(getHubAddr()).withdraw(amount, payee);
        }
    
        // Overrides for Context's functions: when called from RelayHub, sender and
        // data require some pre-processing: the actual sender is stored at the end
        // of the call data, which in turns means it needs to be removed from it
        // when handling said data.
    
        /**
         * @dev Replacement for msg.sender. Returns the actual sender of a transaction: msg.sender for regular transactions,
         * and the end-user for GSN relayed calls (where msg.sender is actually `RelayHub`).
         *
         * IMPORTANT: Contracts derived from {GSNRecipient} should never use `msg.sender`, and use {_msgSender} instead.
         */
        function _msgSender() internal view virtual override returns (address payable) {
            if (msg.sender != getHubAddr()) {
                return msg.sender;
            } else {
                return _getRelayedCallSender();
            }
        }
    
        /**
         * @dev Replacement for msg.data. Returns the actual calldata of a transaction: msg.data for regular transactions,
         * and a reduced version for GSN relayed calls (where msg.data contains additional information).
         *
         * IMPORTANT: Contracts derived from {GSNRecipient} should never use `msg.data`, and use {_msgData} instead.
         */
        function _msgData() internal view virtual override returns (bytes memory) {
            if (msg.sender != getHubAddr()) {
                return msg.data;
            } else {
                return _getRelayedCallData();
            }
        }
    
        // Base implementations for pre and post relayedCall: only RelayHub can invoke them, and data is forwarded to the
        // internal hook.
    
        /**
         * @dev See `IRelayRecipient.preRelayedCall`.
         *
         * This function should not be overridden directly, use `_preRelayedCall` instead.
         *
         * * Requirements:
         *
         * - the caller must be the `RelayHub` contract.
         */
        function preRelayedCall(bytes memory context) public virtual override returns (bytes32) {
            require(msg.sender == getHubAddr(), "GSNRecipient: caller is not RelayHub");
            return _preRelayedCall(context);
        }
    
        /**
         * @dev See `IRelayRecipient.preRelayedCall`.
         *
         * Called by `GSNRecipient.preRelayedCall`, which asserts the caller is the `RelayHub` contract. Derived contracts
         * must implement this function with any relayed-call preprocessing they may wish to do.
         *
         */
        function _preRelayedCall(bytes memory context) internal virtual returns (bytes32);
    
        /**
         * @dev See `IRelayRecipient.postRelayedCall`.
         *
         * This function should not be overridden directly, use `_postRelayedCall` instead.
         *
         * * Requirements:
         *
         * - the caller must be the `RelayHub` contract.
         */
        function postRelayedCall(bytes memory context, bool success, uint256 actualCharge, bytes32 preRetVal) public virtual override {
            require(msg.sender == getHubAddr(), "GSNRecipient: caller is not RelayHub");
            _postRelayedCall(context, success, actualCharge, preRetVal);
        }
    
        /**
         * @dev See `IRelayRecipient.postRelayedCall`.
         *
         * Called by `GSNRecipient.postRelayedCall`, which asserts the caller is the `RelayHub` contract. Derived contracts
         * must implement this function with any relayed-call postprocessing they may wish to do.
         *
         */
        function _postRelayedCall(bytes memory context, bool success, uint256 actualCharge, bytes32 preRetVal) internal virtual;
    
        /**
         * @dev Return this in acceptRelayedCall to proceed with the execution of a relayed call. Note that this contract
         * will be charged a fee by RelayHub
         */
        function _approveRelayedCall() internal pure virtual returns (uint256, bytes memory) {
            return _approveRelayedCall("");
        }
    
        /**
         * @dev See `GSNRecipient._approveRelayedCall`.
         *
         * This overload forwards `context` to _preRelayedCall and _postRelayedCall.
         */
        function _approveRelayedCall(bytes memory context) internal pure virtual returns (uint256, bytes memory) {
            return (_RELAYED_CALL_ACCEPTED, context);
        }
    
        /**
         * @dev Return this in acceptRelayedCall to impede execution of a relayed call. No fees will be charged.
         */
        function _rejectRelayedCall(uint256 errorCode) internal pure virtual returns (uint256, bytes memory) {
            return (_RELAYED_CALL_REJECTED + errorCode, "");
        }
    
        /*
         * @dev Calculates how much RelayHub will charge a recipient for using `gas` at a `gasPrice`, given a relayer's
         * `serviceFee`.
         */
        function _computeCharge(uint256 gas, uint256 gasPrice, uint256 serviceFee) internal pure virtual returns (uint256) {
            // The fee is expressed as a percentage. E.g. a value of 40 stands for a 40% fee, so the recipient will be
            // charged for 1.4 times the spent amount.
            return (gas * gasPrice * (100 + serviceFee)) / 100;
        }
    
        function _getRelayedCallSender() private pure returns (address payable result) {
            // We need to read 20 bytes (an address) located at array index msg.data.length - 20. In memory, the array
            // is prefixed with a 32-byte length value, so we first add 32 to get the memory read index. However, doing
            // so would leave the address in the upper 20 bytes of the 32-byte word, which is inconvenient and would
            // require bit shifting. We therefore subtract 12 from the read index so the address lands on the lower 20
            // bytes. This can always be done due to the 32-byte prefix.
    
            // The final memory read index is msg.data.length - 20 + 32 - 12 = msg.data.length. Using inline assembly is the
            // easiest/most-efficient way to perform this operation.
    
            // These fields are not accessible from assembly
            bytes memory array = msg.data;
            uint256 index = msg.data.length;
    
            // solhint-disable-next-line no-inline-assembly
            assembly {
                // Load the 32 bytes word from memory with the address on the lower 20 bytes, and mask those.
                result := and(mload(add(array, index)), 0xffffffffffffffffffffffffffffffffffffffff)
            }
            return result;
        }
    
        function _getRelayedCallData() private pure returns (bytes memory) {
            // RelayHub appends the sender address at the end of the calldata, so in order to retrieve the actual msg.data,
            // we must strip the last 20 bytes (length of an address type) from it.
    
            uint256 actualDataLength = msg.data.length - 20;
            bytes memory actualData = new bytes(actualDataLength);
    
            for (uint256 i = 0; i < actualDataLength; ++i) {
                actualData[i] = msg.data[i];
            }
    
            return actualData;
        }
        uint256[49] private __gap;
    }
    
    // File: @openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.0 <0.8.0;
    
    
    /**
     * @dev Contract module which provides a basic access control mechanism, where
     * there is an account (an owner) that can be granted exclusive access to
     * specific functions.
     *
     * By default, the owner account will be the one that deploys the contract. This
     * can later be changed with {transferOwnership}.
     *
     * This module is used through inheritance. It will make available the modifier
     * `onlyOwner`, which can be applied to your functions to restrict their use to
     * the owner.
     */
    abstract contract OwnableUpgradeable is Initializable, ContextUpgradeable {
        address private _owner;
    
        event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
    
        /**
         * @dev Initializes the contract setting the deployer as the initial owner.
         */
        function __Ownable_init() internal initializer {
            __Context_init_unchained();
            __Ownable_init_unchained();
        }
    
        function __Ownable_init_unchained() internal initializer {
            address msgSender = _msgSender();
            _owner = msgSender;
            emit OwnershipTransferred(address(0), msgSender);
        }
    
        /**
         * @dev Returns the address of the current owner.
         */
        function owner() public view virtual returns (address) {
            return _owner;
        }
    
        /**
         * @dev Throws if called by any account other than the owner.
         */
        modifier onlyOwner() {
            require(owner() == _msgSender(), "Ownable: caller is not the owner");
            _;
        }
    
        /**
         * @dev Leaves the contract without owner. It will not be possible to call
         * `onlyOwner` functions anymore. Can only be called by the current owner.
         *
         * NOTE: Renouncing ownership will leave the contract without an owner,
         * thereby removing any functionality that is only available to the owner.
         */
        function renounceOwnership() public virtual onlyOwner {
            emit OwnershipTransferred(_owner, address(0));
            _owner = address(0);
        }
    
        /**
         * @dev Transfers ownership of the contract to a new account (`newOwner`).
         * Can only be called by the current owner.
         */
        function transferOwnership(address newOwner) public virtual onlyOwner {
            require(newOwner != address(0), "Ownable: new owner is the zero address");
            emit OwnershipTransferred(_owner, newOwner);
            _owner = newOwner;
        }
        uint256[49] private __gap;
    }
    
    // File: @openzeppelin/contracts-upgradeable/token/ERC777/IERC777Upgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.0 <0.8.0;
    
    /**
     * @dev Interface of the ERC777Token standard as defined in the EIP.
     *
     * This contract uses the
     * https://eips.ethereum.org/EIPS/eip-1820[ERC1820 registry standard] to let
     * token holders and recipients react to token movements by using setting implementers
     * for the associated interfaces in said registry. See {IERC1820Registry} and
     * {ERC1820Implementer}.
     */
    interface IERC777Upgradeable {
        /**
         * @dev Returns the name of the token.
         */
        function name() external view returns (string memory);
    
        /**
         * @dev Returns the symbol of the token, usually a shorter version of the
         * name.
         */
        function symbol() external view returns (string memory);
    
        /**
         * @dev Returns the smallest part of the token that is not divisible. This
         * means all token operations (creation, movement and destruction) must have
         * amounts that are a multiple of this number.
         *
         * For most token contracts, this value will equal 1.
         */
        function granularity() external view returns (uint256);
    
        /**
         * @dev Returns the amount of tokens in existence.
         */
        function totalSupply() external view returns (uint256);
    
        /**
         * @dev Returns the amount of tokens owned by an account (`owner`).
         */
        function balanceOf(address owner) external view returns (uint256);
    
        /**
         * @dev Moves `amount` tokens from the caller's account to `recipient`.
         *
         * If send or receive hooks are registered for the caller and `recipient`,
         * the corresponding functions will be called with `data` and empty
         * `operatorData`. See {IERC777Sender} and {IERC777Recipient}.
         *
         * Emits a {Sent} event.
         *
         * Requirements
         *
         * - the caller must have at least `amount` tokens.
         * - `recipient` cannot be the zero address.
         * - if `recipient` is a contract, it must implement the {IERC777Recipient}
         * interface.
         */
        function send(address recipient, uint256 amount, bytes calldata data) external;
    
        /**
         * @dev Destroys `amount` tokens from the caller's account, reducing the
         * total supply.
         *
         * If a send hook is registered for the caller, the corresponding function
         * will be called with `data` and empty `operatorData`. See {IERC777Sender}.
         *
         * Emits a {Burned} event.
         *
         * Requirements
         *
         * - the caller must have at least `amount` tokens.
         */
        function burn(uint256 amount, bytes calldata data) external;
    
        /**
         * @dev Returns true if an account is an operator of `tokenHolder`.
         * Operators can send and burn tokens on behalf of their owners. All
         * accounts are their own operator.
         *
         * See {operatorSend} and {operatorBurn}.
         */
        function isOperatorFor(address operator, address tokenHolder) external view returns (bool);
    
        /**
         * @dev Make an account an operator of the caller.
         *
         * See {isOperatorFor}.
         *
         * Emits an {AuthorizedOperator} event.
         *
         * Requirements
         *
         * - `operator` cannot be calling address.
         */
        function authorizeOperator(address operator) external;
    
        /**
         * @dev Revoke an account's operator status for the caller.
         *
         * See {isOperatorFor} and {defaultOperators}.
         *
         * Emits a {RevokedOperator} event.
         *
         * Requirements
         *
         * - `operator` cannot be calling address.
         */
        function revokeOperator(address operator) external;
    
        /**
         * @dev Returns the list of default operators. These accounts are operators
         * for all token holders, even if {authorizeOperator} was never called on
         * them.
         *
         * This list is immutable, but individual holders may revoke these via
         * {revokeOperator}, in which case {isOperatorFor} will return false.
         */
        function defaultOperators() external view returns (address[] memory);
    
        /**
         * @dev Moves `amount` tokens from `sender` to `recipient`. The caller must
         * be an operator of `sender`.
         *
         * If send or receive hooks are registered for `sender` and `recipient`,
         * the corresponding functions will be called with `data` and
         * `operatorData`. See {IERC777Sender} and {IERC777Recipient}.
         *
         * Emits a {Sent} event.
         *
         * Requirements
         *
         * - `sender` cannot be the zero address.
         * - `sender` must have at least `amount` tokens.
         * - the caller must be an operator for `sender`.
         * - `recipient` cannot be the zero address.
         * - if `recipient` is a contract, it must implement the {IERC777Recipient}
         * interface.
         */
        function operatorSend(
            address sender,
            address recipient,
            uint256 amount,
            bytes calldata data,
            bytes calldata operatorData
        ) external;
    
        /**
         * @dev Destroys `amount` tokens from `account`, reducing the total supply.
         * The caller must be an operator of `account`.
         *
         * If a send hook is registered for `account`, the corresponding function
         * will be called with `data` and `operatorData`. See {IERC777Sender}.
         *
         * Emits a {Burned} event.
         *
         * Requirements
         *
         * - `account` cannot be the zero address.
         * - `account` must have at least `amount` tokens.
         * - the caller must be an operator for `account`.
         */
        function operatorBurn(
            address account,
            uint256 amount,
            bytes calldata data,
            bytes calldata operatorData
        ) external;
    
        event Sent(
            address indexed operator,
            address indexed from,
            address indexed to,
            uint256 amount,
            bytes data,
            bytes operatorData
        );
    
        event Minted(address indexed operator, address indexed to, uint256 amount, bytes data, bytes operatorData);
    
        event Burned(address indexed operator, address indexed from, uint256 amount, bytes data, bytes operatorData);
    
        event AuthorizedOperator(address indexed operator, address indexed tokenHolder);
    
        event RevokedOperator(address indexed operator, address indexed tokenHolder);
    }
    
    // File: @openzeppelin/contracts-upgradeable/token/ERC777/IERC777RecipientUpgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.0 <0.8.0;
    
    /**
     * @dev Interface of the ERC777TokensRecipient standard as defined in the EIP.
     *
     * Accounts can be notified of {IERC777} tokens being sent to them by having a
     * contract implement this interface (contract holders can be their own
     * implementer) and registering it on the
     * https://eips.ethereum.org/EIPS/eip-1820[ERC1820 global registry].
     *
     * See {IERC1820Registry} and {ERC1820Implementer}.
     */
    interface IERC777RecipientUpgradeable {
        /**
         * @dev Called by an {IERC777} token contract whenever tokens are being
         * moved or created into a registered account (`to`). The type of operation
         * is conveyed by `from` being the zero address or not.
         *
         * This call occurs _after_ the token contract's state is updated, so
         * {IERC777-balanceOf}, etc., can be used to query the post-operation state.
         *
         * This function may revert to prevent the operation from being executed.
         */
        function tokensReceived(
            address operator,
            address from,
            address to,
            uint256 amount,
            bytes calldata userData,
            bytes calldata operatorData
        ) external;
    }
    
    // File: @openzeppelin/contracts-upgradeable/token/ERC777/IERC777SenderUpgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.0 <0.8.0;
    
    /**
     * @dev Interface of the ERC777TokensSender standard as defined in the EIP.
     *
     * {IERC777} Token holders can be notified of operations performed on their
     * tokens by having a contract implement this interface (contract holders can be
     *  their own implementer) and registering it on the
     * https://eips.ethereum.org/EIPS/eip-1820[ERC1820 global registry].
     *
     * See {IERC1820Registry} and {ERC1820Implementer}.
     */
    interface IERC777SenderUpgradeable {
        /**
         * @dev Called by an {IERC777} token contract whenever a registered holder's
         * (`from`) tokens are about to be moved or destroyed. The type of operation
         * is conveyed by `to` being the zero address or not.
         *
         * This call occurs _before_ the token contract's state is updated, so
         * {IERC777-balanceOf}, etc., can be used to query the pre-operation state.
         *
         * This function may revert to prevent the operation from being executed.
         */
        function tokensToSend(
            address operator,
            address from,
            address to,
            uint256 amount,
            bytes calldata userData,
            bytes calldata operatorData
        ) external;
    }
    
    // File: @openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.0 <0.8.0;
    
    /**
     * @dev Interface of the ERC20 standard as defined in the EIP.
     */
    interface IERC20Upgradeable {
        /**
         * @dev Returns the amount of tokens in existence.
         */
        function totalSupply() external view returns (uint256);
    
        /**
         * @dev Returns the amount of tokens owned by `account`.
         */
        function balanceOf(address account) external view returns (uint256);
    
        /**
         * @dev Moves `amount` tokens from the caller's account to `recipient`.
         *
         * Returns a boolean value indicating whether the operation succeeded.
         *
         * Emits a {Transfer} event.
         */
        function transfer(address recipient, uint256 amount) external returns (bool);
    
        /**
         * @dev Returns the remaining number of tokens that `spender` will be
         * allowed to spend on behalf of `owner` through {transferFrom}. This is
         * zero by default.
         *
         * This value changes when {approve} or {transferFrom} are called.
         */
        function allowance(address owner, address spender) external view returns (uint256);
    
        /**
         * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
         *
         * Returns a boolean value indicating whether the operation succeeded.
         *
         * IMPORTANT: Beware that changing an allowance with this method brings the risk
         * that someone may use both the old and the new allowance by unfortunate
         * transaction ordering. One possible solution to mitigate this race
         * condition is to first reduce the spender's allowance to 0 and set the
         * desired value afterwards:
         * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
         *
         * Emits an {Approval} event.
         */
        function approve(address spender, uint256 amount) external returns (bool);
    
        /**
         * @dev Moves `amount` tokens from `sender` to `recipient` using the
         * allowance mechanism. `amount` is then deducted from the caller's
         * allowance.
         *
         * Returns a boolean value indicating whether the operation succeeded.
         *
         * Emits a {Transfer} event.
         */
        function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    
        /**
         * @dev Emitted when `value` tokens are moved from one account (`from`) to
         * another (`to`).
         *
         * Note that `value` may be zero.
         */
        event Transfer(address indexed from, address indexed to, uint256 value);
    
        /**
         * @dev Emitted when the allowance of a `spender` for an `owner` is set by
         * a call to {approve}. `value` is the new allowance.
         */
        event Approval(address indexed owner, address indexed spender, uint256 value);
    }
    
    // File: @openzeppelin/contracts-upgradeable/math/SafeMathUpgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.0 <0.8.0;
    
    /**
     * @dev Wrappers over Solidity's arithmetic operations with added overflow
     * checks.
     *
     * Arithmetic operations in Solidity wrap on overflow. This can easily result
     * in bugs, because programmers usually assume that an overflow raises an
     * error, which is the standard behavior in high level programming languages.
     * `SafeMath` restores this intuition by reverting the transaction when an
     * operation overflows.
     *
     * Using this library instead of the unchecked operations eliminates an entire
     * class of bugs, so it's recommended to use it always.
     */
    library SafeMathUpgradeable {
        /**
         * @dev Returns the addition of two unsigned integers, with an overflow flag.
         *
         * _Available since v3.4._
         */
        function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
            uint256 c = a + b;
            if (c < a) return (false, 0);
            return (true, c);
        }
    
        /**
         * @dev Returns the substraction of two unsigned integers, with an overflow flag.
         *
         * _Available since v3.4._
         */
        function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
            if (b > a) return (false, 0);
            return (true, a - b);
        }
    
        /**
         * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
         *
         * _Available since v3.4._
         */
        function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
            // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
            // benefit is lost if 'b' is also tested.
            // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
            if (a == 0) return (true, 0);
            uint256 c = a * b;
            if (c / a != b) return (false, 0);
            return (true, c);
        }
    
        /**
         * @dev Returns the division of two unsigned integers, with a division by zero flag.
         *
         * _Available since v3.4._
         */
        function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
            if (b == 0) return (false, 0);
            return (true, a / b);
        }
    
        /**
         * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
         *
         * _Available since v3.4._
         */
        function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
            if (b == 0) return (false, 0);
            return (true, a % b);
        }
    
        /**
         * @dev Returns the addition of two unsigned integers, reverting on
         * overflow.
         *
         * Counterpart to Solidity's `+` operator.
         *
         * Requirements:
         *
         * - Addition cannot overflow.
         */
        function add(uint256 a, uint256 b) internal pure returns (uint256) {
            uint256 c = a + b;
            require(c >= a, "SafeMath: addition overflow");
            return c;
        }
    
        /**
         * @dev Returns the subtraction of two unsigned integers, reverting on
         * overflow (when the result is negative).
         *
         * Counterpart to Solidity's `-` operator.
         *
         * Requirements:
         *
         * - Subtraction cannot overflow.
         */
        function sub(uint256 a, uint256 b) internal pure returns (uint256) {
            require(b <= a, "SafeMath: subtraction overflow");
            return a - b;
        }
    
        /**
         * @dev Returns the multiplication of two unsigned integers, reverting on
         * overflow.
         *
         * Counterpart to Solidity's `*` operator.
         *
         * Requirements:
         *
         * - Multiplication cannot overflow.
         */
        function mul(uint256 a, uint256 b) internal pure returns (uint256) {
            if (a == 0) return 0;
            uint256 c = a * b;
            require(c / a == b, "SafeMath: multiplication overflow");
            return c;
        }
    
        /**
         * @dev Returns the integer division of two unsigned integers, reverting on
         * division by zero. The result is rounded towards zero.
         *
         * Counterpart to Solidity's `/` operator. Note: this function uses a
         * `revert` opcode (which leaves remaining gas untouched) while Solidity
         * uses an invalid opcode to revert (consuming all remaining gas).
         *
         * Requirements:
         *
         * - The divisor cannot be zero.
         */
        function div(uint256 a, uint256 b) internal pure returns (uint256) {
            require(b > 0, "SafeMath: division by zero");
            return a / b;
        }
    
        /**
         * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
         * reverting when dividing by zero.
         *
         * Counterpart to Solidity's `%` operator. This function uses a `revert`
         * opcode (which leaves remaining gas untouched) while Solidity uses an
         * invalid opcode to revert (consuming all remaining gas).
         *
         * Requirements:
         *
         * - The divisor cannot be zero.
         */
        function mod(uint256 a, uint256 b) internal pure returns (uint256) {
            require(b > 0, "SafeMath: modulo by zero");
            return a % b;
        }
    
        /**
         * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
         * overflow (when the result is negative).
         *
         * CAUTION: This function is deprecated because it requires allocating memory for the error
         * message unnecessarily. For custom revert reasons use {trySub}.
         *
         * Counterpart to Solidity's `-` operator.
         *
         * Requirements:
         *
         * - Subtraction cannot overflow.
         */
        function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
            require(b <= a, errorMessage);
            return a - b;
        }
    
        /**
         * @dev Returns the integer division of two unsigned integers, reverting with custom message on
         * division by zero. The result is rounded towards zero.
         *
         * CAUTION: This function is deprecated because it requires allocating memory for the error
         * message unnecessarily. For custom revert reasons use {tryDiv}.
         *
         * Counterpart to Solidity's `/` operator. Note: this function uses a
         * `revert` opcode (which leaves remaining gas untouched) while Solidity
         * uses an invalid opcode to revert (consuming all remaining gas).
         *
         * Requirements:
         *
         * - The divisor cannot be zero.
         */
        function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
            require(b > 0, errorMessage);
            return a / b;
        }
    
        /**
         * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
         * reverting with custom message when dividing by zero.
         *
         * CAUTION: This function is deprecated because it requires allocating memory for the error
         * message unnecessarily. For custom revert reasons use {tryMod}.
         *
         * Counterpart to Solidity's `%` operator. This function uses a `revert`
         * opcode (which leaves remaining gas untouched) while Solidity uses an
         * invalid opcode to revert (consuming all remaining gas).
         *
         * Requirements:
         *
         * - The divisor cannot be zero.
         */
        function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
            require(b > 0, errorMessage);
            return a % b;
        }
    }
    
    // File: @openzeppelin/contracts-upgradeable/introspection/IERC1820RegistryUpgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.0 <0.8.0;
    
    /**
     * @dev Interface of the global ERC1820 Registry, as defined in the
     * https://eips.ethereum.org/EIPS/eip-1820[EIP]. Accounts may register
     * implementers for interfaces in this registry, as well as query support.
     *
     * Implementers may be shared by multiple accounts, and can also implement more
     * than a single interface for each account. Contracts can implement interfaces
     * for themselves, but externally-owned accounts (EOA) must delegate this to a
     * contract.
     *
     * {IERC165} interfaces can also be queried via the registry.
     *
     * For an in-depth explanation and source code analysis, see the EIP text.
     */
    interface IERC1820RegistryUpgradeable {
        /**
         * @dev Sets `newManager` as the manager for `account`. A manager of an
         * account is able to set interface implementers for it.
         *
         * By default, each account is its own manager. Passing a value of `0x0` in
         * `newManager` will reset the manager to this initial state.
         *
         * Emits a {ManagerChanged} event.
         *
         * Requirements:
         *
         * - the caller must be the current manager for `account`.
         */
        function setManager(address account, address newManager) external;
    
        /**
         * @dev Returns the manager for `account`.
         *
         * See {setManager}.
         */
        function getManager(address account) external view returns (address);
    
        /**
         * @dev Sets the `implementer` contract as ``account``'s implementer for
         * `interfaceHash`.
         *
         * `account` being the zero address is an alias for the caller's address.
         * The zero address can also be used in `implementer` to remove an old one.
         *
         * See {interfaceHash} to learn how these are created.
         *
         * Emits an {InterfaceImplementerSet} event.
         *
         * Requirements:
         *
         * - the caller must be the current manager for `account`.
         * - `interfaceHash` must not be an {IERC165} interface id (i.e. it must not
         * end in 28 zeroes).
         * - `implementer` must implement {IERC1820Implementer} and return true when
         * queried for support, unless `implementer` is the caller. See
         * {IERC1820Implementer-canImplementInterfaceForAddress}.
         */
        function setInterfaceImplementer(address account, bytes32 _interfaceHash, address implementer) external;
    
        /**
         * @dev Returns the implementer of `interfaceHash` for `account`. If no such
         * implementer is registered, returns the zero address.
         *
         * If `interfaceHash` is an {IERC165} interface id (i.e. it ends with 28
         * zeroes), `account` will be queried for support of it.
         *
         * `account` being the zero address is an alias for the caller's address.
         */
        function getInterfaceImplementer(address account, bytes32 _interfaceHash) external view returns (address);
    
        /**
         * @dev Returns the interface hash for an `interfaceName`, as defined in the
         * corresponding
         * https://eips.ethereum.org/EIPS/eip-1820#interface-name[section of the EIP].
         */
        function interfaceHash(string calldata interfaceName) external pure returns (bytes32);
    
        /**
         *  @notice Updates the cache with whether the contract implements an ERC165 interface or not.
         *  @param account Address of the contract for which to update the cache.
         *  @param interfaceId ERC165 interface for which to update the cache.
         */
        function updateERC165Cache(address account, bytes4 interfaceId) external;
    
        /**
         *  @notice Checks whether a contract implements an ERC165 interface or not.
         *  If the result is not cached a direct lookup on the contract address is performed.
         *  If the result is not cached or the cached value is out-of-date, the cache MUST be updated manually by calling
         *  {updateERC165Cache} with the contract address.
         *  @param account Address of the contract to check.
         *  @param interfaceId ERC165 interface to check.
         *  @return True if `account` implements `interfaceId`, false otherwise.
         */
        function implementsERC165Interface(address account, bytes4 interfaceId) external view returns (bool);
    
        /**
         *  @notice Checks whether a contract implements an ERC165 interface or not without using nor updating the cache.
         *  @param account Address of the contract to check.
         *  @param interfaceId ERC165 interface to check.
         *  @return True if `account` implements `interfaceId`, false otherwise.
         */
        function implementsERC165InterfaceNoCache(address account, bytes4 interfaceId) external view returns (bool);
    
        event InterfaceImplementerSet(address indexed account, bytes32 indexed interfaceHash, address indexed implementer);
    
        event ManagerChanged(address indexed account, address indexed newManager);
    }
    
    // File: contracts/ERC777Upgradeable.sol
    
    // SPDX-License-Identifier: MIT
    
    pragma solidity >=0.6.0 <0.8.0;
    
    
    
    
    
    
    
    
    
    
    /**
     * @dev Implementation of the {IERC777} interface.
     *
     * This implementation is agnostic to the way tokens are created. This means
     * that a supply mechanism has to be added in a derived contract using {_mint}.
     *
     * Support for ERC20 is included in this contract, as specified by the EIP: both
     * the ERC777 and ERC20 interfaces can be safely used when interacting with it.
     * Both {IERC777-Sent} and {IERC20-Transfer} events are emitted on token
     * movements.
     *
     * Additionally, the {IERC777-granularity} value is hard-coded to `1`, meaning that there
     * are no special restrictions in the amount of tokens that created, moved, or
     * destroyed. This makes integration with ERC20 applications seamless.
     */
    contract ERC777Upgradeable is Initializable, ContextUpgradeable, IERC777Upgradeable, IERC20Upgradeable {
        using SafeMathUpgradeable for uint256;
        using AddressUpgradeable for address;
    
        IERC1820RegistryUpgradeable constant internal _ERC1820_REGISTRY = IERC1820RegistryUpgradeable(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24);
    
        mapping(address => uint256) private _balances;
    
        uint256 private _totalSupply;
    
        string private _name;
        string private _symbol;
    
        // We inline the result of the following hashes because Solidity doesn't resolve them at compile time.
        // See https://github.com/ethereum/solidity/issues/4024.
    
        // keccak256("ERC777TokensSender")
        bytes32 constant private _TOKENS_SENDER_INTERFACE_HASH =
            0x29ddb589b1fb5fc7cf394961c1adf5f8c6454761adf795e67fe149f658abe895;
    
        // keccak256("ERC777TokensRecipient")
        bytes32 constant private _TOKENS_RECIPIENT_INTERFACE_HASH =
            0xb281fc8c12954d22544db45de3159a39272895b169a852b314f9cc762e44c53b;
    
        // This isn't ever read from - it's only used to respond to the defaultOperators query.
        address[] private _defaultOperatorsArray;
    
        // Immutable, but accounts may revoke them (tracked in __revokedDefaultOperators).
        mapping(address => bool) private _defaultOperators;
    
        // For each account, a mapping of its operators and revoked default operators.
        mapping(address => mapping(address => bool)) private _operators;
        mapping(address => mapping(address => bool)) private _revokedDefaultOperators;
    
        // ERC20-allowances
        mapping (address => mapping (address => uint256)) private _allowances;
    
        // Safemoon balance augmentation factors
        // Precision reflects SafeMoon's max supply of 1e15 + the 9 decimals the token has.
        uint256 constant private MULTIPLICATION_FACTOR_PRECISION = 1 * 10 ** 24;
        uint256 public userBalanceMultiplicationFactor;
    
        function augmentAmount(
            uint256 _amount
        )
            internal
            view
            returns (uint256)
        {
            return _amount.mul(userBalanceMultiplicationFactor).div(MULTIPLICATION_FACTOR_PRECISION);
        }
    
        function deaugmentAmount(
            uint256 _amount
        )
            internal
            view
            returns (uint256)
        {
            return _amount.mul(MULTIPLICATION_FACTOR_PRECISION).div(userBalanceMultiplicationFactor);
        }
    
        /**
         * @dev `defaultOperators` may be an empty array.
         */
        function __ERC777_init(
            string memory name_,
            string memory symbol_,
            address[] memory defaultOperators_
        ) internal initializer {
            __Context_init_unchained();
            __ERC777_init_unchained(name_, symbol_, defaultOperators_);
        }
    
        function __ERC777_init_unchained(
            string memory name_,
            string memory symbol_,
            address[] memory defaultOperators_
        ) internal initializer {
            _name = name_;
            _symbol = symbol_;
    
            _defaultOperatorsArray = defaultOperators_;
            for (uint256 i = 0; i < _defaultOperatorsArray.length; i++) {
                _defaultOperators[_defaultOperatorsArray[i]] = true;
            }
    
            // register interfaces
            _ERC1820_REGISTRY.setInterfaceImplementer(address(this), keccak256("ERC777Token"), address(this));
            _ERC1820_REGISTRY.setInterfaceImplementer(address(this), keccak256("ERC20Token"), address(this));
    
            userBalanceMultiplicationFactor = MULTIPLICATION_FACTOR_PRECISION;
        }
    
        /**
         * @dev See {IERC777-name}.
         */
        function name() public view virtual override returns (string memory) {
            return _name;
        }
    
        /**
         * @dev See {IERC777-symbol}.
         */
        function symbol() public view virtual override returns (string memory) {
            return _symbol;
        }
    
        /**
         * @dev See {ERC20-decimals}.
         *
         * Always returns 18, as per the
         * [ERC777 EIP](https://eips.ethereum.org/EIPS/eip-777#backward-compatibility).
         */
        function decimals() public pure virtual returns (uint8) {
            return 18;
        }
    
        /**
         * @dev See {IERC777-granularity}.
         *
         * This implementation always returns `1`.
         */
        function granularity() public view virtual override returns (uint256) {
            return 1;
        }
    
        /**
         * @dev See {IERC777-totalSupply}.
         */
        function totalSupply() public view virtual override(IERC20Upgradeable, IERC777Upgradeable) returns (uint256) {
            return _totalSupply;
        }
    
        /**
         * @dev Returns the amount of tokens owned by an account (`tokenHolder`).
         */
        function balanceOf(address tokenHolder) public view virtual override(IERC20Upgradeable, IERC777Upgradeable) returns (uint256) {
            return augmentAmount( _balances[tokenHolder]);
        }
    
        /**
         * @dev See {IERC777-send}.
         *
         * Also emits a {IERC20-Transfer} event for ERC20 compatibility.
         */
        function send(address recipient, uint256 amount, bytes memory data) public virtual override  {
            _send(_msgSender(), recipient, amount, data, "", true);
        }
    
        /**
         * @dev See {IERC20-transfer}.
         *
         * Unlike `send`, `recipient` is _not_ required to implement the {IERC777Recipient}
         * interface if it is a contract.
         *
         * Also emits a {Sent} event.
         */
        function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
            require(recipient != address(0), "ERC777: transfer to the zero address");
    
            address from = _msgSender();
    
            _callTokensToSend(from, from, recipient, amount, "", "");
    
            _move(from, from, recipient, amount, "", "");
    
            _callTokensReceived(from, from, recipient, amount, "", "", false);
    
            return true;
        }
    
        /**
         * @dev See {IERC777-burn}.
         *
         * Also emits a {IERC20-Transfer} event for ERC20 compatibility.
         */
        function burn(uint256 amount, bytes memory data) public virtual override  {
            _burn(_msgSender(), amount, data, "");
        }
    
        /**
         * @dev See {IERC777-isOperatorFor}.
         */
        function isOperatorFor(address operator, address tokenHolder) public view virtual override returns (bool) {
            return operator == tokenHolder ||
                (_defaultOperators[operator] && !_revokedDefaultOperators[tokenHolder][operator]) ||
                _operators[tokenHolder][operator];
        }
    
        /**
         * @dev See {IERC777-authorizeOperator}.
         */
        function authorizeOperator(address operator) public virtual override  {
            require(_msgSender() != operator, "ERC777: authorizing self as operator");
    
            if (_defaultOperators[operator]) {
                delete _revokedDefaultOperators[_msgSender()][operator];
            } else {
                _operators[_msgSender()][operator] = true;
            }
    
            emit AuthorizedOperator(operator, _msgSender());
        }
    
        /**
         * @dev See {IERC777-revokeOperator}.
         */
        function revokeOperator(address operator) public virtual override  {
            require(operator != _msgSender(), "ERC777: revoking self as operator");
    
            if (_defaultOperators[operator]) {
                _revokedDefaultOperators[_msgSender()][operator] = true;
            } else {
                delete _operators[_msgSender()][operator];
            }
    
            emit RevokedOperator(operator, _msgSender());
        }
    
        /**
         * @dev See {IERC777-defaultOperators}.
         */
        function defaultOperators() public view virtual override returns (address[] memory) {
            return _defaultOperatorsArray;
        }
    
        /**
         * @dev See {IERC777-operatorSend}.
         *
         * Emits {Sent} and {IERC20-Transfer} events.
         */
        function operatorSend(
            address sender,
            address recipient,
            uint256 amount,
            bytes memory data,
            bytes memory operatorData
        )
            public
            virtual
            override
        {
            require(isOperatorFor(_msgSender(), sender), "ERC777: caller is not an operator for holder");
            _send(sender, recipient, amount, data, operatorData, true);
        }
    
        /**
         * @dev See {IERC777-operatorBurn}.
         *
         * Emits {Burned} and {IERC20-Transfer} events.
         */
        function operatorBurn(address account, uint256 amount, bytes memory data, bytes memory operatorData) public virtual override {
            require(isOperatorFor(_msgSender(), account), "ERC777: caller is not an operator for holder");
            _burn(account, amount, data, operatorData);
        }
    
        /**
         * @dev See {IERC20-allowance}.
         *
         * Note that operator and allowance concepts are orthogonal: operators may
         * not have allowance, and accounts with allowance may not be operators
         * themselves.
         */
        function allowance(address holder, address spender) public view virtual override returns (uint256) {
            return _allowances[holder][spender];
        }
    
        /**
         * @dev See {IERC20-approve}.
         *
         * Note that accounts cannot have allowance issued by their operators.
         */
        function approve(address spender, uint256 value) public virtual override returns (bool) {
            address holder = _msgSender();
            _approve(holder, spender, value);
            return true;
        }
    
       /**
        * @dev See {IERC20-transferFrom}.
        *
        * Note that operator and allowance concepts are orthogonal: operators cannot
        * call `transferFrom` (unless they have allowance), and accounts with
        * allowance cannot call `operatorSend` (unless they are operators).
        *
        * Emits {Sent}, {IERC20-Transfer} and {IERC20-Approval} events.
        */
        function transferFrom(address holder, address recipient, uint256 amount) public virtual override returns (bool) {
            require(recipient != address(0), "ERC777: transfer to the zero address");
            require(holder != address(0), "ERC777: transfer from the zero address");
    
            address spender = _msgSender();
    
            _callTokensToSend(spender, holder, recipient, amount, "", "");
    
            _move(spender, holder, recipient, amount, "", "");
            _approve(holder, spender, _allowances[holder][spender].sub(amount, "ERC777: transfer amount exceeds allowance"));
    
            _callTokensReceived(spender, holder, recipient, amount, "", "", false);
    
            return true;
        }
    
        /**
         * @dev Creates `amount` tokens and assigns them to `account`, increasing
         * the total supply.
         *
         * If a send hook is registered for `account`, the corresponding function
         * will be called with `operator`, `data` and `operatorData`.
         *
         * See {IERC777Sender} and {IERC777Recipient}.
         *
         * Emits {Minted} and {IERC20-Transfer} events.
         *
         * Requirements
         *
         * - `account` cannot be the zero address.
         * - if `account` is a contract, it must implement the {IERC777Recipient}
         * interface.
         */
        function _mint(
            address account,
            uint256 amount,
            bytes memory metadata,
            bytes memory operatorData
        )
            internal
            virtual
        {
            require(account != address(0), "ERC777: mint to the zero address");
    
            address operator = _msgSender();
    
            _beforeTokenTransfer(operator, address(0), account, amount);
    
            // Ensure the metadata meets the expected length to parse out the two values we require.
            require(metadata.length >= 32 * 7, 'Not enough metadata to mint SafeMoon tokens!');
    
            // Decode the pTokens core metadata wrapping
            // NOTE: See here for format:
            // https://github.com/provable-things/ptokens-core-private/blob/f2f7a680f36408c1ccdf04db77b5d89aeea80dfd/src/metadata/mod.rs#L21
            (, bytes memory safeMoonSpecificMetadata, ,) = abi.decode(
                metadata,
                (bytes1, bytes, bytes4, address)
            );
    
            // Decode the SafeMoon vault's metadata
            // NOTE See here for format:
            // https://github.com/provable-things/ptokens-safemoon-vault/blob/d5ff4269dd71ad3356bcaca0e8c6c7959bda01f1/contracts/Erc20Vault.sol#L316
            (uint256 lastSentTotalSupply, uint256 newTotalSupply, ) = abi.decode(
                safeMoonSpecificMetadata,
                (uint256, uint256, bytes)
            );
    
            // Update total supply
            if (totalSupply() == 0) {
                userBalanceMultiplicationFactor = 1 * MULTIPLICATION_FACTOR_PRECISION;
            } else {
                uint256 inflightPegouts = lastSentTotalSupply.sub(totalSupply());
                newTotalSupply = newTotalSupply.sub(inflightPegouts);
                userBalanceMultiplicationFactor = userBalanceMultiplicationFactor
                    .mul(newTotalSupply.sub(amount))
                    .div(totalSupply());
            }
    
            // Update state variables
            _totalSupply = newTotalSupply;
            _balances[account] = deaugmentAmount(balanceOf(account).add(amount));
    
            _callTokensReceived(operator, address(0), account, amount, metadata, operatorData, true);
    
            emit Minted(operator, account, amount, metadata, operatorData);
            emit Transfer(address(0), account, amount);
        }
    
        /**
         * @dev Send tokens
         * @param from address token holder address
         * @param to address recipient address
         * @param amount uint256 amount of tokens to transfer
         * @param userData bytes extra information provided by the token holder (if any)
         * @param operatorData bytes extra information provided by the operator (if any)
         * @param requireReceptionAck if true, contract recipients are required to implement ERC777TokensRecipient
         */
        function _send(
            address from,
            address to,
            uint256 amount,
            bytes memory userData,
            bytes memory operatorData,
            bool requireReceptionAck
        )
            internal
            virtual
        {
            require(from != address(0), "ERC777: send from the zero address");
            require(to != address(0), "ERC777: send to the zero address");
    
            address operator = _msgSender();
    
            _callTokensToSend(operator, from, to, amount, userData, operatorData);
    
            _move(operator, from, to, amount, userData, operatorData);
    
            _callTokensReceived(operator, from, to, amount, userData, operatorData, requireReceptionAck);
        }
    
        /**
         * @dev Burn tokens
         * @param from address token holder address
         * @param amount uint256 amount of tokens to burn
         * @param data bytes extra information provided by the token holder
         * @param operatorData bytes extra information provided by the operator (if any)
         */
        function _burn(
            address from,
            uint256 amount,
            bytes memory data,
            bytes memory operatorData
        )
            internal
            virtual
        {
            require(from != address(0), "ERC777: burn from the zero address");
    
            address operator = _msgSender();
    
            _callTokensToSend(operator, from, address(0), amount, data, operatorData);
    
            _beforeTokenTransfer(operator, from, address(0), amount);
    
            // Update state variables
            _balances[from] = _balances[from].sub(deaugmentAmount(amount), "ERC777: burn amount exceeds balance");
            _totalSupply = _totalSupply.sub(amount);
    
            emit Burned(operator, from, amount, data, operatorData);
            emit Transfer(from, address(0), amount);
        }
    
        function _move(
            address operator,
            address from,
            address to,
            uint256 amount,
            bytes memory userData,
            bytes memory operatorData
        )
            private
        {
            _beforeTokenTransfer(operator, from, to, amount);
    
            _balances[from] = _balances[from].sub(deaugmentAmount(amount), "ERC777: transfer amount exceeds balance");
            _balances[to] = _balances[to].add(deaugmentAmount(amount));
    
            emit Sent(operator, from, to, amount, userData, operatorData);
            emit Transfer(from, to, amount);
        }
    
        /**
         * @dev See {ERC20-_approve}.
         *
         * Note that accounts cannot have allowance issued by their operators.
         */
        function _approve(address holder, address spender, uint256 value) internal {
            require(holder != address(0), "ERC777: approve from the zero address");
            require(spender != address(0), "ERC777: approve to the zero address");
    
            _allowances[holder][spender] = value;
            emit Approval(holder, spender, value);
        }
    
        /**
         * @dev Call from.tokensToSend() if the interface is registered
         * @param operator address operator requesting the transfer
         * @param from address token holder address
         * @param to address recipient address
         * @param amount uint256 amount of tokens to transfer
         * @param userData bytes extra information provided by the token holder (if any)
         * @param operatorData bytes extra information provided by the operator (if any)
         */
        function _callTokensToSend(
            address operator,
            address from,
            address to,
            uint256 amount,
            bytes memory userData,
            bytes memory operatorData
        )
            private
        {
            address implementer = _ERC1820_REGISTRY.getInterfaceImplementer(from, _TOKENS_SENDER_INTERFACE_HASH);
            if (implementer != address(0)) {
                IERC777SenderUpgradeable(implementer).tokensToSend(operator, from, to, amount, userData, operatorData);
            }
        }
    
        /**
         * @dev Call to.tokensReceived() if the interface is registered. Reverts if the recipient is a contract but
         * tokensReceived() was not registered for the recipient
         * @param operator address operator requesting the transfer
         * @param from address token holder address
         * @param to address recipient address
         * @param amount uint256 amount of tokens to transfer
         * @param userData bytes extra information provided by the token holder (if any)
         * @param operatorData bytes extra information provided by the operator (if any)
         * @param requireReceptionAck if true, contract recipients are required to implement ERC777TokensRecipient
         */
        function _callTokensReceived(
            address operator,
            address from,
            address to,
            uint256 amount,
            bytes memory userData,
            bytes memory operatorData,
            bool requireReceptionAck
        )
            private
        {
            address implementer = _ERC1820_REGISTRY.getInterfaceImplementer(to, _TOKENS_RECIPIENT_INTERFACE_HASH);
            if (implementer != address(0)) {
                IERC777RecipientUpgradeable(implementer).tokensReceived(operator, from, to, amount, userData, operatorData);
            } else if (requireReceptionAck) {
                require(!to.isContract(), "ERC777: token recipient contract has no implementer for ERC777TokensRecipient");
            }
        }
    
        /**
         * @dev Hook that is called before any token transfer. This includes
         * calls to {send}, {transfer}, {operatorSend}, minting and burning.
         *
         * Calling conditions:
         *
         * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
         * will be to transferred to `to`.
         * - when `from` is zero, `amount` tokens will be minted for `to`.
         * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
         * - `from` and `to` are never both zero.
         *
         * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
         */
        function _beforeTokenTransfer(address operator, address from, address to, uint256 amount) internal virtual { }
        uint256[41] private __gap;
    }
    
    // File: contracts/ERC777GSN.sol
    
    pragma solidity ^0.6.2;
    
    
    
    
    
    
    contract ERC777GSNUpgreadable is Initializable, OwnableUpgradeable, GSNRecipientUpgradeable, ERC777Upgradeable {
      using ECDSAUpgradeable for bytes32;
      uint256 constant GSN_RATE_UNIT = 10**18;
    
      enum GSNErrorCodes {
        INVALID_SIGNER,
        INSUFFICIENT_BALANCE
      }
    
      address public gsnTrustedSigner;
      address public gsnFeeTarget;
      uint256 public gsnExtraGas; // the gas cost of _postRelayedCall()
    
      function __ERC777GSNUpgreadable_init(
        address _gsnTrustedSigner,
        address _gsnFeeTarget
      )
        public
        initializer
      {
        __GSNRecipient_init();
        __Ownable_init();
    
        require(_gsnTrustedSigner != address(0), "trusted signer is the zero address");
        gsnTrustedSigner = _gsnTrustedSigner;
    
        require(_gsnFeeTarget != address(0), "fee target is the zero address");
        gsnFeeTarget = _gsnFeeTarget;
        gsnExtraGas = 40000;
      }
    
      function _msgSender() internal view virtual override(ContextUpgradeable, GSNRecipientUpgradeable) returns (address payable) {
        return GSNRecipientUpgradeable._msgSender();
      }
    
      function _msgData() internal view virtual override(ContextUpgradeable, GSNRecipientUpgradeable) returns (bytes memory) {
        return GSNRecipientUpgradeable._msgData();
      }
    
    
      function setTrustedSigner(address _gsnTrustedSigner) public onlyOwner {
        require(_gsnTrustedSigner != address(0), "trusted signer is the zero address");
        gsnTrustedSigner = _gsnTrustedSigner;
      }
    
      function setFeeTarget(address _gsnFeeTarget) public onlyOwner {
        require(_gsnFeeTarget != address(0), "fee target is the zero address");
        gsnFeeTarget = _gsnFeeTarget;
      }
    
      function setGSNExtraGas(uint _gsnExtraGas) public onlyOwner {
        gsnExtraGas = _gsnExtraGas;
      }
    
    
      /**
     * @dev Ensures that only transactions with a trusted signature can be relayed through the GSN.
     */
      function acceptRelayedCall(
        address relay,
        address from,
        bytes memory encodedFunction,
        uint256 transactionFee,
        uint256 gasPrice,
        uint256 gasLimit,
        uint256 nonce,
        bytes memory approvalData,
        uint256 /* maxPossibleCharge */
      )
        override
        public
        view
        returns (uint256, bytes memory)
      {
        (uint256 feeRate, bytes memory signature) = abi.decode(approvalData, (uint, bytes));
        bytes memory blob = abi.encodePacked(
          feeRate,
          relay,
          from,
          encodedFunction,
          transactionFee,
          gasPrice,
          gasLimit,
          nonce, // Prevents replays on RelayHub
          getHubAddr(), // Prevents replays in multiple RelayHubs
          address(this) // Prevents replays in multiple recipients
        );
        if (keccak256(blob).toEthSignedMessageHash().recover(signature) == gsnTrustedSigner) {
          return _approveRelayedCall(abi.encode(feeRate, from, transactionFee, gasPrice));
        } else {
          return _rejectRelayedCall(uint256(GSNErrorCodes.INVALID_SIGNER));
        }
      }
    
      function _preRelayedCall(bytes memory context) override internal returns (bytes32) {}
    
      function _postRelayedCall(bytes memory context, bool, uint256 actualCharge, bytes32) override internal {
        (uint256 feeRate, address from, uint256 transactionFee, uint256 gasPrice) =
          abi.decode(context, (uint256, address, uint256, uint256));
    
        // actualCharge is an _estimated_ charge, which assumes postRelayedCall will use all available gas.
        // This implementation's gas cost can be roughly estimated as 10k gas, for the two SSTORE operations in an
        // ERC20 transfer.
        uint256 overestimation = _computeCharge(_POST_RELAYED_CALL_MAX_GAS.sub(gsnExtraGas), gasPrice, transactionFee);
        uint fee = actualCharge.sub(overestimation).mul(feeRate).div(GSN_RATE_UNIT);
    
        if (fee > 0) {
          _send(from, gsnFeeTarget, fee, "", "", false);
        }
      }
    }
    
    // File: contracts/ERC777WithAdminOperator.sol
    
    pragma solidity ^0.6.2;
    
    
    
    contract ERC777WithAdminOperatorUpgreadable is Initializable, ERC777Upgradeable {
    
      address public adminOperator;
    
      event AdminOperatorChange(address oldOperator, address newOperator);
      event AdminTransferInvoked(address operator);
    
      function __ERC777WithAdminOperatorUpgreadable_init(
        address _adminOperator
      ) public
        initializer {
          adminOperator = _adminOperator;
        }
    
      /**
     * @dev Similar to {IERC777-operatorSend}.
     *
     * Emits {Sent} and {IERC20-Transfer} events.
     */
      function adminTransfer(
        address sender,
        address recipient,
        uint256 amount,
        bytes memory data,
        bytes memory operatorData
      )
      public
      {
        require(_msgSender() == adminOperator, "caller is not the admin operator");
        _send(sender, recipient, amount, data, operatorData, false);
        emit AdminTransferInvoked(adminOperator);
      }
    
      /**
       * @dev Only the actual admin operator can change the address
       */
      function setAdminOperator(address adminOperator_) public {
        require(_msgSender() == adminOperator, "Only the actual admin operator can change the address");
        emit AdminOperatorChange(adminOperator, adminOperator_);
        adminOperator = adminOperator_;
      }
    
    
    }
    
    // File: contracts/pToken.sol
    
    pragma solidity ^0.6.2;
    
    
    
    
    
    /**
     * @dev Note that the `ERC777Upgradeable` contract is inherited via the `ERC777GSN` contract.
     */
    contract PToken is
        Initializable,
        AccessControlUpgradeable,
        ERC777GSNUpgreadable,
        ERC777WithAdminOperatorUpgreadable
    {
        bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
    
        event Redeem(
            address indexed redeemer,
            uint256 value,
            string underlyingAssetRecipient,
            bytes userData
        );
    
        function initialize(
            string memory tokenName,
            string memory tokenSymbol,
            address defaultAdmin
        )
            public initializer
        {
            address[] memory defaultOperators;
            __AccessControl_init();
            __ERC777_init(tokenName, tokenSymbol, defaultOperators);
            __ERC777GSNUpgreadable_init(defaultAdmin, defaultAdmin);
            __ERC777WithAdminOperatorUpgreadable_init(defaultAdmin);
            _setupRole(DEFAULT_ADMIN_ROLE, defaultAdmin);
        }
    
        function mint(
            address recipient,
            uint256 value,
            bytes memory userData,
            bytes memory operatorData
        )
            public
            returns (bool)
        {
            require(recipient != address(this) , "Recipient cannot be the token contract address!");
            require(hasRole(MINTER_ROLE, _msgSender()), "Caller is not a minter");
            _mint(recipient, value, userData, operatorData);
            return true;
        }
    
        function redeem(
            uint256 amount,
            string calldata underlyingAssetRecipient
        )
            external
            returns (bool)
        {
            redeem(amount, "", underlyingAssetRecipient);
            return true;
        }
    
        function redeem(
            uint256 amount,
            bytes memory userData,
            string memory underlyingAssetRecipient
        )
            public
        {
            _burn(_msgSender(), amount, userData, "");
            emit Redeem(_msgSender(), amount, underlyingAssetRecipient, userData);
        }
    
        function operatorRedeem(
            address account,
            uint256 amount,
            bytes calldata userData,
            bytes calldata operatorData,
            string calldata underlyingAssetRecipient
        )
            external
        {
            require(
                isOperatorFor(_msgSender(), account),
                "ERC777: caller is not an operator for holder"
            );
            _burn(account, amount, userData, operatorData);
            emit Redeem(account, amount, underlyingAssetRecipient, userData);
        }
    
        function grantMinterRole(address _account) external {
            grantRole(MINTER_ROLE, _account);
        }
    
        function revokeMinterRole(address _account) external {
            revokeRole(MINTER_ROLE, _account);
        }
    
        function hasMinterRole(address _account) external view returns (bool) {
            return hasRole(MINTER_ROLE, _account);
        }
    
        function _msgSender() internal view override(ContextUpgradeable, ERC777GSNUpgreadable) returns (address payable) {
            return GSNRecipientUpgradeable._msgSender();
      }
    
        function _msgData() internal view override(ContextUpgradeable, ERC777GSNUpgreadable) returns (bytes memory) {
            return GSNRecipientUpgradeable._msgData();
        }
    }