Contract 0xa0354C17832E34dAf2aEae968AF3Dc558d5c6Dc6

Contract Overview

Balance:
0 Ether
Txn Hash Method
Block
From
To
Value
0xe48ce9000ef0d580d66e86a18f8e0f977afae4757b5ae8c0c9beb8261a10950bClose Current Fe...247110042021-05-07 11:12:5211 days 9 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0000821721
0x95136c972f9d0acff54796cf7f2b5c8debae957f5e52f5068a48bff817f9a606Close Current Fe...245630812021-04-30 11:10:4018 days 9 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0001271722
0x6f75a6197cec62653c51eff68b39ee5a33536013e3ceb2a33ed79944ffbcbf05Close Current Fe...244110722021-04-23 7:12:5625 days 13 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.000335865
0x9963f07277e8cfe92066d7c38895a013f563f53062c2fabe9d845e108c93a8b6Accept Ownership243386462021-04-19 21:38:4828 days 22 hrs ago0x73570075092502472e4b61a7058df1a4a1db12f2 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0000220521
0xec4a4dc4705bac73c7cbda986690d289b6a306bef11e0363191f3d8210d22e8dNominate New Own...243377202021-04-19 20:36:1229 days ago0xb64ff7a4a33acdf48d97dab0d764afd0f6176882 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0000448691
0xfbf17b40b598ea7a083b1ea8451acef9972cf3da36934bacd2d25eda6e82baa7Close Current Fe...243317702021-04-19 13:54:0429 days 6 hrs ago0x5bf2c51638e465711451b6f437aef14a9b0246fa IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.000196385
0xf588d9d7082386dac16137d3f46d73e412434564311d1e3484f66adcd9a26753Claim Fees243297372021-04-19 11:36:1229 days 9 hrs ago0x5bf2c51638e465711451b6f437aef14a9b0246fa IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.00355437525
0xbfedf499ef4dc07feca82240a173bde6654dea723c8844dcb660fa8644982751Claim Fees242810112021-04-16 11:28:1632 days 9 hrs ago0x68bf577920d6607c52114cb5e13696a19c2c9efa IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0001504581
0x28c7eb9389bc6321a82370ca6a7ee3020f75ab5cc058396cee961bfb85e1bf80Claim Fees242809992021-04-16 11:27:1232 days 9 hrs ago0x68bf577920d6607c52114cb5e13696a19c2c9efa IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0001504581
0x69e9ecd4a7c8aa79bc97ab4d33fcb6e3feb3c66eec333e4abea5813002bba74fClose Current Fe...242780352021-04-16 7:02:5232 days 13 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.00076303212
0x0e38895b91a83ab54137220b1215c185d30d312c2d653ecf8c0936090d9b0881Close Current Fe...242018552021-04-09 6:52:5239 days 13 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.000410865
0x23af726b2c7f411bd0884c23136cce429e3363b0a4547106eadb0a3c9ea37d0bClose Current Fe...241315492021-04-02 6:42:5246 days 13 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0012717220
0x6dd89cccb3fcc7c8beb184d1a4c65e86a9201de862d4d5f7a0d5101099e55bfbClose Current Fe...240578862021-03-26 6:32:5253 days 14 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0012325815
0x2556dbd8a4a2577fa33ed3e0a1ecb4f9edfb4d3bd198d90321b65bf64f5b9e15Close Current Fe...239827892021-03-19 6:22:5260 days 14 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0005608610
0x2dabc469d07daf6ab396cce4ec7aa7468bfd96d38fb1340e5dbbe8cb8823c2cbClose Current Fe...238786232021-03-12 6:13:0067 days 14 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0016434420
0x7e74a3ce1151382d9206fd56b672ed749ed00f4d07e14ae95a09a9684ceab837Close Current Fe...237667292021-03-05 6:02:5274 days 14 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0005901768
0x2fc19227e73d6e1f0a557cdd09b4f54a49911203fe7dbeb34fdd6fdacb2cb166Close Current Fe...236548952021-02-26 5:53:0481 days 14 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0004930326
0x49c69b40e5d158c65f9251dc896066f6be6e0ea871c204b240edd832d700b55fClose Current Fe...235443302021-02-19 5:42:5288 days 14 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0008412915
0x0c5eb09256c3d5542908b70ebe09e0b035c09a5cb5a1ebb2a3c5cf814e703c33Close Current Fe...234314802021-02-12 5:32:5295 days 15 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.000335865
0xeb6f602d8405898a8c1080f4171782aa97df65eabbd8ab063c09a5a6c6737aefClose Current Fe...233184362021-02-05 5:22:52102 days 15 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0001271722
0x2e68ad6586baa6c895409b7762417038e46ae62098375a44164767071823250cClose Current Fe...232057272021-01-29 5:13:00109 days 15 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.00098497212
0xe36fdc3a6a1796e52e3fca4d1e1f0e73936cfcb93d7c1defc84b7d3a2806a6f7Close Current Fe...231080122021-01-22 5:02:52116 days 15 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0000560411
0x263d9c3f714d2941f2f7fe0a028fd67d579d6000b7ed0f73d584700fc976a071Close Current Fe...230176482021-01-15 4:52:52123 days 15 hrs ago0xfee056f4d9d63a63d6cf16707d49ffae7ff3ff01 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0000820811
0x461c0554b8bc7fdfdd4578bc13ceb2c3bf0d8bd6242976e5f6a91e2428659afaImport Fee Perio...229994822021-01-13 19:06:12125 days 1 hr ago0xb64ff7a4a33acdf48d97dab0d764afd0f6176882 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0001361631
0x18ad12484070fbe93f0c5e7b2d131dbe8408ad1c5a2788512f994323d81f54afImport Fee Perio...229994802021-01-13 19:06:00125 days 1 hr ago0xb64ff7a4a33acdf48d97dab0d764afd0f6176882 IN  0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether0.0000840021
[ Download CSV Export 
Latest 25 internal transaction
Parent Txn Hash Block From To Value
0x072056ef09b9878b025be761b25c0997eedaf9d5d4d1c5e1bfea7186c722ef55248064152021-05-13 2:02:205 days 18 hrs ago 0xa0354c17832e34daf2aeae968af3dc558d5c6dc6 0x53bae964339e8a742b5b47f6c10bbfa8ff138f340 Ether
0x072056ef09b9878b025be761b25c0997eedaf9d5d4d1c5e1bfea7186c722ef55248064152021-05-13 2:02:205 days 18 hrs ago 0xa0354c17832e34daf2aeae968af3dc558d5c6dc6 0x2709b3568a79d7e00d6729e96b84a1996cdb89ef0 Ether
0x072056ef09b9878b025be761b25c0997eedaf9d5d4d1c5e1bfea7186c722ef55248064152021-05-13 2:02:205 days 18 hrs ago 0xf1d0ee19af243bcbc140a2259290b490e4df92a9 0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether
0x072056ef09b9878b025be761b25c0997eedaf9d5d4d1c5e1bfea7186c722ef55248064152021-05-13 2:02:205 days 18 hrs ago 0xf1d0ee19af243bcbc140a2259290b490e4df92a9 0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether
0x3c9511b6aacae18707e9eeabbaca6793799f00f02cd17c49592ba4e9fcef6ee1248063692021-05-13 1:57:445 days 18 hrs ago 0xa0354c17832e34daf2aeae968af3dc558d5c6dc6 0x53bae964339e8a742b5b47f6c10bbfa8ff138f340 Ether
0x3c9511b6aacae18707e9eeabbaca6793799f00f02cd17c49592ba4e9fcef6ee1248063692021-05-13 1:57:445 days 18 hrs ago 0xa0354c17832e34daf2aeae968af3dc558d5c6dc6 0x2709b3568a79d7e00d6729e96b84a1996cdb89ef0 Ether
0x3c9511b6aacae18707e9eeabbaca6793799f00f02cd17c49592ba4e9fcef6ee1248063692021-05-13 1:57:445 days 18 hrs ago 0xf1d0ee19af243bcbc140a2259290b490e4df92a9 0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether
0x3c9511b6aacae18707e9eeabbaca6793799f00f02cd17c49592ba4e9fcef6ee1248063692021-05-13 1:57:445 days 18 hrs ago 0xf1d0ee19af243bcbc140a2259290b490e4df92a9 0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether
0xdce4e233749307d331b26f35b04828393a86a8cb4130a87d4d9169a34a2519c0248017442021-05-12 18:19:206 days 2 hrs ago 0xa0354c17832e34daf2aeae968af3dc558d5c6dc6 0x53bae964339e8a742b5b47f6c10bbfa8ff138f340 Ether
0xdce4e233749307d331b26f35b04828393a86a8cb4130a87d4d9169a34a2519c0248017442021-05-12 18:19:206 days 2 hrs ago 0xa0354c17832e34daf2aeae968af3dc558d5c6dc6 0x2709b3568a79d7e00d6729e96b84a1996cdb89ef0 Ether
0xdce4e233749307d331b26f35b04828393a86a8cb4130a87d4d9169a34a2519c0248017442021-05-12 18:19:206 days 2 hrs ago 0xf1d0ee19af243bcbc140a2259290b490e4df92a9 0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether
0xdce4e233749307d331b26f35b04828393a86a8cb4130a87d4d9169a34a2519c0248017442021-05-12 18:19:206 days 2 hrs ago 0xf1d0ee19af243bcbc140a2259290b490e4df92a9 0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether
0x50c1d03d3cfa4ff83aa2d595da99de7a1fcea24f4232e7b4ff2c38b00ec39155248015662021-05-12 18:01:486 days 2 hrs ago 0xa0354c17832e34daf2aeae968af3dc558d5c6dc6 0x53bae964339e8a742b5b47f6c10bbfa8ff138f340 Ether
0x50c1d03d3cfa4ff83aa2d595da99de7a1fcea24f4232e7b4ff2c38b00ec39155248015662021-05-12 18:01:486 days 2 hrs ago 0xa0354c17832e34daf2aeae968af3dc558d5c6dc6 0x2709b3568a79d7e00d6729e96b84a1996cdb89ef0 Ether
0x50c1d03d3cfa4ff83aa2d595da99de7a1fcea24f4232e7b4ff2c38b00ec39155248015662021-05-12 18:01:486 days 2 hrs ago 0xf1d0ee19af243bcbc140a2259290b490e4df92a9 0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether
0x50c1d03d3cfa4ff83aa2d595da99de7a1fcea24f4232e7b4ff2c38b00ec39155248015662021-05-12 18:01:486 days 2 hrs ago 0xf1d0ee19af243bcbc140a2259290b490e4df92a9 0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether
0x32ea5ad36e44c6194964a662c9f69e882195467d5e64232660fc806ba9ac210c247952262021-05-12 7:36:006 days 13 hrs ago 0xa0354c17832e34daf2aeae968af3dc558d5c6dc6 0xc43b833f93c3896472ded3eff73311f571e387420 Ether
0x32ea5ad36e44c6194964a662c9f69e882195467d5e64232660fc806ba9ac210c247952262021-05-12 7:36:006 days 13 hrs ago 0xa0354c17832e34daf2aeae968af3dc558d5c6dc6 0x78b70223d9fa1a0abe6cd967472fa04fef3c75860 Ether
0x32ea5ad36e44c6194964a662c9f69e882195467d5e64232660fc806ba9ac210c247952262021-05-12 7:36:006 days 13 hrs ago 0x2709b3568a79d7e00d6729e96b84a1996cdb89ef 0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether
0x887126b680b0b5dd358093d55d51801edda8ac9d61ec0e99fcd84fa22deeedb5247948592021-05-12 6:59:486 days 13 hrs ago 0xa0354c17832e34daf2aeae968af3dc558d5c6dc6 0xc43b833f93c3896472ded3eff73311f571e387420 Ether
0x887126b680b0b5dd358093d55d51801edda8ac9d61ec0e99fcd84fa22deeedb5247948592021-05-12 6:59:486 days 13 hrs ago 0xa0354c17832e34daf2aeae968af3dc558d5c6dc6 0x78b70223d9fa1a0abe6cd967472fa04fef3c75860 Ether
0x887126b680b0b5dd358093d55d51801edda8ac9d61ec0e99fcd84fa22deeedb5247948592021-05-12 6:59:486 days 13 hrs ago 0x2709b3568a79d7e00d6729e96b84a1996cdb89ef 0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether
0x6c421f5da10c319b8e04a69d9c810ced25105fed197343ef666a3559492e40b6247940552021-05-12 5:40:326 days 14 hrs ago 0xa0354c17832e34daf2aeae968af3dc558d5c6dc6 0xc43b833f93c3896472ded3eff73311f571e387420 Ether
0x6c421f5da10c319b8e04a69d9c810ced25105fed197343ef666a3559492e40b6247940552021-05-12 5:40:326 days 14 hrs ago 0xa0354c17832e34daf2aeae968af3dc558d5c6dc6 0x78b70223d9fa1a0abe6cd967472fa04fef3c75860 Ether
0x6c421f5da10c319b8e04a69d9c810ced25105fed197343ef666a3559492e40b6247940552021-05-12 5:40:326 days 14 hrs ago 0x2709b3568a79d7e00d6729e96b84a1996cdb89ef 0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether
[ Download CSV Export 
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
FeePool

Compiler Version
v0.5.16+commit.9c3226ce

Optimization Enabled:
Yes with 1500 runs

Other Settings:
default evmVersion

Contract Source Code (Solidity)

/**
 *Submitted for verification at Etherscan.io on 2021-01-13
*/

/*

⚠⚠⚠ WARNING WARNING WARNING ⚠⚠⚠

This is a TARGET contract - DO NOT CONNECT TO IT DIRECTLY IN YOUR CONTRACTS or DAPPS!

This contract has an associated PROXY that MUST be used for all integrations - this TARGET will be REPLACED in an upcoming Synthetix release!
The proxy for this contract can be found here:

https://contracts.synthetix.io/kovan/ProxyFeePool

*//*
   ____            __   __        __   _
  / __/__ __ ___  / /_ / /  ___  / /_ (_)__ __
 _\ \ / // // _ \/ __// _ \/ -_)/ __// / \ \ /
/___/ \_, //_//_/\__//_//_/\__/ \__//_/ /_\_\
     /___/

* Synthetix: FeePool.sol
*
* Latest source (may be newer): https://github.com/Synthetixio/synthetix/blob/master/contracts/FeePool.sol
* Docs: https://docs.synthetix.io/contracts/FeePool
*
* Contract Dependencies: 
*	- EternalStorage
*	- IAddressResolver
*	- IFeePool
*	- LimitedSetup
*	- MixinResolver
*	- MixinSystemSettings
*	- Owned
*	- Proxyable
*	- State
* Libraries: 
*	- SafeDecimalMath
*	- SafeMath
*	- VestingEntries
*
* MIT License
* ===========
*
* Copyright (c) 2021 Synthetix
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*/



pragma solidity ^0.5.16;


// https://docs.synthetix.io/contracts/source/contracts/owned
contract Owned {
    address public owner;
    address public nominatedOwner;

    constructor(address _owner) public {
        require(_owner != address(0), "Owner address cannot be 0");
        owner = _owner;
        emit OwnerChanged(address(0), _owner);
    }

    function nominateNewOwner(address _owner) external onlyOwner {
        nominatedOwner = _owner;
        emit OwnerNominated(_owner);
    }

    function acceptOwnership() external {
        require(msg.sender == nominatedOwner, "You must be nominated before you can accept ownership");
        emit OwnerChanged(owner, nominatedOwner);
        owner = nominatedOwner;
        nominatedOwner = address(0);
    }

    modifier onlyOwner {
        _onlyOwner();
        _;
    }

    function _onlyOwner() private view {
        require(msg.sender == owner, "Only the contract owner may perform this action");
    }

    event OwnerNominated(address newOwner);
    event OwnerChanged(address oldOwner, address newOwner);
}


// Inheritance


// Internal references


// https://docs.synthetix.io/contracts/source/contracts/proxy
contract Proxy is Owned {
    Proxyable public target;

    constructor(address _owner) public Owned(_owner) {}

    function setTarget(Proxyable _target) external onlyOwner {
        target = _target;
        emit TargetUpdated(_target);
    }

    function _emit(
        bytes calldata callData,
        uint numTopics,
        bytes32 topic1,
        bytes32 topic2,
        bytes32 topic3,
        bytes32 topic4
    ) external onlyTarget {
        uint size = callData.length;
        bytes memory _callData = callData;

        assembly {
            /* The first 32 bytes of callData contain its length (as specified by the abi).
             * Length is assumed to be a uint256 and therefore maximum of 32 bytes
             * in length. It is also leftpadded to be a multiple of 32 bytes.
             * This means moving call_data across 32 bytes guarantees we correctly access
             * the data itself. */
            switch numTopics
                case 0 {
                    log0(add(_callData, 32), size)
                }
                case 1 {
                    log1(add(_callData, 32), size, topic1)
                }
                case 2 {
                    log2(add(_callData, 32), size, topic1, topic2)
                }
                case 3 {
                    log3(add(_callData, 32), size, topic1, topic2, topic3)
                }
                case 4 {
                    log4(add(_callData, 32), size, topic1, topic2, topic3, topic4)
                }
        }
    }

    // solhint-disable no-complex-fallback
    function() external payable {
        // Mutable call setting Proxyable.messageSender as this is using call not delegatecall
        target.setMessageSender(msg.sender);

        assembly {
            let free_ptr := mload(0x40)
            calldatacopy(free_ptr, 0, calldatasize)

            /* We must explicitly forward ether to the underlying contract as well. */
            let result := call(gas, sload(target_slot), callvalue, free_ptr, calldatasize, 0, 0)
            returndatacopy(free_ptr, 0, returndatasize)

            if iszero(result) {
                revert(free_ptr, returndatasize)
            }
            return(free_ptr, returndatasize)
        }
    }

    modifier onlyTarget {
        require(Proxyable(msg.sender) == target, "Must be proxy target");
        _;
    }

    event TargetUpdated(Proxyable newTarget);
}


// Inheritance


// Internal references


// https://docs.synthetix.io/contracts/source/contracts/proxyable
contract Proxyable is Owned {
    // This contract should be treated like an abstract contract

    /* The proxy this contract exists behind. */
    Proxy public proxy;
    Proxy public integrationProxy;

    /* The caller of the proxy, passed through to this contract.
     * Note that every function using this member must apply the onlyProxy or
     * optionalProxy modifiers, otherwise their invocations can use stale values. */
    address public messageSender;

    constructor(address payable _proxy) internal {
        // This contract is abstract, and thus cannot be instantiated directly
        require(owner != address(0), "Owner must be set");

        proxy = Proxy(_proxy);
        emit ProxyUpdated(_proxy);
    }

    function setProxy(address payable _proxy) external onlyOwner {
        proxy = Proxy(_proxy);
        emit ProxyUpdated(_proxy);
    }

    function setIntegrationProxy(address payable _integrationProxy) external onlyOwner {
        integrationProxy = Proxy(_integrationProxy);
    }

    function setMessageSender(address sender) external onlyProxy {
        messageSender = sender;
    }

    modifier onlyProxy {
        _onlyProxy();
        _;
    }

    function _onlyProxy() private view {
        require(Proxy(msg.sender) == proxy || Proxy(msg.sender) == integrationProxy, "Only the proxy can call");
    }

    modifier optionalProxy {
        _optionalProxy();
        _;
    }

    function _optionalProxy() private {
        if (Proxy(msg.sender) != proxy && Proxy(msg.sender) != integrationProxy && messageSender != msg.sender) {
            messageSender = msg.sender;
        }
    }

    modifier optionalProxy_onlyOwner {
        _optionalProxy_onlyOwner();
        _;
    }

    // solhint-disable-next-line func-name-mixedcase
    function _optionalProxy_onlyOwner() private {
        if (Proxy(msg.sender) != proxy && Proxy(msg.sender) != integrationProxy && messageSender != msg.sender) {
            messageSender = msg.sender;
        }
        require(messageSender == owner, "Owner only function");
    }

    event ProxyUpdated(address proxyAddress);
}


// https://docs.synthetix.io/contracts/source/contracts/limitedsetup
contract LimitedSetup {
    uint public setupExpiryTime;

    /**
     * @dev LimitedSetup Constructor.
     * @param setupDuration The time the setup period will last for.
     */
    constructor(uint setupDuration) internal {
        setupExpiryTime = now + setupDuration;
    }

    modifier onlyDuringSetup {
        require(now < setupExpiryTime, "Can only perform this action during setup");
        _;
    }
}


// https://docs.synthetix.io/contracts/source/interfaces/iaddressresolver
interface IAddressResolver {
    function getAddress(bytes32 name) external view returns (address);

    function getSynth(bytes32 key) external view returns (address);

    function requireAndGetAddress(bytes32 name, string calldata reason) external view returns (address);
}


// https://docs.synthetix.io/contracts/source/interfaces/isynth
interface ISynth {
    // Views
    function currencyKey() external view returns (bytes32);

    function transferableSynths(address account) external view returns (uint);

    // Mutative functions
    function transferAndSettle(address to, uint value) external returns (bool);

    function transferFromAndSettle(
        address from,
        address to,
        uint value
    ) external returns (bool);

    // Restricted: used internally to Synthetix
    function burn(address account, uint amount) external;

    function issue(address account, uint amount) external;
}


// https://docs.synthetix.io/contracts/source/interfaces/iissuer
interface IIssuer {
    // Views
    function anySynthOrSNXRateIsInvalid() external view returns (bool anyRateInvalid);

    function availableCurrencyKeys() external view returns (bytes32[] memory);

    function availableSynthCount() external view returns (uint);

    function availableSynths(uint index) external view returns (ISynth);

    function canBurnSynths(address account) external view returns (bool);

    function collateral(address account) external view returns (uint);

    function collateralisationRatio(address issuer) external view returns (uint);

    function collateralisationRatioAndAnyRatesInvalid(address _issuer)
        external
        view
        returns (uint cratio, bool anyRateIsInvalid);

    function debtBalanceOf(address issuer, bytes32 currencyKey) external view returns (uint debtBalance);

    function issuanceRatio() external view returns (uint);

    function lastIssueEvent(address account) external view returns (uint);

    function maxIssuableSynths(address issuer) external view returns (uint maxIssuable);

    function minimumStakeTime() external view returns (uint);

    function remainingIssuableSynths(address issuer)
        external
        view
        returns (
            uint maxIssuable,
            uint alreadyIssued,
            uint totalSystemDebt
        );

    function synths(bytes32 currencyKey) external view returns (ISynth);

    function getSynths(bytes32[] calldata currencyKeys) external view returns (ISynth[] memory);

    function synthsByAddress(address synthAddress) external view returns (bytes32);

    function totalIssuedSynths(bytes32 currencyKey, bool excludeEtherCollateral) external view returns (uint);

    function transferableSynthetixAndAnyRateIsInvalid(address account, uint balance)
        external
        view
        returns (uint transferable, bool anyRateIsInvalid);

    // Restricted: used internally to Synthetix
    function issueSynths(address from, uint amount) external;

    function issueSynthsOnBehalf(
        address issueFor,
        address from,
        uint amount
    ) external;

    function issueMaxSynths(address from) external;

    function issueMaxSynthsOnBehalf(address issueFor, address from) external;

    function burnSynths(address from, uint amount) external;

    function burnSynthsOnBehalf(
        address burnForAddress,
        address from,
        uint amount
    ) external;

    function burnSynthsToTarget(address from) external;

    function burnSynthsToTargetOnBehalf(address burnForAddress, address from) external;

    function liquidateDelinquentAccount(
        address account,
        uint susdAmount,
        address liquidator
    ) external returns (uint totalRedeemed, uint amountToLiquidate);
}


// Inheritance


// Internal references


// https://docs.synthetix.io/contracts/source/contracts/addressresolver
contract AddressResolver is Owned, IAddressResolver {
    mapping(bytes32 => address) public repository;

    constructor(address _owner) public Owned(_owner) {}

    /* ========== RESTRICTED FUNCTIONS ========== */

    function importAddresses(bytes32[] calldata names, address[] calldata destinations) external onlyOwner {
        require(names.length == destinations.length, "Input lengths must match");

        for (uint i = 0; i < names.length; i++) {
            bytes32 name = names[i];
            address destination = destinations[i];
            repository[name] = destination;
            emit AddressImported(name, destination);
        }
    }

    /* ========= PUBLIC FUNCTIONS ========== */

    function rebuildCaches(MixinResolver[] calldata destinations) external {
        for (uint i = 0; i < destinations.length; i++) {
            destinations[i].rebuildCache();
        }
    }

    /* ========== VIEWS ========== */

    function areAddressesImported(bytes32[] calldata names, address[] calldata destinations) external view returns (bool) {
        for (uint i = 0; i < names.length; i++) {
            if (repository[names[i]] != destinations[i]) {
                return false;
            }
        }
        return true;
    }

    function getAddress(bytes32 name) external view returns (address) {
        return repository[name];
    }

    function requireAndGetAddress(bytes32 name, string calldata reason) external view returns (address) {
        address _foundAddress = repository[name];
        require(_foundAddress != address(0), reason);
        return _foundAddress;
    }

    function getSynth(bytes32 key) external view returns (address) {
        IIssuer issuer = IIssuer(repository["Issuer"]);
        require(address(issuer) != address(0), "Cannot find Issuer address");
        return address(issuer.synths(key));
    }

    /* ========== EVENTS ========== */

    event AddressImported(bytes32 name, address destination);
}


// solhint-disable payable-fallback

// https://docs.synthetix.io/contracts/source/contracts/readproxy
contract ReadProxy is Owned {
    address public target;

    constructor(address _owner) public Owned(_owner) {}

    function setTarget(address _target) external onlyOwner {
        target = _target;
        emit TargetUpdated(target);
    }

    function() external {
        // The basics of a proxy read call
        // Note that msg.sender in the underlying will always be the address of this contract.
        assembly {
            calldatacopy(0, 0, calldatasize)

            // Use of staticcall - this will revert if the underlying function mutates state
            let result := staticcall(gas, sload(target_slot), 0, calldatasize, 0, 0)
            returndatacopy(0, 0, returndatasize)

            if iszero(result) {
                revert(0, returndatasize)
            }
            return(0, returndatasize)
        }
    }

    event TargetUpdated(address newTarget);
}


// Inheritance


// Internal references


// https://docs.synthetix.io/contracts/source/contracts/mixinresolver
contract MixinResolver {
    AddressResolver public resolver;

    mapping(bytes32 => address) private addressCache;

    constructor(address _resolver) internal {
        resolver = AddressResolver(_resolver);
    }

    /* ========== INTERNAL FUNCTIONS ========== */

    function combineArrays(bytes32[] memory first, bytes32[] memory second)
        internal
        pure
        returns (bytes32[] memory combination)
    {
        combination = new bytes32[](first.length + second.length);

        for (uint i = 0; i < first.length; i++) {
            combination[i] = first[i];
        }

        for (uint j = 0; j < second.length; j++) {
            combination[first.length + j] = second[j];
        }
    }

    /* ========== PUBLIC FUNCTIONS ========== */

    // Note: this function is public not external in order for it to be overridden and invoked via super in subclasses
    function resolverAddressesRequired() public view returns (bytes32[] memory addresses) {}

    function rebuildCache() public {
        bytes32[] memory requiredAddresses = resolverAddressesRequired();
        // The resolver must call this function whenver it updates its state
        for (uint i = 0; i < requiredAddresses.length; i++) {
            bytes32 name = requiredAddresses[i];
            // Note: can only be invoked once the resolver has all the targets needed added
            address destination = resolver.requireAndGetAddress(
                name,
                string(abi.encodePacked("Resolver missing target: ", name))
            );
            addressCache[name] = destination;
            emit CacheUpdated(name, destination);
        }
    }

    /* ========== VIEWS ========== */

    function isResolverCached() external view returns (bool) {
        bytes32[] memory requiredAddresses = resolverAddressesRequired();
        for (uint i = 0; i < requiredAddresses.length; i++) {
            bytes32 name = requiredAddresses[i];
            // false if our cache is invalid or if the resolver doesn't have the required address
            if (resolver.getAddress(name) != addressCache[name] || addressCache[name] == address(0)) {
                return false;
            }
        }

        return true;
    }

    /* ========== INTERNAL FUNCTIONS ========== */

    function requireAndGetAddress(bytes32 name) internal view returns (address) {
        address _foundAddress = addressCache[name];
        require(_foundAddress != address(0), string(abi.encodePacked("Missing address: ", name)));
        return _foundAddress;
    }

    /* ========== EVENTS ========== */

    event CacheUpdated(bytes32 name, address destination);
}


// https://docs.synthetix.io/contracts/source/interfaces/iflexiblestorage
interface IFlexibleStorage {
    // Views
    function getUIntValue(bytes32 contractName, bytes32 record) external view returns (uint);

    function getUIntValues(bytes32 contractName, bytes32[] calldata records) external view returns (uint[] memory);

    function getIntValue(bytes32 contractName, bytes32 record) external view returns (int);

    function getIntValues(bytes32 contractName, bytes32[] calldata records) external view returns (int[] memory);

    function getAddressValue(bytes32 contractName, bytes32 record) external view returns (address);

    function getAddressValues(bytes32 contractName, bytes32[] calldata records) external view returns (address[] memory);

    function getBoolValue(bytes32 contractName, bytes32 record) external view returns (bool);

    function getBoolValues(bytes32 contractName, bytes32[] calldata records) external view returns (bool[] memory);

    function getBytes32Value(bytes32 contractName, bytes32 record) external view returns (bytes32);

    function getBytes32Values(bytes32 contractName, bytes32[] calldata records) external view returns (bytes32[] memory);

    // Mutative functions
    function deleteUIntValue(bytes32 contractName, bytes32 record) external;

    function deleteIntValue(bytes32 contractName, bytes32 record) external;

    function deleteAddressValue(bytes32 contractName, bytes32 record) external;

    function deleteBoolValue(bytes32 contractName, bytes32 record) external;

    function deleteBytes32Value(bytes32 contractName, bytes32 record) external;

    function setUIntValue(
        bytes32 contractName,
        bytes32 record,
        uint value
    ) external;

    function setUIntValues(
        bytes32 contractName,
        bytes32[] calldata records,
        uint[] calldata values
    ) external;

    function setIntValue(
        bytes32 contractName,
        bytes32 record,
        int value
    ) external;

    function setIntValues(
        bytes32 contractName,
        bytes32[] calldata records,
        int[] calldata values
    ) external;

    function setAddressValue(
        bytes32 contractName,
        bytes32 record,
        address value
    ) external;

    function setAddressValues(
        bytes32 contractName,
        bytes32[] calldata records,
        address[] calldata values
    ) external;

    function setBoolValue(
        bytes32 contractName,
        bytes32 record,
        bool value
    ) external;

    function setBoolValues(
        bytes32 contractName,
        bytes32[] calldata records,
        bool[] calldata values
    ) external;

    function setBytes32Value(
        bytes32 contractName,
        bytes32 record,
        bytes32 value
    ) external;

    function setBytes32Values(
        bytes32 contractName,
        bytes32[] calldata records,
        bytes32[] calldata values
    ) external;
}


// Internal references


// https://docs.synthetix.io/contracts/source/contracts/mixinsystemsettings
contract MixinSystemSettings is MixinResolver {
    bytes32 internal constant SETTING_CONTRACT_NAME = "SystemSettings";

    bytes32 internal constant SETTING_WAITING_PERIOD_SECS = "waitingPeriodSecs";
    bytes32 internal constant SETTING_PRICE_DEVIATION_THRESHOLD_FACTOR = "priceDeviationThresholdFactor";
    bytes32 internal constant SETTING_ISSUANCE_RATIO = "issuanceRatio";
    bytes32 internal constant SETTING_FEE_PERIOD_DURATION = "feePeriodDuration";
    bytes32 internal constant SETTING_TARGET_THRESHOLD = "targetThreshold";
    bytes32 internal constant SETTING_LIQUIDATION_DELAY = "liquidationDelay";
    bytes32 internal constant SETTING_LIQUIDATION_RATIO = "liquidationRatio";
    bytes32 internal constant SETTING_LIQUIDATION_PENALTY = "liquidationPenalty";
    bytes32 internal constant SETTING_RATE_STALE_PERIOD = "rateStalePeriod";
    bytes32 internal constant SETTING_EXCHANGE_FEE_RATE = "exchangeFeeRate";
    bytes32 internal constant SETTING_MINIMUM_STAKE_TIME = "minimumStakeTime";
    bytes32 internal constant SETTING_AGGREGATOR_WARNING_FLAGS = "aggregatorWarningFlags";
    bytes32 internal constant SETTING_TRADING_REWARDS_ENABLED = "tradingRewardsEnabled";
    bytes32 internal constant SETTING_DEBT_SNAPSHOT_STALE_TIME = "debtSnapshotStaleTime";
    bytes32 internal constant SETTING_CROSS_DOMAIN_DEPOSIT_GAS_LIMIT = "crossDomainDepositGasLimit";
    bytes32 internal constant SETTING_CROSS_DOMAIN_ESCROW_GAS_LIMIT = "crossDomainEscrowGasLimit";
    bytes32 internal constant SETTING_CROSS_DOMAIN_REWARD_GAS_LIMIT = "crossDomainRewardGasLimit";
    bytes32 internal constant SETTING_CROSS_DOMAIN_WITHDRAWAL_GAS_LIMIT = "crossDomainWithdrawalGasLimit";

    bytes32 internal constant CONTRACT_FLEXIBLESTORAGE = "FlexibleStorage";

    enum CrossDomainMessageGasLimits {Deposit, Escrow, Reward, Withdrawal}

    constructor(address _resolver) internal MixinResolver(_resolver) {}

    function resolverAddressesRequired() public view returns (bytes32[] memory addresses) {
        addresses = new bytes32[](1);
        addresses[0] = CONTRACT_FLEXIBLESTORAGE;
    }

    function flexibleStorage() internal view returns (IFlexibleStorage) {
        return IFlexibleStorage(requireAndGetAddress(CONTRACT_FLEXIBLESTORAGE));
    }

    function _getGasLimitSetting(CrossDomainMessageGasLimits gasLimitType) internal pure returns (bytes32) {
        if (gasLimitType == CrossDomainMessageGasLimits.Deposit) {
            return SETTING_CROSS_DOMAIN_DEPOSIT_GAS_LIMIT;
        } else if (gasLimitType == CrossDomainMessageGasLimits.Escrow) {
            return SETTING_CROSS_DOMAIN_ESCROW_GAS_LIMIT;
        } else if (gasLimitType == CrossDomainMessageGasLimits.Reward) {
            return SETTING_CROSS_DOMAIN_REWARD_GAS_LIMIT;
        } else if (gasLimitType == CrossDomainMessageGasLimits.Withdrawal) {
            return SETTING_CROSS_DOMAIN_WITHDRAWAL_GAS_LIMIT;
        } else {
            revert("Unknown gas limit type");
        }
    }

    function getCrossDomainMessageGasLimit(CrossDomainMessageGasLimits gasLimitType) internal view returns (uint) {
        return flexibleStorage().getUIntValue(SETTING_CONTRACT_NAME, _getGasLimitSetting(gasLimitType));
    }

    function getTradingRewardsEnabled() internal view returns (bool) {
        return flexibleStorage().getBoolValue(SETTING_CONTRACT_NAME, SETTING_TRADING_REWARDS_ENABLED);
    }

    function getWaitingPeriodSecs() internal view returns (uint) {
        return flexibleStorage().getUIntValue(SETTING_CONTRACT_NAME, SETTING_WAITING_PERIOD_SECS);
    }

    function getPriceDeviationThresholdFactor() internal view returns (uint) {
        return flexibleStorage().getUIntValue(SETTING_CONTRACT_NAME, SETTING_PRICE_DEVIATION_THRESHOLD_FACTOR);
    }

    function getIssuanceRatio() internal view returns (uint) {
        // lookup on flexible storage directly for gas savings (rather than via SystemSettings)
        return flexibleStorage().getUIntValue(SETTING_CONTRACT_NAME, SETTING_ISSUANCE_RATIO);
    }

    function getFeePeriodDuration() internal view returns (uint) {
        // lookup on flexible storage directly for gas savings (rather than via SystemSettings)
        return flexibleStorage().getUIntValue(SETTING_CONTRACT_NAME, SETTING_FEE_PERIOD_DURATION);
    }

    function getTargetThreshold() internal view returns (uint) {
        // lookup on flexible storage directly for gas savings (rather than via SystemSettings)
        return flexibleStorage().getUIntValue(SETTING_CONTRACT_NAME, SETTING_TARGET_THRESHOLD);
    }

    function getLiquidationDelay() internal view returns (uint) {
        return flexibleStorage().getUIntValue(SETTING_CONTRACT_NAME, SETTING_LIQUIDATION_DELAY);
    }

    function getLiquidationRatio() internal view returns (uint) {
        return flexibleStorage().getUIntValue(SETTING_CONTRACT_NAME, SETTING_LIQUIDATION_RATIO);
    }

    function getLiquidationPenalty() internal view returns (uint) {
        return flexibleStorage().getUIntValue(SETTING_CONTRACT_NAME, SETTING_LIQUIDATION_PENALTY);
    }

    function getRateStalePeriod() internal view returns (uint) {
        return flexibleStorage().getUIntValue(SETTING_CONTRACT_NAME, SETTING_RATE_STALE_PERIOD);
    }

    function getExchangeFeeRate(bytes32 currencyKey) internal view returns (uint) {
        return
            flexibleStorage().getUIntValue(
                SETTING_CONTRACT_NAME,
                keccak256(abi.encodePacked(SETTING_EXCHANGE_FEE_RATE, currencyKey))
            );
    }

    function getMinimumStakeTime() internal view returns (uint) {
        return flexibleStorage().getUIntValue(SETTING_CONTRACT_NAME, SETTING_MINIMUM_STAKE_TIME);
    }

    function getAggregatorWarningFlags() internal view returns (address) {
        return flexibleStorage().getAddressValue(SETTING_CONTRACT_NAME, SETTING_AGGREGATOR_WARNING_FLAGS);
    }

    function getDebtSnapshotStaleTime() internal view returns (uint) {
        return flexibleStorage().getUIntValue(SETTING_CONTRACT_NAME, SETTING_DEBT_SNAPSHOT_STALE_TIME);
    }
}


// https://docs.synthetix.io/contracts/source/interfaces/ifeepool
interface IFeePool {
    // Views

    // solhint-disable-next-line func-name-mixedcase
    function FEE_ADDRESS() external view returns (address);

    function feesAvailable(address account) external view returns (uint, uint);

    function feePeriodDuration() external view returns (uint);

    function isFeesClaimable(address account) external view returns (bool);

    function targetThreshold() external view returns (uint);

    function totalFeesAvailable() external view returns (uint);

    function totalRewardsAvailable() external view returns (uint);

    // Mutative Functions
    function claimFees() external returns (bool);

    function claimOnBehalf(address claimingForAddress) external returns (bool);

    function closeCurrentFeePeriod() external;

    // Restricted: used internally to Synthetix
    function appendAccountIssuanceRecord(
        address account,
        uint lockedAmount,
        uint debtEntryIndex
    ) external;

    function recordFeePaid(uint sUSDAmount) external;

    function setRewardsToDistribute(uint amount) external;
}


/**
 * @dev Wrappers over Solidity's arithmetic operations with added overflow
 * checks.
 *
 * Arithmetic operations in Solidity wrap on overflow. This can easily result
 * in bugs, because programmers usually assume that an overflow raises an
 * error, which is the standard behavior in high level programming languages.
 * `SafeMath` restores this intuition by reverting the transaction when an
 * operation overflows.
 *
 * Using this library instead of the unchecked operations eliminates an entire
 * class of bugs, so it's recommended to use it always.
 */
library SafeMath {
    /**
     * @dev Returns the addition of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `+` operator.
     *
     * Requirements:
     * - Addition cannot overflow.
     */
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");

        return c;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting on
     * overflow (when the result is negative).
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b <= a, "SafeMath: subtraction overflow");
        uint256 c = a - b;

        return c;
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `*` operator.
     *
     * Requirements:
     * - Multiplication cannot overflow.
     */
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
        // benefit is lost if 'b' is also tested.
        // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
        if (a == 0) {
            return 0;
        }

        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");

        return c;
    }

    /**
     * @dev Returns the integer division of two unsigned integers. Reverts on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        // Solidity only automatically asserts when dividing by 0
        require(b > 0, "SafeMath: division by zero");
        uint256 c = a / b;
        // assert(a == b * c + a % b); // There is no case in which this doesn't hold

        return c;
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * Reverts when dividing by zero.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b != 0, "SafeMath: modulo by zero");
        return a % b;
    }
}


// Libraries


// https://docs.synthetix.io/contracts/source/libraries/safedecimalmath
library SafeDecimalMath {
    using SafeMath for uint;

    /* Number of decimal places in the representations. */
    uint8 public constant decimals = 18;
    uint8 public constant highPrecisionDecimals = 27;

    /* The number representing 1.0. */
    uint public constant UNIT = 10**uint(decimals);

    /* The number representing 1.0 for higher fidelity numbers. */
    uint public constant PRECISE_UNIT = 10**uint(highPrecisionDecimals);
    uint private constant UNIT_TO_HIGH_PRECISION_CONVERSION_FACTOR = 10**uint(highPrecisionDecimals - decimals);

    /**
     * @return Provides an interface to UNIT.
     */
    function unit() external pure returns (uint) {
        return UNIT;
    }

    /**
     * @return Provides an interface to PRECISE_UNIT.
     */
    function preciseUnit() external pure returns (uint) {
        return PRECISE_UNIT;
    }

    /**
     * @return The result of multiplying x and y, interpreting the operands as fixed-point
     * decimals.
     *
     * @dev A unit factor is divided out after the product of x and y is evaluated,
     * so that product must be less than 2**256. As this is an integer division,
     * the internal division always rounds down. This helps save on gas. Rounding
     * is more expensive on gas.
     */
    function multiplyDecimal(uint x, uint y) internal pure returns (uint) {
        /* Divide by UNIT to remove the extra factor introduced by the product. */
        return x.mul(y) / UNIT;
    }

    /**
     * @return The result of safely multiplying x and y, interpreting the operands
     * as fixed-point decimals of the specified precision unit.
     *
     * @dev The operands should be in the form of a the specified unit factor which will be
     * divided out after the product of x and y is evaluated, so that product must be
     * less than 2**256.
     *
     * Unlike multiplyDecimal, this function rounds the result to the nearest increment.
     * Rounding is useful when you need to retain fidelity for small decimal numbers
     * (eg. small fractions or percentages).
     */
    function _multiplyDecimalRound(
        uint x,
        uint y,
        uint precisionUnit
    ) private pure returns (uint) {
        /* Divide by UNIT to remove the extra factor introduced by the product. */
        uint quotientTimesTen = x.mul(y) / (precisionUnit / 10);

        if (quotientTimesTen % 10 >= 5) {
            quotientTimesTen += 10;
        }

        return quotientTimesTen / 10;
    }

    /**
     * @return The result of safely multiplying x and y, interpreting the operands
     * as fixed-point decimals of a precise unit.
     *
     * @dev The operands should be in the precise unit factor which will be
     * divided out after the product of x and y is evaluated, so that product must be
     * less than 2**256.
     *
     * Unlike multiplyDecimal, this function rounds the result to the nearest increment.
     * Rounding is useful when you need to retain fidelity for small decimal numbers
     * (eg. small fractions or percentages).
     */
    function multiplyDecimalRoundPrecise(uint x, uint y) internal pure returns (uint) {
        return _multiplyDecimalRound(x, y, PRECISE_UNIT);
    }

    /**
     * @return The result of safely multiplying x and y, interpreting the operands
     * as fixed-point decimals of a standard unit.
     *
     * @dev The operands should be in the standard unit factor which will be
     * divided out after the product of x and y is evaluated, so that product must be
     * less than 2**256.
     *
     * Unlike multiplyDecimal, this function rounds the result to the nearest increment.
     * Rounding is useful when you need to retain fidelity for small decimal numbers
     * (eg. small fractions or percentages).
     */
    function multiplyDecimalRound(uint x, uint y) internal pure returns (uint) {
        return _multiplyDecimalRound(x, y, UNIT);
    }

    /**
     * @return The result of safely dividing x and y. The return value is a high
     * precision decimal.
     *
     * @dev y is divided after the product of x and the standard precision unit
     * is evaluated, so the product of x and UNIT must be less than 2**256. As
     * this is an integer division, the result is always rounded down.
     * This helps save on gas. Rounding is more expensive on gas.
     */
    function divideDecimal(uint x, uint y) internal pure returns (uint) {
        /* Reintroduce the UNIT factor that will be divided out by y. */
        return x.mul(UNIT).div(y);
    }

    /**
     * @return The result of safely dividing x and y. The return value is as a rounded
     * decimal in the precision unit specified in the parameter.
     *
     * @dev y is divided after the product of x and the specified precision unit
     * is evaluated, so the product of x and the specified precision unit must
     * be less than 2**256. The result is rounded to the nearest increment.
     */
    function _divideDecimalRound(
        uint x,
        uint y,
        uint precisionUnit
    ) private pure returns (uint) {
        uint resultTimesTen = x.mul(precisionUnit * 10).div(y);

        if (resultTimesTen % 10 >= 5) {
            resultTimesTen += 10;
        }

        return resultTimesTen / 10;
    }

    /**
     * @return The result of safely dividing x and y. The return value is as a rounded
     * standard precision decimal.
     *
     * @dev y is divided after the product of x and the standard precision unit
     * is evaluated, so the product of x and the standard precision unit must
     * be less than 2**256. The result is rounded to the nearest increment.
     */
    function divideDecimalRound(uint x, uint y) internal pure returns (uint) {
        return _divideDecimalRound(x, y, UNIT);
    }

    /**
     * @return The result of safely dividing x and y. The return value is as a rounded
     * high precision decimal.
     *
     * @dev y is divided after the product of x and the high precision unit
     * is evaluated, so the product of x and the high precision unit must
     * be less than 2**256. The result is rounded to the nearest increment.
     */
    function divideDecimalRoundPrecise(uint x, uint y) internal pure returns (uint) {
        return _divideDecimalRound(x, y, PRECISE_UNIT);
    }

    /**
     * @dev Convert a standard decimal representation to a high precision one.
     */
    function decimalToPreciseDecimal(uint i) internal pure returns (uint) {
        return i.mul(UNIT_TO_HIGH_PRECISION_CONVERSION_FACTOR);
    }

    /**
     * @dev Convert a high precision decimal to a standard decimal representation.
     */
    function preciseDecimalToDecimal(uint i) internal pure returns (uint) {
        uint quotientTimesTen = i / (UNIT_TO_HIGH_PRECISION_CONVERSION_FACTOR / 10);

        if (quotientTimesTen % 10 >= 5) {
            quotientTimesTen += 10;
        }

        return quotientTimesTen / 10;
    }
}


// https://docs.synthetix.io/contracts/source/interfaces/ierc20
interface IERC20 {
    // ERC20 Optional Views
    function name() external view returns (string memory);

    function symbol() external view returns (string memory);

    function decimals() external view returns (uint8);

    // Views
    function totalSupply() external view returns (uint);

    function balanceOf(address owner) external view returns (uint);

    function allowance(address owner, address spender) external view returns (uint);

    // Mutative functions
    function transfer(address to, uint value) external returns (bool);

    function approve(address spender, uint value) external returns (bool);

    function transferFrom(
        address from,
        address to,
        uint value
    ) external returns (bool);

    // Events
    event Transfer(address indexed from, address indexed to, uint value);

    event Approval(address indexed owner, address indexed spender, uint value);
}


// https://docs.synthetix.io/contracts/source/interfaces/isystemstatus
interface ISystemStatus {
    struct Status {
        bool canSuspend;
        bool canResume;
    }

    struct Suspension {
        bool suspended;
        // reason is an integer code,
        // 0 => no reason, 1 => upgrading, 2+ => defined by system usage
        uint248 reason;
    }

    // Views
    function accessControl(bytes32 section, address account) external view returns (bool canSuspend, bool canResume);

    function requireSystemActive() external view;

    function requireIssuanceActive() external view;

    function requireExchangeActive() external view;

    function requireSynthActive(bytes32 currencyKey) external view;

    function requireSynthsActive(bytes32 sourceCurrencyKey, bytes32 destinationCurrencyKey) external view;

    function synthSuspension(bytes32 currencyKey) external view returns (bool suspended, uint248 reason);

    // Restricted functions
    function suspendSynth(bytes32 currencyKey, uint256 reason) external;

    function updateAccessControl(
        bytes32 section,
        address account,
        bool canSuspend,
        bool canResume
    ) external;
}


interface IVirtualSynth {
    // Views
    function balanceOfUnderlying(address account) external view returns (uint);

    function rate() external view returns (uint);

    function readyToSettle() external view returns (bool);

    function secsLeftInWaitingPeriod() external view returns (uint);

    function settled() external view returns (bool);

    function synth() external view returns (ISynth);

    // Mutative functions
    function settle(address account) external;
}


// https://docs.synthetix.io/contracts/source/interfaces/isynthetix
interface ISynthetix {
    // Views
    function anySynthOrSNXRateIsInvalid() external view returns (bool anyRateInvalid);

    function availableCurrencyKeys() external view returns (bytes32[] memory);

    function availableSynthCount() external view returns (uint);

    function availableSynths(uint index) external view returns (ISynth);

    function collateral(address account) external view returns (uint);

    function collateralisationRatio(address issuer) external view returns (uint);

    function debtBalanceOf(address issuer, bytes32 currencyKey) external view returns (uint);

    function isWaitingPeriod(bytes32 currencyKey) external view returns (bool);

    function maxIssuableSynths(address issuer) external view returns (uint maxIssuable);

    function remainingIssuableSynths(address issuer)
        external
        view
        returns (
            uint maxIssuable,
            uint alreadyIssued,
            uint totalSystemDebt
        );

    function synths(bytes32 currencyKey) external view returns (ISynth);

    function synthsByAddress(address synthAddress) external view returns (bytes32);

    function totalIssuedSynths(bytes32 currencyKey) external view returns (uint);

    function totalIssuedSynthsExcludeEtherCollateral(bytes32 currencyKey) external view returns (uint);

    function transferableSynthetix(address account) external view returns (uint transferable);

    // Mutative Functions
    function burnSynths(uint amount) external;

    function burnSynthsOnBehalf(address burnForAddress, uint amount) external;

    function burnSynthsToTarget() external;

    function burnSynthsToTargetOnBehalf(address burnForAddress) external;

    function exchange(
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey
    ) external returns (uint amountReceived);

    function exchangeOnBehalf(
        address exchangeForAddress,
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey
    ) external returns (uint amountReceived);

    function exchangeWithTracking(
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey,
        address originator,
        bytes32 trackingCode
    ) external returns (uint amountReceived);

    function exchangeOnBehalfWithTracking(
        address exchangeForAddress,
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey,
        address originator,
        bytes32 trackingCode
    ) external returns (uint amountReceived);

    function exchangeWithVirtual(
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey,
        bytes32 trackingCode
    ) external returns (uint amountReceived, IVirtualSynth vSynth);

    function issueMaxSynths() external;

    function issueMaxSynthsOnBehalf(address issueForAddress) external;

    function issueSynths(uint amount) external;

    function issueSynthsOnBehalf(address issueForAddress, uint amount) external;

    function mint() external returns (bool);

    function settle(bytes32 currencyKey)
        external
        returns (
            uint reclaimed,
            uint refunded,
            uint numEntries
        );

    // Liquidations
    function liquidateDelinquentAccount(address account, uint susdAmount) external returns (bool);

    // Restricted Functions

    function mintSecondary(address account, uint amount) external;

    function mintSecondaryRewards(uint amount) external;

    function burnSecondary(address account, uint amount) external;
}


// Inheritance


// Libraries


// Internal references


// https://docs.synthetix.io/contracts/source/contracts/feepoolstate
contract FeePoolState is Owned, LimitedSetup {
    using SafeMath for uint;
    using SafeDecimalMath for uint;

    /* ========== STATE VARIABLES ========== */

    uint8 public constant FEE_PERIOD_LENGTH = 6;

    address public feePool;

    // The IssuanceData activity that's happened in a fee period.
    struct IssuanceData {
        uint debtPercentage;
        uint debtEntryIndex;
    }

    // The IssuanceData activity that's happened in a fee period.
    mapping(address => IssuanceData[FEE_PERIOD_LENGTH]) public accountIssuanceLedger;

    constructor(address _owner, IFeePool _feePool) public Owned(_owner) LimitedSetup(6 weeks) {
        feePool = address(_feePool);
    }

    /* ========== SETTERS ========== */

    /**
     * @notice set the FeePool contract as it is the only authority to be able to call
     * appendAccountIssuanceRecord with the onlyFeePool modifer
     * @dev Must be set by owner when FeePool logic is upgraded
     */
    function setFeePool(IFeePool _feePool) external onlyOwner {
        feePool = address(_feePool);
    }

    /* ========== VIEWS ========== */

    /**
     * @notice Get an accounts issuanceData for
     * @param account users account
     * @param index Index in the array to retrieve. Upto FEE_PERIOD_LENGTH
     */
    function getAccountsDebtEntry(address account, uint index)
        public
        view
        returns (uint debtPercentage, uint debtEntryIndex)
    {
        require(index < FEE_PERIOD_LENGTH, "index exceeds the FEE_PERIOD_LENGTH");

        debtPercentage = accountIssuanceLedger[account][index].debtPercentage;
        debtEntryIndex = accountIssuanceLedger[account][index].debtEntryIndex;
    }

    /**
     * @notice Find the oldest debtEntryIndex for the corresponding closingDebtIndex
     * @param account users account
     * @param closingDebtIndex the last periods debt index on close
     */
    function applicableIssuanceData(address account, uint closingDebtIndex) external view returns (uint, uint) {
        IssuanceData[FEE_PERIOD_LENGTH] memory issuanceData = accountIssuanceLedger[account];

        // We want to use the user's debtEntryIndex at when the period closed
        // Find the oldest debtEntryIndex for the corresponding closingDebtIndex
        for (uint i = 0; i < FEE_PERIOD_LENGTH; i++) {
            if (closingDebtIndex >= issuanceData[i].debtEntryIndex) {
                return (issuanceData[i].debtPercentage, issuanceData[i].debtEntryIndex);
            }
        }
    }

    /* ========== MUTATIVE FUNCTIONS ========== */

    /**
     * @notice Logs an accounts issuance data in the current fee period which is then stored historically
     * @param account Message.Senders account address
     * @param debtRatio Debt of this account as a percentage of the global debt.
     * @param debtEntryIndex The index in the global debt ledger. synthetix.synthetixState().issuanceData(account)
     * @param currentPeriodStartDebtIndex The startingDebtIndex of the current fee period
     * @dev onlyFeePool to call me on synthetix.issue() & synthetix.burn() calls to store the locked SNX
     * per fee period so we know to allocate the correct proportions of fees and rewards per period
      accountIssuanceLedger[account][0] has the latest locked amount for the current period. This can be update as many time
      accountIssuanceLedger[account][1-2] has the last locked amount for a previous period they minted or burned
     */
    function appendAccountIssuanceRecord(
        address account,
        uint debtRatio,
        uint debtEntryIndex,
        uint currentPeriodStartDebtIndex
    ) external onlyFeePool {
        // Is the current debtEntryIndex within this fee period
        if (accountIssuanceLedger[account][0].debtEntryIndex < currentPeriodStartDebtIndex) {
            // If its older then shift the previous IssuanceData entries periods down to make room for the new one.
            issuanceDataIndexOrder(account);
        }

        // Always store the latest IssuanceData entry at [0]
        accountIssuanceLedger[account][0].debtPercentage = debtRatio;
        accountIssuanceLedger[account][0].debtEntryIndex = debtEntryIndex;
    }

    /**
     * @notice Pushes down the entire array of debt ratios per fee period
     */
    function issuanceDataIndexOrder(address account) private {
        for (uint i = FEE_PERIOD_LENGTH - 2; i < FEE_PERIOD_LENGTH; i--) {
            uint next = i + 1;
            accountIssuanceLedger[account][next].debtPercentage = accountIssuanceLedger[account][i].debtPercentage;
            accountIssuanceLedger[account][next].debtEntryIndex = accountIssuanceLedger[account][i].debtEntryIndex;
        }
    }

    /**
     * @notice Import issuer data from synthetixState.issuerData on FeePeriodClose() block #
     * @dev Only callable by the contract owner, and only for 6 weeks after deployment.
     * @param accounts Array of issuing addresses
     * @param ratios Array of debt ratios
     * @param periodToInsert The Fee Period to insert the historical records into
     * @param feePeriodCloseIndex An accounts debtEntryIndex is valid when within the fee peroid,
     * since the input ratio will be an average of the pervious periods it just needs to be
     * > recentFeePeriods[periodToInsert].startingDebtIndex
     * < recentFeePeriods[periodToInsert - 1].startingDebtIndex
     */
    function importIssuerData(
        address[] calldata accounts,
        uint[] calldata ratios,
        uint periodToInsert,
        uint feePeriodCloseIndex
    ) external onlyOwner onlyDuringSetup {
        require(accounts.length == ratios.length, "Length mismatch");

        for (uint i = 0; i < accounts.length; i++) {
            accountIssuanceLedger[accounts[i]][periodToInsert].debtPercentage = ratios[i];
            accountIssuanceLedger[accounts[i]][periodToInsert].debtEntryIndex = feePeriodCloseIndex;
            emit IssuanceDebtRatioEntry(accounts[i], ratios[i], feePeriodCloseIndex);
        }
    }

    /* ========== MODIFIERS ========== */

    modifier onlyFeePool {
        require(msg.sender == address(feePool), "Only the FeePool contract can perform this action");
        _;
    }

    /* ========== Events ========== */
    event IssuanceDebtRatioEntry(address indexed account, uint debtRatio, uint feePeriodCloseIndex);
}


// Inheritance


// https://docs.synthetix.io/contracts/source/contracts/state
contract State is Owned {
    // the address of the contract that can modify variables
    // this can only be changed by the owner of this contract
    address public associatedContract;

    constructor(address _associatedContract) internal {
        // This contract is abstract, and thus cannot be instantiated directly
        require(owner != address(0), "Owner must be set");

        associatedContract = _associatedContract;
        emit AssociatedContractUpdated(_associatedContract);
    }

    /* ========== SETTERS ========== */

    // Change the associated contract to a new address
    function setAssociatedContract(address _associatedContract) external onlyOwner {
        associatedContract = _associatedContract;
        emit AssociatedContractUpdated(_associatedContract);
    }

    /* ========== MODIFIERS ========== */

    modifier onlyAssociatedContract {
        require(msg.sender == associatedContract, "Only the associated contract can perform this action");
        _;
    }

    /* ========== EVENTS ========== */

    event AssociatedContractUpdated(address associatedContract);
}


// Inheritance


// https://docs.synthetix.io/contracts/source/contracts/eternalstorage
/**
 * @notice  This contract is based on the code available from this blog
 * https://blog.colony.io/writing-upgradeable-contracts-in-solidity-6743f0eecc88/
 * Implements support for storing a keccak256 key and value pairs. It is the more flexible
 * and extensible option. This ensures data schema changes can be implemented without
 * requiring upgrades to the storage contract.
 */
contract EternalStorage is Owned, State {
    constructor(address _owner, address _associatedContract) public Owned(_owner) State(_associatedContract) {}

    /* ========== DATA TYPES ========== */
    mapping(bytes32 => uint) internal UIntStorage;
    mapping(bytes32 => string) internal StringStorage;
    mapping(bytes32 => address) internal AddressStorage;
    mapping(bytes32 => bytes) internal BytesStorage;
    mapping(bytes32 => bytes32) internal Bytes32Storage;
    mapping(bytes32 => bool) internal BooleanStorage;
    mapping(bytes32 => int) internal IntStorage;

    // UIntStorage;
    function getUIntValue(bytes32 record) external view returns (uint) {
        return UIntStorage[record];
    }

    function setUIntValue(bytes32 record, uint value) external onlyAssociatedContract {
        UIntStorage[record] = value;
    }

    function deleteUIntValue(bytes32 record) external onlyAssociatedContract {
        delete UIntStorage[record];
    }

    // StringStorage
    function getStringValue(bytes32 record) external view returns (string memory) {
        return StringStorage[record];
    }

    function setStringValue(bytes32 record, string calldata value) external onlyAssociatedContract {
        StringStorage[record] = value;
    }

    function deleteStringValue(bytes32 record) external onlyAssociatedContract {
        delete StringStorage[record];
    }

    // AddressStorage
    function getAddressValue(bytes32 record) external view returns (address) {
        return AddressStorage[record];
    }

    function setAddressValue(bytes32 record, address value) external onlyAssociatedContract {
        AddressStorage[record] = value;
    }

    function deleteAddressValue(bytes32 record) external onlyAssociatedContract {
        delete AddressStorage[record];
    }

    // BytesStorage
    function getBytesValue(bytes32 record) external view returns (bytes memory) {
        return BytesStorage[record];
    }

    function setBytesValue(bytes32 record, bytes calldata value) external onlyAssociatedContract {
        BytesStorage[record] = value;
    }

    function deleteBytesValue(bytes32 record) external onlyAssociatedContract {
        delete BytesStorage[record];
    }

    // Bytes32Storage
    function getBytes32Value(bytes32 record) external view returns (bytes32) {
        return Bytes32Storage[record];
    }

    function setBytes32Value(bytes32 record, bytes32 value) external onlyAssociatedContract {
        Bytes32Storage[record] = value;
    }

    function deleteBytes32Value(bytes32 record) external onlyAssociatedContract {
        delete Bytes32Storage[record];
    }

    // BooleanStorage
    function getBooleanValue(bytes32 record) external view returns (bool) {
        return BooleanStorage[record];
    }

    function setBooleanValue(bytes32 record, bool value) external onlyAssociatedContract {
        BooleanStorage[record] = value;
    }

    function deleteBooleanValue(bytes32 record) external onlyAssociatedContract {
        delete BooleanStorage[record];
    }

    // IntStorage
    function getIntValue(bytes32 record) external view returns (int) {
        return IntStorage[record];
    }

    function setIntValue(bytes32 record, int value) external onlyAssociatedContract {
        IntStorage[record] = value;
    }

    function deleteIntValue(bytes32 record) external onlyAssociatedContract {
        delete IntStorage[record];
    }
}


// Inheritance


// https://docs.synthetix.io/contracts/source/contracts/feepooleternalstorage
contract FeePoolEternalStorage is EternalStorage, LimitedSetup {
    bytes32 internal constant LAST_FEE_WITHDRAWAL = "last_fee_withdrawal";

    constructor(address _owner, address _feePool) public EternalStorage(_owner, _feePool) LimitedSetup(6 weeks) {}

    function importFeeWithdrawalData(address[] calldata accounts, uint[] calldata feePeriodIDs)
        external
        onlyOwner
        onlyDuringSetup
    {
        require(accounts.length == feePeriodIDs.length, "Length mismatch");

        for (uint8 i = 0; i < accounts.length; i++) {
            this.setUIntValue(keccak256(abi.encodePacked(LAST_FEE_WITHDRAWAL, accounts[i])), feePeriodIDs[i]);
        }
    }
}


// https://docs.synthetix.io/contracts/source/interfaces/iexchanger
interface IExchanger {
    // Views
    function calculateAmountAfterSettlement(
        address from,
        bytes32 currencyKey,
        uint amount,
        uint refunded
    ) external view returns (uint amountAfterSettlement);

    function isSynthRateInvalid(bytes32 currencyKey) external view returns (bool);

    function maxSecsLeftInWaitingPeriod(address account, bytes32 currencyKey) external view returns (uint);

    function settlementOwing(address account, bytes32 currencyKey)
        external
        view
        returns (
            uint reclaimAmount,
            uint rebateAmount,
            uint numEntries
        );

    function hasWaitingPeriodOrSettlementOwing(address account, bytes32 currencyKey) external view returns (bool);

    function feeRateForExchange(bytes32 sourceCurrencyKey, bytes32 destinationCurrencyKey)
        external
        view
        returns (uint exchangeFeeRate);

    function getAmountsForExchange(
        uint sourceAmount,
        bytes32 sourceCurrencyKey,
        bytes32 destinationCurrencyKey
    )
        external
        view
        returns (
            uint amountReceived,
            uint fee,
            uint exchangeFeeRate
        );

    function priceDeviationThresholdFactor() external view returns (uint);

    function waitingPeriodSecs() external view returns (uint);

    // Mutative functions
    function exchange(
        address from,
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey,
        address destinationAddress
    ) external returns (uint amountReceived);

    function exchangeOnBehalf(
        address exchangeForAddress,
        address from,
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey
    ) external returns (uint amountReceived);

    function exchangeWithTracking(
        address from,
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey,
        address destinationAddress,
        address originator,
        bytes32 trackingCode
    ) external returns (uint amountReceived);

    function exchangeOnBehalfWithTracking(
        address exchangeForAddress,
        address from,
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey,
        address originator,
        bytes32 trackingCode
    ) external returns (uint amountReceived);

    function exchangeWithVirtual(
        address from,
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey,
        address destinationAddress,
        bytes32 trackingCode
    ) external returns (uint amountReceived, IVirtualSynth vSynth);

    function settle(address from, bytes32 currencyKey)
        external
        returns (
            uint reclaimed,
            uint refunded,
            uint numEntries
        );

    function setLastExchangeRateForSynth(bytes32 currencyKey, uint rate) external;

    function suspendSynthWithInvalidRate(bytes32 currencyKey) external;
}


// https://docs.synthetix.io/contracts/source/interfaces/isynthetixstate
interface ISynthetixState {
    // Views
    function debtLedger(uint index) external view returns (uint);

    function issuanceData(address account) external view returns (uint initialDebtOwnership, uint debtEntryIndex);

    function debtLedgerLength() external view returns (uint);

    function hasIssued(address account) external view returns (bool);

    function lastDebtLedgerEntry() external view returns (uint);

    // Mutative functions
    function incrementTotalIssuerCount() external;

    function decrementTotalIssuerCount() external;

    function setCurrentIssuanceData(address account, uint initialDebtOwnership) external;

    function appendDebtLedgerValue(uint value) external;

    function clearIssuanceData(address account) external;
}


pragma experimental ABIEncoderV2;


library VestingEntries {
    struct VestingEntry {
        uint64 endTime;
        uint64 duration;
        uint64 lastVested;
        uint256 escrowAmount;
        uint256 remainingAmount;
    }
    struct VestingEntryWithID {
        uint64 endTime;
        uint64 duration;
        uint64 lastVested;
        uint256 escrowAmount;
        uint256 remainingAmount;
        uint256 entryID;
    }
}


interface IRewardEscrowV2 {
    // Views
    function balanceOf(address account) external view returns (uint);

    function numVestingEntries(address account) external view returns (uint);

    function totalEscrowedAccountBalance(address account) external view returns (uint);

    function totalVestedAccountBalance(address account) external view returns (uint);

    function getVestingQuantity(address account, uint256[] calldata entryIDs) external view returns (uint);

    function getVestingSchedules(
        address account,
        uint256 index,
        uint256 pageSize
    ) external view returns (VestingEntries.VestingEntryWithID[] memory);

    function getAccountVestingEntryIDs(
        address account,
        uint256 index,
        uint256 pageSize
    ) external view returns (uint256[] memory);

    function getVestingEntryClaimable(address account, uint256 entryID) external view returns (uint);

    function timeSinceLastVested(address account, uint256 entryID) external view returns (uint);

    function ratePerSecond(address account, uint256 entryID) external view returns (uint);

    // Mutative functions
    function vest(uint256[] calldata entryIDs) external;

    function createEscrowEntry(
        address beneficiary,
        uint256 deposit,
        uint256 duration
    ) external;

    function appendVestingEntry(
        address account,
        uint256 quantity,
        uint256 duration
    ) external;

    function migrateVestingSchedule(address _addressToMigrate) external;

    function migrateAccountEscrowBalances(
        address[] calldata accounts,
        uint256[] calldata escrowBalances,
        uint256[] calldata vestedBalances
    ) external;

    // Account Merging
    function startMergingWindow() external;

    function mergeAccount(address accountToMerge, uint256[] calldata entryIDs) external;

    function nominateAccountToMerge(address account) external;

    // L2 Migration
    function importVestingEntries(
        address account,
        uint256 escrowedAmount,
        VestingEntries.VestingEntry[] calldata vestingEntries
    ) external;

    // Return amount of SNX transfered to SynthetixBridgeToOptimism deposit contract
    function burnForMigration(address account, uint256[] calldata entryIDs)
        external
        returns (uint256 escrowedAccountBalance, VestingEntries.VestingEntry[] memory vestingEntries);
}


// https://docs.synthetix.io/contracts/source/interfaces/idelegateapprovals
interface IDelegateApprovals {
    // Views
    function canBurnFor(address authoriser, address delegate) external view returns (bool);

    function canIssueFor(address authoriser, address delegate) external view returns (bool);

    function canClaimFor(address authoriser, address delegate) external view returns (bool);

    function canExchangeFor(address authoriser, address delegate) external view returns (bool);

    // Mutative
    function approveAllDelegatePowers(address delegate) external;

    function removeAllDelegatePowers(address delegate) external;

    function approveBurnOnBehalf(address delegate) external;

    function removeBurnOnBehalf(address delegate) external;

    function approveIssueOnBehalf(address delegate) external;

    function removeIssueOnBehalf(address delegate) external;

    function approveClaimOnBehalf(address delegate) external;

    function removeClaimOnBehalf(address delegate) external;

    function approveExchangeOnBehalf(address delegate) external;

    function removeExchangeOnBehalf(address delegate) external;
}


// https://docs.synthetix.io/contracts/source/interfaces/irewardsdistribution
interface IRewardsDistribution {
    // Structs
    struct DistributionData {
        address destination;
        uint amount;
    }

    // Views
    function authority() external view returns (address);

    function distributions(uint index) external view returns (address destination, uint amount); // DistributionData

    function distributionsLength() external view returns (uint);

    // Mutative Functions
    function distributeRewards(uint amount) external returns (bool);
}


// https://docs.synthetix.io/contracts/source/interfaces/iethercollateralsusd
interface IEtherCollateralsUSD {
    // Views
    function totalIssuedSynths() external view returns (uint256);

    function totalLoansCreated() external view returns (uint256);

    function totalOpenLoanCount() external view returns (uint256);

    // Mutative functions
    function openLoan(uint256 _loanAmount) external payable returns (uint256 loanID);

    function closeLoan(uint256 loanID) external;

    function liquidateUnclosedLoan(address _loanCreatorsAddress, uint256 _loanID) external;

    function depositCollateral(address account, uint256 loanID) external payable;

    function withdrawCollateral(uint256 loanID, uint256 withdrawAmount) external;

    function repayLoan(
        address _loanCreatorsAddress,
        uint256 _loanID,
        uint256 _repayAmount
    ) external;
}


interface ICollateralManager {
    // Manager information
    function hasCollateral(address collateral) external view returns (bool);

    function isSynthManaged(bytes32 currencyKey) external view returns (bool);

    // State information
    function long(bytes32 synth) external view returns (uint amount);

    function short(bytes32 synth) external view returns (uint amount);

    function totalLong() external view returns (uint susdValue, bool anyRateIsInvalid);

    function totalShort() external view returns (uint susdValue, bool anyRateIsInvalid);

    function getBorrowRate() external view returns (uint borrowRate, bool anyRateIsInvalid);

    function getShortRate(bytes32 synth) external view returns (uint shortRate, bool rateIsInvalid);

    function getRatesAndTime(uint index)
        external
        view
        returns (
            uint entryRate,
            uint lastRate,
            uint lastUpdated,
            uint newIndex
        );

    function getShortRatesAndTime(bytes32 currency, uint index)
        external
        view
        returns (
            uint entryRate,
            uint lastRate,
            uint lastUpdated,
            uint newIndex
        );

    function exceedsDebtLimit(uint amount, bytes32 currency) external view returns (bool canIssue, bool anyRateIsInvalid);

    function areSynthsAndCurrenciesSet(bytes32[] calldata requiredSynthNamesInResolver, bytes32[] calldata synthKeys)
        external
        view
        returns (bool);

    function areShortableSynthsSet(bytes32[] calldata requiredSynthNamesInResolver, bytes32[] calldata synthKeys)
        external
        view
        returns (bool);

    // Loans
    function getNewLoanId() external returns (uint id);

    // Manager mutative
    function addCollaterals(address[] calldata collaterals) external;

    function removeCollaterals(address[] calldata collaterals) external;

    function addSynths(bytes32[] calldata synthNamesInResolver, bytes32[] calldata synthKeys) external;

    function removeSynths(bytes32[] calldata synths, bytes32[] calldata synthKeys) external;

    function addShortableSynths(bytes32[2][] calldata requiredSynthAndInverseNamesInResolver, bytes32[] calldata synthKeys)
        external;

    function removeShortableSynths(bytes32[] calldata synths) external;

    // State mutative
    function updateBorrowRates(uint rate) external;

    function updateShortRates(bytes32 currency, uint rate) external;

    function incrementLongs(bytes32 synth, uint amount) external;

    function decrementLongs(bytes32 synth, uint amount) external;

    function incrementShorts(bytes32 synth, uint amount) external;

    function decrementShorts(bytes32 synth, uint amount) external;
}


// Inheritance


// Libraries


// Internal references


// https://docs.synthetix.io/contracts/source/contracts/feepool
contract FeePool is Owned, Proxyable, LimitedSetup, MixinSystemSettings, IFeePool {
    using SafeMath for uint;
    using SafeDecimalMath for uint;

    // Where fees are pooled in sUSD.
    address public constant FEE_ADDRESS = 0xfeEFEEfeefEeFeefEEFEEfEeFeefEEFeeFEEFEeF;

    // sUSD currencyKey. Fees stored and paid in sUSD
    bytes32 private sUSD = "sUSD";

    // This struct represents the issuance activity that's happened in a fee period.
    struct FeePeriod {
        uint64 feePeriodId;
        uint64 startingDebtIndex;
        uint64 startTime;
        uint feesToDistribute;
        uint feesClaimed;
        uint rewardsToDistribute;
        uint rewardsClaimed;
    }

    // A staker(mintr) can claim from the previous fee period (7 days) only.
    // Fee Periods stored and managed from [0], such that [0] is always
    // the current active fee period which is not claimable until the
    // public function closeCurrentFeePeriod() is called closing the
    // current weeks collected fees. [1] is last weeks feeperiod
    uint8 public constant FEE_PERIOD_LENGTH = 2;

    FeePeriod[FEE_PERIOD_LENGTH] private _recentFeePeriods;
    uint256 private _currentFeePeriod;

    /* ========== ADDRESS RESOLVER CONFIGURATION ========== */

    bytes32 private constant CONTRACT_SYSTEMSTATUS = "SystemStatus";
    bytes32 private constant CONTRACT_SYNTHETIX = "Synthetix";
    bytes32 private constant CONTRACT_FEEPOOLSTATE = "FeePoolState";
    bytes32 private constant CONTRACT_FEEPOOLETERNALSTORAGE = "FeePoolEternalStorage";
    bytes32 private constant CONTRACT_EXCHANGER = "Exchanger";
    bytes32 private constant CONTRACT_ISSUER = "Issuer";
    bytes32 private constant CONTRACT_SYNTHETIXSTATE = "SynthetixState";
    bytes32 private constant CONTRACT_REWARDESCROW_V2 = "RewardEscrowV2";
    bytes32 private constant CONTRACT_DELEGATEAPPROVALS = "DelegateApprovals";
    bytes32 private constant CONTRACT_ETH_COLLATERAL_SUSD = "EtherCollateralsUSD";
    bytes32 private constant CONTRACT_COLLATERALMANAGER = "CollateralManager";
    bytes32 private constant CONTRACT_REWARDSDISTRIBUTION = "RewardsDistribution";

    /* ========== ETERNAL STORAGE CONSTANTS ========== */

    bytes32 private constant LAST_FEE_WITHDRAWAL = "last_fee_withdrawal";

    constructor(
        address payable _proxy,
        address _owner,
        address _resolver
    ) public Owned(_owner) Proxyable(_proxy) LimitedSetup(3 weeks) MixinSystemSettings(_resolver) {
        // Set our initial fee period
        _recentFeePeriodsStorage(0).feePeriodId = 1;
        _recentFeePeriodsStorage(0).startTime = uint64(now);
    }

    /* ========== VIEWS ========== */
    function resolverAddressesRequired() public view returns (bytes32[] memory addresses) {
        bytes32[] memory existingAddresses = MixinSystemSettings.resolverAddressesRequired();
        bytes32[] memory newAddresses = new bytes32[](12);
        newAddresses[0] = CONTRACT_SYSTEMSTATUS;
        newAddresses[1] = CONTRACT_SYNTHETIX;
        newAddresses[2] = CONTRACT_FEEPOOLSTATE;
        newAddresses[3] = CONTRACT_FEEPOOLETERNALSTORAGE;
        newAddresses[4] = CONTRACT_EXCHANGER;
        newAddresses[5] = CONTRACT_ISSUER;
        newAddresses[6] = CONTRACT_SYNTHETIXSTATE;
        newAddresses[7] = CONTRACT_REWARDESCROW_V2;
        newAddresses[8] = CONTRACT_DELEGATEAPPROVALS;
        newAddresses[9] = CONTRACT_ETH_COLLATERAL_SUSD;
        newAddresses[10] = CONTRACT_REWARDSDISTRIBUTION;
        newAddresses[11] = CONTRACT_COLLATERALMANAGER;
        addresses = combineArrays(existingAddresses, newAddresses);
    }

    function systemStatus() internal view returns (ISystemStatus) {
        return ISystemStatus(requireAndGetAddress(CONTRACT_SYSTEMSTATUS));
    }

    function synthetix() internal view returns (ISynthetix) {
        return ISynthetix(requireAndGetAddress(CONTRACT_SYNTHETIX));
    }

    function feePoolState() internal view returns (FeePoolState) {
        return FeePoolState(requireAndGetAddress(CONTRACT_FEEPOOLSTATE));
    }

    function feePoolEternalStorage() internal view returns (FeePoolEternalStorage) {
        return FeePoolEternalStorage(requireAndGetAddress(CONTRACT_FEEPOOLETERNALSTORAGE));
    }

    function exchanger() internal view returns (IExchanger) {
        return IExchanger(requireAndGetAddress(CONTRACT_EXCHANGER));
    }

    function etherCollateralsUSD() internal view returns (IEtherCollateralsUSD) {
        return IEtherCollateralsUSD(requireAndGetAddress(CONTRACT_ETH_COLLATERAL_SUSD));
    }

    function collateralManager() internal view returns (ICollateralManager) {
        return ICollateralManager(requireAndGetAddress(CONTRACT_COLLATERALMANAGER));
    }

    function issuer() internal view returns (IIssuer) {
        return IIssuer(requireAndGetAddress(CONTRACT_ISSUER));
    }

    function synthetixState() internal view returns (ISynthetixState) {
        return ISynthetixState(requireAndGetAddress(CONTRACT_SYNTHETIXSTATE));
    }

    function rewardEscrowV2() internal view returns (IRewardEscrowV2) {
        return IRewardEscrowV2(requireAndGetAddress(CONTRACT_REWARDESCROW_V2));
    }

    function delegateApprovals() internal view returns (IDelegateApprovals) {
        return IDelegateApprovals(requireAndGetAddress(CONTRACT_DELEGATEAPPROVALS));
    }

    function rewardsDistribution() internal view returns (IRewardsDistribution) {
        return IRewardsDistribution(requireAndGetAddress(CONTRACT_REWARDSDISTRIBUTION));
    }

    function issuanceRatio() external view returns (uint) {
        return getIssuanceRatio();
    }

    function feePeriodDuration() external view returns (uint) {
        return getFeePeriodDuration();
    }

    function targetThreshold() external view returns (uint) {
        return getTargetThreshold();
    }

    function recentFeePeriods(uint index)
        external
        view
        returns (
            uint64 feePeriodId,
            uint64 startingDebtIndex,
            uint64 startTime,
            uint feesToDistribute,
            uint feesClaimed,
            uint rewardsToDistribute,
            uint rewardsClaimed
        )
    {
        FeePeriod memory feePeriod = _recentFeePeriodsStorage(index);
        return (
            feePeriod.feePeriodId,
            feePeriod.startingDebtIndex,
            feePeriod.startTime,
            feePeriod.feesToDistribute,
            feePeriod.feesClaimed,
            feePeriod.rewardsToDistribute,
            feePeriod.rewardsClaimed
        );
    }

    function _recentFeePeriodsStorage(uint index) internal view returns (FeePeriod storage) {
        return _recentFeePeriods[(_currentFeePeriod + index) % FEE_PERIOD_LENGTH];
    }

    /* ========== MUTATIVE FUNCTIONS ========== */

    /**
     * @notice Logs an accounts issuance data per fee period
     * @param account Message.Senders account address
     * @param debtRatio Debt percentage this account has locked after minting or burning their synth
     * @param debtEntryIndex The index in the global debt ledger. synthetixState.issuanceData(account)
     * @dev onlyIssuer to call me on synthetix.issue() & synthetix.burn() calls to store the locked SNX
     * per fee period so we know to allocate the correct proportions of fees and rewards per period
     */
    function appendAccountIssuanceRecord(
        address account,
        uint debtRatio,
        uint debtEntryIndex
    ) external onlyIssuer {
        feePoolState().appendAccountIssuanceRecord(
            account,
            debtRatio,
            debtEntryIndex,
            _recentFeePeriodsStorage(0).startingDebtIndex
        );

        emitIssuanceDebtRatioEntry(account, debtRatio, debtEntryIndex, _recentFeePeriodsStorage(0).startingDebtIndex);
    }

    /**
     * @notice The Exchanger contract informs us when fees are paid.
     * @param amount susd amount in fees being paid.
     */
    function recordFeePaid(uint amount) external onlyInternalContracts {
        // Keep track off fees in sUSD in the open fee pool period.
        _recentFeePeriodsStorage(0).feesToDistribute = _recentFeePeriodsStorage(0).feesToDistribute.add(amount);
    }

    /**
     * @notice The RewardsDistribution contract informs us how many SNX rewards are sent to RewardEscrow to be claimed.
     */
    function setRewardsToDistribute(uint amount) external {
        address rewardsAuthority = address(rewardsDistribution());
        require(messageSender == rewardsAuthority || msg.sender == rewardsAuthority, "Caller is not rewardsAuthority");
        // Add the amount of SNX rewards to distribute on top of any rolling unclaimed amount
        _recentFeePeriodsStorage(0).rewardsToDistribute = _recentFeePeriodsStorage(0).rewardsToDistribute.add(amount);
    }

    /**
     * @notice Close the current fee period and start a new one.
     */
    function closeCurrentFeePeriod() external issuanceActive {
        require(getFeePeriodDuration() > 0, "Fee Period Duration not set");
        require(_recentFeePeriodsStorage(0).startTime <= (now - getFeePeriodDuration()), "Too early to close fee period");

        // Note:  when FEE_PERIOD_LENGTH = 2, periodClosing is the current period & periodToRollover is the last open claimable period
        FeePeriod storage periodClosing = _recentFeePeriodsStorage(FEE_PERIOD_LENGTH - 2);
        FeePeriod storage periodToRollover = _recentFeePeriodsStorage(FEE_PERIOD_LENGTH - 1);

        // Any unclaimed fees from the last period in the array roll back one period.
        // Because of the subtraction here, they're effectively proportionally redistributed to those who
        // have already claimed from the old period, available in the new period.
        // The subtraction is important so we don't create a ticking time bomb of an ever growing
        // number of fees that can never decrease and will eventually overflow at the end of the fee pool.
        _recentFeePeriodsStorage(FEE_PERIOD_LENGTH - 2).feesToDistribute = periodToRollover
            .feesToDistribute
            .sub(periodToRollover.feesClaimed)
            .add(periodClosing.feesToDistribute);
        _recentFeePeriodsStorage(FEE_PERIOD_LENGTH - 2).rewardsToDistribute = periodToRollover
            .rewardsToDistribute
            .sub(periodToRollover.rewardsClaimed)
            .add(periodClosing.rewardsToDistribute);

        // Shift the previous fee periods across to make room for the new one.
        _currentFeePeriod = _currentFeePeriod.add(FEE_PERIOD_LENGTH).sub(1).mod(FEE_PERIOD_LENGTH);

        // Clear the first element of the array to make sure we don't have any stale values.
        delete _recentFeePeriods[_currentFeePeriod];

        // Open up the new fee period.
        // Increment periodId from the recent closed period feePeriodId
        _recentFeePeriodsStorage(0).feePeriodId = uint64(uint256(_recentFeePeriodsStorage(1).feePeriodId).add(1));
        _recentFeePeriodsStorage(0).startingDebtIndex = uint64(synthetixState().debtLedgerLength());
        _recentFeePeriodsStorage(0).startTime = uint64(now);

        emitFeePeriodClosed(_recentFeePeriodsStorage(1).feePeriodId);
    }

    /**
     * @notice Claim fees for last period when available or not already withdrawn.
     */
    function claimFees() external issuanceActive optionalProxy returns (bool) {
        return _claimFees(messageSender);
    }

    /**
     * @notice Delegated claimFees(). Call from the deletegated address
     * and the fees will be sent to the claimingForAddress.
     * approveClaimOnBehalf() must be called first to approve the deletage address
     * @param claimingForAddress The account you are claiming fees for
     */
    function claimOnBehalf(address claimingForAddress) external issuanceActive optionalProxy returns (bool) {
        require(delegateApprovals().canClaimFor(claimingForAddress, messageSender), "Not approved to claim on behalf");

        return _claimFees(claimingForAddress);
    }

    function _claimFees(address claimingAddress) internal returns (bool) {
        uint rewardsPaid = 0;
        uint feesPaid = 0;
        uint availableFees;
        uint availableRewards;

        // Address won't be able to claim fees if it is too far below the target c-ratio.
        // It will need to burn synths then try claiming again.
        (bool feesClaimable, bool anyRateIsInvalid) = _isFeesClaimableAndAnyRatesInvalid(claimingAddress);

        require(feesClaimable, "C-Ratio below penalty threshold");

        require(!anyRateIsInvalid, "A synth or SNX rate is invalid");

        // Get the claimingAddress available fees and rewards
        (availableFees, availableRewards) = feesAvailable(claimingAddress);

        require(
            availableFees > 0 || availableRewards > 0,
            "No fees or rewards available for period, or fees already claimed"
        );

        // Record the address has claimed for this period
        _setLastFeeWithdrawal(claimingAddress, _recentFeePeriodsStorage(1).feePeriodId);

        if (availableFees > 0) {
            // Record the fee payment in our recentFeePeriods
            feesPaid = _recordFeePayment(availableFees);

            // Send them their fees
            _payFees(claimingAddress, feesPaid);
        }

        if (availableRewards > 0) {
            // Record the reward payment in our recentFeePeriods
            rewardsPaid = _recordRewardPayment(availableRewards);

            // Send them their rewards
            _payRewards(claimingAddress, rewardsPaid);
        }

        emitFeesClaimed(claimingAddress, feesPaid, rewardsPaid);

        return true;
    }

    /**
     * @notice Admin function to import the FeePeriod data from the previous contract
     */
    function importFeePeriod(
        uint feePeriodIndex,
        uint feePeriodId,
        uint startingDebtIndex,
        uint startTime,
        uint feesToDistribute,
        uint feesClaimed,
        uint rewardsToDistribute,
        uint rewardsClaimed
    ) public optionalProxy_onlyOwner onlyDuringSetup {
        require(startingDebtIndex <= synthetixState().debtLedgerLength(), "Cannot import bad data");

        _recentFeePeriods[_currentFeePeriod.add(feePeriodIndex).mod(FEE_PERIOD_LENGTH)] = FeePeriod({
            feePeriodId: uint64(feePeriodId),
            startingDebtIndex: uint64(startingDebtIndex),
            startTime: uint64(startTime),
            feesToDistribute: feesToDistribute,
            feesClaimed: feesClaimed,
            rewardsToDistribute: rewardsToDistribute,
            rewardsClaimed: rewardsClaimed
        });
    }

    /**
     * @notice Record the fee payment in our recentFeePeriods.
     * @param sUSDAmount The amount of fees priced in sUSD.
     */
    function _recordFeePayment(uint sUSDAmount) internal returns (uint) {
        // Don't assign to the parameter
        uint remainingToAllocate = sUSDAmount;

        uint feesPaid;
        // Start at the oldest period and record the amount, moving to newer periods
        // until we've exhausted the amount.
        // The condition checks for overflow because we're going to 0 with an unsigned int.
        for (uint i = FEE_PERIOD_LENGTH - 1; i < FEE_PERIOD_LENGTH; i--) {
            uint feesAlreadyClaimed = _recentFeePeriodsStorage(i).feesClaimed;
            uint delta = _recentFeePeriodsStorage(i).feesToDistribute.sub(feesAlreadyClaimed);

            if (delta > 0) {
                // Take the smaller of the amount left to claim in the period and the amount we need to allocate
                uint amountInPeriod = delta < remainingToAllocate ? delta : remainingToAllocate;

                _recentFeePeriodsStorage(i).feesClaimed = feesAlreadyClaimed.add(amountInPeriod);
                remainingToAllocate = remainingToAllocate.sub(amountInPeriod);
                feesPaid = feesPaid.add(amountInPeriod);

                // No need to continue iterating if we've recorded the whole amount;
                if (remainingToAllocate == 0) return feesPaid;

                // We've exhausted feePeriods to distribute and no fees remain in last period
                // User last to claim would in this scenario have their remainder slashed
                if (i == 0 && remainingToAllocate > 0) {
                    remainingToAllocate = 0;
                }
            }
        }

        return feesPaid;
    }

    /**
     * @notice Record the reward payment in our recentFeePeriods.
     * @param snxAmount The amount of SNX tokens.
     */
    function _recordRewardPayment(uint snxAmount) internal returns (uint) {
        // Don't assign to the parameter
        uint remainingToAllocate = snxAmount;

        uint rewardPaid;

        // Start at the oldest period and record the amount, moving to newer periods
        // until we've exhausted the amount.
        // The condition checks for overflow because we're going to 0 with an unsigned int.
        for (uint i = FEE_PERIOD_LENGTH - 1; i < FEE_PERIOD_LENGTH; i--) {
            uint toDistribute = _recentFeePeriodsStorage(i).rewardsToDistribute.sub(
                _recentFeePeriodsStorage(i).rewardsClaimed
            );

            if (toDistribute > 0) {
                // Take the smaller of the amount left to claim in the period and the amount we need to allocate
                uint amountInPeriod = toDistribute < remainingToAllocate ? toDistribute : remainingToAllocate;

                _recentFeePeriodsStorage(i).rewardsClaimed = _recentFeePeriodsStorage(i).rewardsClaimed.add(amountInPeriod);
                remainingToAllocate = remainingToAllocate.sub(amountInPeriod);
                rewardPaid = rewardPaid.add(amountInPeriod);

                // No need to continue iterating if we've recorded the whole amount;
                if (remainingToAllocate == 0) return rewardPaid;

                // We've exhausted feePeriods to distribute and no rewards remain in last period
                // User last to claim would in this scenario have their remainder slashed
                // due to rounding up of PreciseDecimal
                if (i == 0 && remainingToAllocate > 0) {
                    remainingToAllocate = 0;
                }
            }
        }
        return rewardPaid;
    }

    /**
     * @notice Send the fees to claiming address.
     * @param account The address to send the fees to.
     * @param sUSDAmount The amount of fees priced in sUSD.
     */
    function _payFees(address account, uint sUSDAmount) internal notFeeAddress(account) {
        // Grab the sUSD Synth
        ISynth sUSDSynth = issuer().synths(sUSD);

        // NOTE: we do not control the FEE_ADDRESS so it is not possible to do an
        // ERC20.approve() transaction to allow this feePool to call ERC20.transferFrom
        // to the accounts address

        // Burn the source amount
        sUSDSynth.burn(FEE_ADDRESS, sUSDAmount);

        // Mint their new synths
        sUSDSynth.issue(account, sUSDAmount);
    }

    /**
     * @notice Send the rewards to claiming address - will be locked in rewardEscrow.
     * @param account The address to send the fees to.
     * @param snxAmount The amount of SNX.
     */
    function _payRewards(address account, uint snxAmount) internal notFeeAddress(account) {
        /* Escrow the tokens for 1 year. */
        uint escrowDuration = 52 weeks;

        // Record vesting entry for claiming address and amount
        // SNX already minted to rewardEscrow balance
        rewardEscrowV2().appendVestingEntry(account, snxAmount, escrowDuration);
    }

    /**
     * @notice The total fees available in the system to be withdrawnn in sUSD
     */
    function totalFeesAvailable() external view returns (uint) {
        uint totalFees = 0;

        // Fees in fee period [0] are not yet available for withdrawal
        for (uint i = 1; i < FEE_PERIOD_LENGTH; i++) {
            totalFees = totalFees.add(_recentFeePeriodsStorage(i).feesToDistribute);
            totalFees = totalFees.sub(_recentFeePeriodsStorage(i).feesClaimed);
        }

        return totalFees;
    }

    /**
     * @notice The total SNX rewards available in the system to be withdrawn
     */
    function totalRewardsAvailable() external view returns (uint) {
        uint totalRewards = 0;

        // Rewards in fee period [0] are not yet available for withdrawal
        for (uint i = 1; i < FEE_PERIOD_LENGTH; i++) {
            totalRewards = totalRewards.add(_recentFeePeriodsStorage(i).rewardsToDistribute);
            totalRewards = totalRewards.sub(_recentFeePeriodsStorage(i).rewardsClaimed);
        }

        return totalRewards;
    }

    /**
     * @notice The fees available to be withdrawn by a specific account, priced in sUSD
     * @dev Returns two amounts, one for fees and one for SNX rewards
     */
    function feesAvailable(address account) public view returns (uint, uint) {
        // Add up the fees
        uint[2][FEE_PERIOD_LENGTH] memory userFees = feesByPeriod(account);

        uint totalFees = 0;
        uint totalRewards = 0;

        // Fees & Rewards in fee period [0] are not yet available for withdrawal
        for (uint i = 1; i < FEE_PERIOD_LENGTH; i++) {
            totalFees = totalFees.add(userFees[i][0]);
            totalRewards = totalRewards.add(userFees[i][1]);
        }

        // And convert totalFees to sUSD
        // Return totalRewards as is in SNX amount
        return (totalFees, totalRewards);
    }

    function _isFeesClaimableAndAnyRatesInvalid(address account) internal view returns (bool, bool) {
        // Threshold is calculated from ratio % above the target ratio (issuanceRatio).
        //  0  <  10%:   Claimable
        // 10% > above:  Unable to claim
        (uint ratio, bool anyRateIsInvalid) = issuer().collateralisationRatioAndAnyRatesInvalid(account);
        uint targetRatio = getIssuanceRatio();

        // Claimable if collateral ratio below target ratio
        if (ratio < targetRatio) {
            return (true, anyRateIsInvalid);
        }

        // Calculate the threshold for collateral ratio before fees can't be claimed.
        uint ratio_threshold = targetRatio.multiplyDecimal(SafeDecimalMath.unit().add(getTargetThreshold()));

        // Not claimable if collateral ratio above threshold
        if (ratio > ratio_threshold) {
            return (false, anyRateIsInvalid);
        }

        return (true, anyRateIsInvalid);
    }

    function isFeesClaimable(address account) external view returns (bool feesClaimable) {
        (feesClaimable, ) = _isFeesClaimableAndAnyRatesInvalid(account);
    }

    /**
     * @notice Calculates fees by period for an account, priced in sUSD
     * @param account The address you want to query the fees for
     */
    function feesByPeriod(address account) public view returns (uint[2][FEE_PERIOD_LENGTH] memory results) {
        // What's the user's debt entry index and the debt they owe to the system at current feePeriod
        uint userOwnershipPercentage;
        uint debtEntryIndex;
        FeePoolState _feePoolState = feePoolState();

        (userOwnershipPercentage, debtEntryIndex) = _feePoolState.getAccountsDebtEntry(account, 0);

        // If they don't have any debt ownership and they never minted, they don't have any fees.
        // User ownership can reduce to 0 if user burns all synths,
        // however they could have fees applicable for periods they had minted in before so we check debtEntryIndex.
        if (debtEntryIndex == 0 && userOwnershipPercentage == 0) {
            uint[2][FEE_PERIOD_LENGTH] memory nullResults;
            return nullResults;
        }

        // The [0] fee period is not yet ready to claim, but it is a fee period that they can have
        // fees owing for, so we need to report on it anyway.
        uint feesFromPeriod;
        uint rewardsFromPeriod;
        (feesFromPeriod, rewardsFromPeriod) = _feesAndRewardsFromPeriod(0, userOwnershipPercentage, debtEntryIndex);

        results[0][0] = feesFromPeriod;
        results[0][1] = rewardsFromPeriod;

        // Retrieve user's last fee claim by periodId
        uint lastFeeWithdrawal = getLastFeeWithdrawal(account);

        // Go through our fee periods from the oldest feePeriod[FEE_PERIOD_LENGTH - 1] and figure out what we owe them.
        // Condition checks for periods > 0
        for (uint i = FEE_PERIOD_LENGTH - 1; i > 0; i--) {
            uint next = i - 1;
            uint nextPeriodStartingDebtIndex = _recentFeePeriodsStorage(next).startingDebtIndex;

            // We can skip the period, as no debt minted during period (next period's startingDebtIndex is still 0)
            if (nextPeriodStartingDebtIndex > 0 && lastFeeWithdrawal < _recentFeePeriodsStorage(i).feePeriodId) {
                // We calculate a feePeriod's closingDebtIndex by looking at the next feePeriod's startingDebtIndex
                // we can use the most recent issuanceData[0] for the current feePeriod
                // else find the applicableIssuanceData for the feePeriod based on the StartingDebtIndex of the period
                uint closingDebtIndex = uint256(nextPeriodStartingDebtIndex).sub(1);

                // Gas optimisation - to reuse debtEntryIndex if found new applicable one
                // if applicable is 0,0 (none found) we keep most recent one from issuanceData[0]
                // return if userOwnershipPercentage = 0)
                (userOwnershipPercentage, debtEntryIndex) = _feePoolState.applicableIssuanceData(account, closingDebtIndex);

                (feesFromPeriod, rewardsFromPeriod) = _feesAndRewardsFromPeriod(i, userOwnershipPercentage, debtEntryIndex);

                results[i][0] = feesFromPeriod;
                results[i][1] = rewardsFromPeriod;
            }
        }
    }

    /**
     * @notice ownershipPercentage is a high precision decimals uint based on
     * wallet's debtPercentage. Gives a precise amount of the feesToDistribute
     * for fees in the period. Precision factor is removed before results are
     * returned.
     * @dev The reported fees owing for the current period [0] are just a
     * running balance until the fee period closes
     */
    function _feesAndRewardsFromPeriod(
        uint period,
        uint ownershipPercentage,
        uint debtEntryIndex
    ) internal view returns (uint, uint) {
        // If it's zero, they haven't issued, and they have no fees OR rewards.
        if (ownershipPercentage == 0) return (0, 0);

        uint debtOwnershipForPeriod = ownershipPercentage;

        // If period has closed we want to calculate debtPercentage for the period
        if (period > 0) {
            uint closingDebtIndex = uint256(_recentFeePeriodsStorage(period - 1).startingDebtIndex).sub(1);
            debtOwnershipForPeriod = _effectiveDebtRatioForPeriod(closingDebtIndex, ownershipPercentage, debtEntryIndex);
        }

        // Calculate their percentage of the fees / rewards in this period
        // This is a high precision integer.
        uint feesFromPeriod = _recentFeePeriodsStorage(period).feesToDistribute.multiplyDecimal(debtOwnershipForPeriod);

        uint rewardsFromPeriod = _recentFeePeriodsStorage(period).rewardsToDistribute.multiplyDecimal(
            debtOwnershipForPeriod
        );

        return (feesFromPeriod.preciseDecimalToDecimal(), rewardsFromPeriod.preciseDecimalToDecimal());
    }

    function _effectiveDebtRatioForPeriod(
        uint closingDebtIndex,
        uint ownershipPercentage,
        uint debtEntryIndex
    ) internal view returns (uint) {
        // Figure out their global debt percentage delta at end of fee Period.
        // This is a high precision integer.
        ISynthetixState _synthetixState = synthetixState();
        uint feePeriodDebtOwnership = _synthetixState
            .debtLedger(closingDebtIndex)
            .divideDecimalRoundPrecise(_synthetixState.debtLedger(debtEntryIndex))
            .multiplyDecimalRoundPrecise(ownershipPercentage);

        return feePeriodDebtOwnership;
    }

    function effectiveDebtRatioForPeriod(address account, uint period) external view returns (uint) {
        require(period != 0, "Current period is not closed yet");
        require(period < FEE_PERIOD_LENGTH, "Exceeds the FEE_PERIOD_LENGTH");

        // If the period being checked is uninitialised then return 0. This is only at the start of the system.
        if (_recentFeePeriodsStorage(period - 1).startingDebtIndex == 0) return 0;

        uint closingDebtIndex = uint256(_recentFeePeriodsStorage(period - 1).startingDebtIndex).sub(1);

        uint ownershipPercentage;
        uint debtEntryIndex;
        (ownershipPercentage, debtEntryIndex) = feePoolState().applicableIssuanceData(account, closingDebtIndex);

        // internal function will check closingDebtIndex has corresponding debtLedger entry
        return _effectiveDebtRatioForPeriod(closingDebtIndex, ownershipPercentage, debtEntryIndex);
    }

    /**
     * @notice Get the feePeriodID of the last claim this account made
     * @param _claimingAddress account to check the last fee period ID claim for
     * @return uint of the feePeriodID this account last claimed
     */
    function getLastFeeWithdrawal(address _claimingAddress) public view returns (uint) {
        return feePoolEternalStorage().getUIntValue(keccak256(abi.encodePacked(LAST_FEE_WITHDRAWAL, _claimingAddress)));
    }

    /**
     * @notice Calculate the collateral ratio before user is blocked from claiming.
     */
    function getPenaltyThresholdRatio() public view returns (uint) {
        return getIssuanceRatio().multiplyDecimal(SafeDecimalMath.unit().add(getTargetThreshold()));
    }

    /**
     * @notice Set the feePeriodID of the last claim this account made
     * @param _claimingAddress account to set the last feePeriodID claim for
     * @param _feePeriodID the feePeriodID this account claimed fees for
     */
    function _setLastFeeWithdrawal(address _claimingAddress, uint _feePeriodID) internal {
        feePoolEternalStorage().setUIntValue(
            keccak256(abi.encodePacked(LAST_FEE_WITHDRAWAL, _claimingAddress)),
            _feePeriodID
        );
    }

    /* ========== Modifiers ========== */
    modifier onlyInternalContracts {
        bool isExchanger = msg.sender == address(exchanger());
        bool isSynth = issuer().synthsByAddress(msg.sender) != bytes32(0);
        bool isEtherCollateralsUSD = msg.sender == address(etherCollateralsUSD());
        bool isCollateral = collateralManager().hasCollateral(msg.sender);

        require(isExchanger || isSynth || isEtherCollateralsUSD || isCollateral, "Only Internal Contracts");
        _;
    }

    modifier onlyIssuer {
        require(msg.sender == address(issuer()), "FeePool: Only Issuer Authorised");
        _;
    }

    modifier notFeeAddress(address account) {
        require(account != FEE_ADDRESS, "Fee address not allowed");
        _;
    }

    modifier issuanceActive() {
        systemStatus().requireIssuanceActive();
        _;
    }

    /* ========== Proxy Events ========== */

    event IssuanceDebtRatioEntry(
        address indexed account,
        uint debtRatio,
        uint debtEntryIndex,
        uint feePeriodStartingDebtIndex
    );
    bytes32 private constant ISSUANCEDEBTRATIOENTRY_SIG = keccak256(
        "IssuanceDebtRatioEntry(address,uint256,uint256,uint256)"
    );

    function emitIssuanceDebtRatioEntry(
        address account,
        uint debtRatio,
        uint debtEntryIndex,
        uint feePeriodStartingDebtIndex
    ) internal {
        proxy._emit(
            abi.encode(debtRatio, debtEntryIndex, feePeriodStartingDebtIndex),
            2,
            ISSUANCEDEBTRATIOENTRY_SIG,
            bytes32(uint256(uint160(account))),
            0,
            0
        );
    }

    event FeePeriodClosed(uint feePeriodId);
    bytes32 private constant FEEPERIODCLOSED_SIG = keccak256("FeePeriodClosed(uint256)");

    function emitFeePeriodClosed(uint feePeriodId) internal {
        proxy._emit(abi.encode(feePeriodId), 1, FEEPERIODCLOSED_SIG, 0, 0, 0);
    }

    event FeesClaimed(address account, uint sUSDAmount, uint snxRewards);
    bytes32 private constant FEESCLAIMED_SIG = keccak256("FeesClaimed(address,uint256,uint256)");

    function emitFeesClaimed(
        address account,
        uint sUSDAmount,
        uint snxRewards
    ) internal {
        proxy._emit(abi.encode(account, sUSDAmount, snxRewards), 1, FEESCLAIMED_SIG, 0, 0, 0);
    }
}

Contract ABI

[{"inputs":[{"internalType":"address payable","name":"_proxy","type":"address"},{"internalType":"address","name":"_owner","type":"address"},{"internalType":"address","name":"_resolver","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes32","name":"name","type":"bytes32"},{"indexed":false,"internalType":"address","name":"destination","type":"address"}],"name":"CacheUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"feePeriodId","type":"uint256"}],"name":"FeePeriodClosed","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"uint256","name":"sUSDAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"snxRewards","type":"uint256"}],"name":"FeesClaimed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"uint256","name":"debtRatio","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"debtEntryIndex","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"feePeriodStartingDebtIndex","type":"uint256"}],"name":"IssuanceDebtRatioEntry","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"oldOwner","type":"address"},{"indexed":false,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnerChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnerNominated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"proxyAddress","type":"address"}],"name":"ProxyUpdated","type":"event"},{"constant":true,"inputs":[],"name":"FEE_ADDRESS","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"FEE_PERIOD_LENGTH","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"acceptOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"uint256","name":"debtRatio","type":"uint256"},{"internalType":"uint256","name":"debtEntryIndex","type":"uint256"}],"name":"appendAccountIssuanceRecord","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"claimFees","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"claimingForAddress","type":"address"}],"name":"claimOnBehalf","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"closeCurrentFeePeriod","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"uint256","name":"period","type":"uint256"}],"name":"effectiveDebtRatioForPeriod","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"feePeriodDuration","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"feesAvailable","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"feesByPeriod","outputs":[{"internalType":"uint256[2][2]","name":"results","type":"uint256[2][2]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"_claimingAddress","type":"address"}],"name":"getLastFeeWithdrawal","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getPenaltyThresholdRatio","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"feePeriodIndex","type":"uint256"},{"internalType":"uint256","name":"feePeriodId","type":"uint256"},{"internalType":"uint256","name":"startingDebtIndex","type":"uint256"},{"internalType":"uint256","name":"startTime","type":"uint256"},{"internalType":"uint256","name":"feesToDistribute","type":"uint256"},{"internalType":"uint256","name":"feesClaimed","type":"uint256"},{"internalType":"uint256","name":"rewardsToDistribute","type":"uint256"},{"internalType":"uint256","name":"rewardsClaimed","type":"uint256"}],"name":"importFeePeriod","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"integrationProxy","outputs":[{"internalType":"contract Proxy","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"isFeesClaimable","outputs":[{"internalType":"bool","name":"feesClaimable","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"isResolverCached","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"issuanceRatio","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"messageSender","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"_owner","type":"address"}],"name":"nominateNewOwner","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"nominatedOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"proxy","outputs":[{"internalType":"contract Proxy","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"rebuildCache","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"index","type":"uint256"}],"name":"recentFeePeriods","outputs":[{"internalType":"uint64","name":"feePeriodId","type":"uint64"},{"internalType":"uint64","name":"startingDebtIndex","type":"uint64"},{"internalType":"uint64","name":"startTime","type":"uint64"},{"internalType":"uint256","name":"feesToDistribute","type":"uint256"},{"internalType":"uint256","name":"feesClaimed","type":"uint256"},{"internalType":"uint256","name":"rewardsToDistribute","type":"uint256"},{"internalType":"uint256","name":"rewardsClaimed","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"recordFeePaid","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"resolver","outputs":[{"internalType":"contract AddressResolver","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"resolverAddressesRequired","outputs":[{"internalType":"bytes32[]","name":"addresses","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address payable","name":"_integrationProxy","type":"address"}],"name":"setIntegrationProxy","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"sender","type":"address"}],"name":"setMessageSender","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address payable","name":"_proxy","type":"address"}],"name":"setProxy","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"setRewardsToDistribute","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"setupExpiryTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"targetThreshold","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalFeesAvailable","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalRewardsAvailable","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]

6080604052631cd554d160e21b6008553480156200001c57600080fd5b5060405162004643380380620046438339810160408190526200003f9162000221565b8080621baf8085856001600160a01b038116620000795760405162461bcd60e51b8152600401620000709062000343565b60405180910390fd5b600080546001600160a01b0319166001600160a01b0383161781556040517fb532073b38c83145e3e5135377a08bf9aab55bc0fd7c1179cd4fb995d2a5159c91620000c69184906200030b565b60405180910390a1506000546001600160a01b0316620000fa5760405162461bcd60e51b8152600401620000709062000331565b600280546001600160a01b0319166001600160a01b0383161790556040517ffc80377ca9c49cc11ae6982f390a42db976d5530af7c43889264b13fbbd7c57e9062000147908390620002fb565b60405180910390a1504201600555600680546001600160a01b0319166001600160a01b0392909216919091179055506001620001846000620001e2565b80546001600160401b0319166001600160401b039290921691909117905542620001af6000620001e2565b80546001600160401b0392909216600160801b02600160801b600160c01b0319909216919091179055506200039e915050565b60006009600260ff16836013540181620001f857fe5b06600281106200020457fe5b6005020192915050565b80516200021b8162000384565b92915050565b6000806000606084860312156200023757600080fd5b60006200024586866200020e565b935050602062000258868287016200020e565b92505060406200026b868287016200020e565b9150509250925092565b620002808162000370565b82525050565b62000280816200035e565b6000620002a060118362000355565b7013dddb995c881b5d5cdd081899481cd95d607a1b815260200192915050565b6000620002cf60198362000355565b7f4f776e657220616464726573732063616e6e6f74206265203000000000000000815260200192915050565b602081016200021b828462000275565b604081016200031b828562000275565b6200032a602083018462000286565b9392505050565b602080825281016200021b8162000291565b602080825281016200021b81620002c0565b90815260200190565b60006001600160a01b0382166200021b565b60006200021b8260006200021b826200035e565b6200038f816200035e565b81146200039b57600080fd5b50565b61429580620003ae6000396000f3fe608060405234801561001057600080fd5b506004361061025c5760003560e01c80637418536011610145578063b410a034116100bd578063d67bdd251161008c578063eb1edd6111610071578063eb1edd611461048a578063ec55688914610492578063fd1f498d1461049a5761025c565b8063d67bdd251461047a578063e0e6393d146104825761025c565b8063b410a03414610442578063bc67f8321461044a578063cff2ddad1461045d578063d294f093146104725761025c565b80638da5cb5b116101145780639cbdaeb6116100f95780639cbdaeb61461041f578063ac83419314610427578063b10090b81461042f5761025c565b80638da5cb5b1461040457806397107d6d1461040c5761025c565b806374185360146103cc57806379ba5097146103d457806386645274146103dc578063899ffef4146103ef5761025c565b806333140016116101d857806353a47bb7116101a757806359a2f19f1161018c57806359a2f19f1461039e5780636466f45e146103b15780636de813f1146103c45761025c565b806353a47bb714610381578063569249d0146103965761025c565b8063331400161461032b5780633ebc457a1461034b5780633fcd22401461035357806346ba2d90146103795761025c565b8063131b0ae71161022f57806322425fa41161021457806322425fa4146102fb57806322bf55ef146103035780632af64bd3146103165761025c565b8063131b0ae7146102d35780631627540c146102e85761025c565b806304f3bcec1461026157806307ea50cd1461027f5780630813071c1461029f5780630de58615146102b2575b600080fd5b6102696104ad565b6040516102769190613f3a565b60405180910390f35b61029261028d366004613179565b6104bc565b6040516102769190613e57565b6102926102ad3660046131bd565b610591565b6102c56102c0366004613179565b6106dd565b604051610276929190613e73565b6102e66102e1366004613179565b61075f565b005b6102e66102f6366004613179565b610789565b6102926107e7565b6102e661031136600461329e565b6107f7565b61031e6109a7565b6040516102769190613e49565b61033e610339366004613179565b610ad7565b6040516102769190613e2a565b6102e6610d3b565b61036661036136600461329e565b61108a565b60405161027697969594939291906140d7565b610292611133565b610389611139565b6040516102769190613d57565b610292611148565b61031e6103ac366004613179565b6111a3565b61031e6103bf366004613179565b6111b5565b6102926112df565b6102e6611334565b6102e6611486565b6102e66103ea3660046131f7565b611522565b6103f7611633565b6040516102769190613e38565b610389611919565b6102e661041a366004613179565b611928565b61026961197b565b61029261198a565b6102e661043d36600461331c565b611a2a565b610292611c24565b6102e6610458366004613179565b611c2e565b610465611c58565b604051610276919061413f565b61031e611c5d565b610389611cd4565b610292611ce3565b610389611ced565b610269611d05565b6102e66104a836600461329e565b611d14565b6006546001600160a01b031681565b60006104c6611d91565b6001600160a01b031663bdc963d87f6c6173745f6665655f7769746864726177616c0000000000000000000000000084604051602001610507929190613ce5565b604051602081830303815290604052805190602001206040518263ffffffff1660e01b81526004016105399190613e57565b60206040518083038186803b15801561055157600080fd5b505afa158015610565573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506105899190810190613262565b90505b919050565b6000816105b95760405162461bcd60e51b81526004016105b0906140a9565b60405180910390fd5b600282106105d95760405162461bcd60e51b81526004016105b090614069565b6105e560018303611dbc565b5468010000000000000000900467ffffffffffffffff16610608575060006106d7565b6000610638600161061b60018603611dbc565b5468010000000000000000900467ffffffffffffffff1690611de6565b9050600080610645611e0e565b6001600160a01b031663d29c000a87856040518363ffffffff1660e01b8152600401610672929190613da9565b604080518083038186803b15801561068957600080fd5b505afa15801561069d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506106c191908101906132ec565b90925090506106d1838383611e39565b93505050505b92915050565b6000806106e8613091565b6106f184610ad7565b905060008060015b60028110156107525761071e84826002811061071157fe5b6020020151518490611f84565b925061074884826002811061072f57fe5b602002015160016020020151839063ffffffff611f8416565b91506001016106f9565b509093509150505b915091565b610767611fa9565b600380546001600160a01b0319166001600160a01b0392909216919091179055565b610791611fa9565b600180546001600160a01b0319166001600160a01b0383161790556040517f906a1c6bd7e3091ea86693dd029a831c19049ce77f1dce2ce0bab1cacbabce22906107dc908390613d57565b60405180910390a150565b60006107f1611fd5565b90505b90565b600061080161209c565b6001600160a01b0316331490506000806108196120c7565b6001600160a01b03166316b2213f336040518263ffffffff1660e01b81526004016108449190613d65565b60206040518083038186803b15801561085c57600080fd5b505afa158015610870573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506108949190810190613262565b1415905060006108a26120db565b6001600160a01b0316336001600160a01b031614905060006108c2612106565b6001600160a01b031663b38988f7336040518263ffffffff1660e01b81526004016108ed9190613d65565b60206040518083038186803b15801561090557600080fd5b505afa158015610919573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061093d9190810190613244565b905083806109485750825b806109505750815b806109585750805b6109745760405162461bcd60e51b81526004016105b090613f59565b610992856109826000611dbc565b600101549063ffffffff611f8416565b61099c6000611dbc565b600101555050505050565b600060606109b3611633565b905060005b8151811015610ace5760008282815181106109cf57fe5b602090810291909101810151600081815260079092526040918290205460065492517f21f8a7210000000000000000000000000000000000000000000000000000000081529193506001600160a01b039081169216906321f8a72190610a39908590600401613e57565b60206040518083038186803b158015610a5157600080fd5b505afa158015610a65573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610a89919081019061319f565b6001600160a01b0316141580610ab457506000818152600760205260409020546001600160a01b0316155b15610ac557600093505050506107f4565b506001016109b8565b50600191505090565b610adf613091565b6000806000610aec611e0e565b6040517fb326f84e0000000000000000000000000000000000000000000000000000000081529091506001600160a01b0382169063b326f84e90610b37908890600090600401613d8e565b604080518083038186803b158015610b4e57600080fd5b505afa158015610b62573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610b8691908101906132ec565b909350915081158015610b97575082155b15610baf57610ba4613091565b935061058c92505050565b600080610bbe60008686612131565b8751829052875160200181905290925090506000610bdb886104bc565b905060015b8015610d2f5760001981016000610bf682611dbc565b5468010000000000000000900467ffffffffffffffff1690508015801590610c305750610c2283611dbc565b5467ffffffffffffffff1684105b15610d24576000610c4882600163ffffffff611de616565b6040517fd29c000a0000000000000000000000000000000000000000000000000000000081529091506001600160a01b0389169063d29c000a90610c92908f908590600401613da9565b604080518083038186803b158015610ca957600080fd5b505afa158015610cbd573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610ce191908101906132ec565b909a509850610cf1848b8b612131565b9097509550868b8560028110610d0357fe5b602002015152858b8560028110610d1657fe5b602002015160016020020152505b505060001901610be0565b50505050505050919050565b610d436121d3565b6001600160a01b0316637c3125416040518163ffffffff1660e01b815260040160006040518083038186803b158015610d7b57600080fd5b505afa158015610d8f573d6000803e3d6000fd5b505050506000610d9d611fd5565b11610dba5760405162461bcd60e51b81526004016105b0906140b9565b610dc2611fd5565b4203610dce6000611dbc565b54600160801b900467ffffffffffffffff161115610dfe5760405162461bcd60e51b81526004016105b090613f79565b6000610e0981611dbc565b90506000610e176001611dbc565b9050610e488260010154610e3c83600201548460010154611de690919063ffffffff16565b9063ffffffff611f8416565b610e526000611dbc565b60010155600380830154600483015491830154610e7992610e3c919063ffffffff611de616565b610e836000611dbc565b60030155601354610ec090600290610eb490600190610ea8908463ffffffff611f8416565b9063ffffffff611de616565b9063ffffffff6121fe16565b601381905560099060028110610ed257fe5b6005020180547fffffffffffffffff000000000000000000000000000000000000000000000000168155600060018083018290556002830182905560038301829055600490920155610f3e90610f2781611dbc565b5467ffffffffffffffff169063ffffffff611f8416565b610f486000611dbc565b805467ffffffffffffffff191667ffffffffffffffff92909216919091179055610f7061222e565b6001600160a01b031663cd92eba96040518163ffffffff1660e01b815260040160206040518083038186803b158015610fa857600080fd5b505afa158015610fbc573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610fe09190810190613262565b610fea6000611dbc565b805467ffffffffffffffff9290921668010000000000000000026fffffffffffffffff0000000000000000199092169190911790554261102a6000611dbc565b805467ffffffffffffffff92909216600160801b027fffffffffffffffff0000000000000000ffffffffffffffffffffffffffffffff9092169190911790556110866110766001611dbc565b5467ffffffffffffffff16612259565b5050565b600080600080600080600061109d6130be565b6110a689611dbc565b6040805160e081018252825467ffffffffffffffff808216808452680100000000000000008304821660208501819052600160801b909304909116938301849052600185015460608401819052600286015460808501819052600387015460a0860181905260049097015460c0909501859052919f929e50939c50929a5091985091965090945092505050565b60055481565b6001546001600160a01b031681565b60008060015b600281101561119d5761117461116382611dbc565b60010154839063ffffffff611f8416565b915061119361118282611dbc565b60020154839063ffffffff611de616565b915060010161114e565b50905090565b60006111ae82612318565b5092915050565b60006111bf6121d3565b6001600160a01b0316637c3125416040518163ffffffff1660e01b815260040160006040518083038186803b1580156111f757600080fd5b505afa15801561120b573d6000803e3d6000fd5b5050505061121761240c565b61121f612462565b600480546040517f21f4ae570000000000000000000000000000000000000000000000000000000081526001600160a01b03938416936321f4ae579361126a93889392169101613d73565b60206040518083038186803b15801561128257600080fd5b505afa158015611296573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506112ba9190810190613244565b6112d65760405162461bcd60e51b81526004016105b090614009565b6105898261248d565b60008060015b600281101561119d5761130b6112fa82611dbc565b60030154839063ffffffff611f8416565b915061132a61131982611dbc565b60040154839063ffffffff611de616565b91506001016112e5565b606061133e611633565b905060005b815181101561108657600082828151811061135a57fe5b602002602001015190506000600660009054906101000a90046001600160a01b03166001600160a01b031663dacb2d01838460405160200161139c9190613d41565b6040516020818303038152906040526040518363ffffffff1660e01b81526004016113c8929190613e81565b60206040518083038186803b1580156113e057600080fd5b505afa1580156113f4573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611418919081019061319f565b6000838152600760205260409081902080546001600160a01b0319166001600160a01b038416179055519091507f88a93678a3692f6789d9546fc621bf7234b101ddb7d4fe479455112831b8aa68906114749084908490613e65565b60405180910390a15050600101611343565b6001546001600160a01b031633146114b05760405162461bcd60e51b81526004016105b090613f69565b6000546001546040517fb532073b38c83145e3e5135377a08bf9aab55bc0fd7c1179cd4fb995d2a5159c926114f3926001600160a01b0391821692911690613d73565b60405180910390a160018054600080546001600160a01b03199081166001600160a01b03841617909155169055565b61152a6120c7565b6001600160a01b0316336001600160a01b03161461155a5760405162461bcd60e51b81526004016105b090613fa9565b611562611e0e565b6001600160a01b03166394e1a44884848461157d6000611dbc565b5460405160e086901b7fffffffff000000000000000000000000000000000000000000000000000000001681526115d09493929168010000000000000000900467ffffffffffffffff1690600401613dec565b600060405180830381600087803b1580156115ea57600080fd5b505af11580156115fe573d6000803e3d6000fd5b5050505061162e8383836116126000611dbc565b5468010000000000000000900467ffffffffffffffff16612582565b505050565b60608061163e612651565b60408051600c8082526101a082019092529192506060919060208201610180803883390190505090507f53797374656d53746174757300000000000000000000000000000000000000008160008151811061169557fe5b6020026020010181815250507f53796e7468657469780000000000000000000000000000000000000000000000816001815181106116cf57fe5b6020026020010181815250507f466565506f6f6c537461746500000000000000000000000000000000000000008160028151811061170957fe5b6020026020010181815250507f466565506f6f6c457465726e616c53746f7261676500000000000000000000008160038151811061174357fe5b6020026020010181815250507f45786368616e67657200000000000000000000000000000000000000000000008160048151811061177d57fe5b6020026020010181815250506524b9b9bab2b960d11b816005815181106117a057fe5b6020026020010181815250507f53796e7468657469785374617465000000000000000000000000000000000000816006815181106117da57fe5b6020026020010181815250507f526577617264457363726f7756320000000000000000000000000000000000008160078151811061181457fe5b6020026020010181815250507f44656c6567617465417070726f76616c730000000000000000000000000000008160088151811061184e57fe5b6020026020010181815250507f4574686572436f6c6c61746572616c73555344000000000000000000000000008160098151811061188857fe5b6020026020010181815250507f52657761726473446973747269627574696f6e0000000000000000000000000081600a815181106118c257fe5b6020026020010181815250507f436f6c6c61746572616c4d616e6167657200000000000000000000000000000081600b815181106118fc57fe5b60200260200101818152505061191282826126b0565b9250505090565b6000546001600160a01b031681565b611930611fa9565b600280546001600160a01b0319166001600160a01b0383161790556040517ffc80377ca9c49cc11ae6982f390a42db976d5530af7c43889264b13fbbd7c57e906107dc908390613d65565b6003546001600160a01b031681565b60006107f1611a1661199a612765565b731a60e2e2a8be0bc2b6381dd31fd3fd5f9a28de4c63907af6c06040518163ffffffff1660e01b815260040160206040518083038186803b1580156119de57600080fd5b505af41580156119f2573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610e3c9190810190613262565b611a1e6127dc565b9063ffffffff61285316565b611a3261287d565b6005544210611a535760405162461bcd60e51b81526004016105b090614079565b611a5b61222e565b6001600160a01b031663cd92eba96040518163ffffffff1660e01b815260040160206040518083038186803b158015611a9357600080fd5b505afa158015611aa7573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611acb9190810190613262565b861115611aea5760405162461bcd60e51b81526004016105b090614059565b6040518060e001604052808867ffffffffffffffff1681526020018767ffffffffffffffff1681526020018667ffffffffffffffff168152602001858152602001848152602001838152602001828152506009611b5a600260ff16610eb48c601354611f8490919063ffffffff16565b60028110611b6457fe5b82516005919091029190910180546020840151604085015167ffffffffffffffff1990921667ffffffffffffffff948516176fffffffffffffffff000000000000000019166801000000000000000091851691909102177fffffffffffffffff0000000000000000ffffffffffffffffffffffffffffffff16600160801b9390911692909202919091178155606082015160018201556080820151600282015560a0820151600382015560c0909101516004909101555050505050505050565b60006107f16127dc565b611c36612902565b600480546001600160a01b0319166001600160a01b0392909216919091179055565b600281565b6000611c676121d3565b6001600160a01b0316637c3125416040518163ffffffff1660e01b815260040160006040518083038186803b158015611c9f57600080fd5b505afa158015611cb3573d6000803e3d6000fd5b50505050611cbf61240c565b6004546107f1906001600160a01b031661248d565b6004546001600160a01b031681565b60006107f1612765565b73feefeefeefeefeefeefeefeefeefeefeefeefeef81565b6002546001600160a01b031681565b6000611d1e612941565b6004549091506001600160a01b0380831691161480611d455750336001600160a01b038216145b611d615760405162461bcd60e51b81526004016105b090614019565b611d7f82611d6f6000611dbc565b600301549063ffffffff611f8416565b611d896000611dbc565b600301555050565b60006107f17f466565506f6f6c457465726e616c53746f726167650000000000000000000000612968565b60006009600260ff16836013540181611dd157fe5b0660028110611ddc57fe5b6005020192915050565b600082821115611e085760405162461bcd60e51b81526004016105b090613fb9565b50900390565b60006107f17f466565506f6f6c53746174650000000000000000000000000000000000000000612968565b600080611e4461222e565b90506000611f7885611f6c846001600160a01b03166308d95cd5886040518263ffffffff1660e01b8152600401611e7b9190613e57565b60206040518083038186803b158015611e9357600080fd5b505afa158015611ea7573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611ecb9190810190613262565b6040517f08d95cd50000000000000000000000000000000000000000000000000000000081526001600160a01b038716906308d95cd590611f10908d90600401613e57565b60206040518083038186803b158015611f2857600080fd5b505afa158015611f3c573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611f609190810190613262565b9063ffffffff6129c516565b9063ffffffff6129de16565b925050505b9392505050565b600082820183811015611f7d5760405162461bcd60e51b81526004016105b090613f99565b6000546001600160a01b03163314611fd35760405162461bcd60e51b81526004016105b090614029565b565b6000611fdf6129f7565b6001600160a01b03166323257c2b7f53797374656d53657474696e67730000000000000000000000000000000000007f666565506572696f644475726174696f6e0000000000000000000000000000006040518363ffffffff1660e01b815260040161204c929190613e73565b60206040518083038186803b15801561206457600080fd5b505afa158015612078573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506107f19190810190613262565b60006107f17f45786368616e6765720000000000000000000000000000000000000000000000612968565b60006107f16524b9b9bab2b960d11b612968565b60006107f17f4574686572436f6c6c61746572616c7355534400000000000000000000000000612968565b60006107f17f436f6c6c61746572616c4d616e61676572000000000000000000000000000000612968565b60008083612144575060009050806121cb565b83851561216f57600061215e600161061b60018a03611dbc565b905061216b818787611e39565b9150505b600061218e8261217e89611dbc565b600101549063ffffffff61285316565b905060006121af8361219f8a611dbc565b600301549063ffffffff61285316565b90506121ba82612a22565b6121c382612a22565b945094505050505b935093915050565b60006107f17f53797374656d5374617475730000000000000000000000000000000000000000612968565b60008161221d5760405162461bcd60e51b81526004016105b090613ff9565b81838161222657fe5b069392505050565b60006107f17f53796e7468657469785374617465000000000000000000000000000000000000612968565b6002546040516001600160a01b039091169063907dff979061227f908490602001613e57565b604051602081830303815290604052600160405161229c90613d4c565b6040519081900381207fffffffff0000000000000000000000000000000000000000000000000000000060e086901b1682526122e393929160009081908190600401613ea1565b600060405180830381600087803b1580156122fd57600080fd5b505af1158015612311573d6000803e3d6000fd5b5050505050565b6000806000806123266120c7565b6001600160a01b031663ae3bbbbb866040518263ffffffff1660e01b81526004016123519190613d57565b604080518083038186803b15801561236857600080fd5b505afa15801561237c573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506123a091908101906132bc565b9150915060006123ae6127dc565b9050808310156123c6575060019350915061075a9050565b60006123e36123d661199a612765565b839063ffffffff61285316565b9050808411156123fd57600083955095505050505061075a565b50600194509092505050915091565b6002546001600160a01b0316331480159061243257506003546001600160a01b03163314155b801561244957506004546001600160a01b03163314155b15611fd357600480546001600160a01b03191633179055565b60006107f17f44656c6567617465417070726f76616c73000000000000000000000000000000612968565b600080808080808061249e88612318565b91509150816124bf5760405162461bcd60e51b81526004016105b090614039565b80156124dd5760405162461bcd60e51b81526004016105b090613fe9565b6124e6886106dd565b9094509250831515806124f95750600083115b6125155760405162461bcd60e51b81526004016105b090613fd9565b612533886125236001611dbc565b5467ffffffffffffffff16612a44565b831561254e5761254284612af7565b945061254e8886612bd3565b82156125695761255d83612d6f565b95506125698887612e49565b612574888688612ec3565b506001979650505050505050565b6002546040516001600160a01b039091169063907dff97906125ac908690869086906020016140c9565b60405160208183030381529060405260026040516125c990613d16565b6040519081900381207fffffffff0000000000000000000000000000000000000000000000000000000060e086901b1682526126199392916001600160a01b038b16906000908190600401613f00565b600060405180830381600087803b15801561263357600080fd5b505af1158015612647573d6000803e3d6000fd5b5050505050505050565b604080516001808252818301909252606091602080830190803883390190505090507f466c657869626c6553746f726167650000000000000000000000000000000000816000815181106126a157fe5b60200260200101818152505090565b606081518351016040519080825280602002602001820160405280156126e0578160200160208202803883390190505b50905060005b8351811015612722578381815181106126fb57fe5b602002602001015182828151811061270f57fe5b60209081029190910101526001016126e6565b5060005b82518110156111ae5782818151811061273b57fe5b602002602001015182828651018151811061275257fe5b6020908102919091010152600101612726565b600061276f6129f7565b6001600160a01b03166323257c2b7f53797374656d53657474696e67730000000000000000000000000000000000007f7461726765745468726573686f6c6400000000000000000000000000000000006040518363ffffffff1660e01b815260040161204c929190613e73565b60006127e66129f7565b6001600160a01b03166323257c2b7f53797374656d53657474696e67730000000000000000000000000000000000007f69737375616e6365526174696f000000000000000000000000000000000000006040518363ffffffff1660e01b815260040161204c929190613e73565b6000670de0b6b3a764000061286e848463ffffffff612f8816565b8161287557fe5b049392505050565b6002546001600160a01b031633148015906128a357506003546001600160a01b03163314155b80156128ba57506004546001600160a01b03163314155b156128d257600480546001600160a01b031916331790555b6000546004546001600160a01b03908116911614611fd35760405162461bcd60e51b81526004016105b090613f89565b6002546001600160a01b031633148061292557506003546001600160a01b031633145b611fd35760405162461bcd60e51b81526004016105b090614099565b60006107f17f52657761726473446973747269627574696f6e000000000000000000000000005b60008181526007602090815260408083205490516001600160a01b03909116918215159161299891869101613d21565b604051602081830303815290604052906111ae5760405162461bcd60e51b81526004016105b09190613f48565b6000611f7d83836b033b2e3c9fd0803ce8000000612fc2565b6000611f7d83836b033b2e3c9fd0803ce8000000613006565b60006107f17f466c657869626c6553746f726167650000000000000000000000000000000000612968565b60006305f5e10082046005600a820610612a3a57600a015b600a900492915050565b612a4c611d91565b6001600160a01b0316633562fd207f6c6173745f6665655f7769746864726177616c0000000000000000000000000084604051602001612a8d929190613ce5565b60405160208183030381529060405280519060200120836040518363ffffffff1660e01b8152600401612ac1929190613e73565b600060405180830381600087803b158015612adb57600080fd5b505af1158015612aef573d6000803e3d6000fd5b505050505050565b6000818160015b6002811015612bcb576000612b1282611dbc565b6002015490506000612b3782612b2785611dbc565b600101549063ffffffff611de616565b90508015612bc0576000858210612b4e5785612b50565b815b9050612b62838263ffffffff611f8416565b612b6b85611dbc565b60020155612b7f868263ffffffff611de616565b9550612b91858263ffffffff611f8416565b945085612ba65784965050505050505061058c565b83158015612bb45750600086115b15612bbe57600095505b505b505060001901612afe565b509392505050565b816001600160a01b03811673feefeefeefeefeefeefeefeefeefeefeefeefeef1415612c115760405162461bcd60e51b81526004016105b090614089565b6000612c1b6120c7565b6001600160a01b031663326080396008546040518263ffffffff1660e01b8152600401612c489190613e57565b60206040518083038186803b158015612c6057600080fd5b505afa158015612c74573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250612c989190810190613280565b6040517f9dc29fac0000000000000000000000000000000000000000000000000000000081529091506001600160a01b03821690639dc29fac90612cf69073feefeefeefeefeefeefeefeefeefeefeefeefeef908790600401613da9565b600060405180830381600087803b158015612d1057600080fd5b505af1158015612d24573d6000803e3d6000fd5b50506040517f867904b40000000000000000000000000000000000000000000000000000000081526001600160a01b038416925063867904b491506126199087908790600401613da9565b6000818160015b6002811015612bcb576000612daa612d8d83611dbc565b60040154612d9a84611dbc565b600301549063ffffffff611de616565b90508015612e3f576000848210612dc15784612dc3565b815b9050612de281612dd285611dbc565b600401549063ffffffff611f8416565b612deb84611dbc565b60040155612dff858263ffffffff611de616565b9450612e11848263ffffffff611f8416565b935084612e2557839550505050505061058c565b82158015612e335750600085115b15612e3d57600094505b505b5060001901612d76565b816001600160a01b03811673feefeefeefeefeefeefeefeefeefeefeefeefeef1415612e875760405162461bcd60e51b81526004016105b090614089565b6301dfe200612e94613031565b6001600160a01b0316631bb47b448585846040518463ffffffff1660e01b815260040161261993929190613dc4565b6002546040516001600160a01b039091169063907dff9790612eed90869086908690602001613dc4565b6040516020818303038152906040526001604051612f0a90613d0b565b6040519081900381207fffffffff0000000000000000000000000000000000000000000000000000000060e086901b168252612f5193929160009081908190600401613ea1565b600060405180830381600087803b158015612f6b57600080fd5b505af1158015612f7f573d6000803e3d6000fd5b50505050505050565b600082612f97575060006106d7565b82820282848281612fa457fe5b0414611f7d5760405162461bcd60e51b81526004016105b090614049565b600080612fe884612fdc87600a870263ffffffff612f8816565b9063ffffffff61305c16565b90506005600a825b0610612ffa57600a015b600a9004949350505050565b600080600a830461301d868663ffffffff612f8816565b8161302457fe5b0490506005600a82612ff0565b60006107f17f526577617264457363726f775632000000000000000000000000000000000000612968565b600080821161307d5760405162461bcd60e51b81526004016105b090613fc9565b600082848161308857fe5b04949350505050565b60405180604001604052806002905b6130a8613119565b8152602001906001900390816130a05790505090565b6040518060e00160405280600067ffffffffffffffff168152602001600067ffffffffffffffff168152602001600067ffffffffffffffff168152602001600081526020016000815260200160008152602001600081525090565b60405180604001604052806002906020820280388339509192915050565b80356106d781614220565b80516106d781614220565b80516106d781614237565b80516106d781614240565b80516106d781614249565b80356106d781614240565b60006020828403121561318b57600080fd5b60006131978484613137565b949350505050565b6000602082840312156131b157600080fd5b60006131978484613142565b600080604083850312156131d057600080fd5b60006131dc8585613137565b92505060206131ed8582860161316e565b9150509250929050565b60008060006060848603121561320c57600080fd5b60006132188686613137565b93505060206132298682870161316e565b925050604061323a8682870161316e565b9150509250925092565b60006020828403121561325657600080fd5b6000613197848461314d565b60006020828403121561327457600080fd5b60006131978484613158565b60006020828403121561329257600080fd5b60006131978484613163565b6000602082840312156132b057600080fd5b6000613197848461316e565b600080604083850312156132cf57600080fd5b60006132db8585613158565b92505060206131ed8582860161314d565b600080604083850312156132ff57600080fd5b600061330b8585613158565b92505060206131ed85828601613158565b600080600080600080600080610100898b03121561333957600080fd5b60006133458b8b61316e565b98505060206133568b828c0161316e565b97505060406133678b828c0161316e565b96505060606133788b828c0161316e565b95505060806133898b828c0161316e565b94505060a061339a8b828c0161316e565b93505060c06133ab8b828c0161316e565b92505060e06133bc8b828c0161316e565b9150509295985092959890939650565b60006133d883836134c2565b505060400190565b60006133ec8383613517565b505060200190565b6133fd816141a0565b82525050565b6133fd81614166565b6133fd61341882614166565b6141ff565b61342681614153565b613430818461058c565b925061343b826107f4565b8060005b83811015612aef57815161345387826133cc565b965061345e8361414d565b92505060010161343f565b600061347482614159565b61347e818561415d565b93506134898361414d565b8060005b838110156134b75781516134a188826133e0565b97506134ac8361414d565b92505060010161348d565b509495945050505050565b6134cb81614153565b6134d5818461058c565b92506134e0826107f4565b8060005b83811015612aef5781516134f887826133e0565b96506135038361414d565b9250506001016134e4565b6133fd81614171565b6133fd816107f4565b6133fd61352c826107f4565b6107f4565b600061353c82614159565b613546818561415d565b93506135568185602086016141cf565b61355f81614210565b9093019392505050565b6133fd81614176565b6133fd816141ab565b6133fd816141b9565b600061359160178361415d565b7f4f6e6c7920496e7465726e616c20436f6e747261637473000000000000000000815260200192915050565b60006135ca60358361415d565b7f596f75206d757374206265206e6f6d696e61746564206265666f726520796f7581527f2063616e20616363657074206f776e6572736869700000000000000000000000602082015260400192915050565b6000613629601d8361415d565b7f546f6f206561726c7920746f20636c6f73652066656520706572696f64000000815260200192915050565b600061366260138361415d565b7f4f776e6572206f6e6c792066756e6374696f6e00000000000000000000000000815260200192915050565b600061369b60248361058c565b7f46656573436c61696d656428616464726573732c75696e743235362c75696e7481527f3235362900000000000000000000000000000000000000000000000000000000602082015260240192915050565b60006136fa60378361058c565b7f49737375616e636544656274526174696f456e74727928616464726573732c7581527f696e743235362c75696e743235362c75696e7432353629000000000000000000602082015260370192915050565b6000613759601b8361415d565b7f536166654d6174683a206164646974696f6e206f766572666c6f770000000000815260200192915050565b6000613792601f8361415d565b7f466565506f6f6c3a204f6e6c792049737375657220417574686f726973656400815260200192915050565b60006137cb601e8361415d565b7f536166654d6174683a207375627472616374696f6e206f766572666c6f770000815260200192915050565b6000613804601a8361415d565b7f536166654d6174683a206469766973696f6e206279207a65726f000000000000815260200192915050565b600061383d60408361415d565b7f4e6f2066656573206f72207265776172647320617661696c61626c6520666f7281527f20706572696f642c206f72206665657320616c726561647920636c61696d6564602082015260400192915050565b600061389c60118361058c565b7f4d697373696e6720616464726573733a20000000000000000000000000000000815260110192915050565b60006138d5601e8361415d565b7f412073796e7468206f7220534e58207261746520697320696e76616c69640000815260200192915050565b600061390e60188361415d565b7f536166654d6174683a206d6f64756c6f206279207a65726f0000000000000000815260200192915050565b6000613947601f8361415d565b7f4e6f7420617070726f76656420746f20636c61696d206f6e20626568616c6600815260200192915050565b6000613980601e8361415d565b7f43616c6c6572206973206e6f742072657761726473417574686f726974790000815260200192915050565b60006139b9602f8361415d565b7f4f6e6c792074686520636f6e7472616374206f776e6572206d6179207065726681527f6f726d207468697320616374696f6e0000000000000000000000000000000000602082015260400192915050565b6000613a18601f8361415d565b7f432d526174696f2062656c6f772070656e616c7479207468726573686f6c6400815260200192915050565b6000613a5160218361415d565b7f536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f81527f7700000000000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000613ab060168361415d565b7f43616e6e6f7420696d706f727420626164206461746100000000000000000000815260200192915050565b6000613ae9601d8361415d565b7f4578636565647320746865204645455f504552494f445f4c454e475448000000815260200192915050565b6000613b2260298361415d565b7f43616e206f6e6c7920706572666f726d207468697320616374696f6e2064757281527f696e672073657475700000000000000000000000000000000000000000000000602082015260400192915050565b6000613b8160198361058c565b7f5265736f6c766572206d697373696e67207461726765743a2000000000000000815260190192915050565b6000613bba60178361415d565b7f4665652061646472657373206e6f7420616c6c6f776564000000000000000000815260200192915050565b6000613bf360188361058c565b7f466565506572696f64436c6f7365642875696e74323536290000000000000000815260180192915050565b6000613c2c60178361415d565b7f4f6e6c79207468652070726f78792063616e2063616c6c000000000000000000815260200192915050565b6000613c6560208361415d565b7f43757272656e7420706572696f64206973206e6f7420636c6f73656420796574815260200192915050565b6000613c9e601b8361415d565b7f46656520506572696f64204475726174696f6e206e6f74207365740000000000815260200192915050565b6133fd816141c4565b6133fd8161418d565b6133fd8161419a565b6000613cf18285613520565b602082019150613d01828461340c565b5060140192915050565b60006106d78261368e565b60006106d7826136ed565b6000613d2c8261388f565b9150613d388284613520565b50602001919050565b6000613d2c82613b74565b60006106d782613be6565b602081016106d78284613403565b602081016106d782846133f4565b60408101613d818285613403565b611f7d6020830184613403565b60408101613d9c8285613403565b611f7d602083018461357b565b60408101613db78285613403565b611f7d6020830184613517565b60608101613dd28286613403565b613ddf6020830185613517565b6131976040830184613517565b60808101613dfa8287613403565b613e076020830186613517565b613e146040830185613517565b613e216060830184613cca565b95945050505050565b608081016106d7828461341d565b60208082528101611f7d8184613469565b602081016106d7828461350e565b602081016106d78284613517565b60408101613d818285613517565b60408101613db78285613517565b60408101613e8f8285613517565b81810360208301526131978184613531565b60c08082528101613eb28189613531565b9050613ec1602083018861357b565b613ece6040830187613517565b613edb6060830186613572565b613ee86080830185613572565b613ef560a0830184613572565b979650505050505050565b60c08082528101613f118189613531565b9050613f20602083018861357b565b613f2d6040830187613517565b613edb6060830186613517565b602081016106d78284613569565b60208082528101611f7d8184613531565b6020808252810161058981613584565b60208082528101610589816135bd565b602080825281016105898161361c565b6020808252810161058981613655565b602080825281016105898161374c565b6020808252810161058981613785565b60208082528101610589816137be565b60208082528101610589816137f7565b6020808252810161058981613830565b60208082528101610589816138c8565b6020808252810161058981613901565b602080825281016105898161393a565b6020808252810161058981613973565b60208082528101610589816139ac565b6020808252810161058981613a0b565b6020808252810161058981613a44565b6020808252810161058981613aa3565b6020808252810161058981613adc565b6020808252810161058981613b15565b6020808252810161058981613bad565b6020808252810161058981613c1f565b6020808252810161058981613c58565b6020808252810161058981613c91565b60608101613dd28286613517565b60e081016140e5828a613cd3565b6140f26020830189613cd3565b6140ff6040830188613cd3565b61410c6060830187613517565b6141196080830186613517565b61412660a0830185613517565b61413360c0830184613517565b98975050505050505050565b602081016106d78284613cdc565b60200190565b50600290565b5190565b90815260200190565b600061058982614181565b151590565b600061058982614166565b6001600160a01b031690565b67ffffffffffffffff1690565b60ff1690565b600061058982614176565b600061058961352c836107f4565b6000610589826107f4565b60006105898261418d565b60005b838110156141ea5781810151838201526020016141d2565b838111156141f9576000848401525b50505050565b60006105898260006105898261421a565b601f01601f191690565b60601b90565b61422981614166565b811461423457600080fd5b50565b61422981614171565b614229816107f4565b6142298161417656fea365627a7a72315820aef27c0f942aedd43f7a6c94d0bcd60171d43948b9197426bf72b1d480f232956c6578706572696d656e74616cf564736f6c63430005100040000000000000000000000000c43b833f93c3896472ded3eff73311f571e38742000000000000000000000000b64ff7a4a33acdf48d97dab0d764afd0f6176882000000000000000000000000242a3df52c375bee81b1c668741d7c63af68fdd2

Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)

000000000000000000000000c43b833f93c3896472ded3eff73311f571e38742000000000000000000000000b64ff7a4a33acdf48d97dab0d764afd0f6176882000000000000000000000000242a3df52c375bee81b1c668741d7c63af68fdd2

-----Decoded View---------------
Arg [0] : _proxy (address): 0xc43b833f93c3896472ded3eff73311f571e38742
Arg [1] : _owner (address): 0xb64ff7a4a33acdf48d97dab0d764afd0f6176882
Arg [2] : _resolver (address): 0x242a3df52c375bee81b1c668741d7c63af68fdd2

-----Encoded View---------------
3 Constructor Arguments found :
Arg [0] : 000000000000000000000000c43b833f93c3896472ded3eff73311f571e38742
Arg [1] : 000000000000000000000000b64ff7a4a33acdf48d97dab0d764afd0f6176882
Arg [2] : 000000000000000000000000242a3df52c375bee81b1c668741d7c63af68fdd2


Library Used

SafeDecimalMath : 0x1a60e2e2a8be0bc2b6381dd31fd3fd5f9a28de4c

Block Transaction Difficulty Gas Used Reward
Block Uncle Number Difficulty Gas Used Reward
Loading