Contract 0xF49528689234d46724383669BD8658bfB47f8575

Contract Overview

Balance:
0 Ether
Txn Hash Method
Block
From
To
Value
0xced014330404cb9b63a3e53893707566409de9ad1d004f8d6e61e6a3186ca3c7Accept Ownership243386422021-04-19 21:38:3228 days 23 hrs ago0x73570075092502472e4b61a7058df1a4a1db12f2 IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.0000217861
0x6abf221fb979a5b741fbe3912687c58da8b40033ecb2062562e79a48412eeeaeNominate New Own...243377162021-04-19 20:35:5629 days 8 mins ago0xb64ff7a4a33acdf48d97dab0d764afd0f6176882 IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.0000444811
0x522a46576405416870989d19d44d7d19ef2ea1421742f6ffcddedeb2d0b22b34Settle241868672021-04-07 17:12:4441 days 3 hrs ago0x460f0f944aa6736903cb591e89f9111ac0ca7e7b IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.0125480115
0x9c66da5d574d4584c213bc86e0da09db42bcfc233f41a09d29501b7150044af1Exchange239238432021-03-15 3:57:3664 days 16 hrs ago0x0713a0c92019d867ff978c83074d4475c2a1493b IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.0003076310
0x1342e3b177ecfc85613b03912dc442b8d5abb4181b066ae7ddae44c50b573951Exchange234808272021-02-15 6:53:0092 days 13 hrs ago0x175023d52584a5e29e6c33e88592851359941508 IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.00013881154.5
0x1cb0acc18f45a15dad27129b891adab99e4c9adf0dea156f93018ee8a9023883Settle233087612021-02-04 14:59:08103 days 5 hrs ago0x7bb032d3641b28931a19eba38c702929102c78f9 IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.04230754220
0x052096512459359abf21eb7b4ccb3eedcd998d0b7b1fd3816c96804c86617a98Settle232461142021-01-31 17:14:52107 days 3 hrs ago0xdf0b4944f09807b6cd38afd33169cce1364acacf IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.0479803214147.4
0xcc829180b48829ecaf6f429435de4d6dd82dcd6dbcc4df53fce47edc82b05eb3Settle232321292021-01-30 20:28:00108 days 16 mins ago0x57170b6ae91d06102ec7d984d551d590ce1504e5 IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.021120792169.4
0xa50a3f993c48d60c0bd7f23f2c4934c536b55bded5d99715461577bf8d36bd9bSettle232321102021-01-30 20:26:20108 days 18 mins ago0x57170b6ae91d06102ec7d984d551d590ce1504e5 IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.0211858416169.4
0xfac3ade0971ebe9ae4aef5ff93dfb4c9d2b465b00ac5d8549631b9b638b39bfcSettle232319482021-01-30 20:11:56108 days 32 mins ago0x57170b6ae91d06102ec7d984d551d590ce1504e5 IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.033508167169.4
0xf8973fe5289885cd049d1eaed1a0c786d81cbc9c6c82d47f1bd1ebda9682bf9eExchange With Tr...232317262021-01-30 19:52:12108 days 52 mins ago0x57170b6ae91d06102ec7d984d551d590ce1504e5 IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.0052993402169.4
0x44396db68b2204177df6ced0c84e364cf7d87cff7a3a78f00815b40eb301959eExchange With Tr...232317052021-01-30 19:50:20108 days 54 mins ago0x57170b6ae91d06102ec7d984d551d590ce1504e5 IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.005372521169.4
0xeeadf041fa7b0ed19cb28483aaceabc7ff6e7c17f1396e156cce95f0398aa513Exchange232316812021-01-30 19:48:12108 days 56 mins ago0x57170b6ae91d06102ec7d984d551d590ce1504e5 IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.005213285169.4
0x02c9ff1e021149560773b6f6c0c7fac5c7ca5a0f5d42e0e09746cd4247b84faeSettle228619282021-01-03 2:10:00135 days 18 hrs ago0x97788574f4b71d488fb8dbd02036bf2567326ead IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.0001250671
0xf03d4d337c1cb72a89d36d331c151ef011d70b1745dd5a0143d0dc715864d168Settle228617942021-01-03 1:54:52135 days 18 hrs ago0x97788574f4b71d488fb8dbd02036bf2567326ead IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.0002312541
0x3c1c3c67ec97463aa044494dc9e6b553cfbaf958033ed55127d434b604c55721Settle228557192021-01-02 14:26:40136 days 6 hrs ago0x97788574f4b71d488fb8dbd02036bf2567326ead IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.0002056481
0xc2a8e101bf02330a5bead55dbd2cb3ecaa3bd492d339489c4eeeb65e4bcd2d27Settle228556632021-01-02 14:20:12136 days 6 hrs ago0x97788574f4b71d488fb8dbd02036bf2567326ead IN  0xf49528689234d46724383669bd8658bfb47f85750 Ether0.0002028411
0xe191b1390030ac3ca80ec03cb6db7f96601c68354055c81f9556a24ad33454310x60806040227145202020-12-22 13:28:00147 days 7 hrs ago0xb64ff7a4a33acdf48d97dab0d764afd0f6176882 IN  Contract Creation0 Ether0.0101307822
[ Download CSV Export 
Latest 25 internal transaction
Parent Txn Hash Block From To Value
0xb242c1071c0081d4e2424064c76acd2b5b65e5a9a2cf90887eeee81facf63dcb243785582021-04-21 18:34:5627 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0xa3f59b8e28cabc4411198dda2e65c380bd5d6dfe0 Ether
0xb242c1071c0081d4e2424064c76acd2b5b65e5a9a2cf90887eeee81facf63dcb243785582021-04-21 18:34:5627 days 2 hrs ago 0x57e4f1d434a59ebc0bac64adba0fbf7d56de91f6 0xf49528689234d46724383669bd8658bfb47f85750 Ether
0xb242c1071c0081d4e2424064c76acd2b5b65e5a9a2cf90887eeee81facf63dcb243785582021-04-21 18:34:5627 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0xb1751e5ede811288ce2fc4c65aaca17a955366be0 Ether
0xb242c1071c0081d4e2424064c76acd2b5b65e5a9a2cf90887eeee81facf63dcb243785582021-04-21 18:34:5627 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0xa3f59b8e28cabc4411198dda2e65c380bd5d6dfe0 Ether
0xb242c1071c0081d4e2424064c76acd2b5b65e5a9a2cf90887eeee81facf63dcb243785582021-04-21 18:34:5627 days 2 hrs ago 0x57e4f1d434a59ebc0bac64adba0fbf7d56de91f6 0xf49528689234d46724383669bd8658bfb47f85750 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0x07acc2b253218535c21a3e57bcb81eb13345a34a0 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0xb1751e5ede811288ce2fc4c65aaca17a955366be0 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0xa3f59b8e28cabc4411198dda2e65c380bd5d6dfe0 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0xc9985cac4a69588da66f74e42845b784798fe5ab0 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0xc9985cac4a69588da66f74e42845b784798fe5ab0 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0x07acc2b253218535c21a3e57bcb81eb13345a34a0 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0xdcb48e969e69df65f72f77b2aae145509733e5c00 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0x57e4f1d434a59ebc0bac64adba0fbf7d56de91f60 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0xa0354c17832e34daf2aeae968af3dc558d5c6dc60 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0x2709b3568a79d7e00d6729e96b84a1996cdb89ef0 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0xc9985cac4a69588da66f74e42845b784798fe5ab0 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0x130613411d53076923af9ba1d830205b34126d760 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0x2709b3568a79d7e00d6729e96b84a1996cdb89ef0 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0x57e4f1d434a59ebc0bac64adba0fbf7d56de91f60 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0x2709b3568a79d7e00d6729e96b84a1996cdb89ef0 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0xb1751e5ede811288ce2fc4c65aaca17a955366be0 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0xb1751e5ede811288ce2fc4c65aaca17a955366be0 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0x1a60e2e2a8be0bc2b6381dd31fd3fd5f9a28de4c0 Ether
0x6c6d0355498a15a633ac446fbe43fe905f10d36b2f12a47662f330d77aed07d6243785102021-04-21 18:31:4427 days 2 hrs ago 0xf49528689234d46724383669bd8658bfb47f8575 0xb1751e5ede811288ce2fc4c65aaca17a955366be0 Ether
[ Download CSV Export 
Loading

Contract Source Code Verified (Similar Match)
Note: This contract matches the deployed ByteCode of the Source Code for Contract 0xCcf14fc151EF7B40594207691530D4DB1D17930D

Contract Name:
ExchangerWithVirtualSynth

Compiler Version
v0.5.16+commit.9c3226ce

Optimization Enabled:
Yes with 200 runs

Other Settings:
default evmVersion

Contract Source Code (Solidity)

/**
 *Submitted for verification at Etherscan.io on 2020-12-21
*/

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

* Synthetix: ExchangerWithVirtualSynth.sol
*
* Latest source (may be newer): https://github.com/Synthetixio/synthetix/blob/master/contracts/ExchangerWithVirtualSynth.sol
* Docs: https://docs.synthetix.io/contracts/ExchangerWithVirtualSynth
*
* Contract Dependencies: 
*	- ERC20
*	- Exchanger
*	- IAddressResolver
*	- IERC20
*	- IExchanger
*	- IVirtualSynth
*	- MixinResolver
*	- MixinSystemSettings
*	- Owned
* Libraries: 
*	- SafeDecimalMath
*	- SafeMath
*
* MIT License
* ===========
*
* Copyright (c) 2020 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);
}


// 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() external {
        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_MESSAGE_GAS_LIMIT = "crossDomainMessageGasLimit";

    bytes32 internal constant CONTRACT_FLEXIBLESTORAGE = "FlexibleStorage";

    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 getCrossDomainMessageGasLimit() internal view returns (uint) {
        return flexibleStorage().getUIntValue(SETTING_CONTRACT_NAME, SETTING_CROSS_DOMAIN_MESSAGE_GAS_LIMIT);
    }

    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);
    }
}


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/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;
}


/**
 * @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/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;
}


// https://docs.synthetix.io/contracts/source/interfaces/iexchangestate
interface IExchangeState {
    // Views
    struct ExchangeEntry {
        bytes32 src;
        uint amount;
        bytes32 dest;
        uint amountReceived;
        uint exchangeFeeRate;
        uint timestamp;
        uint roundIdForSrc;
        uint roundIdForDest;
    }

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

    function getEntryAt(
        address account,
        bytes32 currencyKey,
        uint index
    )
        external
        view
        returns (
            bytes32 src,
            uint amount,
            bytes32 dest,
            uint amountReceived,
            uint exchangeFeeRate,
            uint timestamp,
            uint roundIdForSrc,
            uint roundIdForDest
        );

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

    // Mutative functions
    function appendExchangeEntry(
        address account,
        bytes32 src,
        uint amount,
        bytes32 dest,
        uint amountReceived,
        uint exchangeFeeRate,
        uint timestamp,
        uint roundIdForSrc,
        uint roundIdForDest
    ) external;

    function removeEntries(address account, bytes32 currencyKey) external;
}


// https://docs.synthetix.io/contracts/source/interfaces/iexchangerates
interface IExchangeRates {
    // Structs
    struct RateAndUpdatedTime {
        uint216 rate;
        uint40 time;
    }

    struct InversePricing {
        uint entryPoint;
        uint upperLimit;
        uint lowerLimit;
        bool frozenAtUpperLimit;
        bool frozenAtLowerLimit;
    }

    // Views
    function aggregators(bytes32 currencyKey) external view returns (address);

    function aggregatorWarningFlags() external view returns (address);

    function anyRateIsInvalid(bytes32[] calldata currencyKeys) external view returns (bool);

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

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

    function currenciesUsingAggregator(address aggregator) external view returns (bytes32[] memory);

    function effectiveValue(
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey
    ) external view returns (uint value);

    function effectiveValueAndRates(
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey
    )
        external
        view
        returns (
            uint value,
            uint sourceRate,
            uint destinationRate
        );

    function effectiveValueAtRound(
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey,
        uint roundIdForSrc,
        uint roundIdForDest
    ) external view returns (uint value);

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

    function getLastRoundIdBeforeElapsedSecs(
        bytes32 currencyKey,
        uint startingRoundId,
        uint startingTimestamp,
        uint timediff
    ) external view returns (uint);

    function inversePricing(bytes32 currencyKey)
        external
        view
        returns (
            uint entryPoint,
            uint upperLimit,
            uint lowerLimit,
            bool frozenAtUpperLimit,
            bool frozenAtLowerLimit
        );

    function lastRateUpdateTimes(bytes32 currencyKey) external view returns (uint256);

    function oracle() external view returns (address);

    function rateAndTimestampAtRound(bytes32 currencyKey, uint roundId) external view returns (uint rate, uint time);

    function rateAndUpdatedTime(bytes32 currencyKey) external view returns (uint rate, uint time);

    function rateAndInvalid(bytes32 currencyKey) external view returns (uint rate, bool isInvalid);

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

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

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

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

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

    function rateStalePeriod() external view returns (uint);

    function ratesAndUpdatedTimeForCurrencyLastNRounds(bytes32 currencyKey, uint numRounds)
        external
        view
        returns (uint[] memory rates, uint[] memory times);

    function ratesAndInvalidForCurrencies(bytes32[] calldata currencyKeys)
        external
        view
        returns (uint[] memory rates, bool anyRateInvalid);

    function ratesForCurrencies(bytes32[] calldata currencyKeys) external view returns (uint[] memory);

    // Mutative functions
    function freezeRate(bytes32 currencyKey) 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
        );

    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;
}


// 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;
}


// 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/itradingrewards
interface ITradingRewards {
    /* ========== VIEWS ========== */

    function getAvailableRewards() external view returns (uint);

    function getUnassignedRewards() external view returns (uint);

    function getRewardsToken() external view returns (address);

    function getPeriodController() external view returns (address);

    function getCurrentPeriod() external view returns (uint);

    function getPeriodIsClaimable(uint periodID) external view returns (bool);

    function getPeriodIsFinalized(uint periodID) external view returns (bool);

    function getPeriodRecordedFees(uint periodID) external view returns (uint);

    function getPeriodTotalRewards(uint periodID) external view returns (uint);

    function getPeriodAvailableRewards(uint periodID) external view returns (uint);

    function getUnaccountedFeesForAccountForPeriod(address account, uint periodID) external view returns (uint);

    function getAvailableRewardsForAccountForPeriod(address account, uint periodID) external view returns (uint);

    function getAvailableRewardsForAccountForPeriods(address account, uint[] calldata periodIDs)
        external
        view
        returns (uint totalRewards);

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

    function claimRewardsForPeriod(uint periodID) external;

    function claimRewardsForPeriods(uint[] calldata periodIDs) external;

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

    function recordExchangeFeeForAccount(uint usdFeeAmount, address account) external;

    function closeCurrentPeriodWithRewards(uint rewards) external;

    function recoverTokens(address tokenAddress, address recoverAddress) external;

    function recoverUnassignedRewardTokens(address recoverAddress) external;

    function recoverAssignedRewardTokensAndDestroyPeriod(address recoverAddress, uint periodID) external;

    function setPeriodController(address newPeriodController) external;
}


// https://docs.synthetix.io/contracts/source/interfaces/idebtcache
interface IDebtCache {
    // Views

    function cachedDebt() external view returns (uint);

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

    function cacheTimestamp() external view returns (uint);

    function cacheInvalid() external view returns (bool);

    function cacheStale() external view returns (bool);

    function currentSynthDebts(bytes32[] calldata currencyKeys)
        external
        view
        returns (uint[] memory debtValues, bool anyRateIsInvalid);

    function cachedSynthDebts(bytes32[] calldata currencyKeys) external view returns (uint[] memory debtValues);

    function currentDebt() external view returns (uint debt, bool anyRateIsInvalid);

    function cacheInfo()
        external
        view
        returns (
            uint debt,
            uint timestamp,
            bool isInvalid,
            bool isStale
        );

    // Mutative functions

    function takeDebtSnapshot() external;

    function updateCachedSynthDebts(bytes32[] calldata currencyKeys) external;
}


// 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);
}


/**
 * @dev Interface of the ERC20 standard as defined in the EIP. Does not include
 * the optional functions; to access them see `ERC20Detailed`.
 */
interface IERC20 {
    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `recipient`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a `Transfer` event.
     */
    function transfer(address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through `transferFrom`. This is
     * zero by default.
     *
     * This value changes when `approve` or `transferFrom` are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * > Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an `Approval` event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `sender` to `recipient` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a `Transfer` event.
     */
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to `approve`. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);
}


// Inheritance


// Libraries


// Internal references


// Note: use OZ's IERC20 here as using ours will complain about conflicting names
// during the build (VirtualSynth has IERC20 from the OZ ERC20 implementation)


// Used to have strongly-typed access to internal mutative functions in Synthetix
interface ISynthetixInternal {
    function emitExchangeTracking(
        bytes32 trackingCode,
        bytes32 toCurrencyKey,
        uint256 toAmount
    ) external;

    function emitSynthExchange(
        address account,
        bytes32 fromCurrencyKey,
        uint fromAmount,
        bytes32 toCurrencyKey,
        uint toAmount,
        address toAddress
    ) external;

    function emitExchangeReclaim(
        address account,
        bytes32 currencyKey,
        uint amount
    ) external;

    function emitExchangeRebate(
        address account,
        bytes32 currencyKey,
        uint amount
    ) external;
}


interface IExchangerInternalDebtCache {
    function updateCachedSynthDebtsWithRates(bytes32[] calldata currencyKeys, uint[] calldata currencyRates) external;

    function updateCachedSynthDebts(bytes32[] calldata currencyKeys) external;
}


// https://docs.synthetix.io/contracts/source/contracts/exchanger
contract Exchanger is Owned, MixinSystemSettings, IExchanger {
    using SafeMath for uint;
    using SafeDecimalMath for uint;

    struct ExchangeEntrySettlement {
        bytes32 src;
        uint amount;
        bytes32 dest;
        uint reclaim;
        uint rebate;
        uint srcRoundIdAtPeriodEnd;
        uint destRoundIdAtPeriodEnd;
        uint timestamp;
    }

    bytes32 private constant sUSD = "sUSD";

    // SIP-65: Decentralized circuit breaker
    uint public constant CIRCUIT_BREAKER_SUSPENSION_REASON = 65;

    mapping(bytes32 => uint) public lastExchangeRate;

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

    bytes32 private constant CONTRACT_SYSTEMSTATUS = "SystemStatus";
    bytes32 private constant CONTRACT_EXCHANGESTATE = "ExchangeState";
    bytes32 private constant CONTRACT_EXRATES = "ExchangeRates";
    bytes32 private constant CONTRACT_SYNTHETIX = "Synthetix";
    bytes32 private constant CONTRACT_FEEPOOL = "FeePool";
    bytes32 private constant CONTRACT_TRADING_REWARDS = "TradingRewards";
    bytes32 private constant CONTRACT_DELEGATEAPPROVALS = "DelegateApprovals";
    bytes32 private constant CONTRACT_ISSUER = "Issuer";
    bytes32 private constant CONTRACT_DEBTCACHE = "DebtCache";

    constructor(address _owner, address _resolver) public Owned(_owner) MixinSystemSettings(_resolver) {}

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

    function resolverAddressesRequired() public view returns (bytes32[] memory addresses) {
        bytes32[] memory existingAddresses = MixinSystemSettings.resolverAddressesRequired();
        bytes32[] memory newAddresses = new bytes32[](9);
        newAddresses[0] = CONTRACT_SYSTEMSTATUS;
        newAddresses[1] = CONTRACT_EXCHANGESTATE;
        newAddresses[2] = CONTRACT_EXRATES;
        newAddresses[3] = CONTRACT_SYNTHETIX;
        newAddresses[4] = CONTRACT_FEEPOOL;
        newAddresses[5] = CONTRACT_TRADING_REWARDS;
        newAddresses[6] = CONTRACT_DELEGATEAPPROVALS;
        newAddresses[7] = CONTRACT_ISSUER;
        newAddresses[8] = CONTRACT_DEBTCACHE;
        addresses = combineArrays(existingAddresses, newAddresses);
    }

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

    function exchangeState() internal view returns (IExchangeState) {
        return IExchangeState(requireAndGetAddress(CONTRACT_EXCHANGESTATE));
    }

    function exchangeRates() internal view returns (IExchangeRates) {
        return IExchangeRates(requireAndGetAddress(CONTRACT_EXRATES));
    }

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

    function feePool() internal view returns (IFeePool) {
        return IFeePool(requireAndGetAddress(CONTRACT_FEEPOOL));
    }

    function tradingRewards() internal view returns (ITradingRewards) {
        return ITradingRewards(requireAndGetAddress(CONTRACT_TRADING_REWARDS));
    }

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

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

    function debtCache() internal view returns (IExchangerInternalDebtCache) {
        return IExchangerInternalDebtCache(requireAndGetAddress(CONTRACT_DEBTCACHE));
    }

    function maxSecsLeftInWaitingPeriod(address account, bytes32 currencyKey) public view returns (uint) {
        return secsLeftInWaitingPeriodForExchange(exchangeState().getMaxTimestamp(account, currencyKey));
    }

    function waitingPeriodSecs() external view returns (uint) {
        return getWaitingPeriodSecs();
    }

    function tradingRewardsEnabled() external view returns (bool) {
        return getTradingRewardsEnabled();
    }

    function priceDeviationThresholdFactor() external view returns (uint) {
        return getPriceDeviationThresholdFactor();
    }

    function settlementOwing(address account, bytes32 currencyKey)
        public
        view
        returns (
            uint reclaimAmount,
            uint rebateAmount,
            uint numEntries
        )
    {
        (reclaimAmount, rebateAmount, numEntries, ) = _settlementOwing(account, currencyKey);
    }

    // Internal function to emit events for each individual rebate and reclaim entry
    function _settlementOwing(address account, bytes32 currencyKey)
        internal
        view
        returns (
            uint reclaimAmount,
            uint rebateAmount,
            uint numEntries,
            ExchangeEntrySettlement[] memory
        )
    {
        // Need to sum up all reclaim and rebate amounts for the user and the currency key
        numEntries = exchangeState().getLengthOfEntries(account, currencyKey);

        // For each unsettled exchange
        ExchangeEntrySettlement[] memory settlements = new ExchangeEntrySettlement[](numEntries);
        for (uint i = 0; i < numEntries; i++) {
            uint reclaim;
            uint rebate;
            // fetch the entry from storage
            IExchangeState.ExchangeEntry memory exchangeEntry = _getExchangeEntry(account, currencyKey, i);

            // determine the last round ids for src and dest pairs when period ended or latest if not over
            (uint srcRoundIdAtPeriodEnd, uint destRoundIdAtPeriodEnd) = getRoundIdsAtPeriodEnd(exchangeEntry);

            // given these round ids, determine what effective value they should have received
            uint destinationAmount = exchangeRates().effectiveValueAtRound(
                exchangeEntry.src,
                exchangeEntry.amount,
                exchangeEntry.dest,
                srcRoundIdAtPeriodEnd,
                destRoundIdAtPeriodEnd
            );

            // and deduct the fee from this amount using the exchangeFeeRate from storage
            uint amountShouldHaveReceived = _getAmountReceivedForExchange(destinationAmount, exchangeEntry.exchangeFeeRate);

            // SIP-65 settlements where the amount at end of waiting period is beyond the threshold, then
            // settle with no reclaim or rebate
            if (!_isDeviationAboveThreshold(exchangeEntry.amountReceived, amountShouldHaveReceived)) {
                if (exchangeEntry.amountReceived > amountShouldHaveReceived) {
                    // if they received more than they should have, add to the reclaim tally
                    reclaim = exchangeEntry.amountReceived.sub(amountShouldHaveReceived);
                    reclaimAmount = reclaimAmount.add(reclaim);
                } else if (amountShouldHaveReceived > exchangeEntry.amountReceived) {
                    // if less, add to the rebate tally
                    rebate = amountShouldHaveReceived.sub(exchangeEntry.amountReceived);
                    rebateAmount = rebateAmount.add(rebate);
                }
            }

            settlements[i] = ExchangeEntrySettlement({
                src: exchangeEntry.src,
                amount: exchangeEntry.amount,
                dest: exchangeEntry.dest,
                reclaim: reclaim,
                rebate: rebate,
                srcRoundIdAtPeriodEnd: srcRoundIdAtPeriodEnd,
                destRoundIdAtPeriodEnd: destRoundIdAtPeriodEnd,
                timestamp: exchangeEntry.timestamp
            });
        }

        return (reclaimAmount, rebateAmount, numEntries, settlements);
    }

    function _getExchangeEntry(
        address account,
        bytes32 currencyKey,
        uint index
    ) internal view returns (IExchangeState.ExchangeEntry memory) {
        (
            bytes32 src,
            uint amount,
            bytes32 dest,
            uint amountReceived,
            uint exchangeFeeRate,
            uint timestamp,
            uint roundIdForSrc,
            uint roundIdForDest
        ) = exchangeState().getEntryAt(account, currencyKey, index);

        return
            IExchangeState.ExchangeEntry({
                src: src,
                amount: amount,
                dest: dest,
                amountReceived: amountReceived,
                exchangeFeeRate: exchangeFeeRate,
                timestamp: timestamp,
                roundIdForSrc: roundIdForSrc,
                roundIdForDest: roundIdForDest
            });
    }

    function hasWaitingPeriodOrSettlementOwing(address account, bytes32 currencyKey) external view returns (bool) {
        if (maxSecsLeftInWaitingPeriod(account, currencyKey) != 0) {
            return true;
        }

        (uint reclaimAmount, , , ) = _settlementOwing(account, currencyKey);

        return reclaimAmount > 0;
    }

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

    function calculateAmountAfterSettlement(
        address from,
        bytes32 currencyKey,
        uint amount,
        uint refunded
    ) public view returns (uint amountAfterSettlement) {
        amountAfterSettlement = amount;

        // balance of a synth will show an amount after settlement
        uint balanceOfSourceAfterSettlement = IERC20(address(issuer().synths(currencyKey))).balanceOf(from);

        // when there isn't enough supply (either due to reclamation settlement or because the number is too high)
        if (amountAfterSettlement > balanceOfSourceAfterSettlement) {
            // then the amount to exchange is reduced to their remaining supply
            amountAfterSettlement = balanceOfSourceAfterSettlement;
        }

        if (refunded > 0) {
            amountAfterSettlement = amountAfterSettlement.add(refunded);
        }
    }

    function isSynthRateInvalid(bytes32 currencyKey) external view returns (bool) {
        return _isSynthRateInvalid(currencyKey, exchangeRates().rateForCurrency(currencyKey));
    }

    /* ========== MUTATIVE FUNCTIONS ========== */
    function exchange(
        address from,
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey,
        address destinationAddress
    ) external onlySynthetixorSynth returns (uint amountReceived) {
        uint fee;
        (amountReceived, fee, ) = _exchange(
            from,
            sourceCurrencyKey,
            sourceAmount,
            destinationCurrencyKey,
            destinationAddress,
            false
        );

        _processTradingRewards(fee, destinationAddress);
    }

    function exchangeOnBehalf(
        address exchangeForAddress,
        address from,
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey
    ) external onlySynthetixorSynth returns (uint amountReceived) {
        require(delegateApprovals().canExchangeFor(exchangeForAddress, from), "Not approved to act on behalf");

        uint fee;
        (amountReceived, fee, ) = _exchange(
            exchangeForAddress,
            sourceCurrencyKey,
            sourceAmount,
            destinationCurrencyKey,
            exchangeForAddress,
            false
        );

        _processTradingRewards(fee, exchangeForAddress);
    }

    function exchangeWithTracking(
        address from,
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey,
        address destinationAddress,
        address originator,
        bytes32 trackingCode
    ) external onlySynthetixorSynth returns (uint amountReceived) {
        uint fee;
        (amountReceived, fee, ) = _exchange(
            from,
            sourceCurrencyKey,
            sourceAmount,
            destinationCurrencyKey,
            destinationAddress,
            false
        );

        _processTradingRewards(fee, originator);

        _emitTrackingEvent(trackingCode, destinationCurrencyKey, amountReceived);
    }

    function exchangeOnBehalfWithTracking(
        address exchangeForAddress,
        address from,
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey,
        address originator,
        bytes32 trackingCode
    ) external onlySynthetixorSynth returns (uint amountReceived) {
        require(delegateApprovals().canExchangeFor(exchangeForAddress, from), "Not approved to act on behalf");

        uint fee;
        (amountReceived, fee, ) = _exchange(
            exchangeForAddress,
            sourceCurrencyKey,
            sourceAmount,
            destinationCurrencyKey,
            exchangeForAddress,
            false
        );

        _processTradingRewards(fee, originator);

        _emitTrackingEvent(trackingCode, destinationCurrencyKey, amountReceived);
    }

    function exchangeWithVirtual(
        address from,
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey,
        address destinationAddress,
        bytes32 trackingCode
    ) external onlySynthetixorSynth returns (uint amountReceived, IVirtualSynth vSynth) {
        uint fee;
        (amountReceived, fee, vSynth) = _exchange(
            from,
            sourceCurrencyKey,
            sourceAmount,
            destinationCurrencyKey,
            destinationAddress,
            true
        );

        _processTradingRewards(fee, destinationAddress);

        if (trackingCode != bytes32(0)) {
            _emitTrackingEvent(trackingCode, destinationCurrencyKey, amountReceived);
        }
    }

    function _emitTrackingEvent(
        bytes32 trackingCode,
        bytes32 toCurrencyKey,
        uint256 toAmount
    ) internal {
        ISynthetixInternal(address(synthetix())).emitExchangeTracking(trackingCode, toCurrencyKey, toAmount);
    }

    function _processTradingRewards(uint fee, address originator) internal {
        if (fee > 0 && originator != address(0) && getTradingRewardsEnabled()) {
            tradingRewards().recordExchangeFeeForAccount(fee, originator);
        }
    }

    function _suspendIfRateInvalid(bytes32 currencyKey, uint rate) internal returns (bool circuitBroken) {
        if (_isSynthRateInvalid(currencyKey, rate)) {
            systemStatus().suspendSynth(currencyKey, CIRCUIT_BREAKER_SUSPENSION_REASON);
            circuitBroken = true;
        } else {
            lastExchangeRate[currencyKey] = rate;
        }
    }

    function _updateSNXIssuedDebtOnExchange(bytes32[2] memory currencyKeys, uint[2] memory currencyRates) internal {
        bool includesSUSD = currencyKeys[0] == sUSD || currencyKeys[1] == sUSD;
        uint numKeys = includesSUSD ? 2 : 3;

        bytes32[] memory keys = new bytes32[](numKeys);
        keys[0] = currencyKeys[0];
        keys[1] = currencyKeys[1];

        uint[] memory rates = new uint[](numKeys);
        rates[0] = currencyRates[0];
        rates[1] = currencyRates[1];

        if (!includesSUSD) {
            keys[2] = sUSD; // And we'll also update sUSD to account for any fees if it wasn't one of the exchanged currencies
            rates[2] = SafeDecimalMath.unit();
        }

        // Note that exchanges can't invalidate the debt cache, since if a rate is invalid,
        // the exchange will have failed already.
        debtCache().updateCachedSynthDebtsWithRates(keys, rates);
    }

    function _settleAndCalcSourceAmountRemaining(
        uint sourceAmount,
        address from,
        bytes32 sourceCurrencyKey
    ) internal returns (uint sourceAmountAfterSettlement) {
        (, uint refunded, uint numEntriesSettled) = _internalSettle(from, sourceCurrencyKey, false);

        sourceAmountAfterSettlement = sourceAmount;

        // when settlement was required
        if (numEntriesSettled > 0) {
            // ensure the sourceAmount takes this into account
            sourceAmountAfterSettlement = calculateAmountAfterSettlement(from, sourceCurrencyKey, sourceAmount, refunded);
        }
    }

    function _exchange(
        address from,
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey,
        address destinationAddress,
        bool virtualSynth
    )
        internal
        returns (
            uint amountReceived,
            uint fee,
            IVirtualSynth vSynth
        )
    {
        _ensureCanExchange(sourceCurrencyKey, sourceAmount, destinationCurrencyKey);

        uint sourceAmountAfterSettlement = _settleAndCalcSourceAmountRemaining(sourceAmount, from, sourceCurrencyKey);

        // If, after settlement the user has no balance left (highly unlikely), then return to prevent
        // emitting events of 0 and don't revert so as to ensure the settlement queue is emptied
        if (sourceAmountAfterSettlement == 0) {
            return (0, 0, IVirtualSynth(0));
        }

        uint exchangeFeeRate;
        uint sourceRate;
        uint destinationRate;

        // Note: `fee` is denominated in the destinationCurrencyKey.
        (amountReceived, fee, exchangeFeeRate, sourceRate, destinationRate) = _getAmountsForExchangeMinusFees(
            sourceAmountAfterSettlement,
            sourceCurrencyKey,
            destinationCurrencyKey
        );

        // SIP-65: Decentralized Circuit Breaker
        if (
            _suspendIfRateInvalid(sourceCurrencyKey, sourceRate) ||
            _suspendIfRateInvalid(destinationCurrencyKey, destinationRate)
        ) {
            return (0, 0, IVirtualSynth(0));
        }

        // Note: We don't need to check their balance as the burn() below will do a safe subtraction which requires
        // the subtraction to not overflow, which would happen if their balance is not sufficient.

        vSynth = _convert(
            sourceCurrencyKey,
            from,
            sourceAmountAfterSettlement,
            destinationCurrencyKey,
            amountReceived,
            destinationAddress,
            virtualSynth
        );

        // When using a virtual synth, it becomes the destinationAddress for event and settlement tracking
        if (vSynth != IVirtualSynth(0)) {
            destinationAddress = address(vSynth);
        }

        // Remit the fee if required
        if (fee > 0) {
            // Normalize fee to sUSD
            // Note: `fee` is being reused to avoid stack too deep errors.
            fee = exchangeRates().effectiveValue(destinationCurrencyKey, fee, sUSD);

            // Remit the fee in sUSDs
            issuer().synths(sUSD).issue(feePool().FEE_ADDRESS(), fee);

            // Tell the fee pool about this
            feePool().recordFeePaid(fee);
        }

        // Note: As of this point, `fee` is denominated in sUSD.

        // Nothing changes as far as issuance data goes because the total value in the system hasn't changed.
        // But we will update the debt snapshot in case exchange rates have fluctuated since the last exchange
        // in these currencies
        _updateSNXIssuedDebtOnExchange([sourceCurrencyKey, destinationCurrencyKey], [sourceRate, destinationRate]);

        // Let the DApps know there was a Synth exchange
        ISynthetixInternal(address(synthetix())).emitSynthExchange(
            from,
            sourceCurrencyKey,
            sourceAmountAfterSettlement,
            destinationCurrencyKey,
            amountReceived,
            destinationAddress
        );

        // persist the exchange information for the dest key
        appendExchange(
            destinationAddress,
            sourceCurrencyKey,
            sourceAmountAfterSettlement,
            destinationCurrencyKey,
            amountReceived,
            exchangeFeeRate
        );
    }

    function _convert(
        bytes32 sourceCurrencyKey,
        address from,
        uint sourceAmountAfterSettlement,
        bytes32 destinationCurrencyKey,
        uint amountReceived,
        address recipient,
        bool virtualSynth
    ) internal returns (IVirtualSynth vSynth) {
        // Burn the source amount
        issuer().synths(sourceCurrencyKey).burn(from, sourceAmountAfterSettlement);

        // Issue their new synths
        ISynth dest = issuer().synths(destinationCurrencyKey);

        if (virtualSynth) {
            Proxyable synth = Proxyable(address(dest));
            vSynth = _createVirtualSynth(IERC20(address(synth.proxy())), recipient, amountReceived, destinationCurrencyKey);
            dest.issue(address(vSynth), amountReceived);
        } else {
            dest.issue(recipient, amountReceived);
        }
    }

    function _createVirtualSynth(
        IERC20,
        address,
        uint,
        bytes32
    ) internal returns (IVirtualSynth) {
        revert("Cannot be run on this layer");
    }

    // Note: this function can intentionally be called by anyone on behalf of anyone else (the caller just pays the gas)
    function settle(address from, bytes32 currencyKey)
        external
        returns (
            uint reclaimed,
            uint refunded,
            uint numEntriesSettled
        )
    {
        systemStatus().requireSynthActive(currencyKey);
        return _internalSettle(from, currencyKey, true);
    }

    function suspendSynthWithInvalidRate(bytes32 currencyKey) external {
        systemStatus().requireSystemActive();
        require(issuer().synths(currencyKey) != ISynth(0), "No such synth");
        require(_isSynthRateInvalid(currencyKey, exchangeRates().rateForCurrency(currencyKey)), "Synth price is valid");
        systemStatus().suspendSynth(currencyKey, CIRCUIT_BREAKER_SUSPENSION_REASON);
    }

    // SIP-78
    function setLastExchangeRateForSynth(bytes32 currencyKey, uint rate) external onlyExchangeRates {
        require(rate > 0, "Rate must be above 0");
        lastExchangeRate[currencyKey] = rate;
    }

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

    function _ensureCanExchange(
        bytes32 sourceCurrencyKey,
        uint sourceAmount,
        bytes32 destinationCurrencyKey
    ) internal view {
        require(sourceCurrencyKey != destinationCurrencyKey, "Can't be same synth");
        require(sourceAmount > 0, "Zero amount");

        bytes32[] memory synthKeys = new bytes32[](2);
        synthKeys[0] = sourceCurrencyKey;
        synthKeys[1] = destinationCurrencyKey;
        require(!exchangeRates().anyRateIsInvalid(synthKeys), "Src/dest rate invalid or not found");
    }

    function _isSynthRateInvalid(bytes32 currencyKey, uint currentRate) internal view returns (bool) {
        if (currentRate == 0) {
            return true;
        }

        uint lastRateFromExchange = lastExchangeRate[currencyKey];

        if (lastRateFromExchange > 0) {
            return _isDeviationAboveThreshold(lastRateFromExchange, currentRate);
        }

        // if no last exchange for this synth, then we need to look up last 3 rates (+1 for current rate)
        (uint[] memory rates, ) = exchangeRates().ratesAndUpdatedTimeForCurrencyLastNRounds(currencyKey, 4);

        // start at index 1 to ignore current rate
        for (uint i = 1; i < rates.length; i++) {
            // ignore any empty rates in the past (otherwise we will never be able to get validity)
            if (rates[i] > 0 && _isDeviationAboveThreshold(rates[i], currentRate)) {
                return true;
            }
        }

        return false;
    }

    function _isDeviationAboveThreshold(uint base, uint comparison) internal view returns (bool) {
        if (base == 0 || comparison == 0) {
            return true;
        }

        uint factor;
        if (comparison > base) {
            factor = comparison.divideDecimal(base);
        } else {
            factor = base.divideDecimal(comparison);
        }

        return factor >= getPriceDeviationThresholdFactor();
    }

    function _internalSettle(
        address from,
        bytes32 currencyKey,
        bool updateCache
    )
        internal
        returns (
            uint reclaimed,
            uint refunded,
            uint numEntriesSettled
        )
    {
        require(maxSecsLeftInWaitingPeriod(from, currencyKey) == 0, "Cannot settle during waiting period");

        (
            uint reclaimAmount,
            uint rebateAmount,
            uint entries,
            ExchangeEntrySettlement[] memory settlements
        ) = _settlementOwing(from, currencyKey);

        if (reclaimAmount > rebateAmount) {
            reclaimed = reclaimAmount.sub(rebateAmount);
            reclaim(from, currencyKey, reclaimed);
        } else if (rebateAmount > reclaimAmount) {
            refunded = rebateAmount.sub(reclaimAmount);
            refund(from, currencyKey, refunded);
        }

        if (updateCache) {
            bytes32[] memory key = new bytes32[](1);
            key[0] = currencyKey;
            debtCache().updateCachedSynthDebts(key);
        }

        // emit settlement event for each settled exchange entry
        for (uint i = 0; i < settlements.length; i++) {
            emit ExchangeEntrySettled(
                from,
                settlements[i].src,
                settlements[i].amount,
                settlements[i].dest,
                settlements[i].reclaim,
                settlements[i].rebate,
                settlements[i].srcRoundIdAtPeriodEnd,
                settlements[i].destRoundIdAtPeriodEnd,
                settlements[i].timestamp
            );
        }

        numEntriesSettled = entries;

        // Now remove all entries, even if no reclaim and no rebate
        exchangeState().removeEntries(from, currencyKey);
    }

    function reclaim(
        address from,
        bytes32 currencyKey,
        uint amount
    ) internal {
        // burn amount from user
        issuer().synths(currencyKey).burn(from, amount);
        ISynthetixInternal(address(synthetix())).emitExchangeReclaim(from, currencyKey, amount);
    }

    function refund(
        address from,
        bytes32 currencyKey,
        uint amount
    ) internal {
        // issue amount to user
        issuer().synths(currencyKey).issue(from, amount);
        ISynthetixInternal(address(synthetix())).emitExchangeRebate(from, currencyKey, amount);
    }

    function secsLeftInWaitingPeriodForExchange(uint timestamp) internal view returns (uint) {
        uint _waitingPeriodSecs = getWaitingPeriodSecs();
        if (timestamp == 0 || now >= timestamp.add(_waitingPeriodSecs)) {
            return 0;
        }

        return timestamp.add(_waitingPeriodSecs).sub(now);
    }

    function feeRateForExchange(bytes32 sourceCurrencyKey, bytes32 destinationCurrencyKey)
        external
        view
        returns (uint exchangeFeeRate)
    {
        exchangeFeeRate = _feeRateForExchange(sourceCurrencyKey, destinationCurrencyKey);
    }

    function _feeRateForExchange(bytes32 sourceCurrencyKey, bytes32 destinationCurrencyKey)
        internal
        view
        returns (uint exchangeFeeRate)
    {
        // Get the exchange fee rate as per destination currencyKey
        exchangeFeeRate = getExchangeFeeRate(destinationCurrencyKey);

        if (sourceCurrencyKey == sUSD || destinationCurrencyKey == sUSD) {
            return exchangeFeeRate;
        }

        // Is this a swing trade? long to short or short to long skipping sUSD.
        if (
            (sourceCurrencyKey[0] == 0x73 && destinationCurrencyKey[0] == 0x69) ||
            (sourceCurrencyKey[0] == 0x69 && destinationCurrencyKey[0] == 0x73)
        ) {
            // Double the exchange fee
            exchangeFeeRate = exchangeFeeRate.mul(2);
        }

        return exchangeFeeRate;
    }

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

    function _getAmountsForExchangeMinusFees(
        uint sourceAmount,
        bytes32 sourceCurrencyKey,
        bytes32 destinationCurrencyKey
    )
        internal
        view
        returns (
            uint amountReceived,
            uint fee,
            uint exchangeFeeRate,
            uint sourceRate,
            uint destinationRate
        )
    {
        uint destinationAmount;
        (destinationAmount, sourceRate, destinationRate) = exchangeRates().effectiveValueAndRates(
            sourceCurrencyKey,
            sourceAmount,
            destinationCurrencyKey
        );
        exchangeFeeRate = _feeRateForExchange(sourceCurrencyKey, destinationCurrencyKey);
        amountReceived = _getAmountReceivedForExchange(destinationAmount, exchangeFeeRate);
        fee = destinationAmount.sub(amountReceived);
    }

    function _getAmountReceivedForExchange(uint destinationAmount, uint exchangeFeeRate)
        internal
        pure
        returns (uint amountReceived)
    {
        amountReceived = destinationAmount.multiplyDecimal(SafeDecimalMath.unit().sub(exchangeFeeRate));
    }

    function appendExchange(
        address account,
        bytes32 src,
        uint amount,
        bytes32 dest,
        uint amountReceived,
        uint exchangeFeeRate
    ) internal {
        IExchangeRates exRates = exchangeRates();
        uint roundIdForSrc = exRates.getCurrentRoundId(src);
        uint roundIdForDest = exRates.getCurrentRoundId(dest);
        exchangeState().appendExchangeEntry(
            account,
            src,
            amount,
            dest,
            amountReceived,
            exchangeFeeRate,
            now,
            roundIdForSrc,
            roundIdForDest
        );

        emit ExchangeEntryAppended(
            account,
            src,
            amount,
            dest,
            amountReceived,
            exchangeFeeRate,
            roundIdForSrc,
            roundIdForDest
        );
    }

    function getRoundIdsAtPeriodEnd(IExchangeState.ExchangeEntry memory exchangeEntry)
        internal
        view
        returns (uint srcRoundIdAtPeriodEnd, uint destRoundIdAtPeriodEnd)
    {
        IExchangeRates exRates = exchangeRates();
        uint _waitingPeriodSecs = getWaitingPeriodSecs();

        srcRoundIdAtPeriodEnd = exRates.getLastRoundIdBeforeElapsedSecs(
            exchangeEntry.src,
            exchangeEntry.roundIdForSrc,
            exchangeEntry.timestamp,
            _waitingPeriodSecs
        );
        destRoundIdAtPeriodEnd = exRates.getLastRoundIdBeforeElapsedSecs(
            exchangeEntry.dest,
            exchangeEntry.roundIdForDest,
            exchangeEntry.timestamp,
            _waitingPeriodSecs
        );
    }

    // ========== MODIFIERS ==========

    modifier onlySynthetixorSynth() {
        ISynthetix _synthetix = synthetix();
        require(
            msg.sender == address(_synthetix) || _synthetix.synthsByAddress(msg.sender) != bytes32(0),
            "Exchanger: Only synthetix or a synth contract can perform this action"
        );
        _;
    }

    modifier onlyExchangeRates() {
        IExchangeRates _exchangeRates = exchangeRates();
        require(msg.sender == address(_exchangeRates), "Restricted to ExchangeRates");
        _;
    }

    // ========== EVENTS ==========
    event ExchangeEntryAppended(
        address indexed account,
        bytes32 src,
        uint256 amount,
        bytes32 dest,
        uint256 amountReceived,
        uint256 exchangeFeeRate,
        uint256 roundIdForSrc,
        uint256 roundIdForDest
    );

    event ExchangeEntrySettled(
        address indexed from,
        bytes32 src,
        uint256 amount,
        bytes32 dest,
        uint256 reclaim,
        uint256 rebate,
        uint256 srcRoundIdAtPeriodEnd,
        uint256 destRoundIdAtPeriodEnd,
        uint256 exchangeTimestamp
    );
}


/**
 * @dev Implementation of the `IERC20` interface.
 *
 * This implementation is agnostic to the way tokens are created. This means
 * that a supply mechanism has to be added in a derived contract using `_mint`.
 * For a generic mechanism see `ERC20Mintable`.
 *
 * *For a detailed writeup see our guide [How to implement supply
 * mechanisms](https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226).*
 *
 * We have followed general OpenZeppelin guidelines: functions revert instead
 * of returning `false` on failure. This behavior is nonetheless conventional
 * and does not conflict with the expectations of ERC20 applications.
 *
 * Additionally, an `Approval` event is emitted on calls to `transferFrom`.
 * This allows applications to reconstruct the allowance for all accounts just
 * by listening to said events. Other implementations of the EIP may not emit
 * these events, as it isn't required by the specification.
 *
 * Finally, the non-standard `decreaseAllowance` and `increaseAllowance`
 * functions have been added to mitigate the well-known issues around setting
 * allowances. See `IERC20.approve`.
 */
contract ERC20 is IERC20 {
    using SafeMath for uint256;

    mapping (address => uint256) private _balances;

    mapping (address => mapping (address => uint256)) private _allowances;

    uint256 private _totalSupply;

    /**
     * @dev See `IERC20.totalSupply`.
     */
    function totalSupply() public view returns (uint256) {
        return _totalSupply;
    }

    /**
     * @dev See `IERC20.balanceOf`.
     */
    function balanceOf(address account) public view returns (uint256) {
        return _balances[account];
    }

    /**
     * @dev See `IERC20.transfer`.
     *
     * Requirements:
     *
     * - `recipient` cannot be the zero address.
     * - the caller must have a balance of at least `amount`.
     */
    function transfer(address recipient, uint256 amount) public returns (bool) {
        _transfer(msg.sender, recipient, amount);
        return true;
    }

    /**
     * @dev See `IERC20.allowance`.
     */
    function allowance(address owner, address spender) public view returns (uint256) {
        return _allowances[owner][spender];
    }

    /**
     * @dev See `IERC20.approve`.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function approve(address spender, uint256 value) public returns (bool) {
        _approve(msg.sender, spender, value);
        return true;
    }

    /**
     * @dev See `IERC20.transferFrom`.
     *
     * Emits an `Approval` event indicating the updated allowance. This is not
     * required by the EIP. See the note at the beginning of `ERC20`;
     *
     * Requirements:
     * - `sender` and `recipient` cannot be the zero address.
     * - `sender` must have a balance of at least `value`.
     * - the caller must have allowance for `sender`'s tokens of at least
     * `amount`.
     */
    function transferFrom(address sender, address recipient, uint256 amount) public returns (bool) {
        _transfer(sender, recipient, amount);
        _approve(sender, msg.sender, _allowances[sender][msg.sender].sub(amount));
        return true;
    }

    /**
     * @dev Atomically increases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to `approve` that can be used as a mitigation for
     * problems described in `IERC20.approve`.
     *
     * Emits an `Approval` event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
        _approve(msg.sender, spender, _allowances[msg.sender][spender].add(addedValue));
        return true;
    }

    /**
     * @dev Atomically decreases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to `approve` that can be used as a mitigation for
     * problems described in `IERC20.approve`.
     *
     * Emits an `Approval` event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `spender` must have allowance for the caller of at least
     * `subtractedValue`.
     */
    function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
        _approve(msg.sender, spender, _allowances[msg.sender][spender].sub(subtractedValue));
        return true;
    }

    /**
     * @dev Moves tokens `amount` from `sender` to `recipient`.
     *
     * This is internal function is equivalent to `transfer`, and can be used to
     * e.g. implement automatic token fees, slashing mechanisms, etc.
     *
     * Emits a `Transfer` event.
     *
     * Requirements:
     *
     * - `sender` cannot be the zero address.
     * - `recipient` cannot be the zero address.
     * - `sender` must have a balance of at least `amount`.
     */
    function _transfer(address sender, address recipient, uint256 amount) internal {
        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");

        _balances[sender] = _balances[sender].sub(amount);
        _balances[recipient] = _balances[recipient].add(amount);
        emit Transfer(sender, recipient, amount);
    }

    /** @dev Creates `amount` tokens and assigns them to `account`, increasing
     * the total supply.
     *
     * Emits a `Transfer` event with `from` set to the zero address.
     *
     * Requirements
     *
     * - `to` cannot be the zero address.
     */
    function _mint(address account, uint256 amount) internal {
        require(account != address(0), "ERC20: mint to the zero address");

        _totalSupply = _totalSupply.add(amount);
        _balances[account] = _balances[account].add(amount);
        emit Transfer(address(0), account, amount);
    }

     /**
     * @dev Destoys `amount` tokens from `account`, reducing the
     * total supply.
     *
     * Emits a `Transfer` event with `to` set to the zero address.
     *
     * Requirements
     *
     * - `account` cannot be the zero address.
     * - `account` must have at least `amount` tokens.
     */
    function _burn(address account, uint256 value) internal {
        require(account != address(0), "ERC20: burn from the zero address");

        _totalSupply = _totalSupply.sub(value);
        _balances[account] = _balances[account].sub(value);
        emit Transfer(account, address(0), value);
    }

    /**
     * @dev Sets `amount` as the allowance of `spender` over the `owner`s tokens.
     *
     * This is internal function is equivalent to `approve`, and can be used to
     * e.g. set automatic allowances for certain subsystems, etc.
     *
     * Emits an `Approval` event.
     *
     * Requirements:
     *
     * - `owner` cannot be the zero address.
     * - `spender` cannot be the zero address.
     */
    function _approve(address owner, address spender, uint256 value) internal {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");

        _allowances[owner][spender] = value;
        emit Approval(owner, spender, value);
    }

    /**
     * @dev Destoys `amount` tokens from `account`.`amount` is then deducted
     * from the caller's allowance.
     *
     * See `_burn` and `_approve`.
     */
    function _burnFrom(address account, uint256 amount) internal {
        _burn(account, amount);
        _approve(account, msg.sender, _allowances[account][msg.sender].sub(amount));
    }
}


// Inheritance


// Libraries


// Internal references


// Note: use OZ's IERC20 here as using ours will complain about conflicting names
// during the build


// https://docs.synthetix.io/contracts/source/contracts/virtualsynth
contract VirtualSynth is ERC20, IVirtualSynth {
    using SafeMath for uint;
    using SafeDecimalMath for uint;

    IERC20 public synth;
    IAddressResolver public resolver;

    bool public settled = false;

    uint8 public constant decimals = 18;

    // track initial supply so we can calculate the rate even after all supply is burned
    uint public initialSupply;

    // track final settled amount of the synth so we can calculate the rate after settlement
    uint public settledAmount;

    bytes32 public currencyKey;

    constructor(
        IERC20 _synth,
        IAddressResolver _resolver,
        address _recipient,
        uint _amount,
        bytes32 _currencyKey
    ) public ERC20() {
        synth = _synth;
        resolver = _resolver;
        currencyKey = _currencyKey;

        // Assumption: the synth will be issued to us within the same transaction,
        // and this supply matches that
        _mint(_recipient, _amount);

        initialSupply = _amount;
    }

    // INTERNALS

    function exchanger() internal view returns (IExchanger) {
        return IExchanger(resolver.requireAndGetAddress("Exchanger", "Exchanger contract not found"));
    }

    function secsLeft() internal view returns (uint) {
        return exchanger().maxSecsLeftInWaitingPeriod(address(this), currencyKey);
    }

    function calcRate() internal view returns (uint) {
        if (initialSupply == 0) {
            return 0;
        }

        uint synthBalance;

        if (!settled) {
            synthBalance = IERC20(address(synth)).balanceOf(address(this));
            (uint reclaim, uint rebate, ) = exchanger().settlementOwing(address(this), currencyKey);

            if (reclaim > 0) {
                synthBalance = synthBalance.sub(reclaim);
            } else if (rebate > 0) {
                synthBalance = synthBalance.add(rebate);
            }
        } else {
            synthBalance = settledAmount;
        }

        return synthBalance.divideDecimalRound(initialSupply);
    }

    function balanceUnderlying(address account) internal view returns (uint) {
        uint vBalanceOfAccount = balanceOf(account);

        return vBalanceOfAccount.multiplyDecimalRound(calcRate());
    }

    function settleSynth() internal {
        if (settled) {
            return;
        }
        settled = true;

        exchanger().settle(address(this), currencyKey);

        settledAmount = IERC20(address(synth)).balanceOf(address(this));

        emit Settled(totalSupply(), settledAmount);
    }

    // VIEWS

    function name() external view returns (string memory) {
        return string(abi.encodePacked("Virtual Synth ", currencyKey));
    }

    function symbol() external view returns (string memory) {
        return string(abi.encodePacked("v", currencyKey));
    }

    // get the rate of the vSynth to the synth.
    function rate() external view returns (uint) {
        return calcRate();
    }

    // show the balance of the underlying synth that the given address has, given
    // their proportion of totalSupply
    function balanceOfUnderlying(address account) external view returns (uint) {
        return balanceUnderlying(account);
    }

    function secsLeftInWaitingPeriod() external view returns (uint) {
        return secsLeft();
    }

    function readyToSettle() external view returns (bool) {
        return secsLeft() == 0;
    }

    // PUBLIC FUNCTIONS

    // Perform settlement of the underlying exchange if required,
    // then burn the accounts vSynths and transfer them their owed balanceOfUnderlying
    function settle(address account) external {
        settleSynth();

        IERC20(address(synth)).transfer(account, balanceUnderlying(account));

        _burn(account, balanceOf(account));
    }

    event Settled(uint totalSupply, uint amountAfterSettled);
}


// Inheritance


// Internal references


// https://docs.synthetix.io/contracts/source/contracts/exchangerwithvirtualsynth
contract ExchangerWithVirtualSynth is Exchanger {
    constructor(address _owner, address _resolver) public Exchanger(_owner, _resolver) {}

    function _createVirtualSynth(
        IERC20 synth,
        address recipient,
        uint amount,
        bytes32 currencyKey
    ) internal returns (IVirtualSynth vSynth) {
        // prevent inverse synths from being allowed due to purgeability
        require(currencyKey[0] != 0x69, "Cannot virtualize this synth");

        vSynth = new VirtualSynth(synth, resolver, recipient, amount, currencyKey);
        emit VirtualSynthCreated(address(synth), recipient, address(vSynth), currencyKey, amount);
    }

    event VirtualSynthCreated(
        address indexed synth,
        address indexed recipient,
        address vSynth,
        bytes32 currencyKey,
        uint amount
    );
}

Contract ABI

[{"inputs":[{"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":true,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"bytes32","name":"src","type":"bytes32"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"bytes32","name":"dest","type":"bytes32"},{"indexed":false,"internalType":"uint256","name":"amountReceived","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"exchangeFeeRate","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"roundIdForSrc","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"roundIdForDest","type":"uint256"}],"name":"ExchangeEntryAppended","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"bytes32","name":"src","type":"bytes32"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"bytes32","name":"dest","type":"bytes32"},{"indexed":false,"internalType":"uint256","name":"reclaim","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"rebate","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"srcRoundIdAtPeriodEnd","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"destRoundIdAtPeriodEnd","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"exchangeTimestamp","type":"uint256"}],"name":"ExchangeEntrySettled","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":true,"internalType":"address","name":"synth","type":"address"},{"indexed":true,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"address","name":"vSynth","type":"address"},{"indexed":false,"internalType":"bytes32","name":"currencyKey","type":"bytes32"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"VirtualSynthCreated","type":"event"},{"constant":true,"inputs":[],"name":"CIRCUIT_BREAKER_SUSPENSION_REASON","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"acceptOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"bytes32","name":"currencyKey","type":"bytes32"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"refunded","type":"uint256"}],"name":"calculateAmountAfterSettlement","outputs":[{"internalType":"uint256","name":"amountAfterSettlement","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"bytes32","name":"sourceCurrencyKey","type":"bytes32"},{"internalType":"uint256","name":"sourceAmount","type":"uint256"},{"internalType":"bytes32","name":"destinationCurrencyKey","type":"bytes32"},{"internalType":"address","name":"destinationAddress","type":"address"}],"name":"exchange","outputs":[{"internalType":"uint256","name":"amountReceived","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"exchangeForAddress","type":"address"},{"internalType":"address","name":"from","type":"address"},{"internalType":"bytes32","name":"sourceCurrencyKey","type":"bytes32"},{"internalType":"uint256","name":"sourceAmount","type":"uint256"},{"internalType":"bytes32","name":"destinationCurrencyKey","type":"bytes32"}],"name":"exchangeOnBehalf","outputs":[{"internalType":"uint256","name":"amountReceived","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"exchangeForAddress","type":"address"},{"internalType":"address","name":"from","type":"address"},{"internalType":"bytes32","name":"sourceCurrencyKey","type":"bytes32"},{"internalType":"uint256","name":"sourceAmount","type":"uint256"},{"internalType":"bytes32","name":"destinationCurrencyKey","type":"bytes32"},{"internalType":"address","name":"originator","type":"address"},{"internalType":"bytes32","name":"trackingCode","type":"bytes32"}],"name":"exchangeOnBehalfWithTracking","outputs":[{"internalType":"uint256","name":"amountReceived","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"bytes32","name":"sourceCurrencyKey","type":"bytes32"},{"internalType":"uint256","name":"sourceAmount","type":"uint256"},{"internalType":"bytes32","name":"destinationCurrencyKey","type":"bytes32"},{"internalType":"address","name":"destinationAddress","type":"address"},{"internalType":"address","name":"originator","type":"address"},{"internalType":"bytes32","name":"trackingCode","type":"bytes32"}],"name":"exchangeWithTracking","outputs":[{"internalType":"uint256","name":"amountReceived","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"bytes32","name":"sourceCurrencyKey","type":"bytes32"},{"internalType":"uint256","name":"sourceAmount","type":"uint256"},{"internalType":"bytes32","name":"destinationCurrencyKey","type":"bytes32"},{"internalType":"address","name":"destinationAddress","type":"address"},{"internalType":"bytes32","name":"trackingCode","type":"bytes32"}],"name":"exchangeWithVirtual","outputs":[{"internalType":"uint256","name":"amountReceived","type":"uint256"},{"internalType":"contract IVirtualSynth","name":"vSynth","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"bytes32","name":"sourceCurrencyKey","type":"bytes32"},{"internalType":"bytes32","name":"destinationCurrencyKey","type":"bytes32"}],"name":"feeRateForExchange","outputs":[{"internalType":"uint256","name":"exchangeFeeRate","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"sourceAmount","type":"uint256"},{"internalType":"bytes32","name":"sourceCurrencyKey","type":"bytes32"},{"internalType":"bytes32","name":"destinationCurrencyKey","type":"bytes32"}],"name":"getAmountsForExchange","outputs":[{"internalType":"uint256","name":"amountReceived","type":"uint256"},{"internalType":"uint256","name":"fee","type":"uint256"},{"internalType":"uint256","name":"exchangeFeeRate","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"bytes32","name":"currencyKey","type":"bytes32"}],"name":"hasWaitingPeriodOrSettlementOwing","outputs":[{"internalType":"bool","name":"","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":[{"internalType":"bytes32","name":"currencyKey","type":"bytes32"}],"name":"isSynthRateInvalid","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"lastExchangeRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"bytes32","name":"currencyKey","type":"bytes32"}],"name":"maxSecsLeftInWaitingPeriod","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"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":"priceDeviationThresholdFactor","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"rebuildCache","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":"bytes32","name":"currencyKey","type":"bytes32"},{"internalType":"uint256","name":"rate","type":"uint256"}],"name":"setLastExchangeRateForSynth","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"bytes32","name":"currencyKey","type":"bytes32"}],"name":"settle","outputs":[{"internalType":"uint256","name":"reclaimed","type":"uint256"},{"internalType":"uint256","name":"refunded","type":"uint256"},{"internalType":"uint256","name":"numEntriesSettled","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"bytes32","name":"currencyKey","type":"bytes32"}],"name":"settlementOwing","outputs":[{"internalType":"uint256","name":"reclaimAmount","type":"uint256"},{"internalType":"uint256","name":"rebateAmount","type":"uint256"},{"internalType":"uint256","name":"numEntries","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"bytes32","name":"currencyKey","type":"bytes32"}],"name":"suspendSynthWithInvalidRate","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"tradingRewardsEnabled","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"waitingPeriodSecs","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]

60806040523480156200001157600080fd5b5060405162005b2738038062005b27833981810160405260408110156200003757600080fd5b50805160209091015181818080836001600160a01b038116620000a1576040805162461bcd60e51b815260206004820152601960248201527f4f776e657220616464726573732063616e6e6f74206265203000000000000000604482015290519081900360640190fd5b600080546001600160a01b0319166001600160a01b038316908117825560408051928352602083019190915280517fb532073b38c83145e3e5135377a08bf9aab55bc0fd7c1179cd4fb995d2a5159c9281900390910190a150600280546001600160a01b039092166001600160a01b031990921691909117905550505050506159f780620001306000396000f3fe60806040523480156200001157600080fd5b5060043610620001f05760003560e01c80636a1c475811620001115780638da5cb5b11620000a5578063d6f32e06116200007b578063d6f32e061462000591578063dfffca7614620005c0578063f39952241462000613578063f450aa34146200067d57620001f0565b80638da5cb5b1462000557578063c193f0d81462000561578063ce096940146200056b57620001f0565b80637dd1a57a11620000e75780637dd1a57a146200047e57806386baa45c146200049e5780638925711714620004f1578063899ffef414620004fb57620001f0565b80636a1c4758146200042557806374185360146200046a57806379ba5097146200047457620001f0565b80631b16802c11620001895780633fa70f45116200015f5780633fa70f4514620003b65780634c268fc814620003c057806353a47bb714620003fb57806357af302c146200040557620001f0565b80631b16802c146200035f5780632af64bd3146200038e578063372a395a14620003ac57620001f0565b80630b9e31c911620001cb5780630b9e31c914620002a15780631627540c14620002c357806319d5c66514620002ec5780631a5c6095146200033957620001f0565b806304f3bcec14620001f5578063059c29ec146200021b5780630a1e187d146200025c575b600080fd5b620001ff620006a9565b604080516001600160a01b039092168252519081900360200190f35b6200024a600480360360408110156200023357600080fd5b506001600160a01b038135169060200135620006b8565b60408051918252519081900360200190f35b6200024a600480360360a08110156200027457600080fd5b506001600160a01b0381358116916020810135916040820135916060810135916080909101351662000763565b620002c160048036036020811015620002b957600080fd5b50356200086e565b005b620002c160048036036020811015620002db57600080fd5b50356001600160a01b031662000ae3565b6200031b600480360360408110156200030457600080fd5b506001600160a01b03813516906020013562000b41565b60408051938452602084019290925282820152519081900360600190f35b6200024a600480360360408110156200035157600080fd5b508035906020013562000b61565b6200031b600480360360408110156200037757600080fd5b506001600160a01b03813516906020013562000b6f565b6200039862000bf6565b604080519115158252519081900360200190f35b6200024a62000d0c565b6200024a62000d1d565b6200024a60048036036080811015620003d857600080fd5b506001600160a01b03813516906020810135906040810135906060013562000d22565b620001ff62000e4e565b62000398600480360360208110156200041d57600080fd5b503562000e5d565b6200024a600480360360a08110156200043d57600080fd5b506001600160a01b0381358116916020810135909116906040810135906060810135906080013562000eba565b620002c162001098565b620002c16200126b565b6200024a600480360360208110156200049657600080fd5b503562001329565b6200024a600480360360e0811015620004b657600080fd5b506001600160a01b038135811691602081013591604082013591606081013591608082013581169160a08101359091169060c001356200133b565b6200024a62001455565b6200050562001461565b60408051602080825283518183015283519192839290830191858101910280838360005b838110156200054357818101518382015260200162000529565b505050509050019250505060405180910390f35b620001ff6200161c565b620003986200162b565b620002c1600480360360408110156200058357600080fd5b508035906020013562001637565b6200039860048036036040811015620005a957600080fd5b506001600160a01b03813516906020013562001703565b6200024a600480360360e0811015620005d857600080fd5b506001600160a01b038135811691602081013582169160408201359160608101359160808201359160a08101359091169060c001356200173b565b6200065c600480360360c08110156200062b57600080fd5b506001600160a01b0381358116916020810135916040820135916060810135916080820135169060a0013562001926565b604080519283526001600160a01b0390911660208301528051918290030190f35b6200031b600480360360608110156200069557600080fd5b508035906020810135906040013562001a4b565b6002546001600160a01b031681565b60006200075a620006c862001a6d565b6001600160a01b031663f1406dc885856040518363ffffffff1660e01b815260040180836001600160a01b03166001600160a01b031681526020018281526020019250505060206040518083038186803b1580156200072657600080fd5b505afa1580156200073b573d6000803e3d6000fd5b505050506040513d60208110156200075257600080fd5b505162001a8a565b90505b92915050565b6000806200077062001af4565b9050336001600160a01b0382161480620008015750604080516316b2213f60e01b815233600482015290516000916001600160a01b038416916316b2213f91602480820192602092909190829003018186803b158015620007d057600080fd5b505afa158015620007e5573d6000803e3d6000fd5b505050506040513d6020811015620007fc57600080fd5b505114155b6200083e5760405162461bcd60e51b8152600401808060200182810382526045815260200180620058e96045913960600191505060405180910390fd5b6000620008518888888888600062001b0d565b50909350905062000863818562001f3f565b505095945050505050565b6200087862001ff7565b6001600160a01b031663086dabd16040518163ffffffff1660e01b815260040160006040518083038186803b158015620008b157600080fd5b505afa158015620008c6573d6000803e3d6000fd5b5050505060006001600160a01b0316620008df62002013565b6001600160a01b03166332608039836040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b1580156200092357600080fd5b505afa15801562000938573d6000803e3d6000fd5b505050506040513d60208110156200094f57600080fd5b50516001600160a01b031614156200099e576040805162461bcd60e51b815260206004820152600d60248201526c09cde40e6eac6d040e6f2dce8d609b1b604482015290519081900360640190fd5b62000a2581620009ad62002029565b6001600160a01b031663ac82f608846040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b158015620009f157600080fd5b505afa15801562000a06573d6000803e3d6000fd5b505050506040513d602081101562000a1d57600080fd5b505162002046565b62000a6e576040805162461bcd60e51b815260206004820152601460248201527314de5b9d1a081c1c9a58d9481a5cc81d985b1a5960621b604482015290519081900360640190fd5b62000a7862001ff7565b6001600160a01b031663abc0bb6e8260416040518363ffffffff1660e01b81526004018083815260200182815260200192505050600060405180830381600087803b15801562000ac757600080fd5b505af115801562000adc573d6000803e3d6000fd5b5050505050565b62000aed620022b4565b600180546001600160a01b0383166001600160a01b0319909116811790915560408051918252517f906a1c6bd7e3091ea86693dd029a831c19049ce77f1dce2ce0bab1cacbabce229181900360200190a150565b600080600062000b52858562002301565b50919790965090945092505050565b60006200075a8383620025f6565b600080600062000b7e62001ff7565b6001600160a01b03166342a28e21856040518263ffffffff1660e01b81526004018082815260200191505060006040518083038186803b15801562000bc257600080fd5b505afa15801562000bd7573d6000803e3d6000fd5b5050505062000be985856001620026bf565b9250925092509250925092565b6000606062000c0462001461565b905060005b815181101562000d0257600082828151811062000c2257fe5b6020908102919091018101516000818152600383526040908190205460025482516321f8a72160e01b81526004810185905292519395506001600160a01b03918216949116926321f8a721926024808201939291829003018186803b15801562000c8b57600080fd5b505afa15801562000ca0573d6000803e3d6000fd5b505050506040513d602081101562000cb757600080fd5b50516001600160a01b031614158062000ce557506000818152600360205260409020546001600160a01b0316155b1562000cf8576000935050505062000d09565b5060010162000c09565b5060019150505b90565b600062000d1862002a55565b905090565b604181565b81600062000d2f62002013565b6001600160a01b03166332608039866040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b15801562000d7357600080fd5b505afa15801562000d88573d6000803e3d6000fd5b505050506040513d602081101562000d9f57600080fd5b5051604080516370a0823160e01b81526001600160a01b038981166004830152915191909216916370a08231916024808301926020929190829003018186803b15801562000dec57600080fd5b505afa15801562000e01573d6000803e3d6000fd5b505050506040513d602081101562000e1857600080fd5b505190508082111562000e29578091505b821562000e455762000e42828463ffffffff62002b1116565b91505b50949350505050565b6001546001600160a01b031681565b600062000eb28262000e6e62002029565b6001600160a01b031663ac82f608856040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b158015620009f157600080fd5b90505b919050565b60008062000ec762001af4565b9050336001600160a01b038216148062000f585750604080516316b2213f60e01b815233600482015290516000916001600160a01b038416916316b2213f91602480820192602092909190829003018186803b15801562000f2757600080fd5b505afa15801562000f3c573d6000803e3d6000fd5b505050506040513d602081101562000f5357600080fd5b505114155b62000f955760405162461bcd60e51b8152600401808060200182810382526045815260200180620058e96045913960600191505060405180910390fd5b62000f9f62002b6c565b6040805163faf431bb60e01b81526001600160a01b038a8116600483015289811660248301529151929091169163faf431bb91604480820192602092909190829003018186803b15801562000ff357600080fd5b505afa15801562001008573d6000803e3d6000fd5b505050506040513d60208110156200101f57600080fd5b505162001073576040805162461bcd60e51b815260206004820152601d60248201527f4e6f7420617070726f76656420746f20616374206f6e20626568616c66000000604482015290519081900360640190fd5b600062001086888787878c600062001b0d565b50909350905062000863818962001f3f565b6060620010a462001461565b905060005b815181101562001267576000828281518110620010c257fe5b602090810291909101810151600254604080517f5265736f6c766572206d697373696e67207461726765743a2000000000000000818601526039808201859052825180830390910181526059820180845263dacb2d0160e01b9052605d8201858152607d83019384528151609d84015281519597506000966001600160a01b039095169563dacb2d01958995939492939260bd0191908501908083838c5b838110156200117a57818101518382015260200162001160565b50505050905090810190601f168015620011a85780820380516001836020036101000a031916815260200191505b50935050505060206040518083038186803b158015620011c757600080fd5b505afa158015620011dc573d6000803e3d6000fd5b505050506040513d6020811015620011f357600080fd5b505160008381526003602090815260409182902080546001600160a01b0319166001600160a01b03851690811790915582518681529182015281519293507f88a93678a3692f6789d9546fc621bf7234b101ddb7d4fe479455112831b8aa68929081900390910190a15050600101620010a9565b5050565b6001546001600160a01b03163314620012b65760405162461bcd60e51b8152600401808060200182810382526035815260200180620058b46035913960400191505060405180910390fd5b600054600154604080516001600160a01b03938416815292909116602083015280517fb532073b38c83145e3e5135377a08bf9aab55bc0fd7c1179cd4fb995d2a5159c9281900390910190a160018054600080546001600160a01b03199081166001600160a01b03841617909155169055565b60046020526000908152604090205481565b6000806200134862001af4565b9050336001600160a01b0382161480620013d95750604080516316b2213f60e01b815233600482015290516000916001600160a01b038416916316b2213f91602480820192602092909190829003018186803b158015620013a857600080fd5b505afa158015620013bd573d6000803e3d6000fd5b505050506040513d6020811015620013d457600080fd5b505114155b620014165760405162461bcd60e51b8152600401808060200182810382526045815260200180620058e96045913960600191505060405180910390fd5b6000620014298a8a8a8a8a600062001b0d565b5090935090506200143b818662001f3f565b6200144884888562002b8d565b5050979650505050505050565b600062000d1862002c0b565b6060806200146e62002c88565b60408051600980825261014082019092529192506060919060208201610120803883390190505090506b53797374656d53746174757360a01b81600081518110620014b557fe5b6020026020010181815250506c45786368616e6765537461746560981b81600181518110620014e057fe5b6020026020010181815250506c45786368616e6765526174657360981b816002815181106200150b57fe5b602002602001018181525050680a6f2dce8d0cae8d2f60bb1b816003815181106200153257fe5b60200260200101818152505066119959541bdbdb60ca1b816004815181106200155757fe5b6020026020010181815250506d54726164696e675265776172647360901b816005815181106200158357fe5b6020026020010181815250507044656c6567617465417070726f76616c7360781b81600681518110620015b257fe5b6020026020010181815250506524b9b9bab2b960d11b81600781518110620015d657fe5b6020026020010181815250506844656274436163686560b81b81600881518110620015fd57fe5b60200260200101818152505062001615828262002cda565b9250505090565b6000546001600160a01b031681565b600062000d1862002d9f565b60006200164362002029565b9050336001600160a01b03821614620016a3576040805162461bcd60e51b815260206004820152601b60248201527f5265737472696374656420746f2045786368616e676552617465730000000000604482015290519081900360640190fd5b60008211620016f0576040805162461bcd60e51b8152602060048201526014602482015273052617465206d7573742062652061626f766520360641b604482015290519081900360640190fd5b5060009182526004602052604090912055565b6000620017118383620006b8565b1562001720575060016200075d565b60006200172e848462002301565b5050501515949350505050565b6000806200174862001af4565b9050336001600160a01b0382161480620017d95750604080516316b2213f60e01b815233600482015290516000916001600160a01b038416916316b2213f91602480820192602092909190829003018186803b158015620017a857600080fd5b505afa158015620017bd573d6000803e3d6000fd5b505050506040513d6020811015620017d457600080fd5b505114155b620018165760405162461bcd60e51b8152600401808060200182810382526045815260200180620058e96045913960600191505060405180910390fd5b6200182062002b6c565b6040805163faf431bb60e01b81526001600160a01b038c811660048301528b811660248301529151929091169163faf431bb91604480820192602092909190829003018186803b1580156200187457600080fd5b505afa15801562001889573d6000803e3d6000fd5b505050506040513d6020811015620018a057600080fd5b5051620018f4576040805162461bcd60e51b815260206004820152601d60248201527f4e6f7420617070726f76656420746f20616374206f6e20626568616c66000000604482015290519081900360640190fd5b6000620019078a8989898e600062001b0d565b50909350905062001919818662001f3f565b6200144884878562002b8d565b60008060006200193562001af4565b9050336001600160a01b0382161480620019c65750604080516316b2213f60e01b815233600482015290516000916001600160a01b038416916316b2213f91602480820192602092909190829003018186803b1580156200199557600080fd5b505afa158015620019aa573d6000803e3d6000fd5b505050506040513d6020811015620019c157600080fd5b505114155b62001a035760405162461bcd60e51b8152600401808060200182810382526045815260200180620058e96045913960600191505060405180910390fd5b600062001a168a8a8a8a8a600162001b0d565b919550909350905062001a2a818762001f3f565b841562001a3e5762001a3e85888662002b8d565b5050965096945050505050565b600080600062001a5d86868662002e20565b5092999198509650945050505050565b600062000d186c45786368616e6765537461746560981b62002f05565b60008062001a9762002c0b565b905082158062001ab8575062001ab4838263ffffffff62002b1116565b4210155b1562001ac957600091505062000eb5565b62001aed4262001ae0858463ffffffff62002b1116565b9063ffffffff62002fe616565b9392505050565b600062000d18680a6f2dce8d0cae8d2f60bb1b62002f05565b600080600062001b1f88888862003044565b600062001b2e888b8b6200322a565b90508062001b4757506000925082915081905062001f33565b600080600062001b59848d8c62002e20565b939a5091985094509250905062001b718c8362003267565b8062001b84575062001b848a8262003267565b1562001b9f57506000955085945084935062001f3392505050565b62001bb08c8e868d8b8e8e6200330a565b94506001600160a01b0385161562001bc6578498505b851562001e355762001bd762002029565b6001600160a01b031663654a60ac8b88631cd554d160e21b6040518463ffffffff1660e01b815260040180848152602001838152602001828152602001935050505060206040518083038186803b15801562001c3257600080fd5b505afa15801562001c47573d6000803e3d6000fd5b505050506040513d602081101562001c5e57600080fd5b5051955062001c6c62002013565b6001600160a01b03166332608039631cd554d160e21b6040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b15801562001cb757600080fd5b505afa15801562001ccc573d6000803e3d6000fd5b505050506040513d602081101562001ce357600080fd5b50516001600160a01b031663867904b462001cfd620035ff565b6001600160a01b031663eb1edd616040518163ffffffff1660e01b815260040160206040518083038186803b15801562001d3657600080fd5b505afa15801562001d4b573d6000803e3d6000fd5b505050506040513d602081101562001d6257600080fd5b5051604080516001600160e01b031960e085901b1681526001600160a01b039092166004830152602482018a905251604480830192600092919082900301818387803b15801562001db257600080fd5b505af115801562001dc7573d6000803e3d6000fd5b5050505062001dd5620035ff565b6001600160a01b03166322bf55ef876040518263ffffffff1660e01b815260040180828152602001915050600060405180830381600087803b15801562001e1b57600080fd5b505af115801562001e30573d6000803e3d6000fd5b505050505b62001e6960405180604001604052808e81526020018c81525060405180604001604052808581526020018481525062003616565b62001e7362001af4565b6001600160a01b0316636c00f3108e8e878e8c8f6040518763ffffffff1660e01b815260040180876001600160a01b03166001600160a01b03168152602001868152602001858152602001848152602001838152602001826001600160a01b03166001600160a01b031681526020019650505050505050600060405180830381600087803b15801562001f0557600080fd5b505af115801562001f1a573d6000803e3d6000fd5b5050505062001f2e898d868d8b88620038eb565b505050505b96509650969350505050565b60008211801562001f5857506001600160a01b03811615155b801562001f6a575062001f6a62002d9f565b15620012675762001f7a62003b08565b6001600160a01b03166321cad77483836040518363ffffffff1660e01b815260040180838152602001826001600160a01b03166001600160a01b0316815260200192505050600060405180830381600087803b15801562001fda57600080fd5b505af115801562001fef573d6000803e3d6000fd5b505050505050565b600062000d186b53797374656d53746174757360a01b62002f05565b600062000d186524b9b9bab2b960d11b62002f05565b600062000d186c45786368616e6765526174657360981b62002f05565b60008162002057575060016200075d565b6000838152600460205260409020548015620020825762002079818462003b26565b9150506200075d565b60606200208e62002029565b6001600160a01b0316632d7371e18660046040518363ffffffff1660e01b8152600401808381526020018281526020019250505060006040518083038186803b158015620020db57600080fd5b505afa158015620020f0573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f1916820160409081528110156200211a57600080fd5b81019080805160405193929190846401000000008211156200213b57600080fd5b9083019060208201858111156200215157600080fd5b82518660208202830111640100000000821117156200216f57600080fd5b82525081516020918201928201910280838360005b838110156200219e57818101518382015260200162002184565b5050505090500160405260200180516040519392919084640100000000821115620021c857600080fd5b908301906020820185811115620021de57600080fd5b8251866020820283011164010000000082111715620021fc57600080fd5b82525081516020918201928201910280838360005b838110156200222b57818101518382015260200162002211565b505050509050016040525050505090506000600190505b8151811015620022a85760008282815181106200225b57fe5b60200260200101511180156200228c57506200228c8282815181106200227d57fe5b60200260200101518662003b26565b156200229f57600193505050506200075d565b60010162002242565b50600095945050505050565b6000546001600160a01b03163314620022ff5760405162461bcd60e51b815260040180806020018281038252602f8152602001806200592e602f913960400191505060405180910390fd5b565b600080600060606200231262001a6d565b6001600160a01b031663b44e975387876040518363ffffffff1660e01b815260040180836001600160a01b03166001600160a01b031681526020018281526020019250505060206040518083038186803b1580156200237057600080fd5b505afa15801562002385573d6000803e3d6000fd5b505050506040513d60208110156200239c57600080fd5b5051604080518281526020808402820101909152909250606090838015620023e157816020015b620023cd620044d1565b815260200190600190039081620023c35790505b50905060005b83811015620025ea57600080620023fd620044d1565b6200240a8b8b8662003b90565b90506000806200241a8362003cb2565b9150915060006200242a62002029565b6001600160a01b031663266da16b85600001518660200151876040015187876040518663ffffffff1660e01b8152600401808681526020018581526020018481526020018381526020018281526020019550505050505060206040518083038186803b1580156200249a57600080fd5b505afa158015620024af573d6000803e3d6000fd5b505050506040513d6020811015620024c657600080fd5b50516080850151909150600090620024e090839062003e0f565b9050620024f285606001518262003b26565b62002573578085606001511115620025375760608501516200251b908263ffffffff62002fe616565b96506200252f8d8863ffffffff62002b1116565b9c5062002573565b8460600151811115620025735760608501516200255c90829063ffffffff62002fe616565b9550620025708c8763ffffffff62002b1116565b9b505b6040518061010001604052808660000151815260200186602001518152602001866040015181526020018881526020018781526020018581526020018481526020018660a00151815250898981518110620025ca57fe5b6020026020010181905250505050505050508080600101915050620023e7565b50905092959194509250565b6000620026038262003ea8565b9050631cd554d160e21b831480620026215750631cd554d160e21b82145b156200262d576200075d565b607360f81b6001600160f81b0319600085901a60f81b16148015620026665750606960f81b6001600160f81b0319600084901a60f81b16145b80620026a65750606960f81b6001600160f81b0319600085901a60f81b16148015620026a65750607360f81b6001600160f81b0319600084901a60f81b16145b156200075d576200075a81600263ffffffff62003f8516565b6000806000620026d08686620006b8565b156200270e5760405162461bcd60e51b8152600401808060200182810382526023815260200180620059a06023913960400191505060405180910390fd5b60008060006060620027218a8a62002301565b935093509350935082841115620027595762002744848463ffffffff62002fe616565b9650620027538a8a8962003fe3565b62002783565b83831115620027835762002774838563ffffffff62002fe616565b9550620027838a8a886200413d565b87156200287257604080516001808252818301909252606091602080830190803883390190505090508981600081518110620027bb57fe5b602002602001018181525050620027d162004297565b6001600160a01b031663cda218c7826040518263ffffffff1660e01b81526004018080602001828103825283818151815260200191508051906020019060200280838360005b838110156200283157818101518382015260200162002817565b5050505090500192505050600060405180830381600087803b1580156200285757600080fd5b505af11580156200286c573d6000803e3d6000fd5b50505050505b60005b8151811015620029c1578a6001600160a01b03167f8e3ad1f68bec55de3b6fa12ae2674a2a683a17c918a4cbf5157ac5d9ddc6e940838381518110620028b757fe5b602002602001015160000151848481518110620028d057fe5b602002602001015160200151858581518110620028e957fe5b6020026020010151604001518686815181106200290257fe5b6020026020010151606001518787815181106200291b57fe5b6020026020010151608001518888815181106200293457fe5b602002602001015160a001518989815181106200294d57fe5b602002602001015160c001518a8a815181106200296657fe5b602002602001015160e00151604051808981526020018881526020018781526020018681526020018581526020018481526020018381526020018281526020019850505050505050505060405180910390a260010162002875565b50819450620029cf62001a6d565b6001600160a01b031663d0d3d62a8b8b6040518363ffffffff1660e01b815260040180836001600160a01b03166001600160a01b0316815260200182815260200192505050600060405180830381600087803b15801562002a2f57600080fd5b505af115801562002a44573d6000803e3d6000fd5b505050505050505093509350939050565b600062002a61620042b0565b6001600160a01b03166323257c2b6d53797374656d53657474696e677360901b7f7072696365446576696174696f6e5468726573686f6c64466163746f720000006040518363ffffffff1660e01b8152600401808381526020018281526020019250505060206040518083038186803b15801562002ade57600080fd5b505afa15801562002af3573d6000803e3d6000fd5b505050506040513d602081101562002b0a57600080fd5b5051905090565b6000828201838110156200075a576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b600062000d187044656c6567617465417070726f76616c7360781b62002f05565b62002b9762001af4565b6001600160a01b031663ddd03a3f8484846040518463ffffffff1660e01b8152600401808481526020018381526020018281526020019350505050600060405180830381600087803b15801562002bed57600080fd5b505af115801562002c02573d6000803e3d6000fd5b50505050505050565b600062002c17620042b0565b6001600160a01b03166323257c2b6d53797374656d53657474696e677360901b7077616974696e67506572696f645365637360781b6040518363ffffffff1660e01b8152600401808381526020018281526020019250505060206040518083038186803b15801562002ade57600080fd5b604080516001808252818301909252606091602080830190803883390190505090506e466c657869626c6553746f7261676560881b8160008151811062002ccb57fe5b60200260200101818152505090565b6060815183510160405190808252806020026020018201604052801562002d0b578160200160208202803883390190505b50905060005b835181101562002d515783818151811062002d2857fe5b602002602001015182828151811062002d3d57fe5b602090810291909101015260010162002d11565b5060005b825181101562002d985782818151811062002d6c57fe5b602002602001015182828651018151811062002d8457fe5b602090810291909101015260010162002d55565b5092915050565b600062002dab620042b0565b6001600160a01b031663d994502d6d53797374656d53657474696e677360901b741d1c98591a5b99d4995dd85c991cd15b98589b1959605a1b6040518363ffffffff1660e01b8152600401808381526020018281526020019250505060206040518083038186803b15801562002ade57600080fd5b60008060008060008062002e3362002029565b6001600160a01b0316638295016a898b8a6040518463ffffffff1660e01b815260040180848152602001838152602001828152602001935050505060606040518083038186803b15801562002e8757600080fd5b505afa15801562002e9c573d6000803e3d6000fd5b505050506040513d606081101562002eb357600080fd5b5080516020820151604090920151919450909250905062002ed58888620025f6565b935062002ee3818562003e0f565b955062002ef7818763ffffffff62002fe616565b945050939792965093509350565b600081815260036020908152604080832054815170026b4b9b9b4b7339030b2323932b9b99d1607d1b9381019390935260318084018690528251808503909101815260519093019091526001600160a01b0316908162002d985760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b8381101562002faa57818101518382015260200162002f90565b50505050905090810190601f16801562002fd85780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b6000828211156200303e576040805162461bcd60e51b815260206004820152601e60248201527f536166654d6174683a207375627472616374696f6e206f766572666c6f770000604482015290519081900360640190fd5b50900390565b8083141562003090576040805162461bcd60e51b8152602060048201526013602482015272086c2dc4ee840c4ca40e6c2daca40e6f2dce8d606b1b604482015290519081900360640190fd5b60008211620030d4576040805162461bcd60e51b815260206004820152600b60248201526a16995c9bc8185b5bdd5b9d60aa1b604482015290519081900360640190fd5b604080516002808252606080830184529260208301908038833901905050905083816000815181106200310357fe5b60200260200101818152505081816001815181106200311e57fe5b6020026020010181815250506200313462002029565b6001600160a01b0316630a7d36d1826040518263ffffffff1660e01b81526004018080602001828103825283818151815260200191508051906020019060200280838360005b83811015620031945781810151838201526020016200317a565b505050509050019250505060206040518083038186803b158015620031b857600080fd5b505afa158015620031cd573d6000803e3d6000fd5b505050506040513d6020811015620031e457600080fd5b505115620032245760405162461bcd60e51b81526004018080602001828103825260228152602001806200597e6022913960400191505060405180910390fd5b50505050565b60008060006200323d85856000620026bf565b88955090935091505080156200325e576200325b8585888562000d22565b92505b50509392505050565b600062003275838362002046565b15620032f7576200328562001ff7565b6001600160a01b031663abc0bb6e8460416040518363ffffffff1660e01b81526004018083815260200182815260200192505050600060405180830381600087803b158015620032d457600080fd5b505af1158015620032e9573d6000803e3d6000fd5b50505050600190506200075d565b6000928352600460205260409092205590565b60006200331662002013565b6001600160a01b03166332608039896040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b1580156200335a57600080fd5b505afa1580156200336f573d6000803e3d6000fd5b505050506040513d60208110156200338657600080fd5b505160408051632770a7eb60e21b81526001600160a01b038a81166004830152602482018a905291519190921691639dc29fac91604480830192600092919082900301818387803b158015620033db57600080fd5b505af1158015620033f0573d6000803e3d6000fd5b5050505060006200340062002013565b6001600160a01b03166332608039876040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b1580156200344457600080fd5b505afa15801562003459573d6000803e3d6000fd5b505050506040513d60208110156200347057600080fd5b50519050821562003578576000819050620034f5816001600160a01b031663ec5568896040518163ffffffff1660e01b815260040160206040518083038186803b158015620034be57600080fd5b505afa158015620034d3573d6000803e3d6000fd5b505050506040513d6020811015620034ea57600080fd5b505186888a620042cf565b9250816001600160a01b031663867904b484886040518363ffffffff1660e01b815260040180836001600160a01b03166001600160a01b0316815260200182815260200192505050600060405180830381600087803b1580156200355857600080fd5b505af11580156200356d573d6000803e3d6000fd5b5050505050620035f3565b806001600160a01b031663867904b485876040518363ffffffff1660e01b815260040180836001600160a01b03166001600160a01b0316815260200182815260200192505050600060405180830381600087803b158015620035d957600080fd5b505af1158015620035ee573d6000803e3d6000fd5b505050505b50979650505050505050565b600062000d1866119959541bdbdb60ca1b62002f05565b8151600090631cd554d160e21b14806200363a57506020830151631cd554d160e21b145b90506000816200364c5760036200364f565b60025b60ff16905060608160405190808252806020026020018201604052801562003681578160200160208202803883390190505b5090508460006020020151816000815181106200369a57fe5b6020908102919091010152846001602002015181600181518110620036bb57fe5b602002602001018181525050606082604051908082528060200260200182016040528015620036f4578160200160208202803883390190505b5090508460006020020151816000815181106200370d57fe5b60209081029190910101528460016020020151816001815181106200372e57fe5b60200260200101818152505083620037f257631cd554d160e21b826002815181106200375657fe5b602002602001018181525050731a60e2e2a8be0bc2b6381dd31fd3fd5f9a28de4c63907af6c06040518163ffffffff1660e01b815260040160206040518083038186803b158015620037a757600080fd5b505af4158015620037bc573d6000803e3d6000fd5b505050506040513d6020811015620037d357600080fd5b5051815182906002908110620037e557fe5b6020026020010181815250505b620037fc62004297565b6001600160a01b03166317b38db483836040518363ffffffff1660e01b8152600401808060200180602001838103835285818151815260200191508051906020019060200280838360005b838110156200386157818101518382015260200162003847565b50505050905001838103825284818151815260200191508051906020019060200280838360005b83811015620038a257818101518382015260200162003888565b50505050905001945050505050600060405180830381600087803b158015620038ca57600080fd5b505af1158015620038df573d6000803e3d6000fd5b50505050505050505050565b6000620038f762002029565b90506000816001600160a01b0316637a018a1e886040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b1580156200394057600080fd5b505afa15801562003955573d6000803e3d6000fd5b505050506040513d60208110156200396c57600080fd5b505160408051633d00c50f60e11b81526004810188905290519192506000916001600160a01b03851691637a018a1e916024808301926020929190829003018186803b158015620039bc57600080fd5b505afa158015620039d1573d6000803e3d6000fd5b505050506040513d6020811015620039e857600080fd5b50519050620039f662001a6d565b60408051630f2a761760e21b81526001600160a01b038c81166004830152602482018c9052604482018b9052606482018a90526084820189905260a482018890524260c483015260e48201869052610104820185905291519290911691633ca9d85c916101248082019260009290919082900301818387803b15801562003a7c57600080fd5b505af115801562003a91573d6000803e3d6000fd5b5050604080518b8152602081018b90528082018a9052606081018990526080810188905260a0810186905260c0810185905290516001600160a01b038d1693507f62e40d554c7abcdd31074960d8347a2225daeb04d93bc748f049ba2ce946239892509081900360e00190a2505050505050505050565b600062000d186d54726164696e675265776172647360901b62002f05565b600082158062003b34575081155b1562003b43575060016200075d565b60008383111562003b685762003b60838563ffffffff6200440a16565b905062003b7d565b62003b7a848463ffffffff6200440a16565b90505b62003b8762002a55565b11159392505050565b62003b9a620044d1565b60008060008060008060008062003bb062001a6d565b6001600160a01b03166315987eb68d8d8d6040518463ffffffff1660e01b815260040180846001600160a01b03166001600160a01b0316815260200183815260200182815260200193505050506101006040518083038186803b15801562003c1757600080fd5b505afa15801562003c2c573d6000803e3d6000fd5b505050506040513d61010081101562003c4457600080fd5b50805160208083015160408085015160608087015160808089015160a0808b015160c0808d015160e09d8e01518a5161010081018c529d8e529b8d019a909a52978b019690965293890192909252870152850152830152918101919091529c9b505050505050505050505050565b600080600062003cc162002029565b9050600062003ccf62002c0b565b9050816001600160a01b031663109e46a286600001518760c001518860a00151856040518563ffffffff1660e01b81526004018085815260200184815260200183815260200182815260200194505050505060206040518083038186803b15801562003d3a57600080fd5b505afa15801562003d4f573d6000803e3d6000fd5b505050506040513d602081101562003d6657600080fd5b505160408087015160e088015160a0890151835163084f235160e11b81526004810193909352602483019190915260448201526064810184905290519195506001600160a01b0384169163109e46a291608480820192602092909190829003018186803b15801562003dd757600080fd5b505afa15801562003dec573d6000803e3d6000fd5b505050506040513d602081101562003e0357600080fd5b50519395939450505050565b60006200075a62003e9a83731a60e2e2a8be0bc2b6381dd31fd3fd5f9a28de4c63907af6c06040518163ffffffff1660e01b815260040160206040518083038186803b15801562003e5f57600080fd5b505af415801562003e74573d6000803e3d6000fd5b505050506040513d602081101562003e8b57600080fd5b50519063ffffffff62002fe616565b849063ffffffff6200443816565b600062003eb4620042b0565b6001600160a01b03166323257c2b6d53797374656d53657474696e677360901b6e65786368616e67654665655261746560881b856040516020018083815260200182815260200192505050604051602081830303815290604052805190602001206040518363ffffffff1660e01b8152600401808381526020018281526020019250505060206040518083038186803b15801562003f5157600080fd5b505afa15801562003f66573d6000803e3d6000fd5b505050506040513d602081101562003f7d57600080fd5b505192915050565b60008262003f96575060006200075d565b8282028284828162003fa457fe5b04146200075a5760405162461bcd60e51b81526004018080602001828103825260218152602001806200595d6021913960400191505060405180910390fd5b62003fed62002013565b6001600160a01b03166332608039836040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b1580156200403157600080fd5b505afa15801562004046573d6000803e3d6000fd5b505050506040513d60208110156200405d57600080fd5b505160408051632770a7eb60e21b81526001600160a01b0386811660048301526024820185905291519190921691639dc29fac91604480830192600092919082900301818387803b158015620040b257600080fd5b505af1158015620040c7573d6000803e3d6000fd5b50505050620040d562001af4565b6001600160a01b031663ace88afd8484846040518463ffffffff1660e01b815260040180846001600160a01b03166001600160a01b031681526020018381526020018281526020019350505050600060405180830381600087803b15801562002bed57600080fd5b6200414762002013565b6001600160a01b03166332608039836040518263ffffffff1660e01b81526004018082815260200191505060206040518083038186803b1580156200418b57600080fd5b505afa158015620041a0573d6000803e3d6000fd5b505050506040513d6020811015620041b757600080fd5b50516040805163219e412d60e21b81526001600160a01b038681166004830152602482018590529151919092169163867904b491604480830192600092919082900301818387803b1580156200420c57600080fd5b505af115801562004221573d6000803e3d6000fd5b505050506200422f62001af4565b6001600160a01b0316636f01a9868484846040518463ffffffff1660e01b815260040180846001600160a01b03166001600160a01b031681526020018381526020018281526020019350505050600060405180830381600087803b15801562002bed57600080fd5b600062000d186844656274436163686560b81b62002f05565b600062000d186e466c657869626c6553746f7261676560881b62002f05565b6000606960f81b6001600160f81b031983831a60f81b1614156200433a576040805162461bcd60e51b815260206004820152601c60248201527f43616e6e6f74207669727475616c697a6520746869732073796e746800000000604482015290519081900360640190fd5b60025460405186916001600160a01b0316908690869086906200435d906200451c565b6001600160a01b0395861681529385166020850152919093166040808401919091526060830193909352608082015290519081900360a001906000f080158015620043ac573d6000803e3d6000fd5b50604080516001600160a01b03808416825260208201869052818301879052915192935081871692918816917fb5ec76d79549c775883022e4426db5cd36bd5307f216cdb341554c301548ef9f9181900360600190a3949350505050565b60006200075a826200442b85670de0b6b3a764000063ffffffff62003f8516565b9063ffffffff6200446516565b6000670de0b6b3a764000062004455848463ffffffff62003f8516565b816200445d57fe5b049392505050565b6000808211620044bc576040805162461bcd60e51b815260206004820152601a60248201527f536166654d6174683a206469766973696f6e206279207a65726f000000000000604482015290519081900360640190fd5b6000828481620044c857fe5b04949350505050565b60405180610100016040528060008019168152602001600081526020016000801916815260200160008152602001600081526020016000815260200160008152602001600081525090565b611389806200452b8339019056fe60806040526004805460ff60a01b1916905534801561001d57600080fd5b50604051611389380380611389833981810160405260a081101561004057600080fd5b508051602082015160408301516060840151608090940151600380546001600160a01b038087166001600160a01b03199283161790925560048054928616929091169190911790556007819055929391929091906100a783836001600160e01b036100b416565b506005555061020f915050565b6001600160a01b03821661010f576040805162461bcd60e51b815260206004820152601f60248201527f45524332303a206d696e7420746f20746865207a65726f206164647265737300604482015290519081900360640190fd5b610128816002546101ae60201b610b011790919060201c565b6002556001600160a01b03821660009081526020818152604090912054610158918390610b016101ae821b17901c565b6001600160a01b0383166000818152602081815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b600082820183811015610208576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b9392505050565b61116b8061021e6000396000f3fe608060405234801561001057600080fd5b50600436106101425760003560e01c80633db3dc9b116100b85780638f7758391161007c5780638f7758391461036657806395d89b411461036e578063a457c2d714610376578063a9059cbb146103a2578063dbd06c85146103ce578063dd62ed3e146103d657610142565b80633db3dc9b146103005780634be37cea146103085780636a256b291461031057806370a082311461033857806378f2ac261461035e57610142565b806323b872dd1161010a57806323b872dd1461024a5780632c4e722e14610280578063313ce56714610288578063378dc3dc146102a657806339509351146102ae5780633af9e669146102da57610142565b806304f3bcec1461014757806306fdde031461016b578063095ea7b3146101e8578063115f4fee1461022857806318160ddd14610230575b600080fd5b61014f610404565b604080516001600160a01b039092168252519081900360200190f35b610173610413565b6040805160208082528351818301528351919283929083019185019080838360005b838110156101ad578181015183820152602001610195565b50505050905090810190601f1680156101da5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610214600480360360408110156101fe57600080fd5b506001600160a01b03813516906020013561044d565b604080519115158252519081900360200190f35b61014f610464565b610238610473565b60408051918252519081900360200190f35b6102146004803603606081101561026057600080fd5b506001600160a01b03813581169160208101359091169060400135610479565b6102386104d0565b6102906104df565b6040805160ff9092168252519081900360200190f35b6102386104e4565b610214600480360360408110156102c457600080fd5b506001600160a01b0381351690602001356104ea565b610238600480360360208110156102f057600080fd5b50356001600160a01b0316610526565b610238610531565b610238610537565b6103366004803603602081101561032657600080fd5b50356001600160a01b0316610541565b005b6102386004803603602081101561034e57600080fd5b50356001600160a01b03166105f5565b610214610610565b610214610620565b610173610630565b6102146004803603604081101561038c57600080fd5b506001600160a01b03813516906020013561065c565b610214600480360360408110156103b857600080fd5b506001600160a01b038135169060200135610698565b6102386106a5565b610238600480360360408110156103ec57600080fd5b506001600160a01b03813581169160200135166106ab565b6004546001600160a01b031681565b600754604080516d02b34b93a3ab0b61029bcb73a34160951b6020820152602e8082019390935281518082039093018352604e0190525b90565b600061045a3384846106d6565b5060015b92915050565b6003546001600160a01b031681565b60025490565b60006104868484846107c2565b6001600160a01b0384166000908152600160209081526040808320338085529252909120546104c69186916104c1908663ffffffff61090416565b6106d6565b5060019392505050565b60006104da610961565b905090565b601281565b60055481565b3360008181526001602090815260408083206001600160a01b0387168452909152812054909161045a9185906104c1908663ffffffff610b0116565b600061045e82610b62565b60065481565b60006104da610b88565b610549610c22565b6003546001600160a01b031663a9059cbb8261056481610b62565b6040518363ffffffff1660e01b815260040180836001600160a01b03166001600160a01b0316815260200182815260200192505050602060405180830381600087803b1580156105b357600080fd5b505af11580156105c7573d6000803e3d6000fd5b505050506040513d60208110156105dd57600080fd5b506105f29050816105ed816105f5565b610da2565b50565b6001600160a01b031660009081526020819052604090205490565b600061061a610b88565b15905090565b600454600160a01b900460ff1681565b60075460408051603b60f91b602082015260218082019390935281518082039093018352604101905290565b3360008181526001602090815260408083206001600160a01b0387168452909152812054909161045a9185906104c1908663ffffffff61090416565b600061045a3384846107c2565b60075481565b6001600160a01b03918216600090815260016020908152604080832093909416825291909152205490565b6001600160a01b03831661071b5760405162461bcd60e51b81526004018080602001828103825260248152602001806111136024913960400191505060405180910390fd5b6001600160a01b0382166107605760405162461bcd60e51b815260040180806020018281038252602281526020018061108a6022913960400191505060405180910390fd5b6001600160a01b03808416600081815260016020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6001600160a01b0383166108075760405162461bcd60e51b81526004018080602001828103825260258152602001806110ee6025913960400191505060405180910390fd5b6001600160a01b03821661084c5760405162461bcd60e51b81526004018080602001828103825260238152602001806110676023913960400191505060405180910390fd5b6001600160a01b038316600090815260208190526040902054610875908263ffffffff61090416565b6001600160a01b0380851660009081526020819052604080822093909355908416815220546108aa908263ffffffff610b0116565b6001600160a01b038084166000818152602081815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b60008282111561095b576040805162461bcd60e51b815260206004820152601e60248201527f536166654d6174683a207375627472616374696f6e206f766572666c6f770000604482015290519081900360640190fd5b50900390565b6000600554600014156109765750600061044a565b600454600090600160a01b900460ff16610ae257600354604080516370a0823160e01b815230600482015290516001600160a01b03909216916370a0823191602480820192602092909190829003018186803b1580156109d557600080fd5b505afa1580156109e9573d6000803e3d6000fd5b505050506040513d60208110156109ff57600080fd5b50519050600080610a0e610e7b565b6001600160a01b03166319d5c665306007546040518363ffffffff1660e01b815260040180836001600160a01b03166001600160a01b031681526020018281526020019250505060606040518083038186803b158015610a6d57600080fd5b505afa158015610a81573d6000803e3d6000fd5b505050506040513d6060811015610a9757600080fd5b50805160209091015190925090508115610ac257610abb838363ffffffff61090416565b9250610adb565b8015610adb57610ad8838263ffffffff610b0116565b92505b5050610ae7565b506006545b600554610afb90829063ffffffff610f0a16565b91505090565b600082820183811015610b5b576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b9392505050565b600080610b6e836105f5565b9050610b5b610b7b610961565b829063ffffffff610f1f16565b6000610b92610e7b565b6001600160a01b031663059c29ec306007546040518363ffffffff1660e01b815260040180836001600160a01b03166001600160a01b031681526020018281526020019250505060206040518083038186803b158015610bf157600080fd5b505afa158015610c05573d6000803e3d6000fd5b505050506040513d6020811015610c1b57600080fd5b5051905090565b600454600160a01b900460ff1615610c3957610da0565b6004805460ff60a01b1916600160a01b179055610c54610e7b565b6001600160a01b0316631b16802c306007546040518363ffffffff1660e01b815260040180836001600160a01b03166001600160a01b0316815260200182815260200192505050606060405180830381600087803b158015610cb557600080fd5b505af1158015610cc9573d6000803e3d6000fd5b505050506040513d6060811015610cdf57600080fd5b5050600354604080516370a0823160e01b815230600482015290516001600160a01b03909216916370a0823191602480820192602092909190829003018186803b158015610d2c57600080fd5b505afa158015610d40573d6000803e3d6000fd5b505050506040513d6020811015610d5657600080fd5b50516006557ff5b268a3ff315cc44ccceeef86259c9e8eef81ceecb14001543809115380dd62610d84610473565b6006546040805192835260208301919091528051918290030190a15b565b6001600160a01b038216610de75760405162461bcd60e51b81526004018080602001828103825260218152602001806110cd6021913960400191505060405180910390fd5b600254610dfa908263ffffffff61090416565b6002556001600160a01b038216600090815260208190526040902054610e26908263ffffffff61090416565b6001600160a01b038316600081815260208181526040808320949094558351858152935191937fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef929081900390910190a35050565b600480546040805163dacb2d0160e01b81526822bc31b430b733b2b960b91b9381019390935260248301819052601c60448401527f45786368616e67657220636f6e7472616374206e6f7420666f756e64000000006064840152516000926001600160a01b039092169163dacb2d01916084808301926020929190829003018186803b158015610bf157600080fd5b6000610b5b8383670de0b6b3a7640000610f34565b6000610b5b8383670de0b6b3a7640000610f78565b600080610f5a84610f4e87600a870263ffffffff610fa316565b9063ffffffff610ffc16565b90506005600a825b0610610f6c57600a015b600a9004949350505050565b600080600a8304610f8f868663ffffffff610fa316565b81610f9657fe5b0490506005600a82610f62565b600082610fb25750600061045e565b82820282848281610fbf57fe5b0414610b5b5760405162461bcd60e51b81526004018080602001828103825260218152602001806110ac6021913960400191505060405180910390fd5b6000808211611052576040805162461bcd60e51b815260206004820152601a60248201527f536166654d6174683a206469766973696f6e206279207a65726f000000000000604482015290519081900360640190fd5b600082848161105d57fe5b0494935050505056fe45524332303a207472616e7366657220746f20746865207a65726f206164647265737345524332303a20617070726f766520746f20746865207a65726f2061646472657373536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f7745524332303a206275726e2066726f6d20746865207a65726f206164647265737345524332303a207472616e736665722066726f6d20746865207a65726f206164647265737345524332303a20617070726f76652066726f6d20746865207a65726f2061646472657373a265627a7a72315820dfbe46973c7d714370abd8fc3b4cee901887ff452a6c738209dfa7fb39b396ce64736f6c63430005100032596f75206d757374206265206e6f6d696e61746564206265666f726520796f752063616e20616363657074206f776e65727368697045786368616e6765723a204f6e6c792073796e746865746978206f7220612073796e746820636f6e74726163742063616e20706572666f726d207468697320616374696f6e4f6e6c792074686520636f6e7472616374206f776e6572206d617920706572666f726d207468697320616374696f6e536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f775372632f64657374207261746520696e76616c6964206f72206e6f7420666f756e6443616e6e6f7420736574746c6520647572696e672077616974696e6720706572696f64a265627a7a723158206f18b9fa23bab4ff69230d65d4193abd84c4c7a08ddd01a086b6aac721a120c864736f6c63430005100032000000000000000000000000b64ff7a4a33acdf48d97dab0d764afd0f6176882000000000000000000000000242a3df52c375bee81b1c668741d7c63af68fdd2

Library Used

SafeDecimalMath : 0x1a60e2e2a8be0bc2b6381dd31fd3fd5f9a28de4c

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