{"file_path":"contracts/utils/FPSWrapper.sol","creation_status":"success","source_code":"// SPDX-License-Identifier: MIT\r\n\r\npragma solidity ^0.8.0;\r\n\r\nimport \"./ERC20.sol\";\r\nimport \"./ERC20PermitLight.sol\";\r\nimport \"../Equity.sol\";\r\n\r\ncontract FPSWrapper is ERC20 {\r\n\r\n    Equity private immutable fps = Equity(address(0x1bA26788dfDe592fec8bcB0Eaff472a42BE341B2));\r\n\r\n    constructor() ERC20(18){\r\n    }\r\n\r\n    function name() external pure override returns (string memory) {\r\n        return \"Wrapped Frankencoin Pool Share\";\r\n    }\r\n\r\n    function symbol() external pure override returns (string memory) {\r\n        return \"WFPS\";\r\n    }\r\n\r\n    // requires allowance\r\n    function wrap(uint256 amount) public {\r\n        depositFor(msg.sender, amount);\r\n    }\r\n\r\n    /**\r\n     * Same function as in openzeppelin ERC20Wrapper\r\n     * @dev Allow a user to deposit underlying tokens and mint the corresponding number of wrapped tokens.\r\n     */\r\n    function depositFor(address account, uint256 amount) public virtual returns (bool) {\r\n        fps.transferFrom(msg.sender, address(this), amount);\r\n        super._mint(account, amount);\r\n        return true;\r\n    }\r\n\r\n    function unwrap(uint256 amount) public {\r\n        withdrawTo(msg.sender, amount);\r\n    }\r\n\r\n    /**\r\n     * Same function as in openzeppelin ERC20Wrapper\r\n     * @dev Allow a user to burn a number of wrapped tokens and withdraw the corresponding number of underlying tokens.\r\n     */\r\n    function withdrawTo(address account, uint256 amount) public virtual returns (bool) {\r\n        super._burn(msg.sender, amount);\r\n        fps.transfer(account, amount);\r\n        return true;\r\n    }\r\n\r\n    /**\r\n     * @dev Returns the address of the underlying ERC-20 token that is being wrapped.\r\n     */\r\n    function underlying() public view returns (IERC20) {\r\n        return fps;\r\n    }\r\n\r\n    /**\r\n     * Sell immediately, bypassing the 90 day holding requirement if the\r\n     * average wrapped token has been around for long enough and no one\r\n     * cancelled the votes of this contract. Can help with market making\r\n     * between chains when this token is bridged.\r\n     * \r\n     * Anyone can prevent this method from being executable via the\r\n     * halveHoldingDuration function. Also, it won't be executable in an\r\n     * expanding market where the number of wrapped FPS doubles every\r\n     * 90 days such that the average holding period of this contract stays\r\n     * below that duration.\r\n     */\r\n    function unwrapAndSell(uint256 amount) public {\r\n        super._burn(msg.sender, amount);\r\n        fps.redeem(msg.sender, amount);\r\n    }\r\n\r\n    /**\r\n     * Reduces the recorded holding duration of the wrapped FPS. This has two effects:\r\n     * - Averts the risk of this contract accumulating too many votes over time (i.e. 98%)\r\n     * - Can prevent \"unwrapAndSell\" from succeeding (which can be desired to prevent short\r\n     *   term arbitrage at the cost of all other FPS holders)\r\n     * \r\n     * Anyone with 2% of the votes can call this.\r\n     */\r\n    function halveHoldingDuration(address[] calldata helpers) public {\r\n        fps.checkQualified(msg.sender, helpers);\r\n        // causes our votes to be cut in half\r\n        fps.transfer(address(this), totalSupply());\r\n    }\r\n\r\n}","deployed_bytecode":"0x608060405234801561001057600080fd5b50600436106101005760003560e01c806370a0823111610097578063a9059cbb11610066578063a9059cbb1461029f578063dd62ed3e146102b2578063de0e9a3e146102c5578063ea598cb0146102d857600080fd5b806370a082311461022e5780638747842b1461025757806387481e731461026c57806395d89b411461027f57600080fd5b806323b872dd116100d357806323b872dd146101985780632f4f21e2146101ab578063313ce567146101be5780636f307dc3146101f757600080fd5b806306fdde0314610105578063095ea7b31461015057806318160ddd14610173578063205c287814610185575b600080fd5b60408051808201909152601e81527f57726170706564204672616e6b656e636f696e20506f6f6c205368617265000060208201525b60405161014791906109a8565b60405180910390f35b61016361015e366004610a12565b6102eb565b6040519015158152602001610147565b6002545b604051908152602001610147565b610163610193366004610a12565b610302565b6101636101a6366004610a3c565b6103ab565b6101636101b9366004610a12565b6103c3565b6101e57f000000000000000000000000000000000000000000000000000000000000001281565b60405160ff9091168152602001610147565b6040516001600160a01b037f0000000000000000000000001ba26788dfde592fec8bcb0eaff472a42be341b2168152602001610147565b61017761023c366004610a78565b6001600160a01b031660009081526020819052604090205490565b61026a610265366004610a93565b610468565b005b61026a61027a366004610b08565b610595565b6040805180820190915260048152635746505360e01b602082015261013a565b6101636102ad366004610a12565b610634565b6101776102c0366004610b21565b610641565b61026a6102d3366004610b08565b610671565b61026a6102e6366004610b08565b61067b565b60006102f8338484610685565b5060015b92915050565b600061030e33836106e7565b60405163a9059cbb60e01b81526001600160a01b038481166004830152602482018490527f0000000000000000000000001ba26788dfde592fec8bcb0eaff472a42be341b2169063a9059cbb906044016020604051808303816000875af115801561037d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906103a19190610b54565b5060019392505050565b60006103b8848484610771565b6103a184338461088d565b6040516323b872dd60e01b8152336004820152306024820152604481018290526000907f0000000000000000000000001ba26788dfde592fec8bcb0eaff472a42be341b26001600160a01b0316906323b872dd906064016020604051808303816000875af1158015610439573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061045d9190610b54565b506102f88383610913565b60405163352e3a8360e01b81526001600160a01b037f0000000000000000000000001ba26788dfde592fec8bcb0eaff472a42be341b2169063352e3a83906104b890339086908690600401610b76565b60006040518083038186803b1580156104d057600080fd5b505afa1580156104e4573d6000803e3d6000fd5b505050507f0000000000000000000000001ba26788dfde592fec8bcb0eaff472a42be341b26001600160a01b031663a9059cbb3061052160025490565b6040516001600160e01b031960e085901b1681526001600160a01b03909216600483015260248201526044016020604051808303816000875af115801561056c573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906105909190610b54565b505050565b61059f33826106e7565b6040516301e9a69560e41b8152336004820152602481018290527f0000000000000000000000001ba26788dfde592fec8bcb0eaff472a42be341b26001600160a01b031690631e9a6950906044016020604051808303816000875af115801561060c573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106309190610bd0565b5050565b60006102f8338484610771565b6001600160a01b0380831660009081526001602090815260408083209385168352929052908120545b9392505050565b6106303382610302565b61063033826103c3565b6001600160a01b0383811660008181526001602090815260408083209487168084529482529182902085905590518481527f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92591015b60405180910390a3505050565b80600260008282546106f99190610bff565b90915550506001600160a01b03821660009081526020819052604081208054839290610726908490610bff565b90915550506040518181526000906001600160a01b038416907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef906020015b60405180910390a35050565b6001600160a01b03821661078457600080fd5b6001600160a01b0383166000908152602081905260409020548111156107ec576001600160a01b0383166000818152602081905260409081902054905163391434e360e21b815260048101929092526024820152604481018290526064015b60405180910390fd5b6001600160a01b03831660009081526020819052604081208054839290610814908490610bff565b90915550506001600160a01b03821660009081526020819052604081208054839290610841908490610c12565b92505081905550816001600160a01b0316836001600160a01b03167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516106da91815260200190565b6001600160a01b03838116600090815260016020908152604080832093861683529290522054600160ff1b81101561090d57818110156108f957604051637dc7a0d960e11b81526001600160a01b038516600482015260248101829052604481018390526064016107e3565b61090d84846109088585610bff565b610685565b50505050565b6001600160a01b03821661092657600080fd5b80600260008282546109389190610c12565b90915550506001600160a01b03821660009081526020819052604081208054839290610965908490610c12565b90915550506040518181526001600160a01b038316906000907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef90602001610765565b600060208083528351808285015260005b818110156109d5578581018301518582016040015282016109b9565b506000604082860101526040601f19601f8301168501019250505092915050565b80356001600160a01b0381168114610a0d57600080fd5b919050565b60008060408385031215610a2557600080fd5b610a2e836109f6565b946020939093013593505050565b600080600060608486031215610a5157600080fd5b610a5a846109f6565b9250610a68602085016109f6565b9150604084013590509250925092565b600060208284031215610a8a57600080fd5b61066a826109f6565b60008060208385031215610aa657600080fd5b823567ffffffffffffffff80821115610abe57600080fd5b818501915085601f830112610ad257600080fd5b813581811115610ae157600080fd5b8660208260051b8501011115610af657600080fd5b60209290920196919550909350505050565b600060208284031215610b1a57600080fd5b5035919050565b60008060408385031215610b3457600080fd5b610b3d836109f6565b9150610b4b602084016109f6565b90509250929050565b600060208284031215610b6657600080fd5b8151801515811461066a57600080fd5b6001600160a01b03848116825260406020808401829052908301849052600091859160608501845b87811015610bc35783610bb0866109f6565b1682529382019390820190600101610b9e565b5098975050505050505050565b600060208284031215610be257600080fd5b5051919050565b634e487b7160e01b600052601160045260246000fd5b818103818111156102fc576102fc610be9565b808201808211156102fc576102fc610be956fea26469706673582212205d517686de607fa4d19d51f2b6d643a42d03a42bcf8ed10e3210eaca751a533664736f6c63430008130033","optimization_enabled":true,"verified_twin_address_hash":null,"is_verified":true,"compiler_settings":{"compilationTarget":{"contracts/utils/FPSWrapper.sol":"FPSWrapper"},"evmVersion":"london","libraries":{},"metadata":{"bytecodeHash":"ipfs"},"optimizer":{"enabled":true,"runs":200},"remappings":[":eth-gas-reporter/=node_modules/eth-gas-reporter/",":forge-std/=lib/forge-std/src/",":hardhat-deploy/=node_modules/hardhat-deploy/",":hardhat/=node_modules/hardhat/"]},"optimization_runs":200,"sourcify_repo_url":null,"decoded_constructor_args":null,"compiler_version":"0.8.19+commit.7dd6d404","is_verified_via_verifier_alliance":true,"verified_at":"2024-03-23T23:30:13.748720Z","implementations":[],"proxy_type":null,"external_libraries":[],"creation_bytecode":"0x60c0604052731ba26788dfde592fec8bcb0eaff472a42be341b260a05234801561002857600080fd5b50601260805260805160a051610c5b6100766000396000818161020401528181610334015281816103e80152818161047f015281816104ea01526105bb015260006101c30152610c5b6000f3fe608060405234801561001057600080fd5b50600436106101005760003560e01c806370a0823111610097578063a9059cbb11610066578063a9059cbb1461029f578063dd62ed3e146102b2578063de0e9a3e146102c5578063ea598cb0146102d857600080fd5b806370a082311461022e5780638747842b1461025757806387481e731461026c57806395d89b411461027f57600080fd5b806323b872dd116100d357806323b872dd146101985780632f4f21e2146101ab578063313ce567146101be5780636f307dc3146101f757600080fd5b806306fdde0314610105578063095ea7b31461015057806318160ddd14610173578063205c287814610185575b600080fd5b60408051808201909152601e81527f57726170706564204672616e6b656e636f696e20506f6f6c205368617265000060208201525b60405161014791906109a8565b60405180910390f35b61016361015e366004610a12565b6102eb565b6040519015158152602001610147565b6002545b604051908152602001610147565b610163610193366004610a12565b610302565b6101636101a6366004610a3c565b6103ab565b6101636101b9366004610a12565b6103c3565b6101e57f000000000000000000000000000000000000000000000000000000000000000081565b60405160ff9091168152602001610147565b6040516001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000168152602001610147565b61017761023c366004610a78565b6001600160a01b031660009081526020819052604090205490565b61026a610265366004610a93565b610468565b005b61026a61027a366004610b08565b610595565b6040805180820190915260048152635746505360e01b602082015261013a565b6101636102ad366004610a12565b610634565b6101776102c0366004610b21565b610641565b61026a6102d3366004610b08565b610671565b61026a6102e6366004610b08565b61067b565b60006102f8338484610685565b5060015b92915050565b600061030e33836106e7565b60405163a9059cbb60e01b81526001600160a01b038481166004830152602482018490527f0000000000000000000000000000000000000000000000000000000000000000169063a9059cbb906044016020604051808303816000875af115801561037d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906103a19190610b54565b5060019392505050565b60006103b8848484610771565b6103a184338461088d565b6040516323b872dd60e01b8152336004820152306024820152604481018290526000907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b0316906323b872dd906064016020604051808303816000875af1158015610439573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061045d9190610b54565b506102f88383610913565b60405163352e3a8360e01b81526001600160a01b037f0000000000000000000000000000000000000000000000000000000000000000169063352e3a83906104b890339086908690600401610b76565b60006040518083038186803b1580156104d057600080fd5b505afa1580156104e4573d6000803e3d6000fd5b505050507f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031663a9059cbb3061052160025490565b6040516001600160e01b031960e085901b1681526001600160a01b03909216600483015260248201526044016020604051808303816000875af115801561056c573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906105909190610b54565b505050565b61059f33826106e7565b6040516301e9a69560e41b8152336004820152602481018290527f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031690631e9a6950906044016020604051808303816000875af115801561060c573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106309190610bd0565b5050565b60006102f8338484610771565b6001600160a01b0380831660009081526001602090815260408083209385168352929052908120545b9392505050565b6106303382610302565b61063033826103c3565b6001600160a01b0383811660008181526001602090815260408083209487168084529482529182902085905590518481527f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92591015b60405180910390a3505050565b80600260008282546106f99190610bff565b90915550506001600160a01b03821660009081526020819052604081208054839290610726908490610bff565b90915550506040518181526000906001600160a01b038416907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef906020015b60405180910390a35050565b6001600160a01b03821661078457600080fd5b6001600160a01b0383166000908152602081905260409020548111156107ec576001600160a01b0383166000818152602081905260409081902054905163391434e360e21b815260048101929092526024820152604481018290526064015b60405180910390fd5b6001600160a01b03831660009081526020819052604081208054839290610814908490610bff565b90915550506001600160a01b03821660009081526020819052604081208054839290610841908490610c12565b92505081905550816001600160a01b0316836001600160a01b03167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516106da91815260200190565b6001600160a01b03838116600090815260016020908152604080832093861683529290522054600160ff1b81101561090d57818110156108f957604051637dc7a0d960e11b81526001600160a01b038516600482015260248101829052604481018390526064016107e3565b61090d84846109088585610bff565b610685565b50505050565b6001600160a01b03821661092657600080fd5b80600260008282546109389190610c12565b90915550506001600160a01b03821660009081526020819052604081208054839290610965908490610c12565b90915550506040518181526001600160a01b038316906000907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef90602001610765565b600060208083528351808285015260005b818110156109d5578581018301518582016040015282016109b9565b506000604082860101526040601f19601f8301168501019250505092915050565b80356001600160a01b0381168114610a0d57600080fd5b919050565b60008060408385031215610a2557600080fd5b610a2e836109f6565b946020939093013593505050565b600080600060608486031215610a5157600080fd5b610a5a846109f6565b9250610a68602085016109f6565b9150604084013590509250925092565b600060208284031215610a8a57600080fd5b61066a826109f6565b60008060208385031215610aa657600080fd5b823567ffffffffffffffff80821115610abe57600080fd5b818501915085601f830112610ad257600080fd5b813581811115610ae157600080fd5b8660208260051b8501011115610af657600080fd5b60209290920196919550909350505050565b600060208284031215610b1a57600080fd5b5035919050565b60008060408385031215610b3457600080fd5b610b3d836109f6565b9150610b4b602084016109f6565b90509250929050565b600060208284031215610b6657600080fd5b8151801515811461066a57600080fd5b6001600160a01b03848116825260406020808401829052908301849052600091859160608501845b87811015610bc35783610bb0866109f6565b1682529382019390820190600101610b9e565b5098975050505050505050565b600060208284031215610be257600080fd5b5051919050565b634e487b7160e01b600052601160045260246000fd5b818103818111156102fc576102fc610be9565b808201808211156102fc576102fc610be956fea26469706673582212205d517686de607fa4d19d51f2b6d643a42d03a42bcf8ed10e3210eaca751a533664736f6c63430008130033","name":"FPSWrapper","is_blueprint":false,"license_type":"none","is_fully_verified":true,"is_verified_via_eth_bytecode_db":true,"language":"solidity","evm_version":"london","can_be_visualized_via_sol2uml":true,"is_verified_via_sourcify":false,"additional_sources":[{"file_path":"contracts/Equity.sol","source_code":"// SPDX-License-Identifier: MIT\r\n\r\npragma solidity ^0.8.0;\r\n\r\nimport \"./Frankencoin.sol\";\r\nimport \"./utils/MathUtil.sol\";\r\nimport \"./interface/IReserve.sol\";\r\nimport \"./interface/IERC677Receiver.sol\";\r\n\r\n/**\r\n * @title Equity\r\n * @notice If the Frankencoin system was a bank, this contract would represent the equity on its balance sheet.\r\n * Like with a corporation, the owners of the equity capital are the shareholders, or in this case the holders\r\n * of Frankencoin Pool Shares (FPS) tokens. Anyone can mint additional FPS tokens by adding Frankencoins to the\r\n * reserve pool. Also, FPS tokens can be redeemed for Frankencoins again after a minimum holding period.\r\n * Furthermore, the FPS shares come with some voting power. Anyone that held at least 3% of the holding-period-\r\n * weighted reserve pool shares gains veto power and can veto new proposals.\r\n */\r\ncontract Equity is ERC20PermitLight, MathUtil, IReserve {\r\n    /**\r\n     * The VALUATION_FACTOR determines the market cap of the reserve pool shares relative to the equity reserves.\r\n     * The following always holds: Market Cap = Valuation Factor * Equity Reserve = Price * Supply\r\n     *\r\n     * In the absence of profits and losses, the variables grow as follows when FPS tokens are minted:\r\n     *\r\n     * |   Reserve     |   Market Cap  |     Price     |     Supply   |\r\n     * |          1000 |          3000 |             3 |         1000 |\r\n     * |       1000000 |       3000000 |           300 |        10000 |\r\n     * |    1000000000 |    3000000000 |         30000 |       100000 |\r\n     * | 1000000000000 | 3000000000000 |       3000000 |      1000000 |\r\n     *\r\n     * I.e., the supply is proporational to the cubic root of the reserve and the price is proportional to the\r\n     * squared cubic root. When profits accumulate or losses materialize, the reserve, the market cap,\r\n     * and the price are adjusted proportionally, with the supply staying constant. In the absence of an extreme\r\n     * inflation of the Swiss franc, it is unlikely that there will ever be more than ten million FPS.\r\n     */\r\n    uint32 public constant VALUATION_FACTOR = 3;\r\n\r\n    uint256 private constant MINIMUM_EQUITY = 1000 * ONE_DEC18;\r\n\r\n    /**\r\n     * @notice The quorum in basis points. 100 is 1%.\r\n     */\r\n    uint32 private constant QUORUM = 200;\r\n\r\n    /**\r\n     * @notice The number of digits to store the average holding time of share tokens.\r\n     */\r\n    uint8 private constant TIME_RESOLUTION_BITS = 20;\r\n\r\n    /**\r\n     * @notice The minimum holding duration. You are not allowed to redeem your pool shares if you held them\r\n     * for less than the minimum holding duration at average. For example, if you have two pool shares on your\r\n     * address, one acquired 5 days ago and one acquired 105 days ago, you cannot redeem them as the average\r\n     * holding duration of your shares is only 55 days < 90 days.\r\n     */\r\n    uint256 public constant MIN_HOLDING_DURATION = 90 days << TIME_RESOLUTION_BITS; // Set to 5 for local testing\r\n\r\n    Frankencoin public immutable zchf;\r\n\r\n    /**\r\n     * @dev To track the total number of votes we need to know the number of votes at the anchor time and when the\r\n     * anchor time was. This is (hopefully) stored in one 256 bit slot, with the anchor time taking 64 Bits and\r\n     * the total vote count 192 Bits. Given the sub-second resolution of 20 Bits, the implicit assumption is\r\n     * that the timestamp can always be stored in 44 Bits (i.e. it does not exceed half a million years). Further,\r\n     * given 18 decimals (about 60 Bits), this implies that the total supply cannot exceed\r\n     *   192 - 60 - 44 - 20 = 68 Bits\r\n     * Here, we are also save, as 68 Bits would imply more than a trillion outstanding shares. In fact,\r\n     * a limit of about 2**36 shares (that's about 2**96 Bits when taking into account the decimals) is imposed\r\n     * when minting. This means that the maximum supply is billions shares, which is could only be reached in\r\n     * a scenario with hyper inflation, in which case the stablecoin is worthless anyway.\r\n     */\r\n    uint192 private totalVotesAtAnchor; // Total number of votes at the anchor time, see comment on the um\r\n    uint64 private totalVotesAnchorTime; // 44 Bit for the time stamp, 20 Bit sub-second time resolution\r\n\r\n    /**\r\n     * @notice Keeping track on who delegated votes to whom.\r\n     * Note that delegation does not mean you cannot vote / veto any more, it just means that the delegate can\r\n     * benefit from your votes when invoking a veto. Circular delegations are valid, do not help when voting.\r\n     */\r\n    mapping(address owner => address delegate) public delegates;\r\n\r\n    /**\r\n     * @notice A time stamp in the past such that: votes = balance * (time passed since anchor was set)\r\n     */\r\n    mapping(address owner => uint64 timestamp) private voteAnchor; // 44 bits for time stamp, 20 subsecond resolution\r\n\r\n    event Delegation(address indexed from, address indexed to); // indicates a delegation\r\n    event Trade(address who, int amount, uint totPrice, uint newprice); // amount pos or neg for mint or redemption\r\n\r\n    constructor(Frankencoin zchf_) ERC20(18) {\r\n        zchf = zchf_;\r\n    }\r\n\r\n    function name() external pure override returns (string memory) {\r\n        return \"Frankencoin Pool Share\";\r\n    }\r\n\r\n    function symbol() external pure override returns (string memory) {\r\n        return \"FPS\";\r\n    }\r\n\r\n    /**\r\n     * @notice Returns the price of one FPS in ZCHF with 18 decimals precision.\r\n     */\r\n    function price() public view returns (uint256) {\r\n        uint256 equity = zchf.equity();\r\n        if (equity == 0 || totalSupply() == 0) {\r\n            return ONE_DEC18; // initial price is 1000 ZCHF for the first 1000 FPS\r\n        } else {\r\n            return (VALUATION_FACTOR * zchf.equity() * ONE_DEC18) / totalSupply();\r\n        }\r\n    }\r\n\r\n    function _beforeTokenTransfer(address from, address to, uint256 amount) internal override {\r\n        super._beforeTokenTransfer(from, to, amount);\r\n        if (amount > 0) {\r\n            // No need to adjust the sender votes. When they send out 10% of their shares, they also lose 10% of\r\n            // their votes so everything falls nicely into place. Recipient votes should stay the same, but grow\r\n            // faster in the future, requiring an adjustment of the anchor.\r\n            uint256 roundingLoss = _adjustRecipientVoteAnchor(to, amount);\r\n            // The total also must be adjusted and kept accurate by taking into account the rounding error.\r\n            _adjustTotalVotes(from, amount, roundingLoss);\r\n        }\r\n    }\r\n\r\n    /**\r\n     * @notice Returns whether the given address is allowed to redeem FPS, which is the\r\n     * case after their average holding duration is larger than the required minimum.\r\n     */\r\n    function canRedeem(address owner) public view returns (bool) {\r\n        return _anchorTime() - voteAnchor[owner] >= MIN_HOLDING_DURATION;\r\n    }\r\n\r\n    /**\r\n     * @notice Decrease the total votes anchor when tokens lose their voting power due to being moved\r\n     * @param from      sender\r\n     * @param amount    amount to be sent\r\n     */\r\n    function _adjustTotalVotes(address from, uint256 amount, uint256 roundingLoss) internal {\r\n        uint64 time = _anchorTime();\r\n        uint256 lostVotes = from == address(0x0) ? 0 : (time - voteAnchor[from]) * amount;\r\n        totalVotesAtAnchor = uint192(totalVotes() - roundingLoss - lostVotes);\r\n        totalVotesAnchorTime = time;\r\n    }\r\n\r\n    /**\r\n     * @notice the vote anchor of the recipient is moved forward such that the number of calculated\r\n     * votes does not change despite the higher balance.\r\n     * @param to        receiver address\r\n     * @param amount    amount to be received\r\n     * @return the number of votes lost due to rounding errors\r\n     */\r\n    function _adjustRecipientVoteAnchor(address to, uint256 amount) internal returns (uint256) {\r\n        if (to != address(0x0)) {\r\n            uint256 recipientVotes = votes(to); // for example 21 if 7 shares were held for 3 seconds\r\n            uint256 newbalance = balanceOf(to) + amount; // for example 11 if 4 shares are added\r\n            // new example anchor is only 21 / 11 = 1 second in the past\r\n            voteAnchor[to] = uint64(_anchorTime() - recipientVotes / newbalance);\r\n            return recipientVotes % newbalance; // we have lost 21 % 11 = 10 votes\r\n        } else {\r\n            // optimization for burn, vote anchor of null address does not matter\r\n            return 0;\r\n        }\r\n    }\r\n\r\n    /**\r\n     * @notice Time stamp with some additional bits for higher resolution.\r\n     */\r\n    function _anchorTime() internal view returns (uint64) {\r\n        return uint64(block.timestamp << TIME_RESOLUTION_BITS);\r\n    }\r\n\r\n    /**\r\n     * @notice The relative voting power of the address.\r\n     * @return A percentage with 1e18 being 100%\r\n     */\r\n    function relativeVotes(address holder) external view returns (uint256) {\r\n        return (ONE_DEC18 * votes(holder)) / totalVotes();\r\n    }\r\n\r\n    /**\r\n     * @notice The votes of the holder, excluding votes from delegates.\r\n     */\r\n    function votes(address holder) public view returns (uint256) {\r\n        return balanceOf(holder) * (_anchorTime() - voteAnchor[holder]);\r\n    }\r\n\r\n    /**\r\n     * @notice How long the holder already held onto their average FPS in seconds.\r\n     */\r\n    function holdingDuration(address holder) public view returns (uint256) {\r\n        return (_anchorTime() - voteAnchor[holder]) >> TIME_RESOLUTION_BITS;\r\n    }\r\n\r\n    /**\r\n     * @notice Total number of votes in the system.\r\n     */\r\n    function totalVotes() public view returns (uint256) {\r\n        return totalVotesAtAnchor + totalSupply() * (_anchorTime() - totalVotesAnchorTime);\r\n    }\r\n\r\n    /**\r\n     * @notice The number of votes the sender commands when taking the support of the helpers into account.\r\n     * @param sender    The address whose total voting power is of interest\r\n     * @param helpers   An incrementally sorted list of helpers without duplicates and without the sender.\r\n     *                  The call fails if the list contains an address that does not delegate to sender.\r\n     *                  For indirect delegates, i.e. a -> b -> c, both a and b must be included for both to count.\r\n     * @return          The total number of votes of sender at the current point in time.\r\n     */\r\n    function votesDelegated(address sender, address[] calldata helpers) public view returns (uint256) {\r\n        uint256 _votes = votes(sender);\r\n        require(_checkDuplicatesAndSorted(helpers));\r\n        for (uint i = 0; i < helpers.length; i++) {\r\n            address current = helpers[i];\r\n            require(current != sender);\r\n            require(_canVoteFor(sender, current));\r\n            _votes += votes(current);\r\n        }\r\n        return _votes;\r\n    }\r\n\r\n    function _checkDuplicatesAndSorted(address[] calldata helpers) internal pure returns (bool ok) {\r\n        if (helpers.length <= 1) {\r\n            return true;\r\n        } else {\r\n            address prevAddress = helpers[0];\r\n            for (uint i = 1; i < helpers.length; i++) {\r\n                if (helpers[i] <= prevAddress) {\r\n                    return false;\r\n                }\r\n                prevAddress = helpers[i];\r\n            }\r\n            return true;\r\n        }\r\n    }\r\n\r\n    /**\r\n     * @notice Checks whether the sender address is qualified given a list of helpers that delegated their votes\r\n     * directly or indirectly to the sender. It is the responsiblity of the caller to figure out whether\r\n     * helpes are necessary and to identify them by scanning the blockchain for Delegation events.\r\n     */\r\n    function checkQualified(address sender, address[] calldata helpers) public view override {\r\n        uint256 _votes = votesDelegated(sender, helpers);\r\n        if (_votes * 10000 < QUORUM * totalVotes()) revert NotQualified();\r\n    }\r\n\r\n    error NotQualified();\r\n\r\n    /**\r\n     * @notice Increases the voting power of the delegate by your number of votes without taking away any voting power\r\n     * from the sender.\r\n     */\r\n    function delegateVoteTo(address delegate) external {\r\n        delegates[msg.sender] = delegate;\r\n        emit Delegation(msg.sender, delegate);\r\n    }\r\n\r\n    function _canVoteFor(address delegate, address owner) internal view returns (bool) {\r\n        if (owner == delegate) {\r\n            return true;\r\n        } else if (owner == address(0x0)) {\r\n            return false;\r\n        } else {\r\n            return _canVoteFor(delegate, delegates[owner]);\r\n        }\r\n    }\r\n\r\n    /**\r\n     * @notice Since quorum is rather low, it is important to have a way to prevent malicious minority holders\r\n     * from blocking the whole system. This method provides a way for the good guys to team up and destroy\r\n     * the bad guy's votes (at the cost of also reducing their own votes). This mechanism potentially\r\n     * gives full control over the system to whoever has 51% of the votes.\r\n     *\r\n     * Since this is a rather aggressive measure, delegation is not supported. Every holder must call this\r\n     * method on their own.\r\n     * @param targets   The target addresses to remove votes from\r\n     * @param votesToDestroy    The maximum number of votes the caller is willing to sacrifice\r\n     */\r\n    function kamikaze(address[] calldata targets, uint256 votesToDestroy) external {\r\n        uint256 budget = _reduceVotes(msg.sender, votesToDestroy);\r\n        uint256 destroyedVotes = 0;\r\n        for (uint256 i = 0; i < targets.length && destroyedVotes < budget; i++) {\r\n            destroyedVotes += _reduceVotes(targets[i], budget - destroyedVotes);\r\n        }\r\n        require(destroyedVotes > 0); // sanity check\r\n        totalVotesAtAnchor = uint192(totalVotes() - destroyedVotes - budget);\r\n        totalVotesAnchorTime = _anchorTime();\r\n    }\r\n\r\n    function _reduceVotes(address target, uint256 amount) internal returns (uint256) {\r\n        uint256 votesBefore = votes(target);\r\n        if (amount >= votesBefore) {\r\n            amount = votesBefore;\r\n            voteAnchor[target] = _anchorTime();\r\n            return votesBefore;\r\n        } else {\r\n            voteAnchor[target] = uint64(_anchorTime() - (votesBefore - amount) / balanceOf(target));\r\n            return votesBefore - votes(target);\r\n        }\r\n    }\r\n\r\n    /**\r\n     * @notice Call this method to obtain newly minted pool shares in exchange for Frankencoins.\r\n     * No allowance required (i.e. it is hardcoded in the Frankencoin token contract).\r\n     * Make sure to invest at least 10e-12 * market cap to avoid rounding losses.\r\n     *\r\n     * @dev If equity is close to zero or negative, you need to send enough ZCHF to bring equity back to 1000 ZCHF.\r\n     *\r\n     * @param amount            Frankencoins to invest\r\n     * @param expectedShares    Minimum amount of expected shares for frontrunning protection\r\n     */\r\n    function invest(uint256 amount, uint256 expectedShares) external returns (uint256) {\r\n        zchf.transferFrom(msg.sender, address(this), amount);\r\n        uint256 equity = zchf.equity();\r\n        require(equity >= MINIMUM_EQUITY, \"insuf equity\"); // ensures that the initial deposit is at least 1000 ZCHF\r\n\r\n        uint256 shares = _calculateShares(equity <= amount ? 0 : equity - amount, amount);\r\n        require(shares >= expectedShares);\r\n        _mint(msg.sender, shares);\r\n        emit Trade(msg.sender, int(shares), amount, price());\r\n\r\n        // limit the total supply to a reasonable amount to guard against overflows with price and vote calculations\r\n        // the 36 bits are 68 bits for magnitude and 60 bits for precision, as calculated in an above comment\r\n        require(totalSupply() <= type(uint96).max, \"total supply exceeded\");\r\n        return shares;\r\n    }\r\n\r\n    /**\r\n     * @notice Calculate shares received when investing Frankencoins\r\n     * @param investment    ZCHF to be invested\r\n     * @return shares to be received in return\r\n     */\r\n    function calculateShares(uint256 investment) external view returns (uint256) {\r\n        return _calculateShares(zchf.equity(), investment);\r\n    }\r\n\r\n    function _calculateShares(uint256 capitalBefore, uint256 investment) internal view returns (uint256) {\r\n        uint256 totalShares = totalSupply();\r\n        uint256 investmentExFees = (investment * 997) / 1000; // remove 0.3% fee\r\n        // Assign 1000 FPS for the initial deposit, calculate the amount otherwise\r\n        uint256 newTotalShares = capitalBefore < MINIMUM_EQUITY || totalShares == 0\r\n            ? totalShares + 1000 * ONE_DEC18\r\n            : _mulD18(totalShares, _cubicRoot(_divD18(capitalBefore + investmentExFees, capitalBefore)));\r\n        return newTotalShares - totalShares;\r\n    }\r\n\r\n    /**\r\n     * @notice Redeem the given amount of shares owned by the sender and transfer the proceeds to the target.\r\n     * @return The amount of ZCHF transferred to the target\r\n     */\r\n    function redeem(address target, uint256 shares) external returns (uint256) {\r\n        return _redeemFrom(msg.sender, target, shares);\r\n    }\r\n\r\n    /**\r\n     * @notice Like redeem(...), but with an extra parameter to protect against frontrunning.\r\n     * @param expectedProceeds  The minimum acceptable redemption proceeds.\r\n     */\r\n    function redeemExpected(address target, uint256 shares, uint256 expectedProceeds) external returns (uint256) {\r\n        uint256 proceeds = _redeemFrom(msg.sender, target, shares);\r\n        require(proceeds >= expectedProceeds);\r\n        return proceeds;\r\n    }\r\n\r\n    /**\r\n     * @notice Redeem FPS based on an allowance from the owner to the caller.\r\n     * See also redeemExpected(...).\r\n     */\r\n    function redeemFrom(\r\n        address owner,\r\n        address target,\r\n        uint256 shares,\r\n        uint256 expectedProceeds\r\n    ) external returns (uint256) {\r\n        _useAllowance(owner, msg.sender, shares);\r\n        uint256 proceeds = _redeemFrom(owner, target, shares);\r\n        require(proceeds >= expectedProceeds);\r\n        return proceeds;\r\n    }\r\n\r\n    function _redeemFrom(address owner, address target, uint256 shares) internal returns (uint256) {\r\n        require(canRedeem(owner));\r\n        uint256 proceeds = calculateProceeds(shares);\r\n        _burn(owner, shares);\r\n        zchf.transfer(target, proceeds);\r\n        emit Trade(owner, -int(shares), proceeds, price());\r\n        return proceeds;\r\n    }\r\n\r\n    /**\r\n     * @notice Calculate ZCHF received when depositing shares\r\n     * @param shares number of shares we want to exchange for ZCHF,\r\n     *               in dec18 format\r\n     * @return amount of ZCHF received for the shares\r\n     */\r\n    function calculateProceeds(uint256 shares) public view returns (uint256) {\r\n        uint256 totalShares = totalSupply();\r\n        require(shares + ONE_DEC18 < totalShares, \"too many shares\"); // make sure there is always at least one share\r\n        uint256 capital = zchf.equity();\r\n        uint256 reductionAfterFees = (shares * 997) / 1000;\r\n        uint256 newCapital = _mulD18(capital, _power3(_divD18(totalShares - reductionAfterFees, totalShares)));\r\n        return capital - newCapital;\r\n    }\r\n\r\n    /**\r\n     * @notice If there is less than 1000 ZCHF in equity left (maybe even negative), the system is at risk\r\n     * and we should allow qualified FPS holders to restructure the system.\r\n     *\r\n     * Example: there was a devastating loss and equity stands at -1'000'000. Most shareholders have lost hope in the\r\n     * Frankencoin system except for a group of small FPS holders who still believes in it and is willing to provide\r\n     * 2'000'000 ZCHF to save it. These brave souls are essentially donating 1'000'000 to the minter reserve and it\r\n     * would be wrong to force them to share the other million with the passive FPS holders. Instead, they will get\r\n     * the possibility to bootstrap the system again owning 100% of all FPS shares.\r\n     *\r\n     * @param helpers          A list of addresses that delegate to the caller in incremental order\r\n     * @param addressesToWipe  A list of addresses whose FPS will be burned to zero\r\n     */\r\n    function restructureCapTable(address[] calldata helpers, address[] calldata addressesToWipe) external {\r\n        require(zchf.equity() < MINIMUM_EQUITY);\r\n        checkQualified(msg.sender, helpers);\r\n        for (uint256 i = 0; i < addressesToWipe.length; i++) {\r\n            address current = addressesToWipe[i];\r\n            _burn(current, balanceOf(current));\r\n        }\r\n    }\r\n}\r\n"},{"file_path":"contracts/Frankencoin.sol","source_code":"// SPDX-License-Identifier: MIT\r\npragma solidity ^0.8.0;\r\n\r\nimport \"./utils/ERC20PermitLight.sol\";\r\nimport \"./Equity.sol\";\r\nimport \"./interface/IReserve.sol\";\r\nimport \"./interface/IFrankencoin.sol\";\r\n\r\n/**\r\n * @title FrankenCoin\r\n * @notice The Frankencoin (ZCHF) is an ERC-20 token that is designed to track the value of the Swiss franc.\r\n * It is not upgradable, but open to arbitrary minting plugins. These are automatically accepted if none of the\r\n * qualified pool share holders casts a veto, leading to a flexible but conservative governance.\r\n */\r\ncontract Frankencoin is ERC20PermitLight, IFrankencoin {\r\n    /**\r\n     * @notice Minimal fee and application period when suggesting a new minter.\r\n     */\r\n    uint256 public constant MIN_FEE = 1000 * (10 ** 18);\r\n    uint256 public immutable MIN_APPLICATION_PERIOD; // for example 10 days\r\n\r\n    /**\r\n     * @notice The contract that holds the reserve.\r\n     */\r\n    IReserve public immutable override reserve;\r\n\r\n    /**\r\n     * @notice How much of the reserve belongs to the minters. Everything else belongs to the pool share holders.\r\n     * Stored with 6 additional digits of accuracy so no rounding is necessary when dealing with parts per\r\n     * million (ppm) in reserve calculations.\r\n     */\r\n    uint256 private minterReserveE6;\r\n\r\n    /**\r\n     * @notice Map of minters to approval time stamps. If the time stamp is in the past, the minter contract is allowed\r\n     * to mint Frankencoins.\r\n     */\r\n    mapping(address minter => uint256 validityStart) public minters;\r\n\r\n    /**\r\n     * @notice List of positions that are allowed to mint and the minter that registered them.\r\n     */\r\n    mapping(address position => address registeringMinter) public positions;\r\n\r\n    event MinterApplied(address indexed minter, uint256 applicationPeriod, uint256 applicationFee, string message);\r\n    event MinterDenied(address indexed minter, string message);\r\n    event Loss(address indexed reportingMinter, uint256 amount);\r\n    event Profit(address indexed reportingMinter, uint256 amount);\r\n\r\n    error PeriodTooShort();\r\n    error FeeTooLow();\r\n    error AlreadyRegistered();\r\n    error NotMinter();\r\n    error TooLate();\r\n\r\n    modifier minterOnly() {\r\n        if (!isMinter(msg.sender) && !isMinter(positions[msg.sender])) revert NotMinter();\r\n        _;\r\n    }\r\n\r\n    /**\r\n     * @notice Initiates the Frankencoin with the provided minimum application period for new plugins\r\n     * in seconds, for example 10 days, i.e. 3600*24*10 = 864000\r\n     */\r\n    constructor(uint256 _minApplicationPeriod) ERC20(18) {\r\n        MIN_APPLICATION_PERIOD = _minApplicationPeriod;\r\n        reserve = new Equity(this);\r\n    }\r\n\r\n    function name() external pure override returns (string memory) {\r\n        return \"Frankencoin\";\r\n    }\r\n\r\n    function symbol() external pure override returns (string memory) {\r\n        return \"ZCHF\";\r\n    }\r\n\r\n    function initialize(address _minter, string calldata _message) external {\r\n        require(totalSupply() == 0 && reserve.totalSupply() == 0);\r\n        minters[_minter] = block.timestamp;\r\n        emit MinterApplied(_minter, 0, 0, _message);\r\n    }\r\n\r\n    /**\r\n     * @notice Publicly accessible method to suggest a new way of minting Frankencoin.\r\n     * @dev The caller has to pay an application fee that is irrevocably lost even if the new minter is vetoed.\r\n     * The caller must assume that someone will veto the new minter unless there is broad consensus that the new minter\r\n     * adds value to the Frankencoin system. Complex proposals should have application periods and applications fees\r\n     * above the minimum. It is assumed that over time, informal ways to coordinate on new minters emerge. The message\r\n     * parameter might be useful for initiating further communication. Maybe it contains a link to a website describing\r\n     * the proposed minter.\r\n     *\r\n     * @param _minter              An address that is given the permission to mint Frankencoins\r\n     * @param _applicationPeriod   The time others have to veto the suggestion, at least MIN_APPLICATION_PERIOD\r\n     * @param _applicationFee      The fee paid by the caller, at least MIN_FEE\r\n     * @param _message             An optional human readable message to everyone watching this contract\r\n     */\r\n    function suggestMinter(\r\n        address _minter,\r\n        uint256 _applicationPeriod,\r\n        uint256 _applicationFee,\r\n        string calldata _message\r\n    ) external override {\r\n        if (_applicationPeriod < MIN_APPLICATION_PERIOD) revert PeriodTooShort();\r\n        if (_applicationFee < MIN_FEE) revert FeeTooLow();\r\n        if (minters[_minter] != 0) revert AlreadyRegistered();\r\n        _collectProfits(address(this), msg.sender, _applicationFee);\r\n        minters[_minter] = block.timestamp + _applicationPeriod;\r\n        emit MinterApplied(_minter, _applicationPeriod, _applicationFee, _message);\r\n    }\r\n\r\n    /**\r\n     * @notice Make the system more user friendly by skipping the allowance in many cases.\r\n     * @dev We trust minters and the positions they have created to mint and burn as they please, so\r\n     * giving them arbitrary allowances does not pose an additional risk.\r\n     */\r\n    function _allowance(address owner, address spender) internal view override returns (uint256) {\r\n        uint256 explicit = super._allowance(owner, spender);\r\n        if (explicit > 0) {\r\n            return explicit; // don't waste gas checking minter\r\n        } else if (isMinter(spender) || isMinter(getPositionParent(spender)) || spender == address(reserve)) {\r\n            return INFINITY;\r\n        } else {\r\n            return 0;\r\n        }\r\n    }\r\n\r\n    /**\r\n     * @notice The reserve provided by the owners of collateralized positions.\r\n     * @dev The minter reserve can be used to cover losses after the equity holders have been wiped out.\r\n     */\r\n    function minterReserve() public view returns (uint256) {\r\n        return minterReserveE6 / 1000000;\r\n    }\r\n\r\n    /**\r\n     * @notice Allows minters to register collateralized debt positions, thereby giving them the ability to mint Frankencoins.\r\n     * @dev It is assumed that the responsible minter that registers the position ensures that the position can be trusted.\r\n     */\r\n    function registerPosition(address _position) external override {\r\n        if (!isMinter(msg.sender)) revert NotMinter();\r\n        positions[_position] = msg.sender;\r\n    }\r\n\r\n    /**\r\n     * @notice The amount of equity of the Frankencoin system in ZCHF, owned by the holders of Frankencoin Pool Shares.\r\n     * @dev Note that the equity contract technically holds both the minter reserve as well as the equity, so the minter\r\n     * reserve must be subtracted. All fees and other kind of income is added to the Equity contract and essentially\r\n     * constitutes profits attributable to the pool share holders.\r\n     */\r\n    function equity() public view returns (uint256) {\r\n        uint256 balance = balanceOf(address(reserve));\r\n        uint256 minReserve = minterReserve();\r\n        if (balance <= minReserve) {\r\n            return 0;\r\n        } else {\r\n            return balance - minReserve;\r\n        }\r\n    }\r\n\r\n    /**\r\n     * @notice Qualified pool share holders can deny minters during the application period.\r\n     * @dev Calling this function is relatively cheap thanks to the deletion of a storage slot.\r\n     */\r\n    function denyMinter(address _minter, address[] calldata _helpers, string calldata _message) external override {\r\n        if (block.timestamp > minters[_minter]) revert TooLate();\r\n        reserve.checkQualified(msg.sender, _helpers);\r\n        delete minters[_minter];\r\n        emit MinterDenied(_minter, _message);\r\n    }\r\n\r\n    /**\r\n     * @notice Mints the provided amount of ZCHF to the target address, automatically forwarding\r\n     * the minting fee and the reserve to the right place.\r\n     */\r\n    function mintWithReserve(\r\n        address _target,\r\n        uint256 _amount,\r\n        uint32 _reservePPM,\r\n        uint32 _feesPPM\r\n    ) external override minterOnly {\r\n        uint256 usableMint = (_amount * (1000_000 - _feesPPM - _reservePPM)) / 1000_000; // rounding down is fine\r\n        _mint(_target, usableMint);\r\n        _mint(address(reserve), _amount - usableMint); // rest goes to equity as reserves or as fees\r\n        minterReserveE6 += _amount * _reservePPM;\r\n        emit Profit(msg.sender, (_feesPPM * _amount) / 1000_000);\r\n    }\r\n\r\n    function mint(address _target, uint256 _amount) external override minterOnly {\r\n        _mint(_target, _amount);\r\n    }\r\n\r\n    /**\r\n     * Anyone is allowed to burn their ZCHF.\r\n     */\r\n    function burn(uint256 _amount) external {\r\n        _burn(msg.sender, _amount);\r\n    }\r\n\r\n    /**\r\n     * @notice Burn someone elses ZCHF.\r\n     */\r\n    function burnFrom(address _owner, uint256 _amount) external override minterOnly {\r\n        _burn(_owner, _amount);\r\n    }\r\n\r\n    /**\r\n     * @notice Burn that amount without reclaiming the reserve, but freeing it up and thereby essentially donating it to the\r\n     * pool share holders. This can make sense in combination with 'coverLoss', i.e. when it is the pool share\r\n     * holders that bear the risk and depending on the outcome they make a profit or a loss.\r\n     *\r\n     * Design rule: Minters calling this method are only allowed to so for tokens amounts they previously minted with\r\n     * the same _reservePPM amount.\r\n     *\r\n     * For example, if someone minted 50 ZCHF earlier with a 20% reserve requirement (200000 ppm), they got 40 ZCHF\r\n     * and paid 10 ZCHF into the reserve. Now they want to repay the debt by burning 50 ZCHF. When doing so using this\r\n     * method, 50 ZCHF get burned and on top of that, 10 ZCHF previously assigned to the minter's reserved are\r\n     * reassigned to the pool share holders.\r\n     */\r\n    function burnWithoutReserve(uint256 amount, uint32 reservePPM) public override minterOnly {\r\n        _burn(msg.sender, amount);\r\n        uint256 reserveReduction = amount * reservePPM;\r\n        if (reserveReduction > minterReserveE6) {\r\n            emit Profit(msg.sender, minterReserveE6 / 1000_000);\r\n            minterReserveE6 = 0; // should never happen, but we want robust behavior in case it does\r\n        } else {\r\n            minterReserveE6 -= reserveReduction;\r\n            emit Profit(msg.sender, reserveReduction / 1000_000);\r\n        }\r\n    }\r\n\r\n    /**\r\n     * @notice Burns the provided number of tokens plus whatever reserves are associated with that amount given the reserve\r\n     * requirement. The caller is only allowed to use this method for tokens also minted through the caller with the\r\n     * same _reservePPM amount.\r\n     *\r\n     * Example: the calling contract has previously minted 100 ZCHF with a reserve ratio of 20% (i.e. 200000 ppm).\r\n     * Now they have 41 ZCHF that they do not need so they decide to repay that amount. Assuming the reserves are\r\n     * only 90% covered, the call to burnWithReserve will burn the 41 plus 9 from the reserve, reducing the outstanding\r\n     * 'debt' of the caller by 50 ZCHF in total. This total is returned by the method so the caller knows how much less\r\n     * they owe.\r\n     */\r\n    function burnWithReserve(\r\n        uint256 _amountExcludingReserve,\r\n        uint32 _reservePPM\r\n    ) external override minterOnly returns (uint256) {\r\n        uint256 freedAmount = calculateFreedAmount(_amountExcludingReserve, _reservePPM); // 50 in the example\r\n        minterReserveE6 -= freedAmount * _reservePPM; // reduce reserve requirements by original ratio\r\n        _transfer(address(reserve), msg.sender, freedAmount - _amountExcludingReserve); // collect assigned reserve\r\n        _burn(msg.sender, freedAmount); // burn the rest of the freed amount\r\n        return freedAmount;\r\n    }\r\n\r\n    /**\r\n     * @notice Burns the target amount taking the tokens to be burned from the payer and the payer's reserve.\r\n     * Only use this method for tokens also minted by the caller with the same _reservePPM.\r\n     *\r\n     * Example: the calling contract has previously minted 100 ZCHF with a reserve ratio of 20% (i.e. 200000 ppm).\r\n     * To burn half of that again, the minter calls burnFrom with a target amount of 50 ZCHF. Assuming that reserves\r\n     * are only 90% covered, this call will deduct 41 ZCHF from the payer's balance and 9 from the reserve, while\r\n     * reducing the minter reserve by 10.\r\n     */\r\n    function burnFromWithReserve(\r\n        address payer,\r\n        uint256 targetTotalBurnAmount,\r\n        uint32 reservePPM\r\n    ) external override minterOnly returns (uint256) {\r\n        uint256 assigned = calculateAssignedReserve(targetTotalBurnAmount, reservePPM);\r\n        _transfer(address(reserve), payer, assigned); // send reserve to owner\r\n        _burn(payer, targetTotalBurnAmount); // and burn the full amount from the owner's address\r\n        minterReserveE6 -= targetTotalBurnAmount * reservePPM; // reduce reserve requirements by original ratio\r\n        return assigned;\r\n    }\r\n\r\n    /**\r\n     * @notice Calculates the reserve attributable to someone who minted the given amount with the given reserve requirement.\r\n     * Under normal circumstances, this is just the reserver requirement multiplied by the amount. However, after a\r\n     * severe loss of capital that burned into the minter's reserve, this can also be less than that.\r\n     */\r\n    function calculateAssignedReserve(uint256 mintedAmount, uint32 _reservePPM) public view returns (uint256) {\r\n        uint256 theoreticalReserve = (_reservePPM * mintedAmount) / 1000000;\r\n        uint256 currentReserve = balanceOf(address(reserve));\r\n        uint256 minterReserve_ = minterReserve();\r\n        if (currentReserve < minterReserve_) {\r\n            // not enough reserves, owner has to take a loss\r\n            return (theoreticalReserve * currentReserve) / minterReserve_;\r\n        } else {\r\n            return theoreticalReserve;\r\n        }\r\n    }\r\n\r\n    /**\r\n     * @notice Calculate the amount that is freed when returning amountExcludingReserve given a reserve ratio of reservePPM,\r\n     * taking into account potential losses. Example values in the comments.\r\n     */\r\n    function calculateFreedAmount(\r\n        uint256 amountExcludingReserve /* 41 */,\r\n        uint32 reservePPM /* 20% */\r\n    ) public view returns (uint256) {\r\n        uint256 currentReserve = balanceOf(address(reserve)); // 18, 10% below what we should have\r\n        uint256 minterReserve_ = minterReserve(); // 20\r\n        uint256 adjustedReservePPM = currentReserve < minterReserve_\r\n            ? (reservePPM * currentReserve) / minterReserve_\r\n            : reservePPM; // 18%\r\n        return (1000000 * amountExcludingReserve) / (1000000 - adjustedReservePPM); // 41 / (1-18%) = 50\r\n    }\r\n\r\n    /**\r\n     * @notice Notify the Frankencoin that a minter lost economic access to some coins. This does not mean that the coins are\r\n     * literally lost. It just means that some ZCHF will likely never be repaid and that in order to bring the system\r\n     * back into balance, the lost amount of ZCHF must be removed from the reserve instead.\r\n     *\r\n     * For example, if a minter printed 1 million ZCHF for a mortgage and the mortgage turned out to be unsound with\r\n     * the house only yielding 800'000 in the subsequent auction, there is a loss of 200'000 that needs to be covered\r\n     * by the reserve.\r\n     */\r\n    function coverLoss(address source, uint256 _amount) external override minterOnly {\r\n        uint256 reserveLeft = balanceOf(address(reserve));\r\n        if (reserveLeft >= _amount) {\r\n            _transfer(address(reserve), source, _amount);\r\n        } else {\r\n            _transfer(address(reserve), source, reserveLeft);\r\n            _mint(source, _amount - reserveLeft);\r\n        }\r\n        emit Loss(source, _amount);\r\n    }\r\n\r\n    function collectProfits(address source, uint256 _amount) external override minterOnly {\r\n        _collectProfits(msg.sender, source, _amount);\r\n    }\r\n\r\n    function _collectProfits(address minter, address source, uint256 _amount) internal {\r\n        _transfer(source, address(reserve), _amount);\r\n        emit Profit(minter, _amount);\r\n    }\r\n\r\n    /**\r\n     * @notice Returns true if the address is an approved minter.\r\n     */\r\n    function isMinter(address _minter) public view override returns (bool) {\r\n        return minters[_minter] != 0 && block.timestamp >= minters[_minter];\r\n    }\r\n\r\n    /**\r\n     * @notice Returns the address of the minter that created this position or null if the provided address is unknown.\r\n     */\r\n    function getPositionParent(address _position) public view override returns (address) {\r\n        return positions[_position];\r\n    }\r\n}\r\n"},{"file_path":"contracts/interface/IERC20.sol","source_code":"/**\r\n * SPDX-License-Identifier: MIT\r\n *\r\n * Copyright (c) 2016-2019 zOS Global Limited\r\n *\r\n */\r\npragma solidity ^0.8.0;\r\n\r\n/**\r\n * @dev Interface of the ERC20 standard as defined in the EIP. Does not include\r\n * the optional functions; to access them see `ERC20Detailed`.\r\n */\r\n\r\ninterface IERC20 {\r\n    function name() external view returns (string memory);\r\n\r\n    function symbol() external view returns (string memory);\r\n\r\n    function decimals() external view returns (uint8);\r\n\r\n    /**\r\n     * @dev Returns the amount of tokens in existence.\r\n     */\r\n    function totalSupply() external view returns (uint256);\r\n\r\n    /**\r\n     * @dev Returns the amount of tokens owned by `account`.\r\n     */\r\n    function balanceOf(address account) external view returns (uint256);\r\n\r\n    /**\r\n     * @dev Moves `amount` tokens from the caller's account to `recipient`.\r\n     *\r\n     * Returns always true. Throws error on failure.\r\n     *\r\n     * Emits a `Transfer` event.\r\n     */\r\n    function transfer(address recipient, uint256 amount) external returns (bool);\r\n\r\n    /**\r\n     * @dev Returns the remaining number of tokens that `spender` will be\r\n     * allowed to spend on behalf of `owner` through `transferFrom`. This is\r\n     * zero by default.\r\n     *\r\n     * This value can change when `approve` or `transferFrom` are called.\r\n     */\r\n    function allowance(address owner, address spender) external view returns (uint256);\r\n\r\n    /**\r\n     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.\r\n     *\r\n     * Returns a boolean value indicating whether the operation succeeded.\r\n     *\r\n     * > Beware that changing an allowance with this method brings the risk\r\n     * that someone may use both the old and the new allowance by unfortunate\r\n     * transaction ordering. One possible solution to mitigate this race\r\n     * condition is to first reduce the spender's allowance to 0 and set the\r\n     * desired value afterwards:\r\n     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729\r\n     *\r\n     * Emits an `Approval` event.\r\n     */\r\n    function approve(address spender, uint256 amount) external returns (bool);\r\n\r\n    /**\r\n     * @dev Moves `amount` tokens from `sender` to `recipient` using the\r\n     * allowance mechanism. `amount` is then deducted from the caller's\r\n     * allowance.\r\n     *\r\n     * Returns always true. Throws error on failure.\r\n     *\r\n     * Emits a `Transfer` event.\r\n     */\r\n    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);\r\n\r\n    /**\r\n     * @dev Emitted when `value` tokens are moved from one account (`from`) to\r\n     * another (`to`).\r\n     *\r\n     * Note that `value` may be zero.\r\n     */\r\n    event Transfer(address indexed from, address indexed to, uint256 value);\r\n\r\n    /**\r\n     * @dev Emitted when the allowance of a `spender` for an `owner` is set by\r\n     * a call to `approve`. `value` is the new allowance.\r\n     */\r\n    event Approval(address indexed owner, address indexed spender, uint256 value);\r\n}\r\n"},{"file_path":"contracts/interface/IERC677Receiver.sol","source_code":"// SPDX-License-Identifier: MIT\r\npragma solidity ^0.8.0;\r\n\r\ninterface IERC677Receiver {\r\n    \r\n    function onTokenTransfer(address from, uint256 amount, bytes calldata data) external returns (bool);\r\n\r\n}"},{"file_path":"contracts/interface/IFrankencoin.sol","source_code":"// SPDX-License-Identifier: MIT\r\npragma solidity ^0.8.0;\r\n\r\nimport \"./IERC20.sol\";\r\nimport \"./IReserve.sol\";\r\n\r\ninterface IFrankencoin is IERC20 {\r\n    function suggestMinter(address _minter, uint256 _applicationPeriod, uint256 _applicationFee, string calldata _message) external;\r\n\r\n    function registerPosition(address position) external;\r\n\r\n    function denyMinter(address minter, address[] calldata helpers, string calldata message) external;\r\n\r\n    function reserve() external view returns (IReserve);\r\n\r\n    function minterReserve() external view returns (uint256);\r\n\r\n    function calculateAssignedReserve(uint256 mintedAmount, uint32 _reservePPM) external view returns (uint256);\r\n\r\n    function equity() external view returns (uint256);\r\n\r\n    function isMinter(address minter) external view returns (bool);\r\n\r\n    function getPositionParent(address position) external view returns (address);\r\n\r\n    function mint(address target, uint256 amount) external;\r\n\r\n    function mintWithReserve(address target, uint256 amount, uint32 reservePPM, uint32 feePPM) external;\r\n\r\n    function burnFrom(address target, uint256 amount) external;\r\n\r\n    function burnWithoutReserve(uint256 amountIncludingReserve, uint32 reservePPM) external;\r\n\r\n    function burnFromWithReserve(address payer, uint256 targetTotalBurnAmount, uint32 _reservePPM) external returns (uint256);\r\n\r\n    function burnWithReserve(uint256 amountExcludingReserve, uint32 reservePPM) external returns (uint256);\r\n\r\n    function coverLoss(address source, uint256 amount) external;\r\n\r\n    function collectProfits(address source, uint256 _amount) external;\r\n}\r\n"},{"file_path":"contracts/interface/IReserve.sol","source_code":"// SPDX-License-Identifier: MIT\r\npragma solidity ^0.8.0;\r\n\r\nimport \"./IERC20.sol\";\r\n\r\ninterface IReserve is IERC20 {\r\n   function invest(uint256 amount, uint256 expected) external returns (uint256);\r\n   function checkQualified(address sender, address[] calldata helpers) external view;\r\n}"},{"file_path":"contracts/utils/ERC20.sol","source_code":"// SPDX-License-Identifier: MIT\r\n// Copied and adjusted from OpenZeppelin\r\n// Adjustments:\r\n// - modifications to support ERC-677\r\n// - removed require messages to save space\r\n// - removed unnecessary require statements\r\n// - removed GSN Context\r\n// - upgraded to 0.8 to drop SafeMath\r\n// - let name() and symbol() be implemented by subclass\r\n// - infinite allowance support, with 2^255 and above considered infinite\r\n\r\npragma solidity ^0.8.0;\r\n\r\nimport \"../interface/IERC20.sol\";\r\nimport \"../interface/IERC677Receiver.sol\";\r\n\r\n/**\r\n * @dev Implementation of the `IERC20` interface.\r\n *\r\n * This implementation is agnostic to the way tokens are created. This means\r\n * that a supply mechanism has to be added in a derived contract using `_mint`.\r\n * For a generic mechanism see `ERC20Mintable`.\r\n *\r\n * *For a detailed writeup see our guide [How to implement supply\r\n * mechanisms](https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226).*\r\n *\r\n * We have followed general OpenZeppelin guidelines: functions revert instead\r\n * of returning `false` on failure. This behavior is nonetheless conventional\r\n * and does not conflict with the expectations of ERC20 applications.\r\n *\r\n * Additionally, an `Approval` event is emitted on calls to `transferFrom`.\r\n * This allows applications to reconstruct the allowance for all accounts just\r\n * by listening to said events. Other implementations of the EIP may not emit\r\n * these events, as it isn't required by the specification.\r\n *\r\n */\r\n\r\nabstract contract ERC20 is IERC20 {\r\n    mapping(address account => uint256 balance) private _balances;\r\n\r\n    mapping(address account => mapping(address spender => uint256 allowance)) private _allowances;\r\n\r\n    uint256 internal constant INFINITY = (1 << 255);\r\n\r\n    uint256 private _totalSupply;\r\n\r\n    uint8 public immutable override decimals;\r\n\r\n    // Copied from https://github.com/OpenZeppelin/openzeppelin-contracts/pull/4139/files#diff-fa792f7d08644eebc519dac2c29b00a54afc4c6a76b9ef3bba56c8401fe674f6\r\n    // Indicates an error related to the current balance of a sender. Used in transfers.\r\n    error ERC20InsufficientBalance(address sender, uint256 balance, uint256 needed);\r\n    // Indicates a failure with the spender’s allowance. Used in transfers.\r\n    error ERC20InsufficientAllowance(address spender, uint256 allowance, uint256 needed);\r\n\r\n    constructor(uint8 _decimals) {\r\n        decimals = _decimals;\r\n    }\r\n\r\n    /**\r\n     * @dev See `IERC20.totalSupply`.\r\n     */\r\n    function totalSupply() public view override returns (uint256) {\r\n        return _totalSupply;\r\n    }\r\n\r\n    /**\r\n     * @dev See `IERC20.balanceOf`.\r\n     */\r\n    function balanceOf(address account) public view override returns (uint256) {\r\n        return _balances[account];\r\n    }\r\n\r\n    /**\r\n     * @dev See `IERC20.transfer`.\r\n     *\r\n     * Requirements:\r\n     *\r\n     * - `recipient` cannot be the zero address.\r\n     * - the caller must have a balance of at least `amount`.\r\n     */\r\n    function transfer(address recipient, uint256 amount) public virtual override returns (bool) {\r\n        _transfer(msg.sender, recipient, amount);\r\n        return true;\r\n    }\r\n\r\n    /**\r\n     * @dev See `IERC20.allowance`.\r\n     */\r\n    function allowance(address owner, address spender) external view override returns (uint256) {\r\n        return _allowance(owner, spender);\r\n    }\r\n\r\n    function _allowance(address owner, address spender) internal view virtual returns (uint256) {\r\n        return _allowances[owner][spender];\r\n    }\r\n\r\n    /**\r\n     * @dev See `IERC20.approve`.\r\n     *\r\n     * Requirements:\r\n     *\r\n     * - `spender` cannot be the zero address.\r\n     */\r\n    function approve(address spender, uint256 value) external override returns (bool) {\r\n        _approve(msg.sender, spender, value);\r\n        return true;\r\n    }\r\n\r\n    /**\r\n     * @dev See `IERC20.transferFrom`.\r\n     *\r\n     * Emits an `Approval` event indicating the updated allowance. This is not\r\n     * required by the EIP. See the note at the beginning of `ERC20`;\r\n     *\r\n     * Requirements:\r\n     * - `sender` and `recipient` cannot be the zero address.\r\n     * - `sender` must have a balance of at least `value`.\r\n     * - the caller must have allowance for `sender`'s tokens of at least\r\n     * `amount`.\r\n     */\r\n    function transferFrom(address sender, address recipient, uint256 amount) external override returns (bool) {\r\n        _transfer(sender, recipient, amount);\r\n        _useAllowance(sender, msg.sender, amount);\r\n        return true;\r\n    }\r\n\r\n    function _useAllowance(address owner, address spender, uint256 amount) internal {\r\n        uint256 currentAllowance = _allowance(owner, spender);\r\n        if (currentAllowance < INFINITY) {\r\n            // Only decrease the allowance if it was not set to 'infinite'\r\n            // Documented in github.com/aktionariat/contracts/blob/master/doc/infiniteallowance.md\r\n            if (currentAllowance < amount) revert ERC20InsufficientAllowance(owner, currentAllowance, amount);\r\n            _approve(owner, spender, currentAllowance - amount);\r\n        }\r\n    }\r\n\r\n    /**\r\n     * @dev Moves tokens `amount` from `sender` to `recipient`.\r\n     *\r\n     * This is internal function is equivalent to `transfer`, and can be used to\r\n     * e.g. implement automatic token fees, slashing mechanisms, etc.\r\n     *\r\n     * Emits a `Transfer` event.\r\n     *\r\n     * Requirements:\r\n     *\r\n     * - `sender` cannot be the zero address.\r\n     * - `recipient` cannot be the zero address.\r\n     * - `sender` must have a balance of at least `amount`.\r\n     */\r\n    function _transfer(address sender, address recipient, uint256 amount) internal virtual {\r\n        require(recipient != address(0));\r\n\r\n        _beforeTokenTransfer(sender, recipient, amount);\r\n        if (_balances[sender] < amount) revert ERC20InsufficientBalance(sender, _balances[sender], amount);\r\n        _balances[sender] -= amount;\r\n        _balances[recipient] += amount;\r\n        emit Transfer(sender, recipient, amount);\r\n    }\r\n\r\n    /** @dev Creates `amount` tokens and assigns them to `account`, increasing\r\n     * the total supply.\r\n     *\r\n     * Emits a `Transfer` event with `from` set to the zero address.\r\n     *\r\n     * Requirements\r\n     *\r\n     * - `to` cannot be the zero address.\r\n     */\r\n    function _mint(address recipient, uint256 amount) internal virtual {\r\n        require(recipient != address(0));\r\n\r\n        _beforeTokenTransfer(address(0), recipient, amount);\r\n\r\n        _totalSupply += amount;\r\n        _balances[recipient] += amount;\r\n        emit Transfer(address(0), recipient, amount);\r\n    }\r\n\r\n    /**\r\n     * @dev Destroys `amount` tokens from `account`, reducing the\r\n     * total supply.\r\n     *\r\n     * Emits a `Transfer` event with `to` set to the zero address.\r\n     */\r\n    function _burn(address account, uint256 amount) internal virtual {\r\n        _beforeTokenTransfer(account, address(0), amount);\r\n\r\n        _totalSupply -= amount;\r\n        _balances[account] -= amount;\r\n        emit Transfer(account, address(0), amount);\r\n    }\r\n\r\n    /**\r\n     * @dev Sets `amount` as the allowance of `spender` over the `owner`s tokens.\r\n     *\r\n     * This is internal function is equivalent to `approve`, and can be used to\r\n     * e.g. set automatic allowances for certain subsystems, etc.\r\n     *\r\n     * Emits an `Approval` event.\r\n     */\r\n    function _approve(address owner, address spender, uint256 value) internal {\r\n        _allowances[owner][spender] = value;\r\n        emit Approval(owner, spender, value);\r\n    }\r\n\r\n    /**\r\n     * @dev Hook that is called before any transfer of tokens. This includes\r\n     * minting and burning.\r\n     *\r\n     * Calling conditions:\r\n     *\r\n     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens\r\n     * will be to transferred to `to`.\r\n     * - when `from` is zero, `amount` tokens will be minted for `to`.\r\n     * - when `to` is zero, `amount` of ``from``'s tokens will be burned.\r\n     * - `from` and `to` are never both zero.\r\n     *\r\n     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].\r\n     */\r\n    function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual {}\r\n}\r\n"},{"file_path":"contracts/utils/ERC20PermitLight.sol","source_code":"// SPDX-License-Identifier: MIT\r\n// Copied from https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC20.sol\r\n// and modified it.\r\n\r\npragma solidity ^0.8.0;\r\n\r\nimport \"./ERC20.sol\";\r\n\r\nabstract contract ERC20PermitLight is ERC20 {\r\n    /*//////////////////////////////////////////////////////////////\r\n                            EIP-2612 STORAGE\r\n    //////////////////////////////////////////////////////////////*/\r\n\r\n    mapping(address account => uint256 nonce) public nonces;\r\n\r\n    /*//////////////////////////////////////////////////////////////\r\n                             EIP-2612 LOGIC\r\n    //////////////////////////////////////////////////////////////*/\r\n\r\n    function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public {\r\n        require(deadline >= block.timestamp, \"PERMIT_DEADLINE_EXPIRED\");\r\n\r\n        unchecked {\r\n            // unchecked to save a little gas with the nonce increment...\r\n            address recoveredAddress = ecrecover(\r\n                keccak256(\r\n                    abi.encodePacked(\r\n                        \"\\x19\\x01\",\r\n                        DOMAIN_SEPARATOR(),\r\n                        keccak256(\r\n                            abi.encode(\r\n                                // keccak256(\"Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)\"),\r\n                                bytes32(0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9),\r\n                                owner,\r\n                                spender,\r\n                                value,\r\n                                nonces[owner]++,\r\n                                deadline\r\n                            )\r\n                        )\r\n                    )\r\n                ),\r\n                v,\r\n                r,\r\n                s\r\n            );\r\n\r\n            require(recoveredAddress != address(0) && recoveredAddress == owner, \"INVALID_SIGNER\");\r\n            _approve(recoveredAddress, spender, value);\r\n        }\r\n    }\r\n\r\n    function DOMAIN_SEPARATOR() public view returns (bytes32) {\r\n        return\r\n            keccak256(\r\n                abi.encode(\r\n                    //keccak256(\"EIP712Domain(uint256 chainId,address verifyingContract)\");\r\n                    bytes32(0x47e79534a245952e8b16893a336b85a3d9ea9fa8c573f3d803afb92a79469218),\r\n                    block.chainid,\r\n                    address(this)\r\n                )\r\n            );\r\n    }\r\n}\r\n"},{"file_path":"contracts/utils/MathUtil.sol","source_code":"// SPDX-License-Identifier: MIT\r\n\r\npragma solidity ^0.8.0;\r\n\r\n/**\r\n * @title Functions for share valuation\r\n */\r\ncontract MathUtil {\r\n    uint256 internal constant ONE_DEC18 = 10 ** 18;\r\n\r\n    // Let's go for 12 digits of precision (18-6)\r\n    uint256 internal constant THRESH_DEC18 = 10 ** 6;\r\n\r\n    /**\r\n     * @notice Cubic root with Halley approximation\r\n     *         Number 1e18 decimal\r\n     * @param _v     number for which we calculate x**(1/3)\r\n     * @return returns _v**(1/3)\r\n     */\r\n    function _cubicRoot(uint256 _v) internal pure returns (uint256) {\r\n        // Good first guess for _v slightly above 1.0, which is often the case in the Frankencoin system\r\n        uint256 x = _v > ONE_DEC18 && _v < 10 ** 19 ? (_v - ONE_DEC18) / 3 + ONE_DEC18 : ONE_DEC18;\r\n        uint256 diff;\r\n        do {\r\n            uint256 powX3 = _mulD18(_mulD18(x, x), x);\r\n            uint256 xnew = x * (powX3 + 2 * _v) / (2 * powX3 + _v);\r\n            diff = xnew > x ? xnew - x : x - xnew;\r\n            x = xnew;\r\n        } while (diff > THRESH_DEC18);\r\n        return x;\r\n    }\r\n\r\n    function _mulD18(uint256 _a, uint256 _b) internal pure returns (uint256) {\r\n        return (_a * _b) / ONE_DEC18;\r\n    }\r\n\r\n    function _divD18(uint256 _a, uint256 _b) internal pure returns (uint256) {\r\n        return (_a * ONE_DEC18) / _b;\r\n    }\r\n\r\n    function _power3(uint256 _x) internal pure returns (uint256) {\r\n        return _mulD18(_mulD18(_x, _x), _x);\r\n    }\r\n\r\n    function _min(uint256 a, uint256 b) internal pure returns (uint256) {\r\n        return a < b ? a : b;\r\n    }\r\n}\r\n"}],"certified":false,"conflicting_implementations":null,"abi":[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"allowance","type":"uint256"},{"internalType":"uint256","name":"needed","type":"uint256"}],"name":"ERC20InsufficientAllowance","type":"error"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"uint256","name":"balance","type":"uint256"},{"internalType":"uint256","name":"needed","type":"uint256"}],"name":"ERC20InsufficientBalance","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"depositFor","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"helpers","type":"address[]"}],"name":"halveHoldingDuration","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"underlying","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"unwrap","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"unwrapAndSell","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"withdrawTo","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"wrap","outputs":[],"stateMutability":"nonpayable","type":"function"}],"is_changed_bytecode":false,"is_partially_verified":false,"constructor_args":null}