ETH Price: $1,977.40 (-5.29%)

Transaction Decoder

Block:
15687935 at Oct-06-2022 08:41:23 AM +UTC
Transaction Fee:
0.001354484828776028 ETH $2.68
Gas Used:
211,031 Gas / 6.418416388 Gwei

Emitted Events:

248 TransparentUpgradeableProxy.0xa9f51105abd325b0b448a253f5a627f7e2b99b1f2ff238d0779128d29ac3d85b( 0xa9f51105abd325b0b448a253f5a627f7e2b99b1f2ff238d0779128d29ac3d85b, 0000000000000000000000000000000000000000000000000000000000000000, 0000000000000000000000000000000000000000000000000000000000000002, 00000000000000000000000069f36ea1ebf4ec9e53e3aabf11caf62b034ff3ee, 000000000000000000000000000000000000000000000000000000000000ab9b, 0000000000000000000000000000000000000000000000000000000000000001, 000000000000000000000000000000000000000000000ec917ed6691f0b02000 )
249 TransparentUpgradeableProxy.0xa9f51105abd325b0b448a253f5a627f7e2b99b1f2ff238d0779128d29ac3d85b( 0xa9f51105abd325b0b448a253f5a627f7e2b99b1f2ff238d0779128d29ac3d85b, 0000000000000000000000000000000000000000000000000000000000000000, 0000000000000000000000000000000000000000000000000000000000000001, 00000000000000000000000069f36ea1ebf4ec9e53e3aabf11caf62b034ff3ee, 000000000000000000000000000000000000000000000000000000000000ab89, 0000000000000000000000000000000000000000000000000000000000000001, 000000000000000000000000000000000000000000000000146b43928e670000 )
250 TransparentUpgradeableProxy.0xa9f51105abd325b0b448a253f5a627f7e2b99b1f2ff238d0779128d29ac3d85b( 0xa9f51105abd325b0b448a253f5a627f7e2b99b1f2ff238d0779128d29ac3d85b, 0000000000000000000000000000000000000000000000000000000000000000, 0000000000000000000000000000000000000000000000000000000000000000, 00000000000000000000000069f36ea1ebf4ec9e53e3aabf11caf62b034ff3ee, 000000000000000000000000000000000000000000000000000000000000afc5, 0000000000000000000000000000000000000000000000000000000000000001, 00000000000000000000000000000000000000000000000001609b5961f52000 )

Account State Difference:

  Address   Before After State Difference Code
(Lido: Execution Layer Rewards Vault)
230.536735016255428054 Eth230.536964327371160737 Eth0.000229311115732683
0x69f36eA1...b034ff3eE
28.908660288275219912 Eth
Nonce: 62817
27.435961803446443884 Eth
Nonce: 62818
1.472698484828776028
0xE544cF99...0D135aa03 22.026598000005443978 Eth23.497942000005443978 Eth1.471344

Execution Trace

ETH 1.471344 TransparentUpgradeableProxy.7ba3033d( )
  • ETH 1.471344 NestBatchPlatform2.post( channelId=0, scale=1, equivalents=[99250000000000000, 1471344000000000000, 69822650466000000000000] )
    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: NestBatchPlatform2
    // Sources flattened with hardhat v2.6.5 https://hardhat.org
    
    // File contracts/libs/TransferHelper.sol
    
    // SPDX-License-Identifier: GPL-3.0-or-later
    
    pragma solidity ^0.8.6;
    
    // helper methods for interacting with ERC20 tokens and sending ETH that do not consistently return true/false
    library TransferHelper {
        function safeApprove(address token, address to, uint value) internal {
            // bytes4(keccak256(bytes('approve(address,uint)')));
            (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
            require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: APPROVE_FAILED');
        }
    
        function safeTransfer(address token, address to, uint value) internal {
            // bytes4(keccak256(bytes('transfer(address,uint)')));
            (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
            require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FAILED');
        }
    
        function safeTransferFrom(address token, address from, address to, uint value) internal {
            // bytes4(keccak256(bytes('transferFrom(address,address,uint)')));
            (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
            require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FROM_FAILED');
        }
    
        function safeTransferETH(address to, uint value) internal {
            (bool success,) = to.call{value:value,gas:5000}(new bytes(0));
            require(success, 'TransferHelper: ETH_TRANSFER_FAILED');
        }
    }
    
    
    // File contracts/interfaces/INestBatchPriceView.sol
    
    // GPL-3.0-or-later
    
    pragma solidity ^0.8.6;
    
    /// @dev This contract implemented the mining logic of nest
    interface INestBatchPriceView {
        
        /// @dev Get the latest trigger price
        /// @param channelId Target channelId
        /// @param pairIndex Target pairIndex
        /// @return blockNumber The block number of price
        /// @return price The token price. (1eth equivalent to (price) token)
        function triggeredPrice(uint channelId, uint pairIndex) external view returns (uint blockNumber, uint price);
    
        /// @dev Get the full information of latest trigger price
        /// @param channelId Target channelId
        /// @param pairIndex Target pairIndex
        /// @return blockNumber The block number of price
        /// @return price The token price. (1eth equivalent to (price) token)
        /// @return avgPrice Average price
        /// @return sigmaSQ The square of the volatility (18 decimal places). The current implementation assumes that 
        ///         the volatility cannot exceed 1. Correspondingly, when the return value is equal to 999999999999996447,
        ///         it means that the volatility has exceeded the range that can be expressed
        function triggeredPriceInfo(uint channelId, uint pairIndex) external view returns (
            uint blockNumber,
            uint price,
            uint avgPrice,
            uint sigmaSQ
        );
    
        /// @dev Find the price at block number
        /// @param channelId Target channelId
        /// @param pairIndex Target pairIndex
        /// @param height Destination block number
        /// @return blockNumber The block number of price
        /// @return price The token price. (1eth equivalent to (price) token)
        function findPrice(
            uint channelId, 
            uint pairIndex,
            uint height
        ) external view returns (uint blockNumber, uint price);
    
        /// @dev Get the last (num) effective price
        /// @param channelId Target channelId
        /// @param pairIndex Target pairIndex
        /// @param count The number of prices that want to return
        /// @return An array which length is num * 2, each two element expresses one price like blockNumber|price
        function lastPriceList(uint channelId, uint pairIndex, uint count) external view returns (uint[] memory);
    
        /// @dev Returns lastPriceList and triggered price info
        /// @param channelId Target channelId
        /// @param pairIndex Target pairIndex
        /// @param count The number of prices that want to return
        /// @return prices An array which length is num * 2, each two element expresses one price like blockNumber|price
        /// @return triggeredPriceBlockNumber The block number of triggered price
        /// @return triggeredPriceValue The token triggered price. (1eth equivalent to (price) token)
        /// @return triggeredAvgPrice Average price
        /// @return triggeredSigmaSQ The square of the volatility (18 decimal places). The current implementation 
        /// assumes that the volatility cannot exceed 1. Correspondingly, when the return value is equal to 
        /// 999999999999996447, it means that the volatility has exceeded the range that can be expressed
        function lastPriceListAndTriggeredPriceInfo(uint channelId, uint pairIndex, uint count) external view 
        returns (
            uint[] memory prices,
            uint triggeredPriceBlockNumber,
            uint triggeredPriceValue,
            uint triggeredAvgPrice,
            uint triggeredSigmaSQ
        );
    }
    
    
    // File contracts/interfaces/INestBatchPrice2.sol
    
    // GPL-3.0-or-later
    
    pragma solidity ^0.8.6;
    
    /// @dev This contract implemented the mining logic of nest
    interface INestBatchPrice2 {
    
        /// @dev Get the latest trigger price
        /// @param channelId Target channelId
        /// @param pairIndices Array of pair indices
        /// @param payback Address to receive refund
        /// @return prices Price array, i * 2 is the block where the ith price is located, and i * 2 + 1 is the ith price
        function triggeredPrice(
            uint channelId,
            uint[] calldata pairIndices, 
            address payback
        ) external payable returns (uint[] memory prices);
    
        /// @dev Get the full information of latest trigger price
        /// @param channelId Target channelId
        /// @param pairIndices Array of pair indices
        /// @param payback Address to receive refund
        /// @return prices Price array, i * 4 is the block where the ith price is located, i * 4 + 1 is the ith price,
        /// i * 4 + 2 is the ith average price and i * 4 + 3 is the ith volatility
        function triggeredPriceInfo(
            uint channelId, 
            uint[] calldata pairIndices,
            address payback
        ) external payable returns (uint[] memory prices);
    
        /// @dev Find the price at block number
        /// @param channelId Target channelId
        /// @param pairIndices Array of pair indices
        /// @param height Destination block number
        /// @param payback Address to receive refund
        /// @return prices Price array, i * 2 is the block where the ith price is located, and i * 2 + 1 is the ith price
        function findPrice(
            uint channelId,
            uint[] calldata pairIndices, 
            uint height, 
            address payback
        ) external payable returns (uint[] memory prices);
    
        /// @dev Get the last (num) effective price
        /// @param channelId Target channelId
        /// @param pairIndices Array of pair indices
        /// @param count The number of prices that want to return
        /// @param payback Address to receive refund
        /// @return prices Result array, i * count * 2 to (i + 1) * count * 2 - 1 are 
        /// the price results of group i quotation pairs
        function lastPriceList(
            uint channelId, 
            uint[] calldata pairIndices, 
            uint count, 
            address payback
        ) external payable returns (uint[] memory prices);
    
        /// @dev Returns lastPriceList and triggered price info
        /// @param channelId Target channelId
        /// @param pairIndices Array of pair indices
        /// @param count The number of prices that want to return
        /// @param payback Address to receive refund
        /// @return prices result of group i quotation pair. Among them, the first two count * are the latest prices, 
        /// and the last four are: trigger price block number, trigger price, average price and volatility
        function lastPriceListAndTriggeredPriceInfo(
            uint channelId, 
            uint[] calldata pairIndices,
            uint count, 
            address payback
        ) external payable returns (uint[] memory prices);
    }
    
    
    // File contracts/libs/IERC20.sol
    
    // MIT
    
    pragma solidity ^0.8.6;
    
    /**
     * @dev Interface of the ERC20 standard as defined in the EIP.
     */
    interface IERC20 {
        /**
         * @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 contracts/interfaces/INestBatchMining.sol
    
    // GPL-3.0-or-later
    
    pragma solidity ^0.8.6;
    
    /// @dev This interface defines the mining methods for nest
    interface INestBatchMining {
        
        /// @dev PriceChannel open event
        /// @param channelId Target channelId
        /// @param token0 Address of token0, use to mensuration, 0 means eth
        /// @param unit Unit of token0
        /// @param reward Reward token address
        event Open(uint channelId, address token0, uint unit, address reward);
    
        /// @dev Post event
        /// @param channelId Target channelId
        /// @param pairIndex Target pairIndex
        /// @param miner Address of miner
        /// @param index Index of the price sheet
        /// @param scale Scale of this post. (Which times of unit)
        event Post(uint channelId, uint pairIndex, address miner, uint index, uint scale, uint price);
    
        /* ========== Structures ========== */
        
        /// @dev Nest mining configuration structure
        struct Config {
            
            // -- Public configuration
            // The number of times the sheet assets have doubled. 4
            uint8 maxBiteNestedLevel;
            
            // Price effective block interval. 20
            uint16 priceEffectSpan;
    
            // The amount of nest to pledge for each post (Unit: 1000). 100
            uint16 pledgeNest;
        }
    
        /// @dev PriceSheetView structure
        struct PriceSheetView {
            
            // Index of the price sheet
            uint32 index;
    
            // Address of miner
            address miner;
    
            // The block number of this price sheet packaged
            uint32 height;
    
            // The remain number of this price sheet
            uint32 remainNum;
    
            // The eth number which miner will got
            uint32 ethNumBal;
    
            // The eth number which equivalent to token's value which miner will got
            uint32 tokenNumBal;
    
            // The pledged number of nest in this sheet. (Unit: 1000nest)
            uint24 nestNum1k;
    
            // The level of this sheet. 0 expresses initial price sheet, a value greater than 0 expresses bite price sheet
            uint8 level;
    
            // Post fee shares, if there are many sheets in one block, this value is used to divide up mining value
            uint8 shares;
    
            // The token price. (1eth equivalent to (price) token)
            uint152 price;
        }
    
        // Price channel configuration
        struct ChannelConfig {
    
            // Reward per block standard
            uint96 rewardPerBlock;
    
            // Post fee(0.0001eth, DIMI_ETHER). 1000
            uint16 postFeeUnit;
    
            // Single query fee (0.0001 ether, DIMI_ETHER). 100
            uint16 singleFee;
    
            // Reduction rate(10000 based). 8000
            uint16 reductionRate;
        }
    
        /// @dev PricePair view
        struct PairView {
            // Target token address
            address target;
            // Count of price sheets
            uint96 sheetCount;
        }
    
        /// @dev Price channel view
        struct PriceChannelView {
            
            uint channelId;
    
            // Address of token0, use to mensuration, 0 means eth
            address token0;
            // Unit of token0
            uint96 unit;
    
            // Reward token address
            address reward;
            // Reward per block standard
            uint96 rewardPerBlock;
    
            // Reward total
            uint128 vault;
            // The information of mining fee
            uint96 rewards;
            // Post fee(0.0001eth, DIMI_ETHER). 1000
            uint16 postFeeUnit;
            // Count of price pairs in this channel
            uint16 count;
    
            // Address of opener
            address opener;
            // Genesis block of this channel
            uint32 genesisBlock;
            // Single query fee (0.0001 ether, DIMI_ETHER). 100
            uint16 singleFee;
            // Reduction rate(10000 based). 8000
            uint16 reductionRate;
            
            // Price pair array
            PairView[] pairs;
        }
    
        /* ========== Configuration ========== */
    
        /// @dev Modify configuration
        /// @param config Configuration object
        function setConfig(Config calldata config) external;
    
        /// @dev Get configuration
        /// @return Configuration object
        function getConfig() external view returns (Config memory);
    
        /// @dev Open price channel
        /// @param token0 Address of token0, use to mensuration, 0 means eth
        /// @param unit Unit of token0
        /// @param reward Reward token address
        /// @param tokens Target tokens
        /// @param config Channel configuration
        function open(
            address token0, 
            uint96 unit, 
            address reward, 
            address[] calldata tokens,
            ChannelConfig calldata config
        ) external;
    
        /// @dev Modify channel configuration
        /// @param channelId Target channelId
        /// @param config Channel configuration
        function modify(uint channelId, ChannelConfig calldata config) external;
    
        /// @dev Increase vault to channel
        /// @param channelId Target channelId
        /// @param vault Total to increase
        function increase(uint channelId, uint128 vault) external payable;
    
        /// @dev Decrease vault from channel
        /// @param channelId Target channelId
        /// @param vault Total to decrease
        function decrease(uint channelId, uint128 vault) external;
    
        /// @dev Get channel information
        /// @param channelId Target channelId
        /// @return Information of channel
        function getChannelInfo(uint channelId) external view returns (PriceChannelView memory);
    
        /// @dev Post price
        /// @param channelId Target channelId
        /// @param scale Scale of this post. (Which times of unit)
        /// @param equivalents Price array, one to one with pairs
        function post(uint channelId, uint scale, uint[] calldata equivalents) external payable;
    
        /// @notice Call the function to buy TOKEN/NTOKEN from a posted price sheet
        /// @dev bite TOKEN(NTOKEN) by ETH,  (+ethNumBal, -tokenNumBal)
        /// @param channelId Target price channelId
        /// @param pairIndex Target pairIndex. When take token0, use pairIndex direct, or add 65536 conversely
        /// @param index The position of the sheet in priceSheetList[token]
        /// @param takeNum The amount of biting (in the unit of ETH), realAmount = takeNum * newTokenAmountPerEth
        /// @param newEquivalent The new price of token (1 ETH : some TOKEN), here some means newTokenAmountPerEth
        function take(uint channelId, uint pairIndex, uint index, uint takeNum, uint newEquivalent) external payable;
    
        /// @dev List sheets by page
        /// @param channelId Target channelId
        /// @param pairIndex Target pairIndex
        /// @param offset Skip previous (offset) records
        /// @param count Return (count) records
        /// @param order Order. 0 reverse order, non-0 positive order
        /// @return List of price sheets
        function list(
            uint channelId, 
            uint pairIndex, 
            uint offset, 
            uint count, 
            uint order
        ) external view returns (PriceSheetView[] memory);
    
        /// @notice Close a batch of price sheets passed VERIFICATION-PHASE
        /// @dev Empty sheets but in VERIFICATION-PHASE aren't allowed
        /// @param channelId Target channelId
        /// @param indices Two-dimensional array of sheet indices, first means pair indices, seconds means sheet indices
        function close(uint channelId, uint[][] calldata indices) external;
    
        /// @dev View the number of assets specified by the user
        /// @param tokenAddress Destination token address
        /// @param addr Destination address
        /// @return Number of assets
        function balanceOf(address tokenAddress, address addr) external view returns (uint);
    
        /// @dev Withdraw assets
        /// @param tokenAddress Destination token address
        /// @param value The value to withdraw
        function withdraw(address tokenAddress, uint value) external;
    
        /// @dev Estimated mining amount
        /// @param channelId Target channelId
        /// @return Estimated mining amount
        function estimate(uint channelId) external view returns (uint);
    
        /// @dev Query the quantity of the target quotation
        /// @param channelId Target channelId
        /// @param index The index of the sheet
        /// @return minedBlocks Mined block period from previous block
        /// @return totalShares Total shares of sheets in the block
        function getMinedBlocks(
            uint channelId,
            uint index
        ) external view returns (uint minedBlocks, uint totalShares);
    
        /// @dev The function returns eth rewards of specified ntoken
        /// @param channelId Target channelId
        function totalETHRewards(uint channelId) external view returns (uint);
    
        /// @dev Pay
        /// @param channelId Target channelId
        /// @param to Address to receive
        /// @param value Amount to receive
        function pay(uint channelId, address to, uint value) external;
    
        /// @dev Donate to dao
        /// @param channelId Target channelId
        /// @param value Amount to receive
        function donate(uint channelId, uint value) external;
    }
    
    
    // File contracts/interfaces/INestLedger.sol
    
    // GPL-3.0-or-later
    
    pragma solidity ^0.8.6;
    
    /// @dev This interface defines the nest ledger methods
    interface INestLedger {
    
        /// @dev Application Flag Changed event
        /// @param addr DAO application contract address
        /// @param flag Authorization flag, 1 means authorization, 0 means cancel authorization
        event ApplicationChanged(address addr, uint flag);
        
        /// @dev Set DAO application
        /// @param addr DAO application contract address
        /// @param flag Authorization flag, 1 means authorization, 0 means cancel authorization
        function setApplication(address addr, uint flag) external;
    
        /// @dev Check DAO application flag
        /// @param addr DAO application contract address
        /// @return Authorization flag, 1 means authorization, 0 means cancel authorization
        function checkApplication(address addr) external view returns (uint);
    
        /// @dev Add reward
        /// @param channelId Target channelId
        function addETHReward(uint channelId) external payable;
    
        /// @dev The function returns eth rewards of specified ntoken
        /// @param channelId Target channelId
        function totalETHRewards(uint channelId) external view returns (uint);
    
        /// @dev Pay
        /// @param channelId Target channelId
        /// @param tokenAddress Token address of receiving funds (0 means ETH)
        /// @param to Address to receive
        /// @param value Amount to receive
        function pay(uint channelId, address tokenAddress, address to, uint value) external;
    }
    
    
    // File contracts/interfaces/INToken.sol
    
    // GPL-3.0-or-later
    
    pragma solidity ^0.8.6;
    
    /// @dev ntoken interface
    interface INToken {
            
        event Transfer(address indexed from, address indexed to, uint256 value);
        event Approval(address indexed owner, address indexed spender, uint256 value);
    
        /// @dev Mint 
        /// @param value The amount of NToken to add
        function increaseTotal(uint256 value) external;
    
        /// @notice The view of variables about minting 
        /// @dev The naming follows nest v3.0
        /// @return createBlock The block number where the contract was created
        /// @return recentlyUsedBlock The block number where the last minting went
        function checkBlockInfo() external view returns(uint256 createBlock, uint256 recentlyUsedBlock);
    
        /// @dev The ABI keeps unchanged with old NTokens, so as to support token-and-ntoken-mining
        /// @return The address of bidder
        function checkBidder() external view returns(address);
        
        /// @notice The view of totalSupply
        /// @return The total supply of ntoken
        function totalSupply() external view returns (uint256);
    
        /// @dev The view of balances
        /// @param owner The address of an account
        /// @return The balance of the account
        function balanceOf(address owner) external view returns (uint256);
    
        function allowance(address owner, address spender) external view returns (uint256); 
    
        function transfer(address to, uint256 value) external returns (bool);
    
        function approve(address spender, uint256 value) external returns (bool);
    
        function transferFrom(address from, address to, uint256 value) external returns (bool);
    }
    
    
    // File contracts/custom/ChainConfig.sol
    
    // GPL-3.0-or-later
    
    pragma solidity ^0.8.6;
    
    /// @dev Base contract of nest
    contract ChainConfig {
    
        // ******** Ethereum ******** //
    
        // Ethereum average block time interval, 14000 milliseconds
        uint constant ETHEREUM_BLOCK_TIMESPAN = 14000;
    
        // Nest ore drawing attenuation interval. 2400000 blocks, about one year
        uint constant NEST_REDUCTION_SPAN = 2400000;
        // The decay limit of nest ore drawing becomes stable after exceeding this interval. 
        // 24 million blocks, about 10 years
        uint constant NEST_REDUCTION_LIMIT = 24000000; //NEST_REDUCTION_SPAN * 10;
        // Attenuation gradient array, each attenuation step value occupies 16 bits. The attenuation value is an integer
        uint constant NEST_REDUCTION_STEPS = 0x280035004300530068008300A300CC010001400190;
            // 0
            // | (uint(400 / uint(1)) << (16 * 0))
            // | (uint(400 * 8 / uint(10)) << (16 * 1))
            // | (uint(400 * 8 * 8 / uint(10 * 10)) << (16 * 2))
            // | (uint(400 * 8 * 8 * 8 / uint(10 * 10 * 10)) << (16 * 3))
            // | (uint(400 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10)) << (16 * 4))
            // | (uint(400 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10)) << (16 * 5))
            // | (uint(400 * 8 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10 * 10)) << (16 * 6))
            // | (uint(400 * 8 * 8 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10 * 10 * 10)) << (16 * 7))
            // | (uint(400 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10 * 10 * 10 * 10)) << (16 * 8))
            // | (uint(400 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10)) << (16 * 9))
            // //| (uint(400 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10)) << (16 * 10));
            // | (uint(40) << (16 * 10));
    
        // ******** BSC ******** //
        
        // // Ethereum average block time interval, 3000 milliseconds
        // uint constant ETHEREUM_BLOCK_TIMESPAN = 3000;
    
        // // Nest ore drawing attenuation interval. 2400000 blocks, about one year
        // uint constant NEST_REDUCTION_SPAN = 10000000;
        // // The decay limit of nest ore drawing becomes stable after exceeding this interval. 
        // // 24 million blocks, about 10 years
        // uint constant NEST_REDUCTION_LIMIT = 100000000; //NEST_REDUCTION_SPAN * 10;
        // // Attenuation gradient array, each attenuation step value occupies 16 bits. The attenuation value is an integer
        // uint constant NEST_REDUCTION_STEPS = 0x280035004300530068008300A300CC010001400190;
        //     // 0
        //     // | (uint(400 / uint(1)) << (16 * 0))
        //     // | (uint(400 * 8 / uint(10)) << (16 * 1))
        //     // | (uint(400 * 8 * 8 / uint(10 * 10)) << (16 * 2))
        //     // | (uint(400 * 8 * 8 * 8 / uint(10 * 10 * 10)) << (16 * 3))
        //     // | (uint(400 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10)) << (16 * 4))
        //     // | (uint(400 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10)) << (16 * 5))
        //     // | (uint(400 * 8 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10 * 10)) << (16 * 6))
        //     // | (uint(400 * 8 * 8 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10 * 10 * 10)) << (16 * 7))
        //     // | (uint(400 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10 * 10 * 10 * 10)) << (16 * 8))
        //     // | (uint(400 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10)) << (16 * 9))
        //     // //| (uint(400 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10)) << (16 * 10));
        //     // | (uint(40) << (16 * 10));
    
        // ******** Ploygon ******** //
    
        // // Ethereum average block time interval, 2200 milliseconds
        // uint constant ETHEREUM_BLOCK_TIMESPAN = 2200;
    
        // // Nest ore drawing attenuation interval. 2400000 blocks, about one year
        // uint constant NEST_REDUCTION_SPAN = 15000000;
        // // The decay limit of nest ore drawing becomes stable after exceeding this interval. 
        // // 24 million blocks, about 10 years
        // uint constant NEST_REDUCTION_LIMIT = 150000000; //NEST_REDUCTION_SPAN * 10;
        // // Attenuation gradient array, each attenuation step value occupies 16 bits. The attenuation value is an integer
        // uint constant NEST_REDUCTION_STEPS = 0x280035004300530068008300A300CC010001400190;
        //     // 0
        //     // | (uint(400 / uint(1)) << (16 * 0))
        //     // | (uint(400 * 8 / uint(10)) << (16 * 1))
        //     // | (uint(400 * 8 * 8 / uint(10 * 10)) << (16 * 2))
        //     // | (uint(400 * 8 * 8 * 8 / uint(10 * 10 * 10)) << (16 * 3))
        //     // | (uint(400 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10)) << (16 * 4))
        //     // | (uint(400 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10)) << (16 * 5))
        //     // | (uint(400 * 8 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10 * 10)) << (16 * 6))
        //     // | (uint(400 * 8 * 8 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10 * 10 * 10)) << (16 * 7))
        //     // | (uint(400 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10 * 10 * 10 * 10)) << (16 * 8))
        //     // | (uint(400 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10)) << (16 * 9))
        //     // //| (uint(400 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 * 8 / uint(10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10)) << (16 * 10));
            // | (uint(40) << (16 * 10));
    }
    
    
    // File contracts/interfaces/INestMapping.sol
    
    // GPL-3.0-or-later
    
    pragma solidity ^0.8.6;
    
    /// @dev The interface defines methods for nest builtin contract address mapping
    interface INestMapping {
    
        /// @dev Set the built-in contract address of the system
        /// @param nestTokenAddress Address of nest token contract
        /// @param nestNodeAddress Address of nest node contract
        /// @param nestLedgerAddress INestLedger implementation contract address
        /// @param nestMiningAddress INestMining implementation contract address for nest
        /// @param ntokenMiningAddress INestMining implementation contract address for ntoken
        /// @param nestPriceFacadeAddress INestPriceFacade implementation contract address
        /// @param nestVoteAddress INestVote implementation contract address
        /// @param nestQueryAddress INestQuery implementation contract address
        /// @param nnIncomeAddress NNIncome contract address
        /// @param nTokenControllerAddress INTokenController implementation contract address
        function setBuiltinAddress(
            address nestTokenAddress,
            address nestNodeAddress,
            address nestLedgerAddress,
            address nestMiningAddress,
            address ntokenMiningAddress,
            address nestPriceFacadeAddress,
            address nestVoteAddress,
            address nestQueryAddress,
            address nnIncomeAddress,
            address nTokenControllerAddress
        ) external;
    
        /// @dev Get the built-in contract address of the system
        /// @return nestTokenAddress Address of nest token contract
        /// @return nestNodeAddress Address of nest node contract
        /// @return nestLedgerAddress INestLedger implementation contract address
        /// @return nestMiningAddress INestMining implementation contract address for nest
        /// @return ntokenMiningAddress INestMining implementation contract address for ntoken
        /// @return nestPriceFacadeAddress INestPriceFacade implementation contract address
        /// @return nestVoteAddress INestVote implementation contract address
        /// @return nestQueryAddress INestQuery implementation contract address
        /// @return nnIncomeAddress NNIncome contract address
        /// @return nTokenControllerAddress INTokenController implementation contract address
        function getBuiltinAddress() external view returns (
            address nestTokenAddress,
            address nestNodeAddress,
            address nestLedgerAddress,
            address nestMiningAddress,
            address ntokenMiningAddress,
            address nestPriceFacadeAddress,
            address nestVoteAddress,
            address nestQueryAddress,
            address nnIncomeAddress,
            address nTokenControllerAddress
        );
    
        /// @dev Get address of nest token contract
        /// @return Address of nest token contract
        function getNestTokenAddress() external view returns (address);
    
        /// @dev Get address of nest node contract
        /// @return Address of nest node contract
        function getNestNodeAddress() external view returns (address);
    
        /// @dev Get INestLedger implementation contract address
        /// @return INestLedger implementation contract address
        function getNestLedgerAddress() external view returns (address);
    
        /// @dev Get INestMining implementation contract address for nest
        /// @return INestMining implementation contract address for nest
        function getNestMiningAddress() external view returns (address);
    
        /// @dev Get INestMining implementation contract address for ntoken
        /// @return INestMining implementation contract address for ntoken
        function getNTokenMiningAddress() external view returns (address);
    
        /// @dev Get INestPriceFacade implementation contract address
        /// @return INestPriceFacade implementation contract address
        function getNestPriceFacadeAddress() external view returns (address);
    
        /// @dev Get INestVote implementation contract address
        /// @return INestVote implementation contract address
        function getNestVoteAddress() external view returns (address);
    
        /// @dev Get INestQuery implementation contract address
        /// @return INestQuery implementation contract address
        function getNestQueryAddress() external view returns (address);
    
        /// @dev Get NNIncome contract address
        /// @return NNIncome contract address
        function getNnIncomeAddress() external view returns (address);
    
        /// @dev Get INTokenController implementation contract address
        /// @return INTokenController implementation contract address
        function getNTokenControllerAddress() external view returns (address);
    
        /// @dev Registered address. The address registered here is the address accepted by nest system
        /// @param key The key
        /// @param addr Destination address. 0 means to delete the registration information
        function registerAddress(string memory key, address addr) external;
    
        /// @dev Get registered address
        /// @param key The key
        /// @return Destination address. 0 means empty
        function checkAddress(string memory key) external view returns (address);
    }
    
    
    // File contracts/interfaces/INestGovernance.sol
    
    // GPL-3.0-or-later
    
    pragma solidity ^0.8.6;
    /// @dev This interface defines the governance methods
    interface INestGovernance is INestMapping {
    
        /// @dev Set governance authority
        /// @param addr Destination address
        /// @param flag Weight. 0 means to delete the governance permission of the target address. Weight is not 
        ///        implemented in the current system, only the difference between authorized and unauthorized. 
        ///        Here, a uint96 is used to represent the weight, which is only reserved for expansion
        function setGovernance(address addr, uint flag) external;
    
        /// @dev Get governance rights
        /// @param addr Destination address
        /// @return Weight. 0 means to delete the governance permission of the target address. Weight is not 
        ///        implemented in the current system, only the difference between authorized and unauthorized. 
        ///        Here, a uint96 is used to represent the weight, which is only reserved for expansion
        function getGovernance(address addr) external view returns (uint);
    
        /// @dev Check whether the target address has governance rights for the given target
        /// @param addr Destination address
        /// @param flag Permission weight. The permission of the target address must be greater than this weight 
        /// to pass the check
        /// @return True indicates permission
        function checkGovernance(address addr, uint flag) external view returns (bool);
    }
    
    
    // File contracts/NestBase.sol
    
    // GPL-3.0-or-later
    
    pragma solidity ^0.8.6;
    /// @dev Base contract of nest
    contract NestBase {
    
        /// @dev INestGovernance implementation contract address
        address public _governance;
    
        /// @dev To support open-zeppelin/upgrades
        /// @param governance INestGovernance implementation contract address
        function initialize(address governance) public virtual {
            require(_governance == address(0), "NEST:!initialize");
            _governance = governance;
        }
    
        /// @dev Rewritten in the implementation contract, for load other contract addresses. Call 
        ///      super.update(newGovernance) when overriding, and override method without onlyGovernance
        /// @param newGovernance INestGovernance implementation contract address
        function update(address newGovernance) public virtual {
    
            address governance = _governance;
            require(governance == msg.sender || INestGovernance(governance).checkGovernance(msg.sender, 0), "NEST:!gov");
            _governance = newGovernance;
        }
    
        //---------modifier------------
    
        modifier onlyGovernance() {
            require(INestGovernance(_governance).checkGovernance(msg.sender, 0), "NEST:!gov");
            _;
        }
    
        modifier noContract() {
            require(msg.sender == tx.origin, "NEST:!contract");
            _;
        }
    }
    
    
    // File contracts/custom/NestFrequentlyUsed.sol
    
    // GPL-3.0-or-later
    
    pragma solidity ^0.8.6;
    /// @dev Base contract of nest
    contract NestFrequentlyUsed is NestBase {
    
        // Address of nest token contract
        address constant NEST_TOKEN_ADDRESS = 0x04abEdA201850aC0124161F037Efd70c74ddC74C;
    
        // Genesis block number of nest
        // NEST token contract is created at block height 6913517. However, because the mining algorithm of nest1.0
        // is different from that at present, a new mining algorithm is adopted from nest2.0. The new algorithm
        // includes the attenuation logic according to the block. Therefore, it is necessary to trace the block
        // where the nest begins to decay. According to the circulation when nest2.0 is online, the new mining
        // algorithm is used to deduce and convert the nest, and the new algorithm is used to mine the nest2.0
        // on-line flow, the actual block is 5120000
        //uint constant NEST_GENESIS_BLOCK = 0;
    
        // /// @dev Rewritten in the implementation contract, for load other contract addresses. Call 
        // ///      super.update(newGovernance) when overriding, and override method without onlyGovernance
        // /// @param newGovernance IHedgeGovernance implementation contract address
        // function update(address newGovernance) public virtual override {
        //     super.update(newGovernance);
        //     NEST_TOKEN_ADDRESS = INestGovernance(newGovernance).getNestTokenAddress();
        // }
    }
    
    
    // File contracts/NestBatchMining.sol
    
    // GPL-3.0-or-later
    
    pragma solidity ^0.8.6;
    /// @dev This contract implemented the mining logic of nest
    contract NestBatchMining is ChainConfig, NestFrequentlyUsed, INestBatchMining {
    
        /// @dev To support open-zeppelin/upgrades
        /// @param governance INestGovernance implementation contract address
        function initialize(address governance) public override {
            super.initialize(governance);
            // Placeholder in _accounts, the index of a real account must greater than 0
            _accounts.push();
        }
    
        /// @dev Definitions for the price sheet, include the full information. 
        /// (use 256-bits, a storage unit in ethereum evm)
        struct PriceSheet {
            
            // Index of miner account in _accounts. for this way, mapping an address(which need 160-bits) to a 32-bits 
            // integer, support 4 billion accounts
            uint32 miner;
    
            // The block number of this price sheet packaged
            uint32 height;
    
            // The remain number of this price sheet
            uint32 remainNum;
    
            // The eth number which miner will got
            uint32 ethNumBal;
    
            // The eth number which equivalent to token's value which miner will got
            uint32 tokenNumBal;
    
            // The pledged number of nest in this sheet. (Unit: 1000nest)
            uint24 nestNum1k;
    
            // The level of this sheet. 0 expresses initial price sheet, a value greater than 0 expresses bite price sheet
            uint8 level;
    
            // Post fee shares, if there are many sheets in one block, this value is used to divide up mining value
            uint8 shares;
    
            // Represent price as this way, may lose precision, the error less than 1/10^14
            // price = priceFraction * 16 ^ priceExponent
            uint56 priceFloat;
        }
    
        /// @dev Definitions for the price information
        struct PriceInfo {
    
            // Record the index of price sheet, for update price information from price sheet next time.
            uint32 index;
    
            // The block number of this price
            uint32 height;
    
            // The remain number of this price sheet
            uint32 remainNum;
    
            // Price, represent as float
            // Represent price as this way, may lose precision, the error less than 1/10^14
            uint56 priceFloat;
    
            // Avg Price, represent as float
            // Represent price as this way, may lose precision, the error less than 1/10^14
            uint56 avgFloat;
    
            // Square of price volatility, need divide by 2^48
            uint48 sigmaSQ;
        }
    
        // Price pair structure
        struct PricePair {
            address target;
            PriceInfo price;
            PriceSheet[] sheets;    
        }
    
        /// @dev Price channel
        struct PriceChannel {
    
            // Address of token0, use to mensuration, 0 means eth
            address token0;
            // Unit of token0
            uint96 unit;
    
            // Reward token address
            address reward;        
            // Reward per block standard
            uint96 rewardPerBlock;
    
            // Reward total
            uint128 vault;        
            // The information of mining fee
            uint96 rewards;
            // Post fee(0.0001eth, DIMI_ETHER). 1000
            uint16 postFeeUnit;
            // Count of price pairs in this channel
            uint16 count;
    
            // Address of opener
            address opener;
            // Genesis block of this channel
            uint32 genesisBlock;
            // Single query fee (0.0001 ether, DIMI_ETHER). 100
            uint16 singleFee;
            // Reduction rate(10000 based). 8000
            uint16 reductionRate;
            
            // Price pair array
            PricePair[0xFFFF] pairs;
        }
    
        /// @dev Structure is used to represent a storage location. Storage variable can be used to avoid indexing 
        /// from mapping many times
        struct UINT {
            uint value;
        }
    
        /// @dev Account information
        struct Account {
            
            // Address of account
            address addr;
    
            // Balances of mining account
            // tokenAddress=>balance
            mapping(address=>UINT) balances;
        }
    
        // Configuration
        Config _config;
    
        // Registered account information
        Account[] _accounts;
    
        // Mapping from address to index of account. address=>accountIndex
        mapping(address=>uint) _accountMapping;
    
        // Price channels
        PriceChannel[] _channels;
    
        // Unit of post fee. 0.0001 ether
        uint constant DIMI_ETHER = 0.0001 ether;
    
        /* ========== Governance ========== */
    
        /// @dev Modify configuration
        /// @param config Configuration object
        function setConfig(Config calldata config) external override onlyGovernance {
            _config = config;
        }
    
        /// @dev Get configuration
        /// @return Configuration object
        function getConfig() external view override returns (Config memory) {
            return _config;
        }
    
        /// @dev Open price channel
        /// @param token0 Address of token0, use to mensuration, 0 means eth
        /// @param unit Unit of token0
        /// @param reward Reward token address
        /// @param tokens Target tokens
        /// @param config Channel configuration
        function open(
            address token0, 
            uint96 unit, 
            address reward, 
            address[] calldata tokens,
            ChannelConfig calldata config
        ) external override {
    
            // Emit open event
            emit Open(_channels.length, token0, unit, reward);
            
            PriceChannel storage channel = _channels.push();
    
            // Address of token0
            channel.token0 = token0;
            // Unit of token0
            channel.unit = unit;
    
            // Address of reward
            channel.reward = reward;
    
            channel.vault = uint128(0);
            channel.rewards = uint96(0);
            channel.count = uint16(tokens.length);
            
            // Address of opener
            channel.opener = msg.sender;
            // Genesis block of this channel
            channel.genesisBlock = uint32(block.number);
    
            // Create price pairs
            for (uint i = 0; i < tokens.length; ++i) {
                require(token0 != tokens[i], "NOM:token can't equal token0");
                for (uint j = 0; j < i; ++j) {
                    require(tokens[i] != tokens[j], "NOM:token reiterated");
                }
                channel.pairs[i].target = tokens[i];
            }
    
            _modify(channel, config);
        }
    
        /// @dev Modify channel configuration
        /// @param channelId Target channelId
        /// @param config Channel configuration
        function modify(uint channelId, ChannelConfig calldata config) external override {
            PriceChannel storage channel = _channels[channelId];
            require(channel.opener == msg.sender, "NOM:not opener");
            _modify(channel, config);
        }
    
        /// @dev Modify channel configuration
        /// @param channel Target channel
        /// @param config Channel configuration
        function _modify(PriceChannel storage channel, ChannelConfig calldata config) private {
            // Reward per block standard
            channel.rewardPerBlock = config.rewardPerBlock;
    
            // Post fee(0.0001eth, DIMI_ETHER). 1000
            channel.postFeeUnit = config.postFeeUnit;
    
            // Single query fee (0.0001 ether, DIMI_ETHER). 100
            channel.singleFee = config.singleFee;
            // Reduction rate(10000 based). 8000
            channel.reductionRate = config.reductionRate;
        }
    
        /// @dev Add price token, make a pair with token0. (Not support remove, be careful!)
        /// @param channelId Target channelId
        /// @param target Target token address
        function addPair(uint channelId, address target) external {
            PriceChannel storage channel = _channels[channelId];
            require(channel.opener == msg.sender, "NOM:not opener");
            require(channel.token0 != target, "NOM:token can't equal token0");
            uint count = uint(channel.count);
            for (uint j = 0; j < count; ++j) {
                require(channel.pairs[j].target != target, "NOM:token reiterated");
            }
            channel.pairs[count].target = target;
            ++channel.count;
        }
    
        /// @dev Increase vault to channel
        /// @param channelId Target channelId
        /// @param vault Total to increase
        function increase(uint channelId, uint128 vault) external payable override {
            PriceChannel storage channel = _channels[channelId];
            address reward = channel.reward;
            if (reward == address(0)) {
                require(msg.value == uint(vault), "NOM:vault error");
            } else {
                TransferHelper.safeTransferFrom(reward, msg.sender, address(this), uint(vault));
            }
            channel.vault += vault;
        }
    
        /// @dev Decrease vault from channel
        /// @param channelId Target channelId
        /// @param vault Total to decrease
        function decrease(uint channelId, uint128 vault) external override {
            PriceChannel storage channel = _channels[channelId];
            require(channel.opener == msg.sender, "NOM:not opener");
            address reward = channel.reward;
            channel.vault -= vault;
            if (reward == address(0)) {
                payable(msg.sender).transfer(uint(vault));
            } else {
                TransferHelper.safeTransfer(reward, msg.sender, uint(vault));
            }
        }
    
        /// @dev Change opener
        /// @param channelId Target channelId
        /// @param newOpener New opener address
        function changeOpener(uint channelId, address newOpener) external {
            PriceChannel storage channel = _channels[channelId];
            require(channel.opener == msg.sender, "NOM:not opener");
            channel.opener = newOpener;
        }
    
        /// @dev Get channel information
        /// @param channelId Target channelId
        /// @return Information of channel
        function getChannelInfo(uint channelId) external view override returns (PriceChannelView memory) {
            PriceChannel storage channel = _channels[channelId];
    
            uint count = uint(channel.count);
            PairView[] memory pairs = new PairView[](count);
            for (uint i = 0; i < count; ++i) {
                PricePair storage pair = channel.pairs[i];
                pairs[i] = PairView(pair.target, uint96(pair.sheets.length));
            }
    
            return PriceChannelView (
                channelId,
    
                // Address of token0, use to mensuration, 0 means eth
                channel.token0,
                // Unit of token0
                channel.unit,
    
                // Reward token address
                channel.reward,
                // Reward per block standard
                channel.rewardPerBlock,
    
                // Reward total
                channel.vault,
                // The information of mining fee
                channel.rewards,
                // Post fee(0.0001eth, DIMI_ETHER). 1000
                channel.postFeeUnit,
                // Count of price pairs in this channel
                channel.count,
    
                // Address of opener
                channel.opener,
                // Genesis block of this channel
                channel.genesisBlock,
                // Single query fee (0.0001 ether, DIMI_ETHER). 100
                channel.singleFee,
                // Reduction rate(10000 based). 8000
                channel.reductionRate,
    
                pairs
            );
        }
    
        /* ========== Mining ========== */
    
        /// @dev Post price
        /// @param channelId Target channelId
        /// @param scale Scale of this post. (Which times of unit)
        /// @param equivalents Price array, one to one with pairs
        function post(uint channelId, uint scale, uint[] calldata equivalents) external payable override {
    
            // 0. Load config
            Config memory config = _config;
    
            // 1. Check arguments
            require(scale == 1, "NOM:!scale");
    
            // 2. Load price channel
            PriceChannel storage channel = _channels[channelId];
    
            // 3. Freeze assets
            uint accountIndex = _addressIndex(msg.sender);
    
            // Freeze token and nest
            // Because of the use of floating-point representation(fraction * 16 ^ exponent), it may bring some precision 
            // loss After assets are frozen according to tokenAmountPerEth * ethNum, the part with poor accuracy may be 
            // lost when the assets are returned, It should be frozen according to decodeFloat(fraction, exponent) * ethNum
            // However, considering that the loss is less than 1 / 10 ^ 14, the loss here is ignored, and the part of
            // precision loss can be transferred out as system income in the future
            mapping(address=>UINT) storage balances = _accounts[accountIndex].balances;
    
            uint cn = uint(channel.count);
            uint fee = msg.value;
    
            // Freeze nest
            fee = _freeze(balances, NEST_TOKEN_ADDRESS, cn * uint(config.pledgeNest) * 1000 ether, fee);
        
            // Freeze token0
            fee = _freeze(balances, channel.token0, cn * uint(channel.unit) * scale, fee);
    
            // Freeze token1
            while (cn > 0) {
                PricePair storage pair = channel.pairs[--cn];
                uint equivalent = equivalents[cn];
                require(equivalent > 0, "NOM:!equivalent");
                fee = _freeze(balances, pair.target, scale * equivalent, fee);
    
                // Calculate the price
                // According to the current mechanism, the newly added sheet cannot take effect, so the calculated price
                // is placed before the sheet is added, which can reduce unnecessary traversal
                _stat(config, pair);
                
                // 6. Create token price sheet
                emit Post(channelId, cn, msg.sender, pair.sheets.length, scale, equivalent);
                // Only pairIndex 0 has reward
                _create(pair.sheets, accountIndex, uint32(scale), uint(config.pledgeNest), cn == 0 ? 1 : 0, equivalent);
            }
    
            // // 4. Deposit fee
            // // Only postFeeUnit > 0 need fee
            // uint postFeeUnit = uint(channel.postFeeUnit);
            // if (postFeeUnit > 0) {
            //     require(fee >= postFeeUnit * DIMI_ETHER + tx.gasprice * 400000, "NM:!fee");
            // }
            // if (fee > 0) {
            //     channel.rewards += _toUInt96(fee);
            // }
        }
    
        /// @notice Call the function to buy TOKEN/NTOKEN from a posted price sheet
        /// @dev bite TOKEN(NTOKEN) by ETH,  (+ethNumBal, -tokenNumBal)
        /// @param channelId Target price channelId
        /// @param pairIndex Target pairIndex. When take token0, use pairIndex direct, or add 65536 conversely
        /// @param index The position of the sheet in priceSheetList[token]
        /// @param takeNum The amount of biting (in the unit of ETH), realAmount = takeNum * newTokenAmountPerEth
        /// @param newEquivalent The new price of token (1 ETH : some TOKEN), here some means newTokenAmountPerEth
        function take(
            uint channelId, 
            uint pairIndex, 
            uint index, 
            uint takeNum, 
            uint newEquivalent
        ) external payable override {
    
            Config memory config = _config;
    
            // 1. Check arguments
            require(takeNum > 0, "NM:!takeNum");
            require(newEquivalent > 0, "NM:!price");
    
            // 2. Load price sheet
            PriceChannel storage channel = _channels[channelId];
            PricePair storage pair = channel.pairs[pairIndex < 0x10000 ? pairIndex : pairIndex - 0x10000];
            PriceSheet memory sheet = pair.sheets[index];
    
            // 3. Check state
            require(uint(sheet.height) + uint(config.priceEffectSpan) >= block.number, "NM:!state");
            sheet.remainNum = uint32(uint(sheet.remainNum) - takeNum);
    
            uint accountIndex = _addressIndex(msg.sender);
            // Number of nest to be pledged
            // sheet.ethNumBal + sheet.tokenNumBal is always two times to sheet.ethNum
            uint needNest1k = (takeNum << 2) * uint(sheet.nestNum1k) / (uint(sheet.ethNumBal) + uint(sheet.tokenNumBal));
    
            // 4. Calculate the number of eth, token and nest needed, and freeze them
            uint needEthNum = takeNum;
            uint level = uint(sheet.level);
            if (level < 255) {
                if (level < uint(config.maxBiteNestedLevel)) {
                    // Double scale sheet
                    needEthNum <<= 1;
                }
                ++level;
            }
    
            {
                // Freeze nest and token
                // Freeze assets: token0, token1, nest
                mapping(address=>UINT) storage balances = _accounts[accountIndex].balances;
                uint fee = msg.value;
    
                // Target pairIndex. When take token0, use pairIndex direct, or add 65536 conversely
                // pairIndex < 0x10000 means take token0
                if (pairIndex < 0x10000) {
                    // Update the bitten sheet
                    sheet.ethNumBal = uint32(uint(sheet.ethNumBal) - takeNum);
                    sheet.tokenNumBal = uint32(uint(sheet.tokenNumBal) + takeNum);
                    pair.sheets[index] = sheet;
    
                    // Freeze token0
                    fee = _freeze(balances, channel.token0, (needEthNum - takeNum) * uint(channel.unit), fee);
                    // Freeze token1
                    fee = _freeze(
                        balances, 
                        pair.target, 
                        needEthNum * newEquivalent + _decodeFloat(sheet.priceFloat) * takeNum, 
                        fee
                    );
                } 
                // pairIndex >= 0x10000 means take target token1
                else {
                    pairIndex -= 0x10000;
                    // Update the bitten sheet
                    sheet.ethNumBal = uint32(uint(sheet.ethNumBal) + takeNum);
                    sheet.tokenNumBal = uint32(uint(sheet.tokenNumBal) - takeNum);
                    pair.sheets[index] = sheet;
    
                    // Freeze token0
                    fee = _freeze(balances, channel.token0, (needEthNum + takeNum) * uint(channel.unit), fee);
                    // Freeze token1
                    uint backTokenValue = _decodeFloat(sheet.priceFloat) * takeNum;
                    if (needEthNum * newEquivalent > backTokenValue) {
                        fee = _freeze(balances, pair.target, needEthNum * newEquivalent - backTokenValue, fee);
                    } else {
                        _unfreeze(balances, pair.target, backTokenValue - needEthNum * newEquivalent, msg.sender);
                    }
                }
                    
                // Freeze nest
                fee = _freeze(balances, NEST_TOKEN_ADDRESS, needNest1k * 1000 ether, fee);
    
                require(fee == 0, "NOM:!fee");
            }
                
            // 5. Calculate the price
            // According to the current mechanism, the newly added sheet cannot take effect, so the calculated price
            // is placed before the sheet is added, which can reduce unnecessary traversal
            _stat(config, pair);
    
            // 6. Create price sheet
            emit Post(channelId, pairIndex, msg.sender, pair.sheets.length, needEthNum, newEquivalent);
            _create(pair.sheets, accountIndex, uint32(needEthNum), needNest1k, level << 8, newEquivalent);
        }
    
        /// @dev List sheets by page
        /// @param channelId Target channelId
        /// @param pairIndex Target pairIndex
        /// @param offset Skip previous (offset) records
        /// @param count Return (count) records
        /// @param order Order. 0 reverse order, non-0 positive order
        /// @return List of price sheets
        function list(
            uint channelId,
            uint pairIndex,
            uint offset,
            uint count,
            uint order
        ) external view override noContract returns (PriceSheetView[] memory) {
    
            PriceSheet[] storage sheets = _channels[channelId].pairs[pairIndex].sheets;
            PriceSheetView[] memory result = new PriceSheetView[](count);
            uint length = sheets.length;
            uint i = 0;
    
            // Reverse order
            if (order == 0) {
    
                uint index = length - offset;
                uint end = index > count ? index - count : 0;
                while (index > end) {
                    --index;
                    result[i++] = _toPriceSheetView(sheets[index], index);
                }
            } 
            // Positive order
            else {
    
                uint index = offset;
                uint end = index + count;
                if (end > length) {
                    end = length;
                }
                while (index < end) {
                    result[i++] = _toPriceSheetView(sheets[index], index);
                    ++index;
                }
            }
            return result;
        }
    
        /// @notice Close a batch of price sheets passed VERIFICATION-PHASE
        /// @dev Empty sheets but in VERIFICATION-PHASE aren't allowed
        /// @param channelId Target channelId
        /// @param indices Two-dimensional array of sheet indices, first means pair indices, seconds means sheet indices
        function close(uint channelId, uint[][] calldata indices) external override {
            
            Config memory config = _config;
            PriceChannel storage channel = _channels[channelId];
            
            uint accountIndex = 0;
            uint reward = 0;
            uint nestNum1k = 0;
            uint ethNum = 0;
    
            // storage variable must given a value at declaring, this is useless
            mapping(address=>UINT) storage balances = _accounts[0/*accountIndex*/].balances;
            uint[3] memory vars = [
                uint(channel.rewardPerBlock), 
                uint(channel.genesisBlock), 
                uint(channel.reductionRate)
            ];
    
            for (uint j = indices.length; j > 0;) {
                PricePair storage pair = channel.pairs[--j];
    
                ///////////////////////////////////////////////////////////////////////////////////////
                uint tokenValue = 0;
    
                // 1. Traverse sheets
                for (uint i = indices[j].length; i > 0;) {
    
                    // ---------------------------------------------------------------------------------
                    uint index = indices[j][--i];
                    PriceSheet memory sheet = pair.sheets[index];
                    
                    // Batch closing quotation can only close sheet of the same user
                    if (accountIndex == 0) {
                        // accountIndex == 0 means the first sheet, and the number of this sheet is taken
                        accountIndex = uint(sheet.miner);
                        balances = _accounts[accountIndex].balances;
                    } else {
                        // accountIndex != 0 means that it is a follow-up sheet, and the miner number must be 
                        // consistent with the previous record
                        require(accountIndex == uint(sheet.miner), "NM:!miner");
                    }
    
                    // Check the status of the price sheet to see if it has reached the effective block interval 
                    // or has been finished
                    if (accountIndex > 0 && (uint(sheet.height) + uint(config.priceEffectSpan) < block.number)) {
    
                        // Only pairIndex 0 has reward
                        if (j == 0) {
                            uint shares = uint(sheet.shares);
                            // Mining logic
                            // The price sheet which shares is zero doesn't mining
                            if (shares > 0) {
    
                                // Currently, mined represents the number of blocks has mined
                                (uint mined, uint totalShares) = _calcMinedBlocks(pair.sheets, index, sheet);
                                
                                // When rewardPerBlock is very large, this calculated may be truncated, 
                                // resulting in a significant reduction in the actual reward,
                                // This situation is unreasonable and needs to be borne by the opener.
                                reward += (
                                    mined
                                    * shares
                                    * _reduction(uint(sheet.height) - vars[1], vars[2])
                                    * vars[0]
                                    / totalShares / 400
                                );
                            }
                        }
    
                        nestNum1k += uint(sheet.nestNum1k);
                        ethNum += uint(sheet.ethNumBal);
                        tokenValue += _decodeFloat(sheet.priceFloat) * uint(sheet.tokenNumBal);
    
                        // Set sheet.miner to 0, express the sheet is closed
                        sheet.miner = uint32(0);
                        sheet.ethNumBal = uint32(0);
                        sheet.tokenNumBal = uint32(0);
                        pair.sheets[index] = sheet;
                    }
    
                    // ---------------------------------------------------------------------------------
                }
    
                _stat(config, pair);
                ///////////////////////////////////////////////////////////////////////////////////////
    
                // Unfreeze token1
                _unfreeze(balances, pair.target, tokenValue, accountIndex);
            }
    
            // Unfreeze token0
            _unfreeze(balances, channel.token0, ethNum * uint(channel.unit), accountIndex);
            
            // Unfreeze nest
            _unfreeze(balances, NEST_TOKEN_ADDRESS, nestNum1k * 1000 ether, accountIndex);
    
            uint vault = uint(channel.vault);
            if (reward > vault) {
                reward = vault;
            }
            // Record the vault for each channel to prevent the opener use the funds in this contract without increase
            channel.vault = uint96(vault - reward);
            
            // Record reward
            _unfreeze(balances, channel.reward, reward, accountIndex);
        }
    
        /// @dev View the number of assets specified by the user
        /// @param tokenAddress Destination token address
        /// @param addr Destination address
        /// @return Number of assets
        function balanceOf(address tokenAddress, address addr) external view override returns (uint) {
            return _accounts[_accountMapping[addr]].balances[tokenAddress].value;
        }
    
        /// @dev Withdraw assets
        /// @param tokenAddress Destination token address
        /// @param value The value to withdraw
        function withdraw(address tokenAddress, uint value) external override {
    
            // The user's locked nest and the mining pool's nest are stored together. When the nest is mined over,
            // the problem of taking the locked nest as the ore drawing will appear
            // As it will take a long time for nest to finish mining, this problem will not be considered for the time being
            UINT storage balance = _accounts[_accountMapping[msg.sender]].balances[tokenAddress];
            balance.value -= value;
            TransferHelper.safeTransfer(tokenAddress, msg.sender, value);
        }
    
        /// @dev Estimated mining amount
        /// @param channelId Target channelId
        /// @return Estimated mining amount
        function estimate(uint channelId) external view override returns (uint) {
    
            PriceChannel storage channel = _channels[channelId];
            PriceSheet[] storage sheets = channel.pairs[0].sheets;
            uint index = sheets.length;
            uint blocks = 10;
            while (index > 0) {
    
                PriceSheet memory sheet = sheets[--index];
                if (uint(sheet.shares) > 0) {
                    blocks = block.number - uint(sheet.height);
                    break;
                }
            }
    
            return 
                blocks
                * uint(channel.rewardPerBlock) 
                * _reduction(block.number - uint(channel.genesisBlock), uint(channel.reductionRate))
                / 400;
        }
    
        /// @dev Query the quantity of the target quotation
        /// @param channelId Target channelId
        /// @param index The index of the sheet
        /// @return minedBlocks Mined block period from previous block
        /// @return totalShares Total shares of sheets in the block
        function getMinedBlocks(
            uint channelId,
            uint index
        ) external view override returns (uint minedBlocks, uint totalShares) {
            PriceSheet[] storage sheets = _channels[channelId].pairs[0].sheets;
            return _calcMinedBlocks(sheets, index, sheets[index]);
        }
    
        /// @dev The function returns eth rewards of specified ntoken
        /// @param channelId Target channelId
        function totalETHRewards(uint channelId) external view override returns (uint) {
            return uint(_channels[channelId].rewards);
        }
    
        /// @dev Pay
        /// @param channelId Target channelId
        /// @param to Address to receive
        /// @param value Amount to receive
        function pay(uint channelId, address to, uint value) external override {
    
            PriceChannel storage channel = _channels[channelId];
            require(channel.opener == msg.sender, "NOM:!opener");
            channel.rewards -= _toUInt96(value);
            // pay
            payable(to).transfer(value);
        }
    
        /// @dev Donate to dao
        /// @param channelId Target channelId
        /// @param value Amount to receive
        function donate(uint channelId, uint value) external override {
    
            PriceChannel storage channel = _channels[channelId];
            require(channel.opener == msg.sender, "NOM:!opener");
            channel.rewards -= _toUInt96(value);
            INestLedger(INestMapping(_governance).getNestLedgerAddress()).addETHReward { value: value } (channelId);
        }
    
        /// @dev Gets the address corresponding to the given index number
        /// @param index The index number of the specified address
        /// @return The address corresponding to the given index number
        function indexAddress(uint index) public view returns (address) {
            return _accounts[index].addr;
        }
    
        /// @dev Gets the registration index number of the specified address
        /// @param addr Destination address
        /// @return 0 means nonexistent, non-0 means index number
        function getAccountIndex(address addr) external view returns (uint) {
            return _accountMapping[addr];
        }
    
        /// @dev Get the length of registered account array
        /// @return The length of registered account array
        function getAccountCount() external view returns (uint) {
            return _accounts.length;
        }
    
        // Convert PriceSheet to PriceSheetView
        function _toPriceSheetView(PriceSheet memory sheet, uint index) private view returns (PriceSheetView memory) {
    
            return PriceSheetView(
                // Index number
                uint32(index),
                // Miner address
                indexAddress(sheet.miner),
                // The block number of this price sheet packaged
                sheet.height,
                // The remain number of this price sheet
                sheet.remainNum,
                // The eth number which miner will got
                sheet.ethNumBal,
                // The eth number which equivalent to token's value which miner will got
                sheet.tokenNumBal,
                // The pledged number of nest in this sheet. (Unit: 1000nest)
                sheet.nestNum1k,
                // The level of this sheet. 0 expresses initial price sheet, a value greater than 0 expresses 
                // bite price sheet
                sheet.level,
                // Post fee shares
                sheet.shares,
                // Price
                uint152(_decodeFloat(sheet.priceFloat))
            );
        }
    
        // Create price sheet
        function _create(
            PriceSheet[] storage sheets,
            uint accountIndex,
            uint32 ethNum,
            uint nestNum1k,
            uint level_shares,
            uint equivalent
        ) private {
    
            sheets.push(PriceSheet(
                uint32(accountIndex),                       // uint32 miner;
                uint32(block.number),                       // uint32 height;
                ethNum,                                     // uint32 remainNum;
                ethNum,                                     // uint32 ethNumBal;
                ethNum,                                     // uint32 tokenNumBal;
                uint24(nestNum1k),                          // uint32 nestNum1k;
                uint8(level_shares >> 8),                   // uint8 level;
                uint8(level_shares & 0xFF),
                _encodeFloat(equivalent)
            ));
        }
    
        // Calculate price, average price and volatility
        function _stat(Config memory config, PricePair storage pair) private {
            
            PriceSheet[] storage sheets = pair.sheets;
            // Load token price information
            PriceInfo memory p0 = pair.price;
    
            // Length of sheets
            uint length = sheets.length;
            // The index of the sheet to be processed in the sheet array
            uint index = uint(p0.index);
            // The latest block number for which the price has been calculated
            uint prev = uint(p0.height);
            // It's not necessary to load the price information in p0
            // Eth count variable used to calculate price
            uint totalEthNum = 0; 
            // Token count variable for price calculation
            uint totalTokenValue = 0; 
            // Block number of current sheet
            uint height = 0;
    
            // Traverse the sheets to find the effective price
            //uint effectBlock = block.number - uint(config.priceEffectSpan);
            PriceSheet memory sheet;
            for (; ; ++index) {
    
                // Gas attack analysis, each post transaction, calculated according to post, needs to write
                // at least one sheet and freeze two kinds of assets, which needs to consume at least 30000 gas,
                // In addition to the basic cost of the transaction, at least 50000 gas is required.
                // In addition, there are other reading and calculation operations. The gas consumed by each
                // transaction is impossible less than 70000 gas, The attacker can accumulate up to 20 blocks
                // of sheets to be generated. To ensure that the calculation can be completed in one block,
                // it is necessary to ensure that the consumption of each price does not exceed 70000 / 20 = 3500 gas,
                // According to the current logic, each calculation of a price needs to read a storage unit (800)
                // and calculate the consumption, which can not reach the dangerous value of 3500, so the gas attack
                // is not considered
    
                // Traverse the sheets that has reached the effective interval from the current position
                bool flag = index >= length
                    || (height = uint((sheet = sheets[index]).height)) + uint(config.priceEffectSpan) >= block.number;
    
                // Not the same block (or flag is false), calculate the price and update it
                if (flag || prev != height) {
    
                    // totalEthNum > 0 Can calculate the price
                    if (totalEthNum > 0) {
    
                        // Calculate average price and Volatility
                        // Calculation method of volatility of follow-up price
                        uint tmp = _decodeFloat(p0.priceFloat);
                        // New price
                        uint price = totalTokenValue / totalEthNum;
                        // Update price
                        p0.remainNum = uint32(totalEthNum);
                        p0.priceFloat = _encodeFloat(price);
                        // Clear cumulative values
                        totalEthNum = 0;
                        totalTokenValue = 0;
    
                        if (tmp > 0) {
                            // Calculate average price
                            // avgPrice[i + 1] = avgPrice[i] * 90% + price[i] * 10%
                            p0.avgFloat = _encodeFloat((_decodeFloat(p0.avgFloat) * 9 + price) / 10);
    
                            // When the accuracy of the token is very high or the value of the token relative to
                            // eth is very low, the price may be very large, and there may be overflow problem,
                            // it is not considered for the moment
                            tmp = (price << 48) / tmp;
                            if (tmp > 0x1000000000000) {
                                tmp = tmp - 0x1000000000000;
                            } else {
                                tmp = 0x1000000000000 - tmp;
                            }
    
                            // earn = price[i] / price[i - 1] - 1;
                            // seconds = time[i] - time[i - 1];
                            // sigmaSQ[i + 1] = sigmaSQ[i] * 90% + (earn ^ 2 / seconds) * 10%
                            tmp = (
                                uint(p0.sigmaSQ) * 9 + 
                                // It is inevitable that prev greater than p0.height
                                ((tmp * tmp * 1000 / ETHEREUM_BLOCK_TIMESPAN / (prev - uint(p0.height))) >> 48)
                            ) / 10;
    
                            // The current implementation assumes that the volatility cannot exceed 1, and
                            // corresponding to this, when the calculated value exceeds 1, expressed as 0xFFFFFFFFFFFF
                            if (tmp > 0xFFFFFFFFFFFF) {
                                tmp = 0xFFFFFFFFFFFF;
                            }
                            p0.sigmaSQ = uint48(tmp);
                        }
                        // The calculation methods of average price and volatility are different for first price
                        else {
                            // The average price is equal to the price
                            //p0.avgTokenAmount = uint64(price);
                            p0.avgFloat = p0.priceFloat;
    
                            // The volatility is 0
                            p0.sigmaSQ = uint48(0);
                        }
    
                        // Update price block number
                        p0.height = uint32(prev);
                    }
    
                    // Move to new block number
                    prev = height;
                }
    
                if (flag) {
                    break;
                }
    
                // Cumulative price information
                totalEthNum += uint(sheet.remainNum);
                totalTokenValue += _decodeFloat(sheet.priceFloat) * uint(sheet.remainNum);
            }
    
            // Update price information
            if (index > uint(p0.index)) {
                p0.index = uint32(index);
                pair.price = p0;
            }
        }
    
        // Calculation number of blocks which mined
        function _calcMinedBlocks(
            PriceSheet[] storage sheets,
            uint index,
            PriceSheet memory sheet
        ) private view returns (uint minedBlocks, uint totalShares) {
    
            uint length = sheets.length;
            uint height = uint(sheet.height);
            totalShares = uint(sheet.shares);
    
            // Backward looking for sheets in the same block
            for (uint i = index; ++i < length && uint(sheets[i].height) == height;) {
                
                // Multiple sheets in the same block is a small probability event at present, so it can be ignored
                // to read more than once, if there are always multiple sheets in the same block, it means that the
                // sheets are very intensive, and the gas consumed here does not have a great impact
                totalShares += uint(sheets[i].shares);
            }
    
            // Find sheets in the same block forward
            uint prev = height;
            while (index > 0 && uint(prev = sheets[--index].height) == height) {
    
                // Multiple sheets in the same block is a small probability event at present, so it can be ignored 
                // to read more than once, if there are always multiple sheets in the same block, it means that the
                // sheets are very intensive, and the gas consumed here does not have a great impact
                totalShares += uint(sheets[index].shares);
            }
    
            if (index > 0 || height > prev) {
                minedBlocks = height - prev;
            } else {
                minedBlocks = 10;
            }
        }
    
        /// @dev freeze token
        /// @param balances Balances ledger
        /// @param tokenAddress Destination token address
        /// @param tokenValue token amount
        /// @param value The remain value
        function _freeze(
            mapping(address=>UINT) storage balances, 
            address tokenAddress, 
            uint tokenValue,
            uint value
        ) private returns (uint) {
            if (tokenAddress == address(0)) {
                return value - tokenValue;
            } else {
                // Unfreeze nest
                UINT storage balance = balances[tokenAddress];
                uint balanceValue = balance.value;
                if (balanceValue < tokenValue) {
                    balance.value = 0;
                    TransferHelper.safeTransferFrom(tokenAddress, msg.sender, address(this), tokenValue - balanceValue);
                } else {
                    balance.value = balanceValue - tokenValue;
                }
                return value;
            }
        }
    
        function _unfreeze(
            mapping(address=>UINT) storage balances, 
            address tokenAddress, 
            uint tokenValue,
            uint accountIndex
        ) private {
            if (tokenValue > 0) {
                if (tokenAddress == address(0)) {
                    payable(indexAddress(accountIndex)).transfer(tokenValue);
                } else {
                    balances[tokenAddress].value += tokenValue;
                }
            }
        }
    
        function _unfreeze(
            mapping(address=>UINT) storage balances, 
            address tokenAddress, 
            uint tokenValue,
            address owner
        ) private {
            if (tokenValue > 0) {
                if (tokenAddress == address(0)) {
                    payable(owner).transfer(tokenValue);
                } else {
                    balances[tokenAddress].value += tokenValue;
                }
            }
        }
    
        /// @dev Gets the index number of the specified address. If it does not exist, register
        /// @param addr Destination address
        /// @return The index number of the specified address
        function _addressIndex(address addr) private returns (uint) {
    
            uint index = _accountMapping[addr];
            if (index == 0) {
                // If it exceeds the maximum number that 32 bits can store, you can't continue to register a new account.
                // If you need to support a new account, you need to update the contract
                require((_accountMapping[addr] = index = _accounts.length) < 0x100000000, "NM:!accounts");
                _accounts.push().addr = addr;
            }
    
            return index;
        }
    
        function _reduction(uint delta, uint reductionRate) private pure returns (uint) {
            if (delta < NEST_REDUCTION_LIMIT) {
                uint n = delta / NEST_REDUCTION_SPAN;
                return 400 * reductionRate ** n / 10000 ** n;
            }
            return 400 * reductionRate ** 10 / 10000 ** 10;
        }
    
        /* ========== Tools and methods ========== */
    
        /// @dev Encode the uint value as a floating-point representation in the form of fraction * 16 ^ exponent
        /// @param value Destination uint value
        /// @return float format
        function _encodeFloat(uint value) private pure returns (uint56) {
    
            uint exponent = 0; 
            while (value > 0x3FFFFFFFFFFFF) {
                value >>= 4;
                ++exponent;
            }
            return uint56((value << 6) | exponent);
        }
    
        /// @dev Decode the floating-point representation of fraction * 16 ^ exponent to uint
        /// @param floatValue fraction value
        /// @return decode format
        function _decodeFloat(uint56 floatValue) private pure returns (uint) {
            return (uint(floatValue) >> 6) << ((uint(floatValue) & 0x3F) << 2);
        }
    
        // Convert uint to uint96
        function _toUInt96(uint value) internal pure returns (uint96) {
            require(value < 0x1000000000000000000000000);
            return uint96(value);
        }
    
        /* ========== Price Query ========== */
        
        /// @dev Get the latest trigger price
        /// @param pair Target price pair
        /// @return blockNumber The block number of price
        /// @return price The token price. (1eth equivalent to (price) token)
        function _triggeredPrice(PricePair storage pair) internal view returns (uint blockNumber, uint price) {
    
            PriceInfo memory priceInfo = pair.price;
    
            if (uint(priceInfo.remainNum) > 0) {
                return (uint(priceInfo.height) + uint(_config.priceEffectSpan), _decodeFloat(priceInfo.priceFloat));
            }
            
            return (0, 0);
        }
    
        /// @dev Get the full information of latest trigger price
        /// @param pair Target price pair
        /// @return blockNumber The block number of price
        /// @return price The token price. (1eth equivalent to (price) token)
        /// @return avgPrice Average price
        /// @return sigmaSQ The square of the volatility (18 decimal places). The current implementation assumes that 
        ///         the volatility cannot exceed 1. Correspondingly, when the return value is equal to 999999999999996447,
        ///         it means that the volatility has exceeded the range that can be expressed
        function _triggeredPriceInfo(PricePair storage pair) internal view returns (
            uint blockNumber,
            uint price,
            uint avgPrice,
            uint sigmaSQ
        ) {
    
            PriceInfo memory priceInfo = pair.price;
    
            if (uint(priceInfo.remainNum) > 0) {
                return (
                    uint(priceInfo.height) + uint(_config.priceEffectSpan),
                    _decodeFloat(priceInfo.priceFloat),
                    _decodeFloat(priceInfo.avgFloat),
                    (uint(priceInfo.sigmaSQ) * 1 ether) >> 48
                );
            }
    
            return (0, 0, 0, 0);
        }
    
        /// @dev Find the price at block number
        /// @param pair Target price pair
        /// @param height Destination block number
        /// @return blockNumber The block number of price
        /// @return price The token price. (1eth equivalent to (price) token)
        function _findPrice(
            PricePair storage pair,
            uint height
        ) internal view returns (uint blockNumber, uint price) {
    
            PriceSheet[] storage sheets = pair.sheets;
            uint priceEffectSpan = uint(_config.priceEffectSpan);
    
            uint length = sheets.length;
            uint index = 0;
            uint sheetHeight;
            height -= priceEffectSpan;
            {
                // If there is no sheet in this channel, length is 0, length - 1 will overflow,
                uint right = length - 1;
                uint left = 0;
                // Find the index use Binary Search
                while (left < right) {
    
                    index = (left + right) >> 1;
                    sheetHeight = uint(sheets[index].height);
                    if (height > sheetHeight) {
                        left = ++index;
                    } else if (height < sheetHeight) {
                        // When index = 0, this statement will have an underflow exception, which usually 
                        // indicates that the effective block height passed during the call is lower than 
                        // the block height of the first quotation
                        right = --index;
                    } else {
                        break;
                    }
                }
            }
    
            // Calculate price
            uint totalEthNum = 0;
            uint totalTokenValue = 0;
            uint h = 0;
            uint remainNum;
            PriceSheet memory sheet;
    
            // Find sheets forward
            for (uint i = index; i < length;) {
    
                sheet = sheets[i++];
                sheetHeight = uint(sheet.height);
                if (height < sheetHeight) {
                    break;
                }
                remainNum = uint(sheet.remainNum);
                if (remainNum > 0) {
                    if (h == 0) {
                        h = sheetHeight;
                    } else if (h != sheetHeight) {
                        break;
                    }
                    totalEthNum += remainNum;
                    totalTokenValue += _decodeFloat(sheet.priceFloat) * remainNum;
                }
            }
    
            // Find sheets backward
            while (index > 0) {
    
                sheet = sheets[--index];
                remainNum = uint(sheet.remainNum);
                if (remainNum > 0) {
                    sheetHeight = uint(sheet.height);
                    if (h == 0) {
                        h = sheetHeight;
                    } else if (h != sheetHeight) {
                        break;
                    }
                    totalEthNum += remainNum;
                    totalTokenValue += _decodeFloat(sheet.priceFloat) * remainNum;
                }
            }
    
            if (totalEthNum > 0) {
                return (h + priceEffectSpan, totalTokenValue / totalEthNum);
            }
            return (0, 0);
        }
    
        /// @dev Get the last (num) effective price
        /// @param pair Target price pair
        /// @param count The number of prices that want to return
        /// @return An array which length is num * 2, each two element expresses one price like blockNumber|price
        function _lastPriceList(PricePair storage pair, uint count) internal view returns (uint[] memory) {
    
            PriceSheet[] storage sheets = pair.sheets;
            PriceSheet memory sheet;
            uint[] memory array = new uint[](count <<= 1);
    
            uint priceEffectSpan = uint(_config.priceEffectSpan);
            uint index = sheets.length;
            uint totalEthNum = 0;
            uint totalTokenValue = 0;
            uint height = 0;
    
            for (uint i = 0; i < count;) {
    
                bool flag = index == 0;
                if (flag || height != uint((sheet = sheets[--index]).height)) {
                    if (totalEthNum > 0 && height + priceEffectSpan < block.number) {
                        array[i++] = height + priceEffectSpan;
                        array[i++] = totalTokenValue / totalEthNum;
                    }
                    if (flag) {
                        break;
                    }
                    totalEthNum = 0;
                    totalTokenValue = 0;
                    height = uint(sheet.height);
                }
    
                uint remainNum = uint(sheet.remainNum);
                totalEthNum += remainNum;
                totalTokenValue += _decodeFloat(sheet.priceFloat) * remainNum;
            }
    
            return array;
        }
    }
    
    
    // File contracts/NestBatchPlatform2.sol
    
    // GPL-3.0-or-later
    
    pragma solidity ^0.8.6;
    /// @dev This contract implemented the query logic of nest
    contract NestBatchPlatform2 is NestBatchMining, INestBatchPriceView, INestBatchPrice2 {
    
        /* ========== INestBatchPriceView ========== */
    
        /// @dev Get the latest trigger price
        /// @param channelId Target channelId
        /// @param pairIndex Target pairIndex
        /// @return blockNumber The block number of price
        /// @return price The token price. (1eth equivalent to (price) token)
        function triggeredPrice(uint channelId, uint pairIndex) external view override noContract returns (uint blockNumber, uint price) {
            return _triggeredPrice(_channels[channelId].pairs[pairIndex]);
        }
    
        /// @dev Get the full information of latest trigger price
        /// @param channelId Target channelId
        /// @param pairIndex Target pairIndex
        /// @return blockNumber The block number of price
        /// @return price The token price. (1eth equivalent to (price) token)
        /// @return avgPrice Average price
        /// @return sigmaSQ The square of the volatility (18 decimal places). The current implementation assumes that 
        ///         the volatility cannot exceed 1. Correspondingly, when the return value is equal to 999999999999996447,
        ///         it means that the volatility has exceeded the range that can be expressed
        function triggeredPriceInfo(uint channelId, uint pairIndex) external view override noContract returns (
            uint blockNumber,
            uint price,
            uint avgPrice,
            uint sigmaSQ
        ) {
            return _triggeredPriceInfo(_channels[channelId].pairs[pairIndex]);
        }
    
        /// @dev Find the price at block number
        /// @param channelId Target channelId
        /// @param pairIndex Target pairIndex
        /// @param height Destination block number
        /// @return blockNumber The block number of price
        /// @return price The token price. (1eth equivalent to (price) token)
        function findPrice(
            uint channelId,
            uint pairIndex,
            uint height
        ) external view override noContract returns (uint blockNumber, uint price) {
            return _findPrice(_channels[channelId].pairs[pairIndex], height);
        }
    
        /// @dev Get the last (num) effective price
        /// @param channelId Target channelId
        /// @param pairIndex Target pairIndex
        /// @param count The number of prices that want to return
        /// @return An array which length is num * 2, each two element expresses one price like blockNumber|price
        function lastPriceList(uint channelId, uint pairIndex, uint count) external view override noContract returns (uint[] memory) {
            return _lastPriceList(_channels[channelId].pairs[pairIndex], count);
        } 
    
        /// @dev Returns lastPriceList and triggered price info
        /// @param channelId Target channelId
        /// @param pairIndex Target pairIndex
        /// @param count The number of prices that want to return
        /// @return prices An array which length is num * 2, each two element expresses one price like blockNumber|price
        /// @return triggeredPriceBlockNumber The block number of triggered price
        /// @return triggeredPriceValue The token triggered price. (1eth equivalent to (price) token)
        /// @return triggeredAvgPrice Average price
        /// @return triggeredSigmaSQ The square of the volatility (18 decimal places). The current implementation 
        /// assumes that the volatility cannot exceed 1. Correspondingly, when the return value is equal to 
        /// 999999999999996447, it means that the volatility has exceeded the range that can be expressed
        function lastPriceListAndTriggeredPriceInfo(uint channelId, uint pairIndex, uint count) external view override noContract
        returns (
            uint[] memory prices,
            uint triggeredPriceBlockNumber,
            uint triggeredPriceValue,
            uint triggeredAvgPrice,
            uint triggeredSigmaSQ
        ) {
            //return _lastPriceListAndTriggeredPriceInfo(_channels[channelId].pairs[pairIndex], count);
            PricePair storage pair = _channels[channelId].pairs[pairIndex];
            prices = _lastPriceList(pair, count);
            (
                triggeredPriceBlockNumber, 
                triggeredPriceValue, 
                triggeredAvgPrice, 
                triggeredSigmaSQ
            ) = _triggeredPriceInfo(pair);
        }
    
        /* ========== INestBatchPrice ========== */
    
        /// @dev Get the latest trigger price
        /// @param channelId Target channelId
        /// @param pairIndices Array of pair indices
        /// @param payback Address to receive refund
        /// @return prices Price array, i * 2 is the block where the ith price is located, and i * 2 + 1 is the ith price
        function triggeredPrice(
            uint channelId,
            uint[] calldata pairIndices, 
            address payback
        ) external payable override returns (uint[] memory prices) {
            PricePair[0xFFFF] storage pairs = _pay(channelId, payback).pairs;
    
            uint n = pairIndices.length << 1;
            prices = new uint[](n);
            while (n > 0) {
                n -= 2;
                (prices[n], prices[n + 1]) = _triggeredPrice(pairs[pairIndices[n >> 1]]);
            }
        }
    
        /// @dev Get the full information of latest trigger price
        /// @param channelId Target channelId
        /// @param pairIndices Array of pair indices
        /// @param payback Address to receive refund
        /// @return prices Price array, i * 4 is the block where the ith price is located, i * 4 + 1 is the ith price,
        /// i * 4 + 2 is the ith average price and i * 4 + 3 is the ith volatility
        function triggeredPriceInfo(
            uint channelId, 
            uint[] calldata pairIndices,
            address payback
        ) external payable override returns (uint[] memory prices) {
            PricePair[0xFFFF] storage pairs = _pay(channelId, payback).pairs;
    
            uint n = pairIndices.length << 2;
            prices = new uint[](n);
            while (n > 0) {
                n -= 4;
                (prices[n], prices[n + 1], prices[n + 2], prices[n + 3]) = _triggeredPriceInfo(pairs[pairIndices[n >> 2]]);
            }
        }
    
        /// @dev Find the price at block number
        /// @param channelId Target channelId
        /// @param pairIndices Array of pair indices
        /// @param height Destination block number
        /// @param payback Address to receive refund
        /// @return prices Price array, i * 2 is the block where the ith price is located, and i * 2 + 1 is the ith price
        function findPrice(
            uint channelId,
            uint[] calldata pairIndices, 
            uint height, 
            address payback
        ) external payable override returns (uint[] memory prices) {
            PricePair[0xFFFF] storage pairs = _pay(channelId, payback).pairs;
    
            uint n = pairIndices.length << 1;
            prices = new uint[](n);
            while (n > 0) {
                n -= 2;
                (prices[n], prices[n + 1]) = _findPrice(pairs[pairIndices[n >> 1]], height);
            }
        }
    
        /// @dev Get the last (num) effective price
        /// @param channelId Target channelId
        /// @param pairIndices Array of pair indices
        /// @param count The number of prices that want to return
        /// @param payback Address to receive refund
        /// @return prices Result array, i * count * 2 to (i + 1) * count * 2 - 1 are 
        /// the price results of group i quotation pairs
        function lastPriceList(
            uint channelId, 
            uint[] calldata pairIndices, 
            uint count, 
            address payback
        ) external payable override returns (uint[] memory prices) {
            PricePair[0xFFFF] storage pairs = _pay(channelId, payback).pairs;
    
            uint row = count << 1;
            uint n = pairIndices.length * row;
            prices = new uint[](n);
            while (n > 0) {
                n -= row;
                uint[] memory pi = _lastPriceList(pairs[pairIndices[n / row]], count);
                for (uint i = 0; i < row; ++i) {
                    prices[n + i] = pi[i];
                }
            }
        }
    
        /// @dev Returns lastPriceList and triggered price info
        /// @param channelId Target channelId
        /// @param pairIndices Array of pair indices
        /// @param count The number of prices that want to return
        /// @param payback Address to receive refund
        /// @return prices result of group i quotation pair. Among them, the first two count * are the latest prices, 
        /// and the last four are: trigger price block number, trigger price, average price and volatility
        function lastPriceListAndTriggeredPriceInfo(
            uint channelId, 
            uint[] calldata pairIndices,
            uint count, 
            address payback
        ) external payable override returns (uint[] memory prices) {
            PricePair[0xFFFF] storage pairs = _pay(channelId, payback).pairs;
    
            uint row = (count << 1) + 4;
            uint n = pairIndices.length * row;
            prices = new uint[](n);
            while (n > 0) {
                n -= row;
    
                PricePair storage pair = pairs[pairIndices[n / row]];
                uint[] memory pi = _lastPriceList(pair, count);
                for (uint i = 0; i + 4 < row; ++i) {
                    prices[n + i] = pi[i];
                }
                uint j = n + row - 4;
                (
                    prices[j],
                    prices[j + 1],
                    prices[j + 2],
                    prices[j + 3]
                ) = _triggeredPriceInfo(pair);
            }
        }
    
        // Payment of transfer fee
        function _pay(uint channelId, address payback) private returns (PriceChannel storage channel) {
    
            channel = _channels[channelId];
            uint fee = uint(channel.singleFee) * DIMI_ETHER;
            if (msg.value > fee) {
                payable(payback).transfer(msg.value - fee);
                // BSC adopts the old gas calculation strategy. Direct transfer may lead to the excess of gas 
                // in the agency contract. The following methods should be used for transfer
                //TransferHelper.safeTransferETH(payback, msg.value - fee);
            } else {
                require(msg.value == fee, "NOP:!fee");
            }
    
            channel.rewards += _toUInt96(fee);
        }
    }