Contract 0x278ddde1ea2404c82c5dd71694bc0058e8789b70 2
Contract Overview
Balance:
0 ETH
ETH Value:
$0.00
My Name Tag:
Not Available
Txn Hash | Method |
Block
|
From
|
To
|
Value | [Txn Fee] | |||
---|---|---|---|---|---|---|---|---|---|
0xdfc619ab3399c4c5d40ac6d4544dbb8432c30cc30c76dee13880dcd5923bbbc3 | 0x6101e060 | 19325205 | 602 days 18 hrs ago | 0xc7f8d87734ab2cbf70030ac8aa82abfe3e8126cb | IN | Create: PoolSell | 0 ETH | 0.008458165616 ETH |
[ Download CSV Export ]
Latest 25 internal transaction
[ Download CSV Export ]
This contract may be a proxy contract. Click on More Options and select Is this a proxy? to confirm and enable the "Read as Proxy" & "Write as Proxy" tabs.
Contract Name:
PoolSell
Compiler Version
v0.8.12+commit.f00d7308
Optimization Enabled:
Yes with 200 runs
Other Settings:
default evmVersion
Contract Source Code (Solidity Standard Json-Input format)
// SPDX-License-Identifier: BUSL-1.1 // For further clarification please see https://license.premia.legal pragma solidity ^0.8.0; import {ABDKMath64x64} from "abdk-libraries-solidity/ABDKMath64x64.sol"; import {ABDKMath64x64Token} from "@solidstate/abdk-math-extensions/contracts/ABDKMath64x64Token.sol"; import {EnumerableSet, ERC1155EnumerableStorage} from "@solidstate/contracts/token/ERC1155/enumerable/ERC1155EnumerableStorage.sol"; import {IPoolSell} from "./IPoolSell.sol"; import {PoolInternal} from "./PoolInternal.sol"; import {PoolStorage} from "./PoolStorage.sol"; contract PoolSell is IPoolSell, PoolInternal { using ABDKMath64x64 for int128; using EnumerableSet for EnumerableSet.UintSet; using EnumerableSet for EnumerableSet.AddressSet; using PoolStorage for PoolStorage.Layout; constructor( address ivolOracle, address wrappedNativeToken, address premiaMining, address feeReceiver, address feeDiscountAddress, int128 feePremium64x64, address exchangeHelper ) PoolInternal( ivolOracle, wrappedNativeToken, premiaMining, feeReceiver, feeDiscountAddress, feePremium64x64, exchangeHelper ) {} /** * @inheritdoc IPoolSell */ function setBuybackEnabled(bool state, bool isCallPool) external { PoolStorage.layout().setBuybackEnabled(state, isCallPool); } /** * @inheritdoc IPoolSell */ function isBuybackEnabled(address account, bool isCallPool) external view returns (bool) { return PoolStorage.layout().isBuybackEnabled[account][isCallPool]; } /** * @inheritdoc IPoolSell */ function getAvailableBuybackLiquidity( uint64 maturity, int128 strike64x64, bool isCall ) external view returns (uint256) { uint256 shortTokenId = PoolStorage.formatTokenId( PoolStorage.getTokenType(isCall, false), maturity, strike64x64 ); return _getAvailableBuybackLiquidity(shortTokenId); } /** * @inheritdoc IPoolSell */ function sellQuote( address feePayer, uint64 maturity, int128 strike64x64, int128 spot64x64, uint256 contractSize, bool isCall ) external view returns (int128 baseCost64x64, int128 feeCost64x64) { return _quoteSalePrice( PoolStorage.QuoteArgsInternal( feePayer, maturity, strike64x64, spot64x64, contractSize, isCall ) ); } /** * @inheritdoc IPoolSell */ function sell( uint64 maturity, int128 strike64x64, bool isCall, uint256 contractSize ) external { PoolStorage.Layout storage l = PoolStorage.layout(); uint256 baseCost; uint256 feeCost; int128 newPrice64x64 = _update(l); { (int128 baseCost64x64, int128 feeCost64x64) = _quoteSalePrice( PoolStorage.QuoteArgsInternal( msg.sender, maturity, strike64x64, newPrice64x64, contractSize, isCall ) ); baseCost = ABDKMath64x64Token.toDecimals( baseCost64x64, l.getTokenDecimals(isCall) ); feeCost = ABDKMath64x64Token.toDecimals( feeCost64x64, l.getTokenDecimals(isCall) ); } _burnShortTokenLoop( l, maturity, strike64x64, contractSize, baseCost, isCall, true ); uint256 longTokenId = PoolStorage.formatTokenId( PoolStorage.getTokenType(isCall, true), maturity, strike64x64 ); _burn(msg.sender, longTokenId, contractSize); _processAvailableFunds( msg.sender, baseCost - feeCost, isCall, true, true ); _processAvailableFunds( FEE_RECEIVER_ADDRESS, feeCost, isCall, true, false ); emit Sell( msg.sender, longTokenId, contractSize, baseCost, feeCost, newPrice64x64 ); } }
// SPDX-License-Identifier: BSD-4-Clause /* * ABDK Math 64.64 Smart Contract Library. Copyright © 2019 by ABDK Consulting. * Author: Mikhail Vladimirov <[email protected]> */ pragma solidity ^0.8.0; /** * Smart contract library of mathematical functions operating with signed * 64.64-bit fixed point numbers. Signed 64.64-bit fixed point number is * basically a simple fraction whose numerator is signed 128-bit integer and * denominator is 2^64. As long as denominator is always the same, there is no * need to store it, thus in Solidity signed 64.64-bit fixed point numbers are * represented by int128 type holding only the numerator. */ library ABDKMath64x64 { /* * Minimum value signed 64.64-bit fixed point number may have. */ int128 private constant MIN_64x64 = -0x80000000000000000000000000000000; /* * Maximum value signed 64.64-bit fixed point number may have. */ int128 private constant MAX_64x64 = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; /** * Convert signed 256-bit integer number into signed 64.64-bit fixed point * number. Revert on overflow. * * @param x signed 256-bit integer number * @return signed 64.64-bit fixed point number */ function fromInt (int256 x) internal pure returns (int128) { unchecked { require (x >= -0x8000000000000000 && x <= 0x7FFFFFFFFFFFFFFF); return int128 (x << 64); } } /** * Convert signed 64.64 fixed point number into signed 64-bit integer number * rounding down. * * @param x signed 64.64-bit fixed point number * @return signed 64-bit integer number */ function toInt (int128 x) internal pure returns (int64) { unchecked { return int64 (x >> 64); } } /** * Convert unsigned 256-bit integer number into signed 64.64-bit fixed point * number. Revert on overflow. * * @param x unsigned 256-bit integer number * @return signed 64.64-bit fixed point number */ function fromUInt (uint256 x) internal pure returns (int128) { unchecked { require (x <= 0x7FFFFFFFFFFFFFFF); return int128 (int256 (x << 64)); } } /** * Convert signed 64.64 fixed point number into unsigned 64-bit integer * number rounding down. Revert on underflow. * * @param x signed 64.64-bit fixed point number * @return unsigned 64-bit integer number */ function toUInt (int128 x) internal pure returns (uint64) { unchecked { require (x >= 0); return uint64 (uint128 (x >> 64)); } } /** * Convert signed 128.128 fixed point number into signed 64.64-bit fixed point * number rounding down. Revert on overflow. * * @param x signed 128.128-bin fixed point number * @return signed 64.64-bit fixed point number */ function from128x128 (int256 x) internal pure returns (int128) { unchecked { int256 result = x >> 64; require (result >= MIN_64x64 && result <= MAX_64x64); return int128 (result); } } /** * Convert signed 64.64 fixed point number into signed 128.128 fixed point * number. * * @param x signed 64.64-bit fixed point number * @return signed 128.128 fixed point number */ function to128x128 (int128 x) internal pure returns (int256) { unchecked { return int256 (x) << 64; } } /** * Calculate x + y. Revert on overflow. * * @param x signed 64.64-bit fixed point number * @param y signed 64.64-bit fixed point number * @return signed 64.64-bit fixed point number */ function add (int128 x, int128 y) internal pure returns (int128) { unchecked { int256 result = int256(x) + y; require (result >= MIN_64x64 && result <= MAX_64x64); return int128 (result); } } /** * Calculate x - y. Revert on overflow. * * @param x signed 64.64-bit fixed point number * @param y signed 64.64-bit fixed point number * @return signed 64.64-bit fixed point number */ function sub (int128 x, int128 y) internal pure returns (int128) { unchecked { int256 result = int256(x) - y; require (result >= MIN_64x64 && result <= MAX_64x64); return int128 (result); } } /** * Calculate x * y rounding down. Revert on overflow. * * @param x signed 64.64-bit fixed point number * @param y signed 64.64-bit fixed point number * @return signed 64.64-bit fixed point number */ function mul (int128 x, int128 y) internal pure returns (int128) { unchecked { int256 result = int256(x) * y >> 64; require (result >= MIN_64x64 && result <= MAX_64x64); return int128 (result); } } /** * Calculate x * y rounding towards zero, where x is signed 64.64 fixed point * number and y is signed 256-bit integer number. Revert on overflow. * * @param x signed 64.64 fixed point number * @param y signed 256-bit integer number * @return signed 256-bit integer number */ function muli (int128 x, int256 y) internal pure returns (int256) { unchecked { if (x == MIN_64x64) { require (y >= -0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF && y <= 0x1000000000000000000000000000000000000000000000000); return -y << 63; } else { bool negativeResult = false; if (x < 0) { x = -x; negativeResult = true; } if (y < 0) { y = -y; // We rely on overflow behavior here negativeResult = !negativeResult; } uint256 absoluteResult = mulu (x, uint256 (y)); if (negativeResult) { require (absoluteResult <= 0x8000000000000000000000000000000000000000000000000000000000000000); return -int256 (absoluteResult); // We rely on overflow behavior here } else { require (absoluteResult <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); return int256 (absoluteResult); } } } } /** * Calculate x * y rounding down, where x is signed 64.64 fixed point number * and y is unsigned 256-bit integer number. Revert on overflow. * * @param x signed 64.64 fixed point number * @param y unsigned 256-bit integer number * @return unsigned 256-bit integer number */ function mulu (int128 x, uint256 y) internal pure returns (uint256) { unchecked { if (y == 0) return 0; require (x >= 0); uint256 lo = (uint256 (int256 (x)) * (y & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)) >> 64; uint256 hi = uint256 (int256 (x)) * (y >> 128); require (hi <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); hi <<= 64; require (hi <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF - lo); return hi + lo; } } /** * Calculate x / y rounding towards zero. Revert on overflow or when y is * zero. * * @param x signed 64.64-bit fixed point number * @param y signed 64.64-bit fixed point number * @return signed 64.64-bit fixed point number */ function div (int128 x, int128 y) internal pure returns (int128) { unchecked { require (y != 0); int256 result = (int256 (x) << 64) / y; require (result >= MIN_64x64 && result <= MAX_64x64); return int128 (result); } } /** * Calculate x / y rounding towards zero, where x and y are signed 256-bit * integer numbers. Revert on overflow or when y is zero. * * @param x signed 256-bit integer number * @param y signed 256-bit integer number * @return signed 64.64-bit fixed point number */ function divi (int256 x, int256 y) internal pure returns (int128) { unchecked { require (y != 0); bool negativeResult = false; if (x < 0) { x = -x; // We rely on overflow behavior here negativeResult = true; } if (y < 0) { y = -y; // We rely on overflow behavior here negativeResult = !negativeResult; } uint128 absoluteResult = divuu (uint256 (x), uint256 (y)); if (negativeResult) { require (absoluteResult <= 0x80000000000000000000000000000000); return -int128 (absoluteResult); // We rely on overflow behavior here } else { require (absoluteResult <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); return int128 (absoluteResult); // We rely on overflow behavior here } } } /** * Calculate x / y rounding towards zero, where x and y are unsigned 256-bit * integer numbers. Revert on overflow or when y is zero. * * @param x unsigned 256-bit integer number * @param y unsigned 256-bit integer number * @return signed 64.64-bit fixed point number */ function divu (uint256 x, uint256 y) internal pure returns (int128) { unchecked { require (y != 0); uint128 result = divuu (x, y); require (result <= uint128 (MAX_64x64)); return int128 (result); } } /** * Calculate -x. Revert on overflow. * * @param x signed 64.64-bit fixed point number * @return signed 64.64-bit fixed point number */ function neg (int128 x) internal pure returns (int128) { unchecked { require (x != MIN_64x64); return -x; } } /** * Calculate |x|. Revert on overflow. * * @param x signed 64.64-bit fixed point number * @return signed 64.64-bit fixed point number */ function abs (int128 x) internal pure returns (int128) { unchecked { require (x != MIN_64x64); return x < 0 ? -x : x; } } /** * Calculate 1 / x rounding towards zero. Revert on overflow or when x is * zero. * * @param x signed 64.64-bit fixed point number * @return signed 64.64-bit fixed point number */ function inv (int128 x) internal pure returns (int128) { unchecked { require (x != 0); int256 result = int256 (0x100000000000000000000000000000000) / x; require (result >= MIN_64x64 && result <= MAX_64x64); return int128 (result); } } /** * Calculate arithmetics average of x and y, i.e. (x + y) / 2 rounding down. * * @param x signed 64.64-bit fixed point number * @param y signed 64.64-bit fixed point number * @return signed 64.64-bit fixed point number */ function avg (int128 x, int128 y) internal pure returns (int128) { unchecked { return int128 ((int256 (x) + int256 (y)) >> 1); } } /** * Calculate geometric average of x and y, i.e. sqrt (x * y) rounding down. * Revert on overflow or in case x * y is negative. * * @param x signed 64.64-bit fixed point number * @param y signed 64.64-bit fixed point number * @return signed 64.64-bit fixed point number */ function gavg (int128 x, int128 y) internal pure returns (int128) { unchecked { int256 m = int256 (x) * int256 (y); require (m >= 0); require (m < 0x4000000000000000000000000000000000000000000000000000000000000000); return int128 (sqrtu (uint256 (m))); } } /** * Calculate x^y assuming 0^0 is 1, where x is signed 64.64 fixed point number * and y is unsigned 256-bit integer number. Revert on overflow. * * @param x signed 64.64-bit fixed point number * @param y uint256 value * @return signed 64.64-bit fixed point number */ function pow (int128 x, uint256 y) internal pure returns (int128) { unchecked { bool negative = x < 0 && y & 1 == 1; uint256 absX = uint128 (x < 0 ? -x : x); uint256 absResult; absResult = 0x100000000000000000000000000000000; if (absX <= 0x10000000000000000) { absX <<= 63; while (y != 0) { if (y & 0x1 != 0) { absResult = absResult * absX >> 127; } absX = absX * absX >> 127; if (y & 0x2 != 0) { absResult = absResult * absX >> 127; } absX = absX * absX >> 127; if (y & 0x4 != 0) { absResult = absResult * absX >> 127; } absX = absX * absX >> 127; if (y & 0x8 != 0) { absResult = absResult * absX >> 127; } absX = absX * absX >> 127; y >>= 4; } absResult >>= 64; } else { uint256 absXShift = 63; if (absX < 0x1000000000000000000000000) { absX <<= 32; absXShift -= 32; } if (absX < 0x10000000000000000000000000000) { absX <<= 16; absXShift -= 16; } if (absX < 0x1000000000000000000000000000000) { absX <<= 8; absXShift -= 8; } if (absX < 0x10000000000000000000000000000000) { absX <<= 4; absXShift -= 4; } if (absX < 0x40000000000000000000000000000000) { absX <<= 2; absXShift -= 2; } if (absX < 0x80000000000000000000000000000000) { absX <<= 1; absXShift -= 1; } uint256 resultShift = 0; while (y != 0) { require (absXShift < 64); if (y & 0x1 != 0) { absResult = absResult * absX >> 127; resultShift += absXShift; if (absResult > 0x100000000000000000000000000000000) { absResult >>= 1; resultShift += 1; } } absX = absX * absX >> 127; absXShift <<= 1; if (absX >= 0x100000000000000000000000000000000) { absX >>= 1; absXShift += 1; } y >>= 1; } require (resultShift < 64); absResult >>= 64 - resultShift; } int256 result = negative ? -int256 (absResult) : int256 (absResult); require (result >= MIN_64x64 && result <= MAX_64x64); return int128 (result); } } /** * Calculate sqrt (x) rounding down. Revert if x < 0. * * @param x signed 64.64-bit fixed point number * @return signed 64.64-bit fixed point number */ function sqrt (int128 x) internal pure returns (int128) { unchecked { require (x >= 0); return int128 (sqrtu (uint256 (int256 (x)) << 64)); } } /** * Calculate binary logarithm of x. Revert if x <= 0. * * @param x signed 64.64-bit fixed point number * @return signed 64.64-bit fixed point number */ function log_2 (int128 x) internal pure returns (int128) { unchecked { require (x > 0); int256 msb = 0; int256 xc = x; if (xc >= 0x10000000000000000) { xc >>= 64; msb += 64; } if (xc >= 0x100000000) { xc >>= 32; msb += 32; } if (xc >= 0x10000) { xc >>= 16; msb += 16; } if (xc >= 0x100) { xc >>= 8; msb += 8; } if (xc >= 0x10) { xc >>= 4; msb += 4; } if (xc >= 0x4) { xc >>= 2; msb += 2; } if (xc >= 0x2) msb += 1; // No need to shift xc anymore int256 result = msb - 64 << 64; uint256 ux = uint256 (int256 (x)) << uint256 (127 - msb); for (int256 bit = 0x8000000000000000; bit > 0; bit >>= 1) { ux *= ux; uint256 b = ux >> 255; ux >>= 127 + b; result += bit * int256 (b); } return int128 (result); } } /** * Calculate natural logarithm of x. Revert if x <= 0. * * @param x signed 64.64-bit fixed point number * @return signed 64.64-bit fixed point number */ function ln (int128 x) internal pure returns (int128) { unchecked { require (x > 0); return int128 (int256 ( uint256 (int256 (log_2 (x))) * 0xB17217F7D1CF79ABC9E3B39803F2F6AF >> 128)); } } /** * Calculate binary exponent of x. Revert on overflow. * * @param x signed 64.64-bit fixed point number * @return signed 64.64-bit fixed point number */ function exp_2 (int128 x) internal pure returns (int128) { unchecked { require (x < 0x400000000000000000); // Overflow if (x < -0x400000000000000000) return 0; // Underflow uint256 result = 0x80000000000000000000000000000000; if (x & 0x8000000000000000 > 0) result = result * 0x16A09E667F3BCC908B2FB1366EA957D3E >> 128; if (x & 0x4000000000000000 > 0) result = result * 0x1306FE0A31B7152DE8D5A46305C85EDEC >> 128; if (x & 0x2000000000000000 > 0) result = result * 0x1172B83C7D517ADCDF7C8C50EB14A791F >> 128; if (x & 0x1000000000000000 > 0) result = result * 0x10B5586CF9890F6298B92B71842A98363 >> 128; if (x & 0x800000000000000 > 0) result = result * 0x1059B0D31585743AE7C548EB68CA417FD >> 128; if (x & 0x400000000000000 > 0) result = result * 0x102C9A3E778060EE6F7CACA4F7A29BDE8 >> 128; if (x & 0x200000000000000 > 0) result = result * 0x10163DA9FB33356D84A66AE336DCDFA3F >> 128; if (x & 0x100000000000000 > 0) result = result * 0x100B1AFA5ABCBED6129AB13EC11DC9543 >> 128; if (x & 0x80000000000000 > 0) result = result * 0x10058C86DA1C09EA1FF19D294CF2F679B >> 128; if (x & 0x40000000000000 > 0) result = result * 0x1002C605E2E8CEC506D21BFC89A23A00F >> 128; if (x & 0x20000000000000 > 0) result = result * 0x100162F3904051FA128BCA9C55C31E5DF >> 128; if (x & 0x10000000000000 > 0) result = result * 0x1000B175EFFDC76BA38E31671CA939725 >> 128; if (x & 0x8000000000000 > 0) result = result * 0x100058BA01FB9F96D6CACD4B180917C3D >> 128; if (x & 0x4000000000000 > 0) result = result * 0x10002C5CC37DA9491D0985C348C68E7B3 >> 128; if (x & 0x2000000000000 > 0) result = result * 0x1000162E525EE054754457D5995292026 >> 128; if (x & 0x1000000000000 > 0) result = result * 0x10000B17255775C040618BF4A4ADE83FC >> 128; if (x & 0x800000000000 > 0) result = result * 0x1000058B91B5BC9AE2EED81E9B7D4CFAB >> 128; if (x & 0x400000000000 > 0) result = result * 0x100002C5C89D5EC6CA4D7C8ACC017B7C9 >> 128; if (x & 0x200000000000 > 0) result = result * 0x10000162E43F4F831060E02D839A9D16D >> 128; if (x & 0x100000000000 > 0) result = result * 0x100000B1721BCFC99D9F890EA06911763 >> 128; if (x & 0x80000000000 > 0) result = result * 0x10000058B90CF1E6D97F9CA14DBCC1628 >> 128; if (x & 0x40000000000 > 0) result = result * 0x1000002C5C863B73F016468F6BAC5CA2B >> 128; if (x & 0x20000000000 > 0) result = result * 0x100000162E430E5A18F6119E3C02282A5 >> 128; if (x & 0x10000000000 > 0) result = result * 0x1000000B1721835514B86E6D96EFD1BFE >> 128; if (x & 0x8000000000 > 0) result = result * 0x100000058B90C0B48C6BE5DF846C5B2EF >> 128; if (x & 0x4000000000 > 0) result = result * 0x10000002C5C8601CC6B9E94213C72737A >> 128; if (x & 0x2000000000 > 0) result = result * 0x1000000162E42FFF037DF38AA2B219F06 >> 128; if (x & 0x1000000000 > 0) result = result * 0x10000000B17217FBA9C739AA5819F44F9 >> 128; if (x & 0x800000000 > 0) result = result * 0x1000000058B90BFCDEE5ACD3C1CEDC823 >> 128; if (x & 0x400000000 > 0) result = result * 0x100000002C5C85FE31F35A6A30DA1BE50 >> 128; if (x & 0x200000000 > 0) result = result * 0x10000000162E42FF0999CE3541B9FFFCF >> 128; if (x & 0x100000000 > 0) result = result * 0x100000000B17217F80F4EF5AADDA45554 >> 128; if (x & 0x80000000 > 0) result = result * 0x10000000058B90BFBF8479BD5A81B51AD >> 128; if (x & 0x40000000 > 0) result = result * 0x1000000002C5C85FDF84BD62AE30A74CC >> 128; if (x & 0x20000000 > 0) result = result * 0x100000000162E42FEFB2FED257559BDAA >> 128; if (x & 0x10000000 > 0) result = result * 0x1000000000B17217F7D5A7716BBA4A9AE >> 128; if (x & 0x8000000 > 0) result = result * 0x100000000058B90BFBE9DDBAC5E109CCE >> 128; if (x & 0x4000000 > 0) result = result * 0x10000000002C5C85FDF4B15DE6F17EB0D >> 128; if (x & 0x2000000 > 0) result = result * 0x1000000000162E42FEFA494F1478FDE05 >> 128; if (x & 0x1000000 > 0) result = result * 0x10000000000B17217F7D20CF927C8E94C >> 128; if (x & 0x800000 > 0) result = result * 0x1000000000058B90BFBE8F71CB4E4B33D >> 128; if (x & 0x400000 > 0) result = result * 0x100000000002C5C85FDF477B662B26945 >> 128; if (x & 0x200000 > 0) result = result * 0x10000000000162E42FEFA3AE53369388C >> 128; if (x & 0x100000 > 0) result = result * 0x100000000000B17217F7D1D351A389D40 >> 128; if (x & 0x80000 > 0) result = result * 0x10000000000058B90BFBE8E8B2D3D4EDE >> 128; if (x & 0x40000 > 0) result = result * 0x1000000000002C5C85FDF4741BEA6E77E >> 128; if (x & 0x20000 > 0) result = result * 0x100000000000162E42FEFA39FE95583C2 >> 128; if (x & 0x10000 > 0) result = result * 0x1000000000000B17217F7D1CFB72B45E1 >> 128; if (x & 0x8000 > 0) result = result * 0x100000000000058B90BFBE8E7CC35C3F0 >> 128; if (x & 0x4000 > 0) result = result * 0x10000000000002C5C85FDF473E242EA38 >> 128; if (x & 0x2000 > 0) result = result * 0x1000000000000162E42FEFA39F02B772C >> 128; if (x & 0x1000 > 0) result = result * 0x10000000000000B17217F7D1CF7D83C1A >> 128; if (x & 0x800 > 0) result = result * 0x1000000000000058B90BFBE8E7BDCBE2E >> 128; if (x & 0x400 > 0) result = result * 0x100000000000002C5C85FDF473DEA871F >> 128; if (x & 0x200 > 0) result = result * 0x10000000000000162E42FEFA39EF44D91 >> 128; if (x & 0x100 > 0) result = result * 0x100000000000000B17217F7D1CF79E949 >> 128; if (x & 0x80 > 0) result = result * 0x10000000000000058B90BFBE8E7BCE544 >> 128; if (x & 0x40 > 0) result = result * 0x1000000000000002C5C85FDF473DE6ECA >> 128; if (x & 0x20 > 0) result = result * 0x100000000000000162E42FEFA39EF366F >> 128; if (x & 0x10 > 0) result = result * 0x1000000000000000B17217F7D1CF79AFA >> 128; if (x & 0x8 > 0) result = result * 0x100000000000000058B90BFBE8E7BCD6D >> 128; if (x & 0x4 > 0) result = result * 0x10000000000000002C5C85FDF473DE6B2 >> 128; if (x & 0x2 > 0) result = result * 0x1000000000000000162E42FEFA39EF358 >> 128; if (x & 0x1 > 0) result = result * 0x10000000000000000B17217F7D1CF79AB >> 128; result >>= uint256 (int256 (63 - (x >> 64))); require (result <= uint256 (int256 (MAX_64x64))); return int128 (int256 (result)); } } /** * Calculate natural exponent of x. Revert on overflow. * * @param x signed 64.64-bit fixed point number * @return signed 64.64-bit fixed point number */ function exp (int128 x) internal pure returns (int128) { unchecked { require (x < 0x400000000000000000); // Overflow if (x < -0x400000000000000000) return 0; // Underflow return exp_2 ( int128 (int256 (x) * 0x171547652B82FE1777D0FFDA0D23A7D12 >> 128)); } } /** * Calculate x / y rounding towards zero, where x and y are unsigned 256-bit * integer numbers. Revert on overflow or when y is zero. * * @param x unsigned 256-bit integer number * @param y unsigned 256-bit integer number * @return unsigned 64.64-bit fixed point number */ function divuu (uint256 x, uint256 y) private pure returns (uint128) { unchecked { require (y != 0); uint256 result; if (x <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) result = (x << 64) / y; else { uint256 msb = 192; uint256 xc = x >> 192; if (xc >= 0x100000000) { xc >>= 32; msb += 32; } if (xc >= 0x10000) { xc >>= 16; msb += 16; } if (xc >= 0x100) { xc >>= 8; msb += 8; } if (xc >= 0x10) { xc >>= 4; msb += 4; } if (xc >= 0x4) { xc >>= 2; msb += 2; } if (xc >= 0x2) msb += 1; // No need to shift xc anymore result = (x << 255 - msb) / ((y - 1 >> msb - 191) + 1); require (result <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); uint256 hi = result * (y >> 128); uint256 lo = result * (y & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); uint256 xh = x >> 192; uint256 xl = x << 64; if (xl < lo) xh -= 1; xl -= lo; // We rely on overflow behavior here lo = hi << 128; if (xl < lo) xh -= 1; xl -= lo; // We rely on overflow behavior here assert (xh == hi >> 128); result += xl / y; } require (result <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); return uint128 (result); } } /** * Calculate sqrt (x) rounding down, where x is unsigned 256-bit integer * number. * * @param x unsigned 256-bit integer number * @return unsigned 128-bit integer number */ function sqrtu (uint256 x) private pure returns (uint128) { unchecked { if (x == 0) return 0; else { uint256 xx = x; uint256 r = 1; if (xx >= 0x100000000000000000000000000000000) { xx >>= 128; r <<= 64; } if (xx >= 0x10000000000000000) { xx >>= 64; r <<= 32; } if (xx >= 0x100000000) { xx >>= 32; r <<= 16; } if (xx >= 0x10000) { xx >>= 16; r <<= 8; } if (xx >= 0x100) { xx >>= 8; r <<= 4; } if (xx >= 0x10) { xx >>= 4; r <<= 2; } if (xx >= 0x8) { r <<= 1; } r = (r + x / r) >> 1; r = (r + x / r) >> 1; r = (r + x / r) >> 1; r = (r + x / r) >> 1; r = (r + x / r) >> 1; r = (r + x / r) >> 1; r = (r + x / r) >> 1; // Seven iterations should be enough uint256 r1 = x / r; return uint128 (r < r1 ? r : r1); } } } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import { ABDKMath64x64 } from 'abdk-libraries-solidity/ABDKMath64x64.sol'; /** * @title SolidState token extensions for ABDKMath64x64 library */ library ABDKMath64x64Token { using ABDKMath64x64 for int128; /** * @notice convert 64x64 fixed point representation of token amount to decimal * @param value64x64 64x64 fixed point representation of token amount * @param decimals token display decimals * @return value decimal representation of token amount */ function toDecimals(int128 value64x64, uint8 decimals) internal pure returns (uint256 value) { value = value64x64.mulu(10**decimals); } /** * @notice convert decimal representation of token amount to 64x64 fixed point * @param value decimal representation of token amount * @param decimals token display decimals * @return value64x64 64x64 fixed point representation of token amount */ function fromDecimals(uint256 value, uint8 decimals) internal pure returns (int128 value64x64) { value64x64 = ABDKMath64x64.divu(value, 10**decimals); } /** * @notice convert 64x64 fixed point representation of token amount to wei (18 decimals) * @param value64x64 64x64 fixed point representation of token amount * @return value wei representation of token amount */ function toWei(int128 value64x64) internal pure returns (uint256 value) { value = toDecimals(value64x64, 18); } /** * @notice convert wei representation (18 decimals) of token amount to 64x64 fixed point * @param value wei representation of token amount * @return value64x64 64x64 fixed point representation of token amount */ function fromWei(uint256 value) internal pure returns (int128 value64x64) { value64x64 = fromDecimals(value, 18); } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import { EnumerableSet } from '../../../utils/EnumerableSet.sol'; library ERC1155EnumerableStorage { struct Layout { mapping(uint256 => uint256) totalSupply; mapping(uint256 => EnumerableSet.AddressSet) accountsByToken; mapping(address => EnumerableSet.UintSet) tokensByAccount; } bytes32 internal constant STORAGE_SLOT = keccak256('solidstate.contracts.storage.ERC1155Enumerable'); function layout() internal pure returns (Layout storage l) { bytes32 slot = STORAGE_SLOT; assembly { l.slot := slot } } }
// SPDX-License-Identifier: LGPL-3.0-or-later pragma solidity ^0.8.0; interface IPoolSell { /** * @notice Enable or disable buyback * @param state whether to enable or disable buyback * @param isCallPool true to set state for call pool, false for put pool */ function setBuybackEnabled(bool state, bool isCallPool) external; /** * @notice Get whether buyback is enabled or not for a given LP * @param account LP account for which to check * @param isCallPool true to get state for call pool, false for put pool * @return whether buyback is enabled or not */ function isBuybackEnabled(address account, bool isCallPool) external view returns (bool); /** * @notice calculate the total available buyback liquidity for an option * @param maturity timestamp of option maturity * @param strike64x64 64x64 fixed point representation of strike price * @param isCall true for call, false for put * @return total available buyback liquidity for this option */ function getAvailableBuybackLiquidity( uint64 maturity, int128 strike64x64, bool isCall ) external view returns (uint256); /** * @notice calculate price of option contract * @param feePayer address of the fee payer * @param maturity timestamp of option maturity * @param strike64x64 64x64 fixed point representation of strike price * @param contractSize size of option contract * @param isCall true for call, false for put * @return baseCost64x64 64x64 fixed point representation of option cost denominated in underlying currency (without fee) * @return feeCost64x64 64x64 fixed point representation of option fee cost denominated in underlying currency for call, or base currency for put */ function sellQuote( address feePayer, uint64 maturity, int128 strike64x64, int128 spot64x64, uint256 contractSize, bool isCall ) external view returns (int128 baseCost64x64, int128 feeCost64x64); /** * @notice sell options back to the pool to LP who enabled buyback * @param maturity timestamp of option maturity * @param strike64x64 64x64 fixed point representation of strike price * @param contractSize size of option contract * @param isCall true for call, false for put */ function sell( uint64 maturity, int128 strike64x64, bool isCall, uint256 contractSize ) external; }
// SPDX-License-Identifier: BUSL-1.1 // For further clarification please see https://license.premia.legal pragma solidity ^0.8.0; import {ABDKMath64x64Token} from "@solidstate/abdk-math-extensions/contracts/ABDKMath64x64Token.sol"; import {IERC173} from "@solidstate/contracts/access/IERC173.sol"; import {OwnableStorage} from "@solidstate/contracts/access/OwnableStorage.sol"; import {SafeERC20} from "@solidstate/contracts/utils/SafeERC20.sol"; import {IERC20} from "@solidstate/contracts/token/ERC20/IERC20.sol"; import {ERC1155EnumerableInternal, ERC1155EnumerableStorage, EnumerableSet} from "@solidstate/contracts/token/ERC1155/enumerable/ERC1155Enumerable.sol"; import {IWETH} from "@solidstate/contracts/utils/IWETH.sol"; import {ABDKMath64x64} from "abdk-libraries-solidity/ABDKMath64x64.sol"; import {IExchangeHelper} from "../interfaces/IExchangeHelper.sol"; import {OptionMath} from "../libraries/OptionMath.sol"; import {IPremiaMining} from "../mining/IPremiaMining.sol"; import {IVolatilitySurfaceOracle} from "../oracle/IVolatilitySurfaceOracle.sol"; import {IFeeDiscount} from "../staking/IFeeDiscount.sol"; import {IPoolEvents} from "./IPoolEvents.sol"; import {IPoolInternal} from "./IPoolInternal.sol"; import {PoolStorage} from "./PoolStorage.sol"; /** * @title Premia option pool * @dev deployed standalone and referenced by PoolProxy */ contract PoolInternal is IPoolInternal, IPoolEvents, ERC1155EnumerableInternal { using ABDKMath64x64 for int128; using EnumerableSet for EnumerableSet.AddressSet; using EnumerableSet for EnumerableSet.UintSet; using PoolStorage for PoolStorage.Layout; using SafeERC20 for IERC20; struct Interval { uint256 contractSize; uint256 tokenAmount; uint256 payment; uint256 apyFee; } address internal immutable WRAPPED_NATIVE_TOKEN; address internal immutable PREMIA_MINING_ADDRESS; address internal immutable FEE_RECEIVER_ADDRESS; address internal immutable FEE_DISCOUNT_ADDRESS; address internal immutable IVOL_ORACLE_ADDRESS; address internal immutable EXCHANGE_HELPER; int128 internal immutable FEE_PREMIUM_64x64; uint256 internal immutable UNDERLYING_FREE_LIQ_TOKEN_ID; uint256 internal immutable BASE_FREE_LIQ_TOKEN_ID; uint256 internal immutable UNDERLYING_RESERVED_LIQ_TOKEN_ID; uint256 internal immutable BASE_RESERVED_LIQ_TOKEN_ID; uint256 internal constant INVERSE_BASIS_POINT = 1e4; uint256 internal constant BATCHING_PERIOD = 260; // Multiply sell quote by this constant int128 internal constant SELL_COEFFICIENT_64x64 = 0xb333333333333333; // 0.7 constructor( address ivolOracle, address wrappedNativeToken, address premiaMining, address feeReceiver, address feeDiscountAddress, int128 feePremium64x64, address exchangeHelper ) { IVOL_ORACLE_ADDRESS = ivolOracle; WRAPPED_NATIVE_TOKEN = wrappedNativeToken; PREMIA_MINING_ADDRESS = premiaMining; FEE_RECEIVER_ADDRESS = feeReceiver; // PremiaFeeDiscount contract address FEE_DISCOUNT_ADDRESS = feeDiscountAddress; FEE_PREMIUM_64x64 = feePremium64x64; EXCHANGE_HELPER = exchangeHelper; UNDERLYING_FREE_LIQ_TOKEN_ID = PoolStorage.formatTokenId( PoolStorage.TokenType.UNDERLYING_FREE_LIQ, 0, 0 ); BASE_FREE_LIQ_TOKEN_ID = PoolStorage.formatTokenId( PoolStorage.TokenType.BASE_FREE_LIQ, 0, 0 ); UNDERLYING_RESERVED_LIQ_TOKEN_ID = PoolStorage.formatTokenId( PoolStorage.TokenType.UNDERLYING_RESERVED_LIQ, 0, 0 ); BASE_RESERVED_LIQ_TOKEN_ID = PoolStorage.formatTokenId( PoolStorage.TokenType.BASE_RESERVED_LIQ, 0, 0 ); } modifier onlyProtocolOwner() { require( msg.sender == IERC173(OwnableStorage.layout().owner).owner(), "Not protocol owner" ); _; } function _fetchFeeDiscount64x64(address feePayer) internal view returns (int128 discount64x64) { if (FEE_DISCOUNT_ADDRESS != address(0)) { discount64x64 = ABDKMath64x64.divu( IFeeDiscount(FEE_DISCOUNT_ADDRESS).getDiscount(feePayer), INVERSE_BASIS_POINT ); } } function _withdrawFees(bool isCall) internal returns (uint256 amount) { uint256 tokenId = _getReservedLiquidityTokenId(isCall); amount = _balanceOf(FEE_RECEIVER_ADDRESS, tokenId); if (amount > 0) { _burn(FEE_RECEIVER_ADDRESS, tokenId, amount); _pushTo( FEE_RECEIVER_ADDRESS, PoolStorage.layout().getPoolToken(isCall), amount ); emit FeeWithdrawal(isCall, amount); } } /** * @notice calculate price of option contract * @param args structured quote arguments * @return result quote result */ function _quotePurchasePrice(PoolStorage.QuoteArgsInternal memory args) internal view returns (PoolStorage.QuoteResultInternal memory result) { require( args.strike64x64 > 0 && args.spot64x64 > 0 && args.maturity > 0, "invalid args" ); PoolStorage.Layout storage l = PoolStorage.layout(); // pessimistically adjust spot price to account for price feed lag if (args.isCall) { args.spot64x64 = args.spot64x64.add( args.spot64x64.mul(l.spotOffset64x64) ); } else { args.spot64x64 = args.spot64x64.sub( args.spot64x64.mul(l.spotOffset64x64) ); } int128 contractSize64x64 = ABDKMath64x64Token.fromDecimals( args.contractSize, l.underlyingDecimals ); (int128 adjustedCLevel64x64, int128 oldLiquidity64x64) = l .getRealPoolState(args.isCall); require(oldLiquidity64x64 > 0, "no liq"); int128 timeToMaturity64x64 = ABDKMath64x64.divu( args.maturity - block.timestamp, 365 days ); int128 annualizedVolatility64x64 = IVolatilitySurfaceOracle( IVOL_ORACLE_ADDRESS ).getAnnualizedVolatility64x64( l.base, l.underlying, args.spot64x64, args.strike64x64, timeToMaturity64x64 ); require(annualizedVolatility64x64 > 0, "vol = 0"); int128 collateral64x64 = args.isCall ? contractSize64x64 : contractSize64x64.mul(args.strike64x64); ( int128 price64x64, int128 cLevel64x64, int128 slippageCoefficient64x64 ) = OptionMath.quotePrice( OptionMath.QuoteArgs( annualizedVolatility64x64.mul(annualizedVolatility64x64), args.strike64x64, args.spot64x64, timeToMaturity64x64, adjustedCLevel64x64, oldLiquidity64x64, oldLiquidity64x64.sub(collateral64x64), 0x10000000000000000, // 64x64 fixed point representation of 1 l.getMinApy64x64(), args.isCall ) ); result.baseCost64x64 = args.isCall ? price64x64.mul(contractSize64x64).div(args.spot64x64) : price64x64.mul(contractSize64x64); result.feeCost64x64 = result.baseCost64x64.mul(FEE_PREMIUM_64x64); result.cLevel64x64 = cLevel64x64; result.slippageCoefficient64x64 = slippageCoefficient64x64; result.feeCost64x64 -= result.feeCost64x64.mul( _fetchFeeDiscount64x64(args.feePayer) ); } function _quoteSalePrice(PoolStorage.QuoteArgsInternal memory args) internal view returns (int128 baseCost64x64, int128 feeCost64x64) { require( args.strike64x64 > 0 && args.spot64x64 > 0 && args.maturity > 0, "invalid args" ); PoolStorage.Layout storage l = PoolStorage.layout(); int128 timeToMaturity64x64 = ABDKMath64x64.divu( args.maturity - block.timestamp, 365 days ); int128 annualizedVolatility64x64 = IVolatilitySurfaceOracle( IVOL_ORACLE_ADDRESS ).getAnnualizedVolatility64x64( l.base, l.underlying, args.spot64x64, args.strike64x64, timeToMaturity64x64 ); require(annualizedVolatility64x64 > 0, "vol = 0"); int128 blackScholesPrice64x64 = OptionMath._blackScholesPrice( annualizedVolatility64x64.mul(annualizedVolatility64x64), args.strike64x64, args.spot64x64, timeToMaturity64x64, args.isCall ); int128 exerciseValue64x64 = ABDKMath64x64Token.fromDecimals( _calculateExerciseValue( l, args.contractSize, args.spot64x64, args.strike64x64, args.isCall ), args.isCall ? l.underlyingDecimals : l.baseDecimals ); if (args.isCall) { exerciseValue64x64 = exerciseValue64x64.mul(args.spot64x64); } int128 sellCLevel64x64; { uint256 longTokenId = PoolStorage.formatTokenId( PoolStorage.getTokenType(args.isCall, true), args.maturity, args.strike64x64 ); // Initialize to min value, and replace by current if min not set or current is lower sellCLevel64x64 = l.minCLevel64x64[longTokenId]; { (int128 currentCLevel64x64, ) = l.getRealPoolState(args.isCall); if ( sellCLevel64x64 == 0 || currentCLevel64x64 < sellCLevel64x64 ) { sellCLevel64x64 = currentCLevel64x64; } } } int128 contractSize64x64 = ABDKMath64x64Token.fromDecimals( args.contractSize, l.underlyingDecimals ); baseCost64x64 = SELL_COEFFICIENT_64x64 .mul(sellCLevel64x64) .mul( blackScholesPrice64x64.mul(contractSize64x64).sub( exerciseValue64x64 ) ) .add(exerciseValue64x64); if (args.isCall) { baseCost64x64 = baseCost64x64.div(args.spot64x64); } feeCost64x64 = baseCost64x64.mul(FEE_PREMIUM_64x64); feeCost64x64 -= feeCost64x64.mul(_fetchFeeDiscount64x64(args.feePayer)); baseCost64x64 -= feeCost64x64; } function _getAvailableBuybackLiquidity(uint256 shortTokenId) internal view returns (uint256 totalLiquidity) { PoolStorage.Layout storage l = PoolStorage.layout(); EnumerableSet.AddressSet storage accounts = ERC1155EnumerableStorage .layout() .accountsByToken[shortTokenId]; (PoolStorage.TokenType tokenType, , ) = PoolStorage.parseTokenId( shortTokenId ); bool isCall = tokenType == PoolStorage.TokenType.SHORT_CALL; uint256 length = accounts.length(); for (uint256 i = 0; i < length; i++) { address lp = accounts.at(i); if (l.isBuybackEnabled[lp][isCall]) { totalLiquidity += _balanceOf(lp, shortTokenId); } } } /** * @notice burn corresponding long and short option tokens * @param l storage layout struct * @param account holder of tokens to annihilate * @param maturity timestamp of option maturity * @param strike64x64 64x64 fixed point representation of strike price * @param isCall true for call, false for put * @param contractSize quantity of option contract tokens to annihilate * @return collateralFreed amount of collateral freed, including APY fee rebate */ function _annihilate( PoolStorage.Layout storage l, address account, uint64 maturity, int128 strike64x64, bool isCall, uint256 contractSize ) internal returns (uint256 collateralFreed) { uint256 longTokenId = PoolStorage.formatTokenId( PoolStorage.getTokenType(isCall, true), maturity, strike64x64 ); uint256 shortTokenId = PoolStorage.formatTokenId( PoolStorage.getTokenType(isCall, false), maturity, strike64x64 ); uint256 tokenAmount = l.contractSizeToBaseTokenAmount( contractSize, strike64x64, isCall ); // calculate unconsumed APY fee so that it may be refunded uint256 intervalApyFee = _calculateApyFee( l, shortTokenId, tokenAmount, maturity ); _burn(account, longTokenId, contractSize); uint256 rebate = _fulfillApyFee( l, account, shortTokenId, contractSize, intervalApyFee, isCall ); _burn(account, shortTokenId, contractSize); collateralFreed = tokenAmount + rebate + intervalApyFee; emit Annihilate(shortTokenId, contractSize); } /** * @notice deposit underlying currency, underwriting calls of that currency with respect to base currency * @param amount quantity of underlying currency to deposit * @param isCallPool whether to deposit underlying in the call pool or base in the put pool */ function _deposit( PoolStorage.Layout storage l, uint256 amount, bool isCallPool ) internal { // Reset gradual divestment timestamp delete l.divestmentTimestamps[msg.sender][isCallPool]; _processPendingDeposits(l, isCallPool); l.depositedAt[msg.sender][isCallPool] = block.timestamp; _addUserTVL(l, msg.sender, isCallPool, amount); _processAvailableFunds(msg.sender, amount, isCallPool, false, false); emit Deposit(msg.sender, isCallPool, amount); } /** * @notice purchase option * @param l storage layout struct * @param account recipient of purchased option * @param maturity timestamp of option maturity * @param strike64x64 64x64 fixed point representation of strike price * @param isCall true for call, false for put * @param contractSize size of option contract * @param newPrice64x64 64x64 fixed point representation of current spot price * @return baseCost quantity of tokens required to purchase long position * @return feeCost quantity of tokens required to pay fees */ function _purchase( PoolStorage.Layout storage l, address account, uint64 maturity, int128 strike64x64, bool isCall, uint256 contractSize, int128 newPrice64x64 ) internal returns (uint256 baseCost, uint256 feeCost) { require(maturity > block.timestamp, "expired"); require(contractSize >= l.underlyingMinimum, "too small"); { uint256 tokenAmount = l.contractSizeToBaseTokenAmount( contractSize, strike64x64, isCall ); uint256 freeLiquidityTokenId = _getFreeLiquidityTokenId(isCall); require( tokenAmount <= _totalSupply(freeLiquidityTokenId) - l.totalPendingDeposits(isCall) - (_balanceOf(account, freeLiquidityTokenId) - l.pendingDepositsOf(account, isCall)), "insuf liq" ); } PoolStorage.QuoteResultInternal memory quote = _quotePurchasePrice( PoolStorage.QuoteArgsInternal( account, maturity, strike64x64, newPrice64x64, contractSize, isCall ) ); baseCost = ABDKMath64x64Token.toDecimals( quote.baseCost64x64, l.getTokenDecimals(isCall) ); feeCost = ABDKMath64x64Token.toDecimals( quote.feeCost64x64, l.getTokenDecimals(isCall) ); uint256 longTokenId = PoolStorage.formatTokenId( PoolStorage.getTokenType(isCall, true), maturity, strike64x64 ); { int128 minCLevel64x64 = l.minCLevel64x64[longTokenId]; if (minCLevel64x64 == 0 || quote.cLevel64x64 < minCLevel64x64) { l.minCLevel64x64[longTokenId] = quote.cLevel64x64; } } // mint long option token for buyer _mint(account, longTokenId, contractSize); int128 oldLiquidity64x64 = l.totalFreeLiquiditySupply64x64(isCall); // burn free liquidity tokens from other underwriters _mintShortTokenLoop( l, account, maturity, strike64x64, contractSize, baseCost, isCall ); int128 newLiquidity64x64 = l.totalFreeLiquiditySupply64x64(isCall); _setCLevel(l, oldLiquidity64x64, newLiquidity64x64, isCall); // mint reserved liquidity tokens for fee receiver _processAvailableFunds( FEE_RECEIVER_ADDRESS, feeCost, isCall, true, false ); emit Purchase( account, longTokenId, contractSize, baseCost, feeCost, newPrice64x64 ); } /** * @notice reassign short position to new underwriter * @param l storage layout struct * @param account holder of positions to be reassigned * @param maturity timestamp of option maturity * @param strike64x64 64x64 fixed point representation of strike price * @param isCall true for call, false for put * @param contractSize quantity of option contract tokens to reassign * @param newPrice64x64 64x64 fixed point representation of current spot price * @return baseCost quantity of tokens required to reassign short position * @return feeCost quantity of tokens required to pay fees * @return netCollateralFreed quantity of liquidity freed */ function _reassign( PoolStorage.Layout storage l, address account, uint64 maturity, int128 strike64x64, bool isCall, uint256 contractSize, int128 newPrice64x64 ) internal returns ( uint256 baseCost, uint256 feeCost, uint256 netCollateralFreed ) { (baseCost, feeCost) = _purchase( l, account, maturity, strike64x64, isCall, contractSize, newPrice64x64 ); uint256 totalCollateralFreed = _annihilate( l, account, maturity, strike64x64, isCall, contractSize ); netCollateralFreed = totalCollateralFreed - baseCost - feeCost; } /** * @notice exercise option on behalf of holder * @dev used for processing of expired options if passed holder is zero address * @param holder owner of long option tokens to exercise * @param longTokenId long option token id * @param contractSize quantity of tokens to exercise */ function _exercise( address holder, uint256 longTokenId, uint256 contractSize ) internal { uint64 maturity; int128 strike64x64; bool isCall; bool onlyExpired = holder == address(0); { PoolStorage.TokenType tokenType; (tokenType, maturity, strike64x64) = PoolStorage.parseTokenId( longTokenId ); require( tokenType == PoolStorage.TokenType.LONG_CALL || tokenType == PoolStorage.TokenType.LONG_PUT, "invalid type" ); require(!onlyExpired || maturity < block.timestamp, "not expired"); isCall = tokenType == PoolStorage.TokenType.LONG_CALL; } PoolStorage.Layout storage l = PoolStorage.layout(); int128 spot64x64 = _update(l); if (maturity < block.timestamp) { spot64x64 = l.getPriceUpdateAfter(maturity); } require( onlyExpired || ( isCall ? (spot64x64 > strike64x64) : (spot64x64 < strike64x64) ), "not ITM" ); uint256 exerciseValue = _calculateExerciseValue( l, contractSize, spot64x64, strike64x64, isCall ); if (onlyExpired) { // burn long option tokens from multiple holders // transfer profit to and emit Exercise event for each holder in loop _burnLongTokenLoop( contractSize, exerciseValue, longTokenId, isCall ); } else { // burn long option tokens from sender _burnLongTokenInterval( holder, longTokenId, contractSize, exerciseValue, isCall ); } // burn short option tokens from multiple underwriters _burnShortTokenLoop( l, maturity, strike64x64, contractSize, exerciseValue, isCall, false ); } function _calculateExerciseValue( PoolStorage.Layout storage l, uint256 contractSize, int128 spot64x64, int128 strike64x64, bool isCall ) internal view returns (uint256 exerciseValue) { // calculate exercise value if option is in-the-money if (isCall) { if (spot64x64 > strike64x64) { exerciseValue = spot64x64.sub(strike64x64).div(spot64x64).mulu( contractSize ); } } else { if (spot64x64 < strike64x64) { exerciseValue = l.contractSizeToBaseTokenAmount( contractSize, strike64x64.sub(spot64x64), isCall ); } } } function _mintShortTokenLoop( PoolStorage.Layout storage l, address buyer, uint64 maturity, int128 strike64x64, uint256 contractSize, uint256 premium, bool isCall ) internal { uint256 shortTokenId = PoolStorage.formatTokenId( PoolStorage.getTokenType(isCall, false), maturity, strike64x64 ); uint256 tokenAmount = l.contractSizeToBaseTokenAmount( contractSize, strike64x64, isCall ); // calculate anticipated APY fee so that it may be reserved uint256 apyFee = _calculateApyFee( l, shortTokenId, tokenAmount, maturity ); while (tokenAmount > 0) { address underwriter = l.liquidityQueueAscending[isCall][address(0)]; uint256 balance = _balanceOf( underwriter, _getFreeLiquidityTokenId(isCall) ); // if underwriter is in process of divestment, remove from queue if (!l.getReinvestmentStatus(underwriter, isCall)) { _burn(underwriter, _getFreeLiquidityTokenId(isCall), balance); _processAvailableFunds( underwriter, balance, isCall, true, false ); _subUserTVL(l, underwriter, isCall, balance); continue; } // if underwriter has insufficient liquidity, remove from queue if (balance < l.getMinimumAmount(isCall)) { l.removeUnderwriter(underwriter, isCall); continue; } // move interval to end of queue if underwriter is buyer if (underwriter == buyer) { l.removeUnderwriter(underwriter, isCall); l.addUnderwriter(underwriter, isCall); continue; } balance -= l.pendingDepositsOf(underwriter, isCall); Interval memory interval; // amount of liquidity provided by underwriter, accounting for reinvested premium interval.tokenAmount = (balance * (tokenAmount + premium - apyFee)) / tokenAmount; // skip underwriters whose liquidity is pending deposit processing if (interval.tokenAmount == 0) continue; // truncate interval if underwriter has excess liquidity available if (interval.tokenAmount > tokenAmount) interval.tokenAmount = tokenAmount; // calculate derived interval variables interval.contractSize = (contractSize * interval.tokenAmount) / tokenAmount; interval.payment = (premium * interval.tokenAmount) / tokenAmount; interval.apyFee = (apyFee * interval.tokenAmount) / tokenAmount; _mintShortTokenInterval( l, underwriter, buyer, shortTokenId, interval, isCall ); tokenAmount -= interval.tokenAmount; contractSize -= interval.contractSize; premium -= interval.payment; apyFee -= interval.apyFee; } } function _mintShortTokenInterval( PoolStorage.Layout storage l, address underwriter, address longReceiver, uint256 shortTokenId, Interval memory interval, bool isCallPool ) internal { // track prepaid APY fees _reserveApyFee(l, underwriter, shortTokenId, interval.apyFee); // if payment is equal to collateral amount plus APY fee, this is a manual underwrite bool isManualUnderwrite = interval.payment == interval.tokenAmount + interval.apyFee; if (!isManualUnderwrite) { // burn free liquidity tokens from underwriter _burn( underwriter, _getFreeLiquidityTokenId(isCallPool), interval.tokenAmount + interval.apyFee - interval.payment ); } // mint short option tokens for underwriter _mint(underwriter, shortTokenId, interval.contractSize); _addUserTVL( l, underwriter, isCallPool, interval.payment - interval.apyFee ); emit Underwrite( underwriter, longReceiver, shortTokenId, interval.contractSize, isManualUnderwrite ? 0 : interval.payment, isManualUnderwrite ); } function _burnLongTokenLoop( uint256 contractSize, uint256 exerciseValue, uint256 longTokenId, bool isCallPool ) internal { EnumerableSet.AddressSet storage holders = ERC1155EnumerableStorage .layout() .accountsByToken[longTokenId]; while (contractSize > 0) { address longTokenHolder = holders.at(holders.length() - 1); uint256 intervalContractSize = _balanceOf( longTokenHolder, longTokenId ); // truncate interval if holder has excess long position size if (intervalContractSize > contractSize) intervalContractSize = contractSize; uint256 intervalExerciseValue = (exerciseValue * intervalContractSize) / contractSize; _burnLongTokenInterval( longTokenHolder, longTokenId, intervalContractSize, intervalExerciseValue, isCallPool ); contractSize -= intervalContractSize; exerciseValue -= intervalExerciseValue; } } function _burnLongTokenInterval( address holder, uint256 longTokenId, uint256 contractSize, uint256 exerciseValue, bool isCallPool ) internal { _burn(holder, longTokenId, contractSize); if (exerciseValue > 0) { _processAvailableFunds( holder, exerciseValue, isCallPool, true, true ); } emit Exercise(holder, longTokenId, contractSize, exerciseValue, 0); } function _burnShortTokenLoop( PoolStorage.Layout storage l, uint64 maturity, int128 strike64x64, uint256 contractSize, uint256 payment, bool isCall, bool onlyBuybackLiquidity ) internal { uint256 shortTokenId = PoolStorage.formatTokenId( PoolStorage.getTokenType(isCall, false), maturity, strike64x64 ); uint256 tokenAmount = l.contractSizeToBaseTokenAmount( contractSize, strike64x64, isCall ); // calculate unconsumed APY fee so that it may be refunded uint256 apyFee = _calculateApyFee( l, shortTokenId, tokenAmount, maturity ); EnumerableSet.AddressSet storage underwriters = ERC1155EnumerableStorage .layout() .accountsByToken[shortTokenId]; uint256 index = underwriters.length(); while (contractSize > 0) { address underwriter = underwriters.at(--index); // skip underwriters who do not provide buyback liqudity, if applicable if ( onlyBuybackLiquidity && !l.isBuybackEnabled[underwriter][isCall] ) continue; Interval memory interval; // amount of liquidity provided by underwriter interval.contractSize = _balanceOf(underwriter, shortTokenId); // truncate interval if underwriter has excess short position size if (interval.contractSize > contractSize) interval.contractSize = contractSize; // calculate derived interval variables interval.tokenAmount = (tokenAmount * interval.contractSize) / contractSize; interval.payment = (payment * interval.contractSize) / contractSize; interval.apyFee = (apyFee * interval.contractSize) / contractSize; _burnShortTokenInterval( l, underwriter, shortTokenId, interval, isCall, onlyBuybackLiquidity ); contractSize -= interval.contractSize; tokenAmount -= interval.tokenAmount; payment -= interval.payment; apyFee -= interval.apyFee; } } function _burnShortTokenInterval( PoolStorage.Layout storage l, address underwriter, uint256 shortTokenId, Interval memory interval, bool isCallPool, bool isSale ) internal { // track prepaid APY fees uint256 refundWithRebate = interval.apyFee + _fulfillApyFee( l, underwriter, shortTokenId, interval.contractSize, interval.apyFee, isCallPool ); // burn short option tokens from underwriter _burn(underwriter, shortTokenId, interval.contractSize); bool divest = !l.getReinvestmentStatus(underwriter, isCallPool); _processAvailableFunds( underwriter, interval.tokenAmount - interval.payment + refundWithRebate, isCallPool, divest, false ); if (divest) { _subUserTVL(l, underwriter, isCallPool, interval.tokenAmount); } else { if (refundWithRebate > interval.payment) { _addUserTVL( l, underwriter, isCallPool, refundWithRebate - interval.payment ); } else if (interval.payment > refundWithRebate) { _subUserTVL( l, underwriter, isCallPool, interval.payment - refundWithRebate ); } } if (isSale) { emit AssignSale( underwriter, shortTokenId, interval.tokenAmount - interval.payment, interval.contractSize ); } else { emit AssignExercise( underwriter, shortTokenId, interval.tokenAmount - interval.payment, interval.contractSize, 0 ); } } function _calculateApyFee( PoolStorage.Layout storage l, uint256 shortTokenId, uint256 tokenAmount, uint64 maturity ) internal view returns (uint256 apyFee) { if (block.timestamp < maturity) { int128 apyFeeRate64x64 = _totalSupply(shortTokenId) == 0 ? l.getFeeApy64x64() : l.feeReserveRates[shortTokenId]; apyFee = apyFeeRate64x64.mulu( (tokenAmount * (maturity - block.timestamp)) / (365 days) ); } } function _reserveApyFee( PoolStorage.Layout storage l, address underwriter, uint256 shortTokenId, uint256 amount ) internal { l.feesReserved[underwriter][shortTokenId] += amount; emit APYFeeReserved(underwriter, shortTokenId, amount); } /** * @notice credit fee receiver with fees earned and calculate rebate for underwriter * @dev short tokens which have acrrued fee must not be burned or transferred until after this helper is called * @param l storage layout struct * @param underwriter holder of short position who reserved fees * @param shortTokenId short token id whose reserved fees to pay and rebate * @param intervalContractSize size of position for which to calculate accrued fees * @param intervalApyFee quantity of fees reserved but not yet accrued * @param isCallPool true for call, false for put */ function _fulfillApyFee( PoolStorage.Layout storage l, address underwriter, uint256 shortTokenId, uint256 intervalContractSize, uint256 intervalApyFee, bool isCallPool ) internal returns (uint256 rebate) { // calculate proportion of fees reserved corresponding to interval uint256 feesReserved = l.feesReserved[underwriter][shortTokenId]; uint256 intervalFeesReserved = (feesReserved * intervalContractSize) / _balanceOf(underwriter, shortTokenId); // deduct fees for time not elapsed l.feesReserved[underwriter][shortTokenId] -= intervalFeesReserved; // apply rebate to fees accrued rebate = _fetchFeeDiscount64x64(underwriter).mulu( intervalFeesReserved - intervalApyFee ); // credit fee receiver with fees paid uint256 intervalFeesPaid = intervalFeesReserved - intervalApyFee - rebate; _processAvailableFunds( FEE_RECEIVER_ADDRESS, intervalFeesPaid, isCallPool, true, false ); emit APYFeePaid(underwriter, shortTokenId, intervalFeesPaid); } function _addToDepositQueue( address account, uint256 amount, bool isCallPool ) internal { PoolStorage.Layout storage l = PoolStorage.layout(); uint256 freeLiqTokenId = _getFreeLiquidityTokenId(isCallPool); if (_totalSupply(freeLiqTokenId) > 0) { uint256 nextBatch = (block.timestamp / BATCHING_PERIOD) * BATCHING_PERIOD + BATCHING_PERIOD; l.pendingDeposits[account][nextBatch][isCallPool] += amount; PoolStorage.BatchData storage batchData = l.nextDeposits[ isCallPool ]; batchData.totalPendingDeposits += amount; batchData.eta = nextBatch; } _mint(account, freeLiqTokenId, amount); } function _processPendingDeposits(PoolStorage.Layout storage l, bool isCall) internal { PoolStorage.BatchData storage batchData = l.nextDeposits[isCall]; if (batchData.eta == 0 || block.timestamp < batchData.eta) return; int128 oldLiquidity64x64 = l.totalFreeLiquiditySupply64x64(isCall); _setCLevel( l, oldLiquidity64x64, oldLiquidity64x64.add( ABDKMath64x64Token.fromDecimals( batchData.totalPendingDeposits, l.getTokenDecimals(isCall) ) ), isCall ); delete l.nextDeposits[isCall]; } function _getFreeLiquidityTokenId(bool isCall) internal view returns (uint256 freeLiqTokenId) { freeLiqTokenId = isCall ? UNDERLYING_FREE_LIQ_TOKEN_ID : BASE_FREE_LIQ_TOKEN_ID; } function _getReservedLiquidityTokenId(bool isCall) internal view returns (uint256 reservedLiqTokenId) { reservedLiqTokenId = isCall ? UNDERLYING_RESERVED_LIQ_TOKEN_ID : BASE_RESERVED_LIQ_TOKEN_ID; } function _setCLevel( PoolStorage.Layout storage l, int128 oldLiquidity64x64, int128 newLiquidity64x64, bool isCallPool ) internal { int128 oldCLevel64x64 = l.getDecayAdjustedCLevel64x64(isCallPool); int128 cLevel64x64 = l.applyCLevelLiquidityChangeAdjustment( oldCLevel64x64, oldLiquidity64x64, newLiquidity64x64, isCallPool ); l.setCLevel(cLevel64x64, isCallPool); emit UpdateCLevel( isCallPool, cLevel64x64, oldLiquidity64x64, newLiquidity64x64 ); } /** * @notice calculate and store updated market state * @param l storage layout struct * @return newPrice64x64 64x64 fixed point representation of current spot price */ function _update(PoolStorage.Layout storage l) internal returns (int128 newPrice64x64) { if (l.updatedAt == block.timestamp) { return (l.getPriceUpdate(block.timestamp)); } newPrice64x64 = l.fetchPriceUpdate(); if (l.getPriceUpdate(block.timestamp) == 0) { l.setPriceUpdate(block.timestamp, newPrice64x64); } l.updatedAt = block.timestamp; _processPendingDeposits(l, true); _processPendingDeposits(l, false); } /** * @notice transfer ERC20 tokens to message sender * @param token ERC20 token address * @param amount quantity of token to transfer */ function _pushTo( address to, address token, uint256 amount ) internal { if (amount == 0) return; require(IERC20(token).transfer(to, amount), "ERC20 transfer failed"); } /** * @notice transfer ERC20 tokens from message sender * @param l storage layout struct * @param from address from which tokens are pulled from * @param amount quantity of token to transfer * @param isCallPool whether funds correspond to call or put pool * @param creditMessageValue whether to attempt to treat message value as credit */ function _pullFrom( PoolStorage.Layout storage l, address from, uint256 amount, bool isCallPool, bool creditMessageValue ) internal { uint256 credit; if (creditMessageValue) { credit = _creditMessageValue(amount, isCallPool); } if (amount > credit) { credit += _creditReservedLiquidity( from, amount - credit, isCallPool ); } if (amount > credit) { require( IERC20(l.getPoolToken(isCallPool)).transferFrom( from, address(this), amount - credit ), "ERC20 transfer failed" ); } } /** * @notice transfer or reinvest available user funds * @param account owner of funds * @param amount quantity of funds available * @param isCallPool whether funds correspond to call or put pool * @param divest whether to reserve funds or reinvest * @param transferOnDivest whether to transfer divested funds to owner */ function _processAvailableFunds( address account, uint256 amount, bool isCallPool, bool divest, bool transferOnDivest ) internal { if (divest) { if (transferOnDivest) { _pushTo( account, PoolStorage.layout().getPoolToken(isCallPool), amount ); } else { _mint( account, _getReservedLiquidityTokenId(isCallPool), amount ); } } else { _addToDepositQueue(account, amount, isCallPool); } } /** * @notice validate that pool accepts ether deposits and calculate credit amount from message value * @param amount total deposit quantity * @param isCallPool whether to deposit underlying in the call pool or base in the put pool * @return credit quantity of credit to apply */ function _creditMessageValue(uint256 amount, bool isCallPool) internal returns (uint256 credit) { if (msg.value > 0) { require( PoolStorage.layout().getPoolToken(isCallPool) == WRAPPED_NATIVE_TOKEN, "not WETH deposit" ); if (msg.value > amount) { unchecked { (bool success, ) = payable(msg.sender).call{ value: msg.value - amount }(""); require(success, "ETH refund failed"); credit = amount; } } else { credit = msg.value; } IWETH(WRAPPED_NATIVE_TOKEN).deposit{value: credit}(); } } /** * @notice calculate credit amount from reserved liquidity * @param account address whose reserved liquidity to use as credit * @param amount total deposit quantity * @param isCallPool whether to deposit underlying in the call pool or base in the put pool * @return credit quantity of credit to apply */ function _creditReservedLiquidity( address account, uint256 amount, bool isCallPool ) internal returns (uint256 credit) { uint256 reservedLiqTokenId = _getReservedLiquidityTokenId(isCallPool); uint256 balance = _balanceOf(account, reservedLiqTokenId); if (balance > 0) { credit = balance > amount ? amount : balance; _burn(account, reservedLiqTokenId, credit); } } function _mint( address account, uint256 tokenId, uint256 amount ) internal { _mint(account, tokenId, amount, ""); } function _addUserTVL( PoolStorage.Layout storage l, address user, bool isCallPool, uint256 amount ) internal { uint256 userTVL = l.userTVL[user][isCallPool]; uint256 totalTVL = l.totalTVL[isCallPool]; IPremiaMining(PREMIA_MINING_ADDRESS).allocatePending( user, address(this), isCallPool, userTVL, userTVL + amount, totalTVL ); l.userTVL[user][isCallPool] = userTVL + amount; l.totalTVL[isCallPool] = totalTVL + amount; } function _subUserTVL( PoolStorage.Layout storage l, address user, bool isCallPool, uint256 amount ) internal { uint256 userTVL = l.userTVL[user][isCallPool]; uint256 totalTVL = l.totalTVL[isCallPool]; uint256 newUserTVL; uint256 newTotalTVL; if (userTVL < amount) { amount = userTVL; } newUserTVL = userTVL - amount; newTotalTVL = totalTVL - amount; IPremiaMining(PREMIA_MINING_ADDRESS).allocatePending( user, address(this), isCallPool, userTVL, newUserTVL, totalTVL ); l.userTVL[user][isCallPool] = newUserTVL; l.totalTVL[isCallPool] = newTotalTVL; } /** * @dev pull token from user, send to exchangeHelper and trigger a trade from exchangeHelper * @param s swap arguments * @param tokenOut token to swap for. should always equal to the pool token. * @return amountCredited amount of tokenOut we got from the trade. */ function _swapForPoolTokens( IPoolInternal.SwapArgs memory s, address tokenOut ) internal returns (uint256 amountCredited) { if (msg.value > 0) { require(s.tokenIn == WRAPPED_NATIVE_TOKEN, "wrong tokenIn"); IWETH(WRAPPED_NATIVE_TOKEN).deposit{value: msg.value}(); IWETH(WRAPPED_NATIVE_TOKEN).transfer(EXCHANGE_HELPER, msg.value); } if (s.amountInMax > 0) { IERC20(s.tokenIn).safeTransferFrom( msg.sender, EXCHANGE_HELPER, s.amountInMax ); } amountCredited = IExchangeHelper(EXCHANGE_HELPER).swapWithToken( s.tokenIn, tokenOut, s.amountInMax + msg.value, s.callee, s.allowanceTarget, s.data, s.refundAddress ); require( amountCredited >= s.amountOutMin, "not enough output from trade" ); } /** * @notice ERC1155 hook: track eligible underwriters * @param operator transaction sender * @param from token sender * @param to token receiver * @param ids token ids transferred * @param amounts token quantities transferred * @param data data payload */ function _beforeTokenTransfer( address operator, address from, address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data ) internal virtual override { super._beforeTokenTransfer(operator, from, to, ids, amounts, data); PoolStorage.Layout storage l = PoolStorage.layout(); for (uint256 i; i < ids.length; i++) { uint256 id = ids[i]; uint256 amount = amounts[i]; if (amount == 0) continue; if (from == address(0)) { l.tokenIds.add(id); } if (to == address(0) && _totalSupply(id) == 0) { l.tokenIds.remove(id); } // prevent transfer of free and reserved liquidity during waiting period if ( id == UNDERLYING_FREE_LIQ_TOKEN_ID || id == BASE_FREE_LIQ_TOKEN_ID || id == UNDERLYING_RESERVED_LIQ_TOKEN_ID || id == BASE_RESERVED_LIQ_TOKEN_ID ) { if (from != address(0) && to != address(0)) { bool isCallPool = id == UNDERLYING_FREE_LIQ_TOKEN_ID || id == UNDERLYING_RESERVED_LIQ_TOKEN_ID; require( l.depositedAt[from][isCallPool] + (1 days) < block.timestamp, "liq lock 1d" ); } } if ( id == UNDERLYING_FREE_LIQ_TOKEN_ID || id == BASE_FREE_LIQ_TOKEN_ID ) { bool isCallPool = id == UNDERLYING_FREE_LIQ_TOKEN_ID; uint256 minimum = l.getMinimumAmount(isCallPool); if (from != address(0)) { uint256 balance = _balanceOf(from, id); if (balance > minimum && balance <= amount + minimum) { require( balance - l.pendingDepositsOf(from, isCallPool) >= amount, "Insuf balance" ); l.removeUnderwriter(from, isCallPool); } if (to != address(0)) { _subUserTVL(l, from, isCallPool, amount); _addUserTVL(l, to, isCallPool, amount); } } if (to != address(0)) { uint256 balance = _balanceOf(to, id); if (balance <= minimum && balance + amount >= minimum) { l.addUnderwriter(to, isCallPool); } } } // Update userTVL on SHORT options transfers (PoolStorage.TokenType tokenType, , ) = PoolStorage.parseTokenId( id ); if ( tokenType == PoolStorage.TokenType.SHORT_CALL || tokenType == PoolStorage.TokenType.SHORT_PUT ) { _beforeShortTokenTransfer(l, from, to, id, amount); } } } function _beforeShortTokenTransfer( PoolStorage.Layout storage l, address from, address to, uint256 id, uint256 amount ) private { // total supply has already been updated, so compare to amount rather than 0 if (from == address(0) && _totalSupply(id) == amount) { l.feeReserveRates[id] = l.getFeeApy64x64(); } if (to == address(0) && _totalSupply(id) == 0) { delete l.feeReserveRates[id]; } if (from != address(0) && to != address(0)) { ( PoolStorage.TokenType tokenType, uint64 maturity, int128 strike64x64 ) = PoolStorage.parseTokenId(id); bool isCall = tokenType == PoolStorage.TokenType.SHORT_CALL; uint256 collateral = l.contractSizeToBaseTokenAmount( amount, strike64x64, isCall ); uint256 intervalApyFee = _calculateApyFee( l, id, collateral, maturity ); uint256 rebate = _fulfillApyFee( l, from, id, amount, intervalApyFee, isCall ); _reserveApyFee(l, to, id, intervalApyFee); bool divest = !l.getReinvestmentStatus(from, isCall); if (rebate > 0) { _processAvailableFunds(from, rebate, isCall, divest, false); } _subUserTVL( l, from, isCall, divest ? collateral : collateral - rebate ); _addUserTVL(l, to, isCall, collateral); } } }
// SPDX-License-Identifier: BUSL-1.1 // For further clarification please see https://license.premia.legal pragma solidity ^0.8.0; import {AggregatorInterface} from "@chainlink/contracts/src/v0.8/interfaces/AggregatorInterface.sol"; import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; import {ABDKMath64x64Token} from "@solidstate/abdk-math-extensions/contracts/ABDKMath64x64Token.sol"; import {EnumerableSet, ERC1155EnumerableStorage} from "@solidstate/contracts/token/ERC1155/enumerable/ERC1155EnumerableStorage.sol"; import {ABDKMath64x64} from "abdk-libraries-solidity/ABDKMath64x64.sol"; import {OptionMath} from "../libraries/OptionMath.sol"; library PoolStorage { using ABDKMath64x64 for int128; using PoolStorage for PoolStorage.Layout; enum TokenType { UNDERLYING_FREE_LIQ, BASE_FREE_LIQ, UNDERLYING_RESERVED_LIQ, BASE_RESERVED_LIQ, LONG_CALL, SHORT_CALL, LONG_PUT, SHORT_PUT } struct PoolSettings { address underlying; address base; address underlyingOracle; address baseOracle; } struct QuoteArgsInternal { address feePayer; // address of the fee payer uint64 maturity; // timestamp of option maturity int128 strike64x64; // 64x64 fixed point representation of strike price int128 spot64x64; // 64x64 fixed point representation of spot price uint256 contractSize; // size of option contract bool isCall; // true for call, false for put } struct QuoteResultInternal { int128 baseCost64x64; // 64x64 fixed point representation of option cost denominated in underlying currency (without fee) int128 feeCost64x64; // 64x64 fixed point representation of option fee cost denominated in underlying currency for call, or base currency for put int128 cLevel64x64; // 64x64 fixed point representation of C-Level of Pool after purchase int128 slippageCoefficient64x64; // 64x64 fixed point representation of slippage coefficient for given order size } struct BatchData { uint256 eta; uint256 totalPendingDeposits; } bytes32 internal constant STORAGE_SLOT = keccak256("premia.contracts.storage.Pool"); uint256 private constant C_DECAY_BUFFER = 12 hours; uint256 private constant C_DECAY_INTERVAL = 4 hours; struct Layout { // ERC20 token addresses address base; address underlying; // AggregatorV3Interface oracle addresses address baseOracle; address underlyingOracle; // token metadata uint8 underlyingDecimals; uint8 baseDecimals; // minimum amounts uint256 baseMinimum; uint256 underlyingMinimum; // deposit caps uint256 _deprecated_basePoolCap; uint256 _deprecated_underlyingPoolCap; // market state int128 _deprecated_steepness64x64; int128 cLevelBase64x64; int128 cLevelUnderlying64x64; uint256 cLevelBaseUpdatedAt; uint256 cLevelUnderlyingUpdatedAt; uint256 updatedAt; // User -> isCall -> depositedAt mapping(address => mapping(bool => uint256)) depositedAt; mapping(address => mapping(bool => uint256)) divestmentTimestamps; // doubly linked list of free liquidity intervals // isCall -> User -> User mapping(bool => mapping(address => address)) liquidityQueueAscending; mapping(bool => mapping(address => address)) liquidityQueueDescending; // minimum resolution price bucket => price mapping(uint256 => int128) bucketPrices64x64; // sequence id (minimum resolution price bucket / 256) => price update sequence mapping(uint256 => uint256) priceUpdateSequences; // isCall -> batch data mapping(bool => BatchData) nextDeposits; // user -> batch timestamp -> isCall -> pending amount mapping(address => mapping(uint256 => mapping(bool => uint256))) pendingDeposits; EnumerableSet.UintSet tokenIds; // user -> isCallPool -> total value locked of user (Used for liquidity mining) mapping(address => mapping(bool => uint256)) userTVL; // isCallPool -> total value locked mapping(bool => uint256) totalTVL; // steepness values int128 steepnessBase64x64; int128 steepnessUnderlying64x64; // User -> isCallPool -> isBuybackEnabled mapping(address => mapping(bool => bool)) isBuybackEnabled; // LongTokenId -> minC mapping(uint256 => int128) minCLevel64x64; // APY fee tracking // underwriter -> shortTokenId -> amount mapping(address => mapping(uint256 => uint256)) feesReserved; // shortTokenId -> 64x64 fixed point representation of apy fee mapping(uint256 => int128) feeReserveRates; // APY fee paid by underwriters // Also used along with multiplier to calculate minimum option price as APY int128 feeApy64x64; // adjustment applied to spot price for puchase calculations int128 spotOffset64x64; } function layout() internal pure returns (Layout storage l) { bytes32 slot = STORAGE_SLOT; assembly { l.slot := slot } } /** * @notice calculate ERC1155 token id for given option parameters * @param tokenType TokenType enum * @param maturity timestamp of option maturity * @param strike64x64 64x64 fixed point representation of strike price * @return tokenId token id */ function formatTokenId( TokenType tokenType, uint64 maturity, int128 strike64x64 ) internal pure returns (uint256 tokenId) { tokenId = (uint256(tokenType) << 248) + (uint256(maturity) << 128) + uint256(int256(strike64x64)); } /** * @notice derive option maturity and strike price from ERC1155 token id * @param tokenId token id * @return tokenType TokenType enum * @return maturity timestamp of option maturity * @return strike64x64 option strike price */ function parseTokenId(uint256 tokenId) internal pure returns ( TokenType tokenType, uint64 maturity, int128 strike64x64 ) { assembly { tokenType := shr(248, tokenId) maturity := shr(128, tokenId) strike64x64 := tokenId } } function getTokenType(bool isCall, bool isLong) internal pure returns (TokenType tokenType) { if (isCall) { tokenType = isLong ? TokenType.LONG_CALL : TokenType.SHORT_CALL; } else { tokenType = isLong ? TokenType.LONG_PUT : TokenType.SHORT_PUT; } } function getPoolToken(Layout storage l, bool isCall) internal view returns (address token) { token = isCall ? l.underlying : l.base; } function getTokenDecimals(Layout storage l, bool isCall) internal view returns (uint8 decimals) { decimals = isCall ? l.underlyingDecimals : l.baseDecimals; } function getMinimumAmount(Layout storage l, bool isCall) internal view returns (uint256 minimumAmount) { minimumAmount = isCall ? l.underlyingMinimum : l.baseMinimum; } /** * @notice get the total supply of free liquidity tokens, minus pending deposits * @param l storage layout struct * @param isCall whether query is for call or put pool * @return 64x64 fixed point representation of total free liquidity */ function totalFreeLiquiditySupply64x64(Layout storage l, bool isCall) internal view returns (int128) { uint256 tokenId = formatTokenId( isCall ? TokenType.UNDERLYING_FREE_LIQ : TokenType.BASE_FREE_LIQ, 0, 0 ); return ABDKMath64x64Token.fromDecimals( ERC1155EnumerableStorage.layout().totalSupply[tokenId] - l.totalPendingDeposits(isCall), l.getTokenDecimals(isCall) ); } function getReinvestmentStatus( Layout storage l, address account, bool isCallPool ) internal view returns (bool) { uint256 timestamp = l.divestmentTimestamps[account][isCallPool]; return timestamp == 0 || timestamp > block.timestamp; } function getFeeApy64x64(Layout storage l) internal view returns (int128 feeApy64x64) { feeApy64x64 = l.feeApy64x64; if (feeApy64x64 == 0) { // if APY fee is not set, set to 0.025 feeApy64x64 = 0x666666666666666; } } function getMinApy64x64(Layout storage l) internal view returns (int128 feeApy64x64) { feeApy64x64 = l.getFeeApy64x64() << 3; } function addUnderwriter( Layout storage l, address account, bool isCallPool ) internal { require(account != address(0)); mapping(address => address) storage asc = l.liquidityQueueAscending[ isCallPool ]; mapping(address => address) storage desc = l.liquidityQueueDescending[ isCallPool ]; if (_isInQueue(account, asc, desc)) return; address last = desc[address(0)]; asc[last] = account; desc[account] = last; desc[address(0)] = account; } function removeUnderwriter( Layout storage l, address account, bool isCallPool ) internal { require(account != address(0)); mapping(address => address) storage asc = l.liquidityQueueAscending[ isCallPool ]; mapping(address => address) storage desc = l.liquidityQueueDescending[ isCallPool ]; if (!_isInQueue(account, asc, desc)) return; address prev = desc[account]; address next = asc[account]; asc[prev] = next; desc[next] = prev; delete asc[account]; delete desc[account]; } function isInQueue( Layout storage l, address account, bool isCallPool ) internal view returns (bool) { mapping(address => address) storage asc = l.liquidityQueueAscending[ isCallPool ]; mapping(address => address) storage desc = l.liquidityQueueDescending[ isCallPool ]; return _isInQueue(account, asc, desc); } function _isInQueue( address account, mapping(address => address) storage asc, mapping(address => address) storage desc ) private view returns (bool) { return asc[account] != address(0) || desc[address(0)] == account; } /** * @notice get current C-Level, without accounting for pending adjustments * @param l storage layout struct * @param isCall whether query is for call or put pool * @return cLevel64x64 64x64 fixed point representation of C-Level */ function getRawCLevel64x64(Layout storage l, bool isCall) internal view returns (int128 cLevel64x64) { cLevel64x64 = isCall ? l.cLevelUnderlying64x64 : l.cLevelBase64x64; } /** * @notice get current C-Level, accounting for unrealized decay * @param l storage layout struct * @param isCall whether query is for call or put pool * @return cLevel64x64 64x64 fixed point representation of C-Level */ function getDecayAdjustedCLevel64x64(Layout storage l, bool isCall) internal view returns (int128 cLevel64x64) { // get raw C-Level from storage cLevel64x64 = l.getRawCLevel64x64(isCall); // account for C-Level decay cLevel64x64 = l.applyCLevelDecayAdjustment(cLevel64x64, isCall); } /** * @notice get updated C-Level and pool liquidity level, accounting for decay and pending deposits * @param l storage layout struct * @param isCall whether to update C-Level for call or put pool * @return cLevel64x64 64x64 fixed point representation of C-Level * @return liquidity64x64 64x64 fixed point representation of new liquidity amount */ function getRealPoolState(Layout storage l, bool isCall) internal view returns (int128 cLevel64x64, int128 liquidity64x64) { PoolStorage.BatchData storage batchData = l.nextDeposits[isCall]; int128 oldCLevel64x64 = l.getDecayAdjustedCLevel64x64(isCall); int128 oldLiquidity64x64 = l.totalFreeLiquiditySupply64x64(isCall); if ( batchData.totalPendingDeposits > 0 && batchData.eta != 0 && block.timestamp >= batchData.eta ) { liquidity64x64 = ABDKMath64x64Token .fromDecimals( batchData.totalPendingDeposits, l.getTokenDecimals(isCall) ) .add(oldLiquidity64x64); cLevel64x64 = l.applyCLevelLiquidityChangeAdjustment( oldCLevel64x64, oldLiquidity64x64, liquidity64x64, isCall ); } else { cLevel64x64 = oldCLevel64x64; liquidity64x64 = oldLiquidity64x64; } } /** * @notice calculate updated C-Level, accounting for unrealized decay * @param l storage layout struct * @param oldCLevel64x64 64x64 fixed point representation pool C-Level before accounting for decay * @param isCall whether query is for call or put pool * @return cLevel64x64 64x64 fixed point representation of C-Level of Pool after accounting for decay */ function applyCLevelDecayAdjustment( Layout storage l, int128 oldCLevel64x64, bool isCall ) internal view returns (int128 cLevel64x64) { uint256 timeElapsed = block.timestamp - (isCall ? l.cLevelUnderlyingUpdatedAt : l.cLevelBaseUpdatedAt); // do not apply C decay if less than 24 hours have elapsed if (timeElapsed > C_DECAY_BUFFER) { timeElapsed -= C_DECAY_BUFFER; } else { return oldCLevel64x64; } int128 timeIntervalsElapsed64x64 = ABDKMath64x64.divu( timeElapsed, C_DECAY_INTERVAL ); uint256 tokenId = formatTokenId( isCall ? TokenType.UNDERLYING_FREE_LIQ : TokenType.BASE_FREE_LIQ, 0, 0 ); uint256 tvl = l.totalTVL[isCall]; uint256 availableSupply = (ERC1155EnumerableStorage .layout() .totalSupply[tokenId] - l.totalPendingDeposits(isCall)); if (tvl < availableSupply) { // workaround for TVL underflow issue availableSupply = tvl; } int128 utilization = ABDKMath64x64.divu(tvl - availableSupply, tvl); return OptionMath.calculateCLevelDecay( OptionMath.CalculateCLevelDecayArgs( timeIntervalsElapsed64x64, oldCLevel64x64, utilization, 0xb333333333333333, // 0.7 0xe666666666666666, // 0.9 0x10000000000000000, // 1.0 0x10000000000000000, // 1.0 0xe666666666666666, // 0.9 0x56fc2a2c515da32ea // 2e ) ); } /** * @notice calculate updated C-Level, accounting for change in liquidity * @param l storage layout struct * @param oldCLevel64x64 64x64 fixed point representation pool C-Level before accounting for liquidity change * @param oldLiquidity64x64 64x64 fixed point representation of previous liquidity * @param newLiquidity64x64 64x64 fixed point representation of current liquidity * @param isCallPool whether to update C-Level for call or put pool * @return cLevel64x64 64x64 fixed point representation of C-Level */ function applyCLevelLiquidityChangeAdjustment( Layout storage l, int128 oldCLevel64x64, int128 oldLiquidity64x64, int128 newLiquidity64x64, bool isCallPool ) internal view returns (int128 cLevel64x64) { int128 steepness64x64 = isCallPool ? l.steepnessUnderlying64x64 : l.steepnessBase64x64; // fallback to deprecated storage value if side-specific value is not set if (steepness64x64 == 0) steepness64x64 = l._deprecated_steepness64x64; cLevel64x64 = OptionMath.calculateCLevel( oldCLevel64x64, oldLiquidity64x64, newLiquidity64x64, steepness64x64 ); if (cLevel64x64 < 0xb333333333333333) { cLevel64x64 = int128(0xb333333333333333); // 64x64 fixed point representation of 0.7 } } /** * @notice set C-Level to arbitrary pre-calculated value * @param cLevel64x64 new C-Level of pool * @param isCallPool whether to update C-Level for call or put pool */ function setCLevel( Layout storage l, int128 cLevel64x64, bool isCallPool ) internal { if (isCallPool) { l.cLevelUnderlying64x64 = cLevel64x64; l.cLevelUnderlyingUpdatedAt = block.timestamp; } else { l.cLevelBase64x64 = cLevel64x64; l.cLevelBaseUpdatedAt = block.timestamp; } } function setOracles( Layout storage l, address baseOracle, address underlyingOracle ) internal { require( AggregatorV3Interface(baseOracle).decimals() == AggregatorV3Interface(underlyingOracle).decimals(), "Pool: oracle decimals must match" ); l.baseOracle = baseOracle; l.underlyingOracle = underlyingOracle; } function fetchPriceUpdate(Layout storage l) internal view returns (int128 price64x64) { int256 priceUnderlying = AggregatorInterface(l.underlyingOracle) .latestAnswer(); int256 priceBase = AggregatorInterface(l.baseOracle).latestAnswer(); return ABDKMath64x64.divi(priceUnderlying, priceBase); } /** * @notice set price update for hourly bucket corresponding to given timestamp * @param l storage layout struct * @param timestamp timestamp to update * @param price64x64 64x64 fixed point representation of price */ function setPriceUpdate( Layout storage l, uint256 timestamp, int128 price64x64 ) internal { uint256 bucket = timestamp / (1 hours); l.bucketPrices64x64[bucket] = price64x64; l.priceUpdateSequences[bucket >> 8] += 1 << (255 - (bucket & 255)); } /** * @notice get price update for hourly bucket corresponding to given timestamp * @param l storage layout struct * @param timestamp timestamp to query * @return 64x64 fixed point representation of price */ function getPriceUpdate(Layout storage l, uint256 timestamp) internal view returns (int128) { return l.bucketPrices64x64[timestamp / (1 hours)]; } /** * @notice get first price update available following given timestamp * @param l storage layout struct * @param timestamp timestamp to query * @return 64x64 fixed point representation of price */ function getPriceUpdateAfter(Layout storage l, uint256 timestamp) internal view returns (int128) { // price updates are grouped into hourly buckets uint256 bucket = timestamp / (1 hours); // divide by 256 to get the index of the relevant price update sequence uint256 sequenceId = bucket >> 8; // get position within sequence relevant to current price update uint256 offset = bucket & 255; // shift to skip buckets from earlier in sequence uint256 sequence = (l.priceUpdateSequences[sequenceId] << offset) >> offset; // iterate through future sequences until a price update is found // sequence corresponding to current timestamp used as upper bound uint256 currentPriceUpdateSequenceId = block.timestamp / (256 hours); while (sequence == 0 && sequenceId <= currentPriceUpdateSequenceId) { sequence = l.priceUpdateSequences[++sequenceId]; } // if no price update is found (sequence == 0) function will return 0 // this should never occur, as each relevant external function triggers a price update // the most significant bit of the sequence corresponds to the offset of the relevant bucket uint256 msb; for (uint256 i = 128; i > 0; i >>= 1) { if (sequence >> i > 0) { msb += i; sequence >>= i; } } return l.bucketPrices64x64[((sequenceId + 1) << 8) - msb - 1]; } function totalPendingDeposits(Layout storage l, bool isCallPool) internal view returns (uint256) { return l.nextDeposits[isCallPool].totalPendingDeposits; } function pendingDepositsOf( Layout storage l, address account, bool isCallPool ) internal view returns (uint256) { return l.pendingDeposits[account][l.nextDeposits[isCallPool].eta][ isCallPool ]; } function contractSizeToBaseTokenAmount( Layout storage l, uint256 contractSize, int128 price64x64, bool isCallPool ) internal view returns (uint256 tokenAmount) { if (isCallPool) { tokenAmount = contractSize; } else { uint256 value = price64x64.mulu(contractSize); int128 value64x64 = ABDKMath64x64Token.fromDecimals( value, l.underlyingDecimals ); tokenAmount = ABDKMath64x64Token.toDecimals( value64x64, l.baseDecimals ); } } function setBuybackEnabled( Layout storage l, bool state, bool isCallPool ) internal { l.isBuybackEnabled[msg.sender][isCallPool] = state; } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; /** * @title Set implementation with enumeration functions * @dev derived from https://github.com/OpenZeppelin/openzeppelin-contracts (MIT license) */ library EnumerableSet { struct Set { bytes32[] _values; // 1-indexed to allow 0 to signify nonexistence mapping(bytes32 => uint256) _indexes; } struct Bytes32Set { Set _inner; } struct AddressSet { Set _inner; } struct UintSet { Set _inner; } function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) { return _at(set._inner, index); } function at(AddressSet storage set, uint256 index) internal view returns (address) { return address(uint160(uint256(_at(set._inner, index)))); } function at(UintSet storage set, uint256 index) internal view returns (uint256) { return uint256(_at(set._inner, index)); } function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) { return _contains(set._inner, value); } function contains(AddressSet storage set, address value) internal view returns (bool) { return _contains(set._inner, bytes32(uint256(uint160(value)))); } function contains(UintSet storage set, uint256 value) internal view returns (bool) { return _contains(set._inner, bytes32(value)); } function indexOf(Bytes32Set storage set, bytes32 value) internal view returns (uint256) { return _indexOf(set._inner, value); } function indexOf(AddressSet storage set, address value) internal view returns (uint256) { return _indexOf(set._inner, bytes32(uint256(uint160(value)))); } function indexOf(UintSet storage set, uint256 value) internal view returns (uint256) { return _indexOf(set._inner, bytes32(value)); } function length(Bytes32Set storage set) internal view returns (uint256) { return _length(set._inner); } function length(AddressSet storage set) internal view returns (uint256) { return _length(set._inner); } function length(UintSet storage set) internal view returns (uint256) { return _length(set._inner); } function add(Bytes32Set storage set, bytes32 value) internal returns (bool) { return _add(set._inner, value); } function add(AddressSet storage set, address value) internal returns (bool) { return _add(set._inner, bytes32(uint256(uint160(value)))); } function add(UintSet storage set, uint256 value) internal returns (bool) { return _add(set._inner, bytes32(value)); } function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) { return _remove(set._inner, value); } function remove(AddressSet storage set, address value) internal returns (bool) { return _remove(set._inner, bytes32(uint256(uint160(value)))); } function remove(UintSet storage set, uint256 value) internal returns (bool) { return _remove(set._inner, bytes32(value)); } function _at(Set storage set, uint256 index) private view returns (bytes32) { require( set._values.length > index, 'EnumerableSet: index out of bounds' ); return set._values[index]; } function _contains(Set storage set, bytes32 value) private view returns (bool) { return set._indexes[value] != 0; } function _indexOf(Set storage set, bytes32 value) private view returns (uint256) { unchecked { return set._indexes[value] - 1; } } function _length(Set storage set) private view returns (uint256) { return set._values.length; } function _add(Set storage set, bytes32 value) private returns (bool) { if (!_contains(set, value)) { set._values.push(value); set._indexes[value] = set._values.length; return true; } else { return false; } } function _remove(Set storage set, bytes32 value) private returns (bool) { uint256 valueIndex = set._indexes[value]; if (valueIndex != 0) { unchecked { bytes32 last = set._values[set._values.length - 1]; // move last value to now-vacant index set._values[valueIndex - 1] = last; set._indexes[last] = valueIndex; } // clear last index set._values.pop(); delete set._indexes[value]; return true; } else { return false; } } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; /** * @title Contract ownership standard interface * @dev see https://eips.ethereum.org/EIPS/eip-173 */ interface IERC173 { event OwnershipTransferred( address indexed previousOwner, address indexed newOwner ); /** * @notice get the ERC173 contract owner * @return conract owner */ function owner() external view returns (address); /** * @notice transfer contract ownership to new account * @param account address of new owner */ function transferOwnership(address account) external; }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; library OwnableStorage { struct Layout { address owner; } bytes32 internal constant STORAGE_SLOT = keccak256('solidstate.contracts.storage.Ownable'); function layout() internal pure returns (Layout storage l) { bytes32 slot = STORAGE_SLOT; assembly { l.slot := slot } } function setOwner(Layout storage l, address owner) internal { l.owner = owner; } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import { IERC20 } from '../token/ERC20/IERC20.sol'; import { AddressUtils } from './AddressUtils.sol'; /** * @title Safe ERC20 interaction library * @dev derived from https://github.com/OpenZeppelin/openzeppelin-contracts/ (MIT license) */ library SafeERC20 { using AddressUtils for address; function safeTransfer( IERC20 token, address to, uint256 value ) internal { _callOptionalReturn( token, abi.encodeWithSelector(token.transfer.selector, to, value) ); } function safeTransferFrom( IERC20 token, address from, address to, uint256 value ) internal { _callOptionalReturn( token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value) ); } /** * @dev safeApprove (like approve) should only be called when setting an initial allowance or when resetting it to zero; otherwise prefer safeIncreaseAllowance and safeDecreaseAllowance */ function safeApprove( IERC20 token, address spender, uint256 value ) internal { require( (value == 0) || (token.allowance(address(this), spender) == 0), 'SafeERC20: approve from non-zero to non-zero allowance' ); _callOptionalReturn( token, abi.encodeWithSelector(token.approve.selector, spender, value) ); } function safeIncreaseAllowance( IERC20 token, address spender, uint256 value ) internal { uint256 newAllowance = token.allowance(address(this), spender) + value; _callOptionalReturn( token, abi.encodeWithSelector( token.approve.selector, spender, newAllowance ) ); } function safeDecreaseAllowance( IERC20 token, address spender, uint256 value ) internal { unchecked { uint256 oldAllowance = token.allowance(address(this), spender); require( oldAllowance >= value, 'SafeERC20: decreased allowance below zero' ); uint256 newAllowance = oldAllowance - value; _callOptionalReturn( token, abi.encodeWithSelector( token.approve.selector, spender, newAllowance ) ); } } /** * @notice send transaction data and check validity of return value, if present * @param token ERC20 token interface * @param data transaction data */ function _callOptionalReturn(IERC20 token, bytes memory data) private { bytes memory returndata = address(token).functionCall( data, 'SafeERC20: low-level call failed' ); if (returndata.length > 0) { require( abi.decode(returndata, (bool)), 'SafeERC20: ERC20 operation did not succeed' ); } } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import { IERC20Internal } from './IERC20Internal.sol'; /** * @title ERC20 interface * @dev see https://github.com/ethereum/EIPs/issues/20 */ interface IERC20 is IERC20Internal { /** * @notice query the total minted token supply * @return token supply */ function totalSupply() external view returns (uint256); /** * @notice query the token balance of given account * @param account address to query * @return token balance */ function balanceOf(address account) external view returns (uint256); /** * @notice query the allowance granted from given holder to given spender * @param holder approver of allowance * @param spender recipient of allowance * @return token allowance */ function allowance(address holder, address spender) external view returns (uint256); /** * @notice grant approval to spender to spend tokens * @dev prefer ERC20Extended functions to avoid transaction-ordering vulnerability (see https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729) * @param spender recipient of allowance * @param amount quantity of tokens approved for spending * @return success status (always true; otherwise function should revert) */ function approve(address spender, uint256 amount) external returns (bool); /** * @notice transfer tokens to given recipient * @param recipient beneficiary of token transfer * @param amount quantity of tokens to transfer * @return success status (always true; otherwise function should revert) */ function transfer(address recipient, uint256 amount) external returns (bool); /** * @notice transfer tokens to given recipient on behalf of given holder * @param holder holder of tokens prior to transfer * @param recipient beneficiary of token transfer * @param amount quantity of tokens to transfer * @return success status (always true; otherwise function should revert) */ function transferFrom( address holder, address recipient, uint256 amount ) external returns (bool); }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import { EnumerableSet } from '../../../utils/EnumerableSet.sol'; import { ERC1155Base, ERC1155BaseInternal } from '../base/ERC1155Base.sol'; import { IERC1155Enumerable } from './IERC1155Enumerable.sol'; import { ERC1155EnumerableInternal, ERC1155EnumerableStorage } from './ERC1155EnumerableInternal.sol'; /** * @title ERC1155 implementation including enumerable and aggregate functions */ abstract contract ERC1155Enumerable is IERC1155Enumerable, ERC1155Base, ERC1155EnumerableInternal { using EnumerableSet for EnumerableSet.AddressSet; using EnumerableSet for EnumerableSet.UintSet; /** * @inheritdoc IERC1155Enumerable */ function totalSupply(uint256 id) public view virtual returns (uint256) { return _totalSupply(id); } /** * @inheritdoc IERC1155Enumerable */ function totalHolders(uint256 id) public view virtual returns (uint256) { return _totalHolders(id); } /** * @inheritdoc IERC1155Enumerable */ function accountsByToken(uint256 id) public view virtual returns (address[] memory) { return _accountsByToken(id); } /** * @inheritdoc IERC1155Enumerable */ function tokensByAccount(address account) public view virtual returns (uint256[] memory) { return _tokensByAccount(account); } /** * @notice ERC1155 hook: update aggregate values * @inheritdoc ERC1155EnumerableInternal */ function _beforeTokenTransfer( address operator, address from, address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data ) internal virtual override(ERC1155BaseInternal, ERC1155EnumerableInternal) { super._beforeTokenTransfer(operator, from, to, ids, amounts, data); } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import { IERC20 } from '../token/ERC20/IERC20.sol'; import { IERC20Metadata } from '../token/ERC20/metadata/IERC20Metadata.sol'; /** * @title WETH (Wrapped ETH) interface */ interface IWETH is IERC20, IERC20Metadata { /** * @notice convert ETH to WETH */ function deposit() external payable; /** * @notice convert WETH to ETH * @dev if caller is a contract, it should have a fallback or receive function * @param amount quantity of WETH to convert, denominated in wei */ function withdraw(uint256 amount) external; }
// SPDX-License-Identifier: LGPL-3.0-or-later pragma solidity ^0.8.0; /** * @title Premia Exchange Helper * @dev deployed standalone and referenced by internal functions * @dev do NOT set approval to this contract! */ interface IExchangeHelper { /** * @notice perform arbitrary swap transaction * @param sourceToken source token to pull into this address * @param targetToken target token to buy * @param pullAmount amount of source token to start the trade * @param callee exchange address to call to execute the trade. * @param allowanceTarget address for which to set allowance for the trade * @param data calldata to execute the trade * @param refundAddress address that un-used source token goes to * @return amountOut quantity of targetToken yielded by swap */ function swapWithToken( address sourceToken, address targetToken, uint256 pullAmount, address callee, address allowanceTarget, bytes calldata data, address refundAddress ) external returns (uint256 amountOut); }
// SPDX-License-Identifier: BUSL-1.1 // For further clarification please see https://license.premia.legal pragma solidity ^0.8.0; import {ABDKMath64x64} from "abdk-libraries-solidity/ABDKMath64x64.sol"; library OptionMath { using ABDKMath64x64 for int128; struct QuoteArgs { int128 varianceAnnualized64x64; // 64x64 fixed point representation of annualized variance int128 strike64x64; // 64x64 fixed point representation of strike price int128 spot64x64; // 64x64 fixed point representation of spot price int128 timeToMaturity64x64; // 64x64 fixed point representation of duration of option contract (in years) int128 oldCLevel64x64; // 64x64 fixed point representation of C-Level of Pool before purchase int128 oldPoolState; // 64x64 fixed point representation of current state of the pool int128 newPoolState; // 64x64 fixed point representation of state of the pool after trade int128 steepness64x64; // 64x64 fixed point representation of Pool state delta multiplier int128 minAPY64x64; // 64x64 fixed point representation of minimum APY for capital locked up to underwrite options bool isCall; // whether to price "call" or "put" option } struct CalculateCLevelDecayArgs { int128 timeIntervalsElapsed64x64; // 64x64 fixed point representation of quantity of discrete arbitrary intervals elapsed since last update int128 oldCLevel64x64; // 64x64 fixed point representation of C-Level prior to accounting for decay int128 utilization64x64; // 64x64 fixed point representation of pool capital utilization rate int128 utilizationLowerBound64x64; int128 utilizationUpperBound64x64; int128 cLevelLowerBound64x64; int128 cLevelUpperBound64x64; int128 cConvergenceULowerBound64x64; int128 cConvergenceUUpperBound64x64; } // 64x64 fixed point integer constants int128 internal constant ONE_64x64 = 0x10000000000000000; int128 internal constant THREE_64x64 = 0x30000000000000000; // 64x64 fixed point constants used in Choudhury’s approximation of the Black-Scholes CDF int128 private constant CDF_CONST_0 = 0x09109f285df452394; // 2260 / 3989 int128 private constant CDF_CONST_1 = 0x19abac0ea1da65036; // 6400 / 3989 int128 private constant CDF_CONST_2 = 0x0d3c84b78b749bd6b; // 3300 / 3989 /** * @notice recalculate C-Level based on change in liquidity * @param initialCLevel64x64 64x64 fixed point representation of C-Level of Pool before update * @param oldPoolState64x64 64x64 fixed point representation of liquidity in pool before update * @param newPoolState64x64 64x64 fixed point representation of liquidity in pool after update * @param steepness64x64 64x64 fixed point representation of steepness coefficient * @return 64x64 fixed point representation of new C-Level */ function calculateCLevel( int128 initialCLevel64x64, int128 oldPoolState64x64, int128 newPoolState64x64, int128 steepness64x64 ) external pure returns (int128) { return newPoolState64x64 .sub(oldPoolState64x64) .div( oldPoolState64x64 > newPoolState64x64 ? oldPoolState64x64 : newPoolState64x64 ) .mul(steepness64x64) .neg() .exp() .mul(initialCLevel64x64); } /** * @notice calculate the price of an option using the Premia Finance model * @param args arguments of quotePrice * @return premiaPrice64x64 64x64 fixed point representation of Premia option price * @return cLevel64x64 64x64 fixed point representation of C-Level of Pool after purchase */ function quotePrice(QuoteArgs memory args) external pure returns ( int128 premiaPrice64x64, int128 cLevel64x64, int128 slippageCoefficient64x64 ) { int128 deltaPoolState64x64 = args .newPoolState .sub(args.oldPoolState) .div(args.oldPoolState) .mul(args.steepness64x64); int128 tradingDelta64x64 = deltaPoolState64x64.neg().exp(); int128 blackScholesPrice64x64 = _blackScholesPrice( args.varianceAnnualized64x64, args.strike64x64, args.spot64x64, args.timeToMaturity64x64, args.isCall ); cLevel64x64 = tradingDelta64x64.mul(args.oldCLevel64x64); slippageCoefficient64x64 = ONE_64x64.sub(tradingDelta64x64).div( deltaPoolState64x64 ); premiaPrice64x64 = blackScholesPrice64x64.mul(cLevel64x64).mul( slippageCoefficient64x64 ); int128 intrinsicValue64x64; if (args.isCall && args.strike64x64 < args.spot64x64) { intrinsicValue64x64 = args.spot64x64.sub(args.strike64x64); } else if (!args.isCall && args.strike64x64 > args.spot64x64) { intrinsicValue64x64 = args.strike64x64.sub(args.spot64x64); } int128 collateralValue64x64 = args.isCall ? args.spot64x64 : args.strike64x64; int128 minPrice64x64 = intrinsicValue64x64.add( collateralValue64x64.mul(args.minAPY64x64).mul( args.timeToMaturity64x64 ) ); if (minPrice64x64 > premiaPrice64x64) { premiaPrice64x64 = minPrice64x64; } } /** * @notice calculate the decay of C-Level based on heat diffusion function * @param args structured CalculateCLevelDecayArgs * @return cLevelDecayed64x64 C-Level after accounting for decay */ function calculateCLevelDecay(CalculateCLevelDecayArgs memory args) external pure returns (int128 cLevelDecayed64x64) { int128 convFHighU64x64 = (args.utilization64x64 >= args.utilizationUpperBound64x64 && args.oldCLevel64x64 <= args.cLevelLowerBound64x64) ? ONE_64x64 : int128(0); int128 convFLowU64x64 = (args.utilization64x64 <= args.utilizationLowerBound64x64 && args.oldCLevel64x64 >= args.cLevelUpperBound64x64) ? ONE_64x64 : int128(0); cLevelDecayed64x64 = args .oldCLevel64x64 .sub(args.cConvergenceULowerBound64x64.mul(convFLowU64x64)) .sub(args.cConvergenceUUpperBound64x64.mul(convFHighU64x64)) .mul( convFLowU64x64 .mul(ONE_64x64.sub(args.utilization64x64)) .add(convFHighU64x64.mul(args.utilization64x64)) .mul(args.timeIntervalsElapsed64x64) .neg() .exp() ) .add( args.cConvergenceULowerBound64x64.mul(convFLowU64x64).add( args.cConvergenceUUpperBound64x64.mul(convFHighU64x64) ) ); } /** * @notice calculate the exponential decay coefficient for a given interval * @param oldTimestamp timestamp of previous update * @param newTimestamp current timestamp * @return 64x64 fixed point representation of exponential decay coefficient */ function _decay(uint256 oldTimestamp, uint256 newTimestamp) internal pure returns (int128) { return ONE_64x64.sub( (-ABDKMath64x64.divu(newTimestamp - oldTimestamp, 7 days)).exp() ); } /** * @notice calculate Choudhury’s approximation of the Black-Scholes CDF * @param input64x64 64x64 fixed point representation of random variable * @return 64x64 fixed point representation of the approximated CDF of x */ function _N(int128 input64x64) internal pure returns (int128) { // squaring via mul is cheaper than via pow int128 inputSquared64x64 = input64x64.mul(input64x64); int128 value64x64 = (-inputSquared64x64 >> 1).exp().div( CDF_CONST_0.add(CDF_CONST_1.mul(input64x64.abs())).add( CDF_CONST_2.mul(inputSquared64x64.add(THREE_64x64).sqrt()) ) ); return input64x64 > 0 ? ONE_64x64.sub(value64x64) : value64x64; } /** * @notice calculate the price of an option using the Black-Scholes model * @param varianceAnnualized64x64 64x64 fixed point representation of annualized variance * @param strike64x64 64x64 fixed point representation of strike price * @param spot64x64 64x64 fixed point representation of spot price * @param timeToMaturity64x64 64x64 fixed point representation of duration of option contract (in years) * @param isCall whether to price "call" or "put" option * @return 64x64 fixed point representation of Black-Scholes option price */ function _blackScholesPrice( int128 varianceAnnualized64x64, int128 strike64x64, int128 spot64x64, int128 timeToMaturity64x64, bool isCall ) internal pure returns (int128) { int128 cumulativeVariance64x64 = timeToMaturity64x64.mul( varianceAnnualized64x64 ); int128 cumulativeVarianceSqrt64x64 = cumulativeVariance64x64.sqrt(); int128 d1_64x64 = spot64x64 .div(strike64x64) .ln() .add(cumulativeVariance64x64 >> 1) .div(cumulativeVarianceSqrt64x64); int128 d2_64x64 = d1_64x64.sub(cumulativeVarianceSqrt64x64); if (isCall) { return spot64x64.mul(_N(d1_64x64)).sub(strike64x64.mul(_N(d2_64x64))); } else { return -spot64x64.mul(_N(-d1_64x64)).sub( strike64x64.mul(_N(-d2_64x64)) ); } } }
// SPDX-License-Identifier: LGPL-3.0-or-later pragma solidity ^0.8.0; import {PremiaMiningStorage} from "./PremiaMiningStorage.sol"; interface IPremiaMining { function addPremiaRewards(uint256 _amount) external; function premiaRewardsAvailable() external view returns (uint256); function getTotalAllocationPoints() external view returns (uint256); function getPoolInfo(address pool, bool isCallPool) external view returns (PremiaMiningStorage.PoolInfo memory); function getPremiaPerYear() external view returns (uint256); function addPool(address _pool, uint256 _allocPoints) external; function setPoolAllocPoints( address[] memory _pools, uint256[] memory _allocPoints ) external; function pendingPremia( address _pool, bool _isCallPool, address _user ) external view returns (uint256); function updatePool( address _pool, bool _isCallPool, uint256 _totalTVL ) external; function allocatePending( address _user, address _pool, bool _isCallPool, uint256 _userTVLOld, uint256 _userTVLNew, uint256 _totalTVL ) external; function claim( address _user, address _pool, bool _isCallPool, uint256 _userTVLOld, uint256 _userTVLNew, uint256 _totalTVL ) external; }
// SPDX-License-Identifier: LGPL-3.0-or-later pragma solidity ^0.8.0; import {VolatilitySurfaceOracleStorage} from "./VolatilitySurfaceOracleStorage.sol"; interface IVolatilitySurfaceOracle { /** * @notice Pack IV model parameters into a single bytes32 * @dev This function is used to pack the parameters into a single variable, which is then used as input in `update` * @param params Parameters of IV model to pack * @return result The packed parameters of IV model */ function formatParams(int256[5] memory params) external pure returns (bytes32 result); /** * @notice Unpack IV model parameters from a bytes32 * @param input Packed IV model parameters to unpack * @return params The unpacked parameters of the IV model */ function parseParams(bytes32 input) external pure returns (int256[] memory params); /** * @notice Get the list of whitelisted relayers * @return The list of whitelisted relayers */ function getWhitelistedRelayers() external view returns (address[] memory); /** * @notice Get the IV model parameters of a token pair * @param base The base token of the pair * @param underlying The underlying token of the pair * @return The IV model parameters */ function getParams(address base, address underlying) external view returns (VolatilitySurfaceOracleStorage.Update memory); /** * @notice Get unpacked IV model parameters * @param base The base token of the pair * @param underlying The underlying token of the pair * @return The unpacked IV model parameters */ function getParamsUnpacked(address base, address underlying) external view returns (int256[] memory); /** * @notice Get time to maturity in years, as a 64x64 fixed point representation * @param maturity Maturity timestamp * @return Time to maturity (in years), as a 64x64 fixed point representation */ function getTimeToMaturity64x64(uint64 maturity) external view returns (int128); /** * @notice calculate the annualized volatility for given set of parameters * @param base The base token of the pair * @param underlying The underlying token of the pair * @param spot64x64 64x64 fixed point representation of spot price * @param strike64x64 64x64 fixed point representation of strike price * @param timeToMaturity64x64 64x64 fixed point representation of time to maturity (denominated in years) * @return 64x64 fixed point representation of annualized implied volatility, where 1 is defined as 100% */ function getAnnualizedVolatility64x64( address base, address underlying, int128 spot64x64, int128 strike64x64, int128 timeToMaturity64x64 ) external view returns (int128); /** * @notice calculate the price of an option using the Black-Scholes model * @param base The base token of the pair * @param underlying The underlying token of the pair * @param spot64x64 Spot price, as a 64x64 fixed point representation * @param strike64x64 Strike, as a64x64 fixed point representation * @param timeToMaturity64x64 64x64 fixed point representation of time to maturity (denominated in years) * @param isCall Whether it is for call or put * @return 64x64 fixed point representation of the Black Scholes price */ function getBlackScholesPrice64x64( address base, address underlying, int128 spot64x64, int128 strike64x64, int128 timeToMaturity64x64, bool isCall ) external view returns (int128); /** * @notice Get Black Scholes price as an uint256 with 18 decimals * @param base The base token of the pair * @param underlying The underlying token of the pair * @param spot64x64 Spot price, as a 64x64 fixed point representation * @param strike64x64 Strike, as a64x64 fixed point representation * @param timeToMaturity64x64 64x64 fixed point representation of time to maturity (denominated in years) * @param isCall Whether it is for call or put * @return Black scholes price, as an uint256 with 18 decimals */ function getBlackScholesPrice( address base, address underlying, int128 spot64x64, int128 strike64x64, int128 timeToMaturity64x64, bool isCall ) external view returns (uint256); /** * @notice Add relayers to the whitelist so that they can add oracle surfaces * @param accounts The addresses to add to the whitelist */ function addWhitelistedRelayers(address[] memory accounts) external; /** * @notice Remove relayers from the whitelist so that they cannot add oracle surfaces * @param accounts The addresses to remove from the whitelist */ function removeWhitelistedRelayers(address[] memory accounts) external; /** * @notice Update a list of IV model parameters * @param base List of base tokens * @param underlying List of underlying tokens * @param parameters List of IV model parameters */ function updateParams( address[] memory base, address[] memory underlying, bytes32[] memory parameters ) external; }
// SPDX-License-Identifier: LGPL-3.0-or-later pragma solidity ^0.8.0; import {FeeDiscountStorage} from "./FeeDiscountStorage.sol"; interface IFeeDiscount { event Staked( address indexed user, uint256 amount, uint256 stakePeriod, uint256 lockedUntil ); event Unstaked(address indexed user, uint256 amount); struct StakeLevel { uint256 amount; // Amount to stake uint256 discount; // Discount when amount is reached } /** * @notice Stake using IERC2612 permit * @param amount The amount of xPremia to stake * @param period The lockup period (in seconds) * @param deadline Deadline after which permit will fail * @param v V * @param r R * @param s S */ function stakeWithPermit( uint256 amount, uint256 period, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /** * @notice Lockup xPremia for protocol fee discounts * Longer period of locking will apply a multiplier on the amount staked, in the fee discount calculation * @param amount The amount of xPremia to stake * @param period The lockup period (in seconds) */ function stake(uint256 amount, uint256 period) external; /** * @notice Unstake xPremia (If lockup period has ended) * @param amount The amount of xPremia to unstake */ function unstake(uint256 amount) external; ////////// // View // ////////// /** * Calculate the stake amount of a user, after applying the bonus from the lockup period chosen * @param user The user from which to query the stake amount * @return The user stake amount after applying the bonus */ function getStakeAmountWithBonus(address user) external view returns (uint256); /** * @notice Calculate the % of fee discount for user, based on his stake * @param user The _user for which the discount is for * @return Percentage of protocol fee discount (in basis point) * Ex : 1000 = 10% fee discount */ function getDiscount(address user) external view returns (uint256); /** * @notice Get stake levels * @return Stake levels * Ex : 2500 = -25% */ function getStakeLevels() external returns (StakeLevel[] memory); /** * @notice Get stake period multiplier * @param period The duration (in seconds) for which tokens are locked * @return The multiplier for this staking period * Ex : 20000 = x2 */ function getStakePeriodMultiplier(uint256 period) external returns (uint256); /** * @notice Get staking infos of a user * @param user The user address for which to get staking infos * @return The staking infos of the user */ function getUserInfo(address user) external view returns (FeeDiscountStorage.UserInfo memory); }
// SPDX-License-Identifier: LGPL-3.0-or-later pragma solidity ^0.8.0; interface IPoolEvents { event Purchase( address indexed user, uint256 longTokenId, uint256 contractSize, uint256 baseCost, uint256 feeCost, int128 spot64x64 ); event Sell( address indexed user, uint256 longTokenId, uint256 contractSize, uint256 baseCost, uint256 feeCost, int128 spot64x64 ); event Exercise( address indexed user, uint256 longTokenId, uint256 contractSize, uint256 exerciseValue, uint256 fee ); event Underwrite( address indexed underwriter, address indexed longReceiver, uint256 shortTokenId, uint256 intervalContractSize, uint256 intervalPremium, bool isManualUnderwrite ); event AssignExercise( address indexed underwriter, uint256 shortTokenId, uint256 freedAmount, uint256 intervalContractSize, uint256 fee ); event AssignSale( address indexed underwriter, uint256 shortTokenId, uint256 freedAmount, uint256 intervalContractSize ); event Deposit(address indexed user, bool isCallPool, uint256 amount); event Withdrawal( address indexed user, bool isCallPool, uint256 depositedAt, uint256 amount ); event FeeWithdrawal(bool indexed isCallPool, uint256 amount); event APYFeeReserved( address underwriter, uint256 shortTokenId, uint256 amount ); event APYFeePaid(address underwriter, uint256 shortTokenId, uint256 amount); event Annihilate(uint256 shortTokenId, uint256 amount); event UpdateCLevel( bool indexed isCall, int128 cLevel64x64, int128 oldLiquidity64x64, int128 newLiquidity64x64 ); event UpdateSteepness(int128 steepness64x64, bool isCallPool); event UpdateSpotOffset(int128 spotOffset64x64); }
// SPDX-License-Identifier: LGPL-3.0-or-later pragma solidity ^0.8.0; interface IPoolInternal { struct SwapArgs { // token to pass in to swap address tokenIn; // amount of tokenIn to trade uint256 amountInMax; //min amount out to be used to purchase uint256 amountOutMin; // exchange address to call to execute the trade address callee; // address for which to set allowance for the trade address allowanceTarget; // data to execute the trade bytes data; // address to which refund excess tokens address refundAddress; } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import { UintUtils } from './UintUtils.sol'; library AddressUtils { using UintUtils for uint256; function toString(address account) internal pure returns (string memory) { return uint256(uint160(account)).toHexString(20); } function isContract(address account) internal view returns (bool) { uint256 size; assembly { size := extcodesize(account) } return size > 0; } function sendValue(address payable account, uint256 amount) internal { (bool success, ) = account.call{ value: amount }(''); require(success, 'AddressUtils: failed to send value'); } function functionCall(address target, bytes memory data) internal returns (bytes memory) { return functionCall(target, data, 'AddressUtils: failed low-level call'); } function functionCall( address target, bytes memory data, string memory error ) internal returns (bytes memory) { return _functionCallWithValue(target, data, 0, error); } function functionCallWithValue( address target, bytes memory data, uint256 value ) internal returns (bytes memory) { return functionCallWithValue( target, data, value, 'AddressUtils: failed low-level call with value' ); } function functionCallWithValue( address target, bytes memory data, uint256 value, string memory error ) internal returns (bytes memory) { require( address(this).balance >= value, 'AddressUtils: insufficient balance for call' ); return _functionCallWithValue(target, data, value, error); } function _functionCallWithValue( address target, bytes memory data, uint256 value, string memory error ) private returns (bytes memory) { require( isContract(target), 'AddressUtils: function call to non-contract' ); (bool success, bytes memory returnData) = target.call{ value: value }( data ); if (success) { return returnData; } else if (returnData.length > 0) { assembly { let returnData_size := mload(returnData) revert(add(32, returnData), returnData_size) } } else { revert(error); } } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; /** * @title Partial ERC20 interface needed by internal functions */ interface IERC20Internal { event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; /** * @title utility functions for uint256 operations * @dev derived from https://github.com/OpenZeppelin/openzeppelin-contracts/ (MIT license) */ library UintUtils { bytes16 private constant HEX_SYMBOLS = '0123456789abcdef'; function toString(uint256 value) internal pure returns (string memory) { if (value == 0) { return '0'; } uint256 temp = value; uint256 digits; while (temp != 0) { digits++; temp /= 10; } bytes memory buffer = new bytes(digits); while (value != 0) { digits -= 1; buffer[digits] = bytes1(uint8(48 + uint256(value % 10))); value /= 10; } return string(buffer); } function toHexString(uint256 value) internal pure returns (string memory) { if (value == 0) { return '0x00'; } uint256 length = 0; for (uint256 temp = value; temp != 0; temp >>= 8) { unchecked { length++; } } return toHexString(value, length); } function toHexString(uint256 value, uint256 length) internal pure returns (string memory) { bytes memory buffer = new bytes(2 * length + 2); buffer[0] = '0'; buffer[1] = 'x'; unchecked { for (uint256 i = 2 * length + 1; i > 1; --i) { buffer[i] = HEX_SYMBOLS[value & 0xf]; value >>= 4; } } require(value == 0, 'UintUtils: hex length insufficient'); return string(buffer); } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import { IERC1155 } from '../IERC1155.sol'; import { IERC1155Receiver } from '../IERC1155Receiver.sol'; import { ERC1155BaseInternal, ERC1155BaseStorage } from './ERC1155BaseInternal.sol'; /** * @title Base ERC1155 contract * @dev derived from https://github.com/OpenZeppelin/openzeppelin-contracts/ (MIT license) */ abstract contract ERC1155Base is IERC1155, ERC1155BaseInternal { /** * @inheritdoc IERC1155 */ function balanceOf(address account, uint256 id) public view virtual returns (uint256) { return _balanceOf(account, id); } /** * @inheritdoc IERC1155 */ function balanceOfBatch(address[] memory accounts, uint256[] memory ids) public view virtual returns (uint256[] memory) { require( accounts.length == ids.length, 'ERC1155: accounts and ids length mismatch' ); mapping(uint256 => mapping(address => uint256)) storage balances = ERC1155BaseStorage.layout().balances; uint256[] memory batchBalances = new uint256[](accounts.length); unchecked { for (uint256 i; i < accounts.length; i++) { require( accounts[i] != address(0), 'ERC1155: batch balance query for the zero address' ); batchBalances[i] = balances[ids[i]][accounts[i]]; } } return batchBalances; } /** * @inheritdoc IERC1155 */ function isApprovedForAll(address account, address operator) public view virtual returns (bool) { return ERC1155BaseStorage.layout().operatorApprovals[account][operator]; } /** * @inheritdoc IERC1155 */ function setApprovalForAll(address operator, bool status) public virtual { require( msg.sender != operator, 'ERC1155: setting approval status for self' ); ERC1155BaseStorage.layout().operatorApprovals[msg.sender][ operator ] = status; emit ApprovalForAll(msg.sender, operator, status); } /** * @inheritdoc IERC1155 */ function safeTransferFrom( address from, address to, uint256 id, uint256 amount, bytes memory data ) public virtual { require( from == msg.sender || isApprovedForAll(from, msg.sender), 'ERC1155: caller is not owner nor approved' ); _safeTransfer(msg.sender, from, to, id, amount, data); } /** * @inheritdoc IERC1155 */ function safeBatchTransferFrom( address from, address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data ) public virtual { require( from == msg.sender || isApprovedForAll(from, msg.sender), 'ERC1155: caller is not owner nor approved' ); _safeTransferBatch(msg.sender, from, to, ids, amounts, data); } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; /** * @title ERC1155 enumerable and aggregate function interface */ interface IERC1155Enumerable { /** * @notice query total minted supply of given token * @param id token id to query * @return token supply */ function totalSupply(uint256 id) external view returns (uint256); /** * @notice query total number of holders for given token * @param id token id to query * @return quantity of holders */ function totalHolders(uint256 id) external view returns (uint256); /** * @notice query holders of given token * @param id token id to query * @return list of holder addresses */ function accountsByToken(uint256 id) external view returns (address[] memory); /** * @notice query tokens held by given address * @param account address to query * @return list of token ids */ function tokensByAccount(address account) external view returns (uint256[] memory); }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import { EnumerableSet } from '../../../utils/EnumerableSet.sol'; import { ERC1155BaseInternal, ERC1155BaseStorage } from '../base/ERC1155BaseInternal.sol'; import { ERC1155EnumerableStorage } from './ERC1155EnumerableStorage.sol'; /** * @title ERC1155Enumerable internal functions */ abstract contract ERC1155EnumerableInternal is ERC1155BaseInternal { using EnumerableSet for EnumerableSet.AddressSet; using EnumerableSet for EnumerableSet.UintSet; /** * @notice query total minted supply of given token * @param id token id to query * @return token supply */ function _totalSupply(uint256 id) internal view virtual returns (uint256) { return ERC1155EnumerableStorage.layout().totalSupply[id]; } /** * @notice query total number of holders for given token * @param id token id to query * @return quantity of holders */ function _totalHolders(uint256 id) internal view virtual returns (uint256) { return ERC1155EnumerableStorage.layout().accountsByToken[id].length(); } /** * @notice query holders of given token * @param id token id to query * @return list of holder addresses */ function _accountsByToken(uint256 id) internal view virtual returns (address[] memory) { EnumerableSet.AddressSet storage accounts = ERC1155EnumerableStorage .layout() .accountsByToken[id]; address[] memory addresses = new address[](accounts.length()); unchecked { for (uint256 i; i < accounts.length(); i++) { addresses[i] = accounts.at(i); } } return addresses; } /** * @notice query tokens held by given address * @param account address to query * @return list of token ids */ function _tokensByAccount(address account) internal view virtual returns (uint256[] memory) { EnumerableSet.UintSet storage tokens = ERC1155EnumerableStorage .layout() .tokensByAccount[account]; uint256[] memory ids = new uint256[](tokens.length()); unchecked { for (uint256 i; i < tokens.length(); i++) { ids[i] = tokens.at(i); } } return ids; } /** * @notice ERC1155 hook: update aggregate values * @inheritdoc ERC1155BaseInternal */ function _beforeTokenTransfer( address operator, address from, address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data ) internal virtual override { super._beforeTokenTransfer(operator, from, to, ids, amounts, data); if (from != to) { ERC1155EnumerableStorage.Layout storage l = ERC1155EnumerableStorage .layout(); mapping(uint256 => EnumerableSet.AddressSet) storage tokenAccounts = l.accountsByToken; EnumerableSet.UintSet storage fromTokens = l.tokensByAccount[from]; EnumerableSet.UintSet storage toTokens = l.tokensByAccount[to]; for (uint256 i; i < ids.length; ) { uint256 amount = amounts[i]; if (amount > 0) { uint256 id = ids[i]; if (from == address(0)) { l.totalSupply[id] += amount; } else if (_balanceOf(from, id) == amount) { tokenAccounts[id].remove(from); fromTokens.remove(id); } if (to == address(0)) { l.totalSupply[id] -= amount; } else if (_balanceOf(to, id) == 0) { tokenAccounts[id].add(to); toTokens.add(id); } } unchecked { i++; } } } } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import { IERC1155Internal } from './IERC1155Internal.sol'; import { IERC165 } from '../../introspection/IERC165.sol'; /** * @title ERC1155 interface * @dev see https://github.com/ethereum/EIPs/issues/1155 */ interface IERC1155 is IERC1155Internal, IERC165 { /** * @notice query the balance of given token held by given address * @param account address to query * @param id token to query * @return token balance */ function balanceOf(address account, uint256 id) external view returns (uint256); /** * @notice query the balances of given tokens held by given addresses * @param accounts addresss to query * @param ids tokens to query * @return token balances */ function balanceOfBatch(address[] calldata accounts, uint256[] calldata ids) external view returns (uint256[] memory); /** * @notice query approval status of given operator with respect to given address * @param account address to query for approval granted * @param operator address to query for approval received * @return whether operator is approved to spend tokens held by account */ function isApprovedForAll(address account, address operator) external view returns (bool); /** * @notice grant approval to or revoke approval from given operator to spend held tokens * @param operator address whose approval status to update * @param status whether operator should be considered approved */ function setApprovalForAll(address operator, bool status) external; /** * @notice transfer tokens between given addresses, checking for ERC1155Receiver implementation if applicable * @param from sender of tokens * @param to receiver of tokens * @param id token ID * @param amount quantity of tokens to transfer * @param data data payload */ function safeTransferFrom( address from, address to, uint256 id, uint256 amount, bytes calldata data ) external; /** * @notice transfer batch of tokens between given addresses, checking for ERC1155Receiver implementation if applicable * @param from sender of tokens * @param to receiver of tokens * @param ids list of token IDs * @param amounts list of quantities of tokens to transfer * @param data data payload */ function safeBatchTransferFrom( address from, address to, uint256[] calldata ids, uint256[] calldata amounts, bytes calldata data ) external; }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import { IERC165 } from '../../introspection/IERC165.sol'; /** * @title ERC1155 transfer receiver interface */ interface IERC1155Receiver is IERC165 { /** * @notice validate receipt of ERC1155 transfer * @param operator executor of transfer * @param from sender of tokens * @param id token ID received * @param value quantity of tokens received * @param data data payload * @return function's own selector if transfer is accepted */ function onERC1155Received( address operator, address from, uint256 id, uint256 value, bytes calldata data ) external returns (bytes4); /** * @notice validate receipt of ERC1155 batch transfer * @param operator executor of transfer * @param from sender of tokens * @param ids token IDs received * @param values quantities of tokens received * @param data data payload * @return function's own selector if transfer is accepted */ function onERC1155BatchReceived( address operator, address from, uint256[] calldata ids, uint256[] calldata values, bytes calldata data ) external returns (bytes4); }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import { AddressUtils } from '../../../utils/AddressUtils.sol'; import { IERC1155Internal } from '../IERC1155Internal.sol'; import { IERC1155Receiver } from '../IERC1155Receiver.sol'; import { ERC1155BaseStorage } from './ERC1155BaseStorage.sol'; /** * @title Base ERC1155 internal functions * @dev derived from https://github.com/OpenZeppelin/openzeppelin-contracts/ (MIT license) */ abstract contract ERC1155BaseInternal is IERC1155Internal { using AddressUtils for address; /** * @notice query the balance of given token held by given address * @param account address to query * @param id token to query * @return token balance */ function _balanceOf(address account, uint256 id) internal view virtual returns (uint256) { require( account != address(0), 'ERC1155: balance query for the zero address' ); return ERC1155BaseStorage.layout().balances[id][account]; } /** * @notice mint given quantity of tokens for given address * @dev ERC1155Receiver implementation is not checked * @param account beneficiary of minting * @param id token ID * @param amount quantity of tokens to mint * @param data data payload */ function _mint( address account, uint256 id, uint256 amount, bytes memory data ) internal virtual { require(account != address(0), 'ERC1155: mint to the zero address'); _beforeTokenTransfer( msg.sender, address(0), account, _asSingletonArray(id), _asSingletonArray(amount), data ); ERC1155BaseStorage.layout().balances[id][account] += amount; emit TransferSingle(msg.sender, address(0), account, id, amount); } /** * @notice mint given quantity of tokens for given address * @param account beneficiary of minting * @param id token ID * @param amount quantity of tokens to mint * @param data data payload */ function _safeMint( address account, uint256 id, uint256 amount, bytes memory data ) internal virtual { _mint(account, id, amount, data); _doSafeTransferAcceptanceCheck( msg.sender, address(0), account, id, amount, data ); } /** * @notice mint batch of tokens for given address * @dev ERC1155Receiver implementation is not checked * @param account beneficiary of minting * @param ids list of token IDs * @param amounts list of quantities of tokens to mint * @param data data payload */ function _mintBatch( address account, uint256[] memory ids, uint256[] memory amounts, bytes memory data ) internal virtual { require(account != address(0), 'ERC1155: mint to the zero address'); require( ids.length == amounts.length, 'ERC1155: ids and amounts length mismatch' ); _beforeTokenTransfer( msg.sender, address(0), account, ids, amounts, data ); mapping(uint256 => mapping(address => uint256)) storage balances = ERC1155BaseStorage.layout().balances; for (uint256 i; i < ids.length; ) { balances[ids[i]][account] += amounts[i]; unchecked { i++; } } emit TransferBatch(msg.sender, address(0), account, ids, amounts); } /** * @notice mint batch of tokens for given address * @param account beneficiary of minting * @param ids list of token IDs * @param amounts list of quantities of tokens to mint * @param data data payload */ function _safeMintBatch( address account, uint256[] memory ids, uint256[] memory amounts, bytes memory data ) internal virtual { _mintBatch(account, ids, amounts, data); _doSafeBatchTransferAcceptanceCheck( msg.sender, address(0), account, ids, amounts, data ); } /** * @notice burn given quantity of tokens held by given address * @param account holder of tokens to burn * @param id token ID * @param amount quantity of tokens to burn */ function _burn( address account, uint256 id, uint256 amount ) internal virtual { require(account != address(0), 'ERC1155: burn from the zero address'); _beforeTokenTransfer( msg.sender, account, address(0), _asSingletonArray(id), _asSingletonArray(amount), '' ); mapping(address => uint256) storage balances = ERC1155BaseStorage .layout() .balances[id]; unchecked { require( balances[account] >= amount, 'ERC1155: burn amount exceeds balances' ); balances[account] -= amount; } emit TransferSingle(msg.sender, account, address(0), id, amount); } /** * @notice burn given batch of tokens held by given address * @param account holder of tokens to burn * @param ids token IDs * @param amounts quantities of tokens to burn */ function _burnBatch( address account, uint256[] memory ids, uint256[] memory amounts ) internal virtual { require(account != address(0), 'ERC1155: burn from the zero address'); require( ids.length == amounts.length, 'ERC1155: ids and amounts length mismatch' ); _beforeTokenTransfer(msg.sender, account, address(0), ids, amounts, ''); mapping(uint256 => mapping(address => uint256)) storage balances = ERC1155BaseStorage.layout().balances; unchecked { for (uint256 i; i < ids.length; i++) { uint256 id = ids[i]; require( balances[id][account] >= amounts[i], 'ERC1155: burn amount exceeds balance' ); balances[id][account] -= amounts[i]; } } emit TransferBatch(msg.sender, account, address(0), ids, amounts); } /** * @notice transfer tokens between given addresses * @dev ERC1155Receiver implementation is not checked * @param operator executor of transfer * @param sender sender of tokens * @param recipient receiver of tokens * @param id token ID * @param amount quantity of tokens to transfer * @param data data payload */ function _transfer( address operator, address sender, address recipient, uint256 id, uint256 amount, bytes memory data ) internal virtual { require( recipient != address(0), 'ERC1155: transfer to the zero address' ); _beforeTokenTransfer( operator, sender, recipient, _asSingletonArray(id), _asSingletonArray(amount), data ); mapping(uint256 => mapping(address => uint256)) storage balances = ERC1155BaseStorage.layout().balances; unchecked { uint256 senderBalance = balances[id][sender]; require( senderBalance >= amount, 'ERC1155: insufficient balances for transfer' ); balances[id][sender] = senderBalance - amount; } balances[id][recipient] += amount; emit TransferSingle(operator, sender, recipient, id, amount); } /** * @notice transfer tokens between given addresses * @param operator executor of transfer * @param sender sender of tokens * @param recipient receiver of tokens * @param id token ID * @param amount quantity of tokens to transfer * @param data data payload */ function _safeTransfer( address operator, address sender, address recipient, uint256 id, uint256 amount, bytes memory data ) internal virtual { _transfer(operator, sender, recipient, id, amount, data); _doSafeTransferAcceptanceCheck( operator, sender, recipient, id, amount, data ); } /** * @notice transfer batch of tokens between given addresses * @dev ERC1155Receiver implementation is not checked * @param operator executor of transfer * @param sender sender of tokens * @param recipient receiver of tokens * @param ids token IDs * @param amounts quantities of tokens to transfer * @param data data payload */ function _transferBatch( address operator, address sender, address recipient, uint256[] memory ids, uint256[] memory amounts, bytes memory data ) internal virtual { require( recipient != address(0), 'ERC1155: transfer to the zero address' ); require( ids.length == amounts.length, 'ERC1155: ids and amounts length mismatch' ); _beforeTokenTransfer(operator, sender, recipient, ids, amounts, data); mapping(uint256 => mapping(address => uint256)) storage balances = ERC1155BaseStorage.layout().balances; for (uint256 i; i < ids.length; ) { uint256 token = ids[i]; uint256 amount = amounts[i]; unchecked { uint256 senderBalance = balances[token][sender]; require( senderBalance >= amount, 'ERC1155: insufficient balances for transfer' ); balances[token][sender] = senderBalance - amount; i++; } // balance increase cannot be unchecked because ERC1155Base neither tracks nor validates a totalSupply balances[token][recipient] += amount; } emit TransferBatch(operator, sender, recipient, ids, amounts); } /** * @notice transfer batch of tokens between given addresses * @param operator executor of transfer * @param sender sender of tokens * @param recipient receiver of tokens * @param ids token IDs * @param amounts quantities of tokens to transfer * @param data data payload */ function _safeTransferBatch( address operator, address sender, address recipient, uint256[] memory ids, uint256[] memory amounts, bytes memory data ) internal virtual { _transferBatch(operator, sender, recipient, ids, amounts, data); _doSafeBatchTransferAcceptanceCheck( operator, sender, recipient, ids, amounts, data ); } /** * @notice wrap given element in array of length 1 * @param element element to wrap * @return singleton array */ function _asSingletonArray(uint256 element) private pure returns (uint256[] memory) { uint256[] memory array = new uint256[](1); array[0] = element; return array; } /** * @notice revert if applicable transfer recipient is not valid ERC1155Receiver * @param operator executor of transfer * @param from sender of tokens * @param to receiver of tokens * @param id token ID * @param amount quantity of tokens to transfer * @param data data payload */ function _doSafeTransferAcceptanceCheck( address operator, address from, address to, uint256 id, uint256 amount, bytes memory data ) private { if (to.isContract()) { try IERC1155Receiver(to).onERC1155Received( operator, from, id, amount, data ) returns (bytes4 response) { require( response == IERC1155Receiver.onERC1155Received.selector, 'ERC1155: ERC1155Receiver rejected tokens' ); } catch Error(string memory reason) { revert(reason); } catch { revert('ERC1155: transfer to non ERC1155Receiver implementer'); } } } /** * @notice revert if applicable transfer recipient is not valid ERC1155Receiver * @param operator executor of transfer * @param from sender of tokens * @param to receiver of tokens * @param ids token IDs * @param amounts quantities of tokens to transfer * @param data data payload */ function _doSafeBatchTransferAcceptanceCheck( address operator, address from, address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data ) private { if (to.isContract()) { try IERC1155Receiver(to).onERC1155BatchReceived( operator, from, ids, amounts, data ) returns (bytes4 response) { require( response == IERC1155Receiver.onERC1155BatchReceived.selector, 'ERC1155: ERC1155Receiver rejected tokens' ); } catch Error(string memory reason) { revert(reason); } catch { revert('ERC1155: transfer to non ERC1155Receiver implementer'); } } } /** * @notice ERC1155 hook, called before all transfers including mint and burn * @dev function should be overridden and new implementation must call super * @dev called for both single and batch transfers * @param operator executor of transfer * @param from sender of tokens * @param to receiver of tokens * @param ids token IDs * @param amounts quantities of tokens to transfer * @param data data payload */ function _beforeTokenTransfer( address operator, address from, address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data ) internal virtual {} }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import { IERC165 } from '../../introspection/IERC165.sol'; /** * @title Partial ERC1155 interface needed by internal functions */ interface IERC1155Internal { event TransferSingle( address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value ); event TransferBatch( address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] values ); event ApprovalForAll( address indexed account, address indexed operator, bool approved ); }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; /** * @title ERC165 interface registration interface * @dev see https://eips.ethereum.org/EIPS/eip-165 */ interface IERC165 { /** * @notice query whether contract has registered support for given interface * @param interfaceId interface id * @return bool whether interface is supported */ function supportsInterface(bytes4 interfaceId) external view returns (bool); }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; library ERC1155BaseStorage { struct Layout { mapping(uint256 => mapping(address => uint256)) balances; mapping(address => mapping(address => bool)) operatorApprovals; } bytes32 internal constant STORAGE_SLOT = keccak256('solidstate.contracts.storage.ERC1155Base'); function layout() internal pure returns (Layout storage l) { bytes32 slot = STORAGE_SLOT; assembly { l.slot := slot } } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; /** * @title ERC20 metadata interface */ interface IERC20Metadata { /** * @notice return token name * @return token name */ function name() external view returns (string memory); /** * @notice return token symbol * @return token symbol */ function symbol() external view returns (string memory); /** * @notice return token decimals, generally used only for display purposes * @return token decimals */ function decimals() external view returns (uint8); }
// SPDX-License-Identifier: BUSL-1.1 // For further clarification please see https://license.premia.legal pragma solidity ^0.8.0; library PremiaMiningStorage { bytes32 internal constant STORAGE_SLOT = keccak256("premia.contracts.storage.PremiaMining"); // Info of each pool. struct PoolInfo { uint256 allocPoint; // How many allocation points assigned to this pool. PREMIA to distribute per block. uint256 lastRewardTimestamp; // Last timestamp that PREMIA distribution occurs uint256 accPremiaPerShare; // Accumulated PREMIA per share, times 1e12. See below. } // Info of each user. struct UserInfo { uint256 reward; // Total allocated unclaimed reward uint256 rewardDebt; // Reward debt. See explanation below. // // We do some fancy math here. Basically, any point in time, the amount of PREMIA // entitled to a user but is pending to be distributed is: // // pending reward = (user.amount * pool.accPremiaPerShare) - user.rewardDebt // // Whenever a user deposits or withdraws LP tokens to a pool. Here's what happens: // 1. The pool's `accPremiaPerShare` (and `lastRewardBlock`) gets updated. // 2. User receives the pending reward sent to his/her address. // 3. User's `amount` gets updated. // 4. User's `rewardDebt` gets updated. } struct Layout { // Total PREMIA left to distribute uint256 premiaAvailable; // Amount of premia distributed per year uint256 premiaPerYear; // pool -> isCallPool -> PoolInfo mapping(address => mapping(bool => PoolInfo)) poolInfo; // pool -> isCallPool -> user -> UserInfo mapping(address => mapping(bool => mapping(address => UserInfo))) userInfo; // Total allocation points. Must be the sum of all allocation points in all pools. uint256 totalAllocPoint; } function layout() internal pure returns (Layout storage l) { bytes32 slot = STORAGE_SLOT; assembly { l.slot := slot } } }
// SPDX-License-Identifier: BUSL-1.1 pragma solidity ^0.8.0; import {EnumerableSet} from "@solidstate/contracts/utils/EnumerableSet.sol"; library VolatilitySurfaceOracleStorage { bytes32 internal constant STORAGE_SLOT = keccak256("premia.contracts.storage.VolatilitySurfaceOracle"); uint256 internal constant PARAM_BITS = 51; uint256 internal constant PARAM_BITS_MINUS_ONE = 50; uint256 internal constant PARAM_AMOUNT = 5; // START_BIT = PARAM_BITS * (PARAM_AMOUNT - 1) uint256 internal constant START_BIT = 204; struct Update { uint256 updatedAt; bytes32 params; } struct Layout { // Base token -> Underlying token -> Update mapping(address => mapping(address => Update)) parameters; // Relayer addresses which can be trusted to provide accurate option trades EnumerableSet.AddressSet whitelistedRelayers; } function layout() internal pure returns (Layout storage l) { bytes32 slot = STORAGE_SLOT; assembly { l.slot := slot } } function getParams( Layout storage l, address base, address underlying ) internal view returns (bytes32) { return l.parameters[base][underlying].params; } function parseParams(bytes32 input) internal pure returns (int256[] memory params) { params = new int256[](PARAM_AMOUNT); // Value to add to negative numbers to cast them to int256 int256 toAdd = (int256(-1) >> PARAM_BITS) << PARAM_BITS; assembly { let i := 0 // Value equal to -1 let mid := shl(PARAM_BITS_MINUS_ONE, 1) for { } lt(i, PARAM_AMOUNT) { } { let offset := sub(START_BIT, mul(PARAM_BITS, i)) let param := shr( offset, sub( input, shl( add(offset, PARAM_BITS), shr(add(offset, PARAM_BITS), input) ) ) ) // Check if value is a negative number and needs casting if or(eq(param, mid), gt(param, mid)) { param := add(param, toAdd) } // Store result in the params array mstore(add(params, add(0x20, mul(0x20, i))), param) i := add(i, 1) } } } function formatParams(int256[5] memory params) internal pure returns (bytes32 result) { int256 max = int256(1 << PARAM_BITS_MINUS_ONE); unchecked { for (uint256 i = 0; i < PARAM_AMOUNT; i++) { require(params[i] < max && params[i] > -max, "Out of bounds"); } } assembly { let i := 0 for { } lt(i, PARAM_AMOUNT) { } { let offset := sub(START_BIT, mul(PARAM_BITS, i)) let param := mload(add(params, mul(0x20, i))) result := add( result, shl( offset, sub(param, shl(PARAM_BITS, shr(PARAM_BITS, param))) ) ) i := add(i, 1) } } } }
// SPDX-License-Identifier: BUSL-1.1 // For further clarification please see https://license.premia.legal pragma solidity ^0.8.0; library FeeDiscountStorage { bytes32 internal constant STORAGE_SLOT = keccak256("premia.contracts.staking.PremiaFeeDiscount"); struct UserInfo { uint256 balance; // Balance staked by user uint64 stakePeriod; // Stake period selected by user uint64 lockedUntil; // Timestamp at which the lock ends } struct Layout { // User data with xPREMIA balance staked and date at which lock ends mapping(address => UserInfo) userInfo; } function layout() internal pure returns (Layout storage l) { bytes32 slot = STORAGE_SLOT; assembly { l.slot := slot } } }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; interface AggregatorInterface { function latestAnswer() external view returns (int256); function latestTimestamp() external view returns (uint256); function latestRound() external view returns (uint256); function getAnswer(uint256 roundId) external view returns (int256); function getTimestamp(uint256 roundId) external view returns (uint256); event AnswerUpdated(int256 indexed current, uint256 indexed roundId, uint256 updatedAt); event NewRound(uint256 indexed roundId, address indexed startedBy, uint256 startedAt); }
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; interface AggregatorV3Interface { function decimals() external view returns (uint8); function description() external view returns (string memory); function version() external view returns (uint256); // getRoundData and latestRoundData should both raise "No data present" // if they do not have data to report, instead of returning unset values // which could be misinterpreted as actual reported values. function getRoundData(uint80 _roundId) external view returns ( uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound ); function latestRoundData() external view returns ( uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound ); }
{ "optimizer": { "enabled": true, "runs": 200 }, "outputSelection": { "*": { "*": [ "evm.bytecode", "evm.deployedBytecode", "devdoc", "userdoc", "metadata", "abi" ] } }, "libraries": { "contracts/libraries/OptionMath.sol": { "OptionMath": "0xc7a7275bc25a7bf07c6d0c2f8784c5450cb9b8f5" } } }
[{"inputs":[{"internalType":"address","name":"ivolOracle","type":"address"},{"internalType":"address","name":"wrappedNativeToken","type":"address"},{"internalType":"address","name":"premiaMining","type":"address"},{"internalType":"address","name":"feeReceiver","type":"address"},{"internalType":"address","name":"feeDiscountAddress","type":"address"},{"internalType":"int128","name":"feePremium64x64","type":"int128"},{"internalType":"address","name":"exchangeHelper","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"underwriter","type":"address"},{"indexed":false,"internalType":"uint256","name":"shortTokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"APYFeePaid","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"underwriter","type":"address"},{"indexed":false,"internalType":"uint256","name":"shortTokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"APYFeeReserved","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"shortTokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Annihilate","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"operator","type":"address"},{"indexed":false,"internalType":"bool","name":"approved","type":"bool"}],"name":"ApprovalForAll","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"underwriter","type":"address"},{"indexed":false,"internalType":"uint256","name":"shortTokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"freedAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"intervalContractSize","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"fee","type":"uint256"}],"name":"AssignExercise","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"underwriter","type":"address"},{"indexed":false,"internalType":"uint256","name":"shortTokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"freedAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"intervalContractSize","type":"uint256"}],"name":"AssignSale","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"bool","name":"isCallPool","type":"bool"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"longTokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"contractSize","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"exerciseValue","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"fee","type":"uint256"}],"name":"Exercise","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bool","name":"isCallPool","type":"bool"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"FeeWithdrawal","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"longTokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"contractSize","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"baseCost","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"feeCost","type":"uint256"},{"indexed":false,"internalType":"int128","name":"spot64x64","type":"int128"}],"name":"Purchase","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"longTokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"contractSize","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"baseCost","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"feeCost","type":"uint256"},{"indexed":false,"internalType":"int128","name":"spot64x64","type":"int128"}],"name":"Sell","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"operator","type":"address"},{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256[]","name":"ids","type":"uint256[]"},{"indexed":false,"internalType":"uint256[]","name":"values","type":"uint256[]"}],"name":"TransferBatch","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"operator","type":"address"},{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"id","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"TransferSingle","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"underwriter","type":"address"},{"indexed":true,"internalType":"address","name":"longReceiver","type":"address"},{"indexed":false,"internalType":"uint256","name":"shortTokenId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"intervalContractSize","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"intervalPremium","type":"uint256"},{"indexed":false,"internalType":"bool","name":"isManualUnderwrite","type":"bool"}],"name":"Underwrite","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bool","name":"isCall","type":"bool"},{"indexed":false,"internalType":"int128","name":"cLevel64x64","type":"int128"},{"indexed":false,"internalType":"int128","name":"oldLiquidity64x64","type":"int128"},{"indexed":false,"internalType":"int128","name":"newLiquidity64x64","type":"int128"}],"name":"UpdateCLevel","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"int128","name":"spotOffset64x64","type":"int128"}],"name":"UpdateSpotOffset","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"int128","name":"steepness64x64","type":"int128"},{"indexed":false,"internalType":"bool","name":"isCallPool","type":"bool"}],"name":"UpdateSteepness","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"bool","name":"isCallPool","type":"bool"},{"indexed":false,"internalType":"uint256","name":"depositedAt","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Withdrawal","type":"event"},{"inputs":[{"internalType":"uint64","name":"maturity","type":"uint64"},{"internalType":"int128","name":"strike64x64","type":"int128"},{"internalType":"bool","name":"isCall","type":"bool"}],"name":"getAvailableBuybackLiquidity","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"bool","name":"isCallPool","type":"bool"}],"name":"isBuybackEnabled","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint64","name":"maturity","type":"uint64"},{"internalType":"int128","name":"strike64x64","type":"int128"},{"internalType":"bool","name":"isCall","type":"bool"},{"internalType":"uint256","name":"contractSize","type":"uint256"}],"name":"sell","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"feePayer","type":"address"},{"internalType":"uint64","name":"maturity","type":"uint64"},{"internalType":"int128","name":"strike64x64","type":"int128"},{"internalType":"int128","name":"spot64x64","type":"int128"},{"internalType":"uint256","name":"contractSize","type":"uint256"},{"internalType":"bool","name":"isCall","type":"bool"}],"name":"sellQuote","outputs":[{"internalType":"int128","name":"baseCost64x64","type":"int128"},{"internalType":"int128","name":"feeCost64x64","type":"int128"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bool","name":"state","type":"bool"},{"internalType":"bool","name":"isCallPool","type":"bool"}],"name":"setBuybackEnabled","outputs":[],"stateMutability":"nonpayable","type":"function"}]
Contract Creation Code
6101e06040523480156200001257600080fd5b50604051620048f8380380620048f8833981016040819052620000359162000174565b6001600160a01b038781166101005286811660805285811660a05284811660c05283811660e052600f83900b61014052811661012052868686868686866200008c600080806200010b602090811b6200039517901c565b6101608181525050620000ae60016000806200010b60201b620003951760201c565b6101808181525050620000d060026000806200010b60201b620003951760201c565b6101a08181525050620000f260036000806200010b60201b620003951760201c565b6101c052506200024d9c50505050505050505050505050565b600081600f0b6080846001600160401b0316901b60f886600781111562000136576200013662000210565b6200014392911b62000226565b6200014f919062000226565b949350505050565b80516001600160a01b03811681146200016f57600080fd5b919050565b600080600080600080600060e0888a0312156200019057600080fd5b6200019b8862000157565b9650620001ab6020890162000157565b9550620001bb6040890162000157565b9450620001cb6060890162000157565b9350620001db6080890162000157565b925060a088015180600f0b8114620001f257600080fd5b91506200020260c0890162000157565b905092959891949750929550565b634e487b7160e01b600052602160045260246000fd5b600082198211156200024857634e487b7160e01b600052601160045260246000fd5b500190565b60805160a05160c05160e05161010051610120516101405161016051610180516101a0516101c0516145d662000322600039600081816118c70152611cf301526000818161189d015281816119400152611d1901526000818161187301528181611a040152612def01526000818161184a01528181611917015281816119db01528181611a2d0152612e150152600061074d01526000505060006104e901526000818161106501526110b501526000818161030c01526124b601526000818161260d01526126e10152600050506145d66000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c80635a1c4b971461005c57806380beb6f41461008e57806382b3e955146100e957806385d54ce31461010a578063ac3fa4f81461012d575b600080fd5b61006f61006a36600461406b565b610140565b60408051600f93840b81529190920b6020820152015b60405180910390f35b6100e761009c3660046140e0565b3360009081527fbbd6af8edd89d04327b00c29df7f272b9b1ae01bf6d9c54a784f935706df53056020908152604080832084151584529091529020805460ff19168315151790555050565b005b6100fc6100f7366004614119565b6101a2565b604051908152602001610085565b61011d610118366004614162565b6101d0565b6040519015158152602001610085565b6100e761013b36600461417e565b61021f565b6000806101936040518060c001604052808a6001600160a01b031681526020018967ffffffffffffffff16815260200188600f0b815260200187600f0b81526020018681526020018515158152506103db565b91509150965096945050505050565b6000806101ba6101b38460006107b2565b8686610395565b90506101c5816107e5565b9150505b9392505050565b6001600160a01b03821660009081527fbbd6af8edd89d04327b00c29df7f272b9b1ae01bf6d9c54a784f935706df530560209081526040808320841515845290915290205460ff165b92915050565b6000805160206145818339815191526000808061023b846108cb565b90506000806102906040518060c00160405280336001600160a01b031681526020018c67ffffffffffffffff1681526020018b600f0b815260200185600f0b81526020018981526020018a15158152506103db565b90925090506102a8826102a3888b61092b565b610959565b94506102b8816102a3888b61092b565b935050506102cc84898988878b6001610974565b60006102e36102dc8860016107b2565b8a8a610395565b90506102f0338288610b47565b610307336102fe85876141e3565b89600180610cda565b6103367f0000000000000000000000000000000000000000000000000000000000000000848960016000610cda565b604080518281526020810188905290810185905260608101849052600f83900b608082015233907f2aa636510213a4ef3578d8b9fdcadcb1c62c2d77d6a452e3b60b1f59be427add9060a00160405180910390a2505050505050505050565b600081600f0b60808467ffffffffffffffff16901b60f88660078111156103be576103be6141fa565b6103c992911b614210565b6103d39190614210565b949350505050565b60008060008360400151600f0b1380156103fc575060008360600151600f0b135b801561041657506000836020015167ffffffffffffffff16115b6104565760405162461bcd60e51b815260206004820152600c60248201526b696e76616c6964206172677360a01b60448201526064015b60405180910390fd5b60006000805160206145818339815191529050600061049242866020015167ffffffffffffffff1661048891906141e3565b6301e13380610d32565b8254600184015460608801516040808a01519051631af9d50d60e11b81526001600160a01b0394851660048201529284166024840152600f91820b6044840152810b606483015283900b60848201529192506000917f0000000000000000000000000000000000000000000000000000000000000000909116906335f3aa1a9060a401602060405180830381865afa158015610532573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906105569190614228565b9050600081600f0b136105955760405162461bcd60e51b81526020600482015260076024820152660766f6c203d20360cc1b604482015260640161044d565b60006105bd6105a8600f84900b84610d69565b88604001518960600151868b60a00151610da0565b905060006106136105e1868a608001518b606001518c604001518d60a00151610e81565b8960a001516105fe576003870154600160a81b900460ff16610ef1565b6003870154600160a01b900460ff165b610ef1565b90508760a001511561063c57610639886060015182600f0b610d6990919063ffffffff16565b90505b6000806106606106518b60a0015160016107b2565b8b602001518c60400151610395565b6000818152601b8901602052604081205460a08d0151600f9190910b94509192509061068d908990610f07565b50905082600f0b600014806106a7575082600f0b81600f0b125b156106b0578092505b5050608089015160038701546000916106d291600160a01b900460ff16610ef1565b905061071a836107116106f6826106ed600f8a900b87610d69565b600f0b90610fa3565b61070867b33333333333333387610d69565b600f0b90610d69565b600f0b90610fd6565b98508960a0015115610743576107408a606001518a600f0b61100990919063ffffffff16565b98505b610771600f8a900b7f0000000000000000000000000000000000000000000000000000000000000000610d69565b975061078e6107838b60000151611061565b600f8a900b90610d69565b6107989089614245565b97506107a4888a614245565b985050505050505050915091565b600082156107d057816107c65760056107c9565b60045b9050610219565b816107dc5760076101c9565b60069392505050565b60008181527fb31c2c74f86ca3ce94d901f5f5bbe66f7161eec2f7b5aa0b75a86371436424eb602052604081206000805160206145818339815191529060f884901c83600582600781111561083c5761083c6141fa565b149050600061084a8461112a565b905060005b818110156108c05760006108638683611134565b6001600160a01b0381166000908152601a890160209081526040808320881515845290915290205490915060ff16156108ad576108a0818a611140565b6108aa9089614210565b97505b50806108b881614295565b91505061084f565b505050505050919050565b60004282600c015414156108e35761021982426111f3565b6108ec82611221565b90506108f882426111f3565b600f0b61090a5761090a824283611326565b42600c83015561091b8260016113a4565b6109268260006113a4565b919050565b600081610946576003830154600160a81b900460ff166101c9565b505060030154600160a01b900460ff1690565b60006101c961096983600a614394565b600f85900b9061142f565b600061098b6109848460006107b2565b8888610395565b9050600061099b89878987611497565b905060006109ab8a84848c6114fd565b60008481527fb31c2c74f86ca3ce94d901f5f5bbe66f7161eec2f7b5aa0b75a86371436424eb602052604081209192506109e48261112a565b90505b8815610b39576000610a046109fb836143a3565b92508383611134565b9050868015610a3b57506001600160a01b0381166000908152601a8e01602090815260408083208b1515845290915290205460ff16155b15610a4657506109e7565b610a716040518060800160405280600081526020016000815260200160008152602001600081525090565b610a7b8288611140565b8082528b1015610a89578a81525b80518b90610a9790886143ba565b610aa191906143ef565b602082015280518b90610ab4908c6143ba565b610abe91906143ef565b604082015280518b90610ad190876143ba565b610adb91906143ef565b6060820152610aee8e8389848d8d611580565b8051610afa908c6141e3565b9a50806020015186610b0c91906141e3565b955080604001518a610b1e91906141e3565b9950806060015185610b3091906141e3565b945050506109e7565b505050505050505050505050565b6001600160a01b038316610ba95760405162461bcd60e51b815260206004820152602360248201527f455243313135353a206275726e2066726f6d20746865207a65726f206164647260448201526265737360e81b606482015260840161044d565b610bd733846000610bb98661173a565b610bc28661173a565b60405180602001604052806000815250611785565b60008281527f1799cf914cb0cb442ca7c7ac709ee40d0cb89e87351dc08d517fbda27d50c68b602090815260408083206001600160a01b03871684529182905290912054821115610c785760405162461bcd60e51b815260206004820152602560248201527f455243313135353a206275726e20616d6f756e7420657863656564732062616c604482015264616e63657360d81b606482015260840161044d565b6001600160a01b03841660008181526020838152604080832080548790039055805187815291820186905291929133917fc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f6291015b60405180910390a450505050565b8115610d20578015610d0d57610d0885610d0260008051602061458183398151915286611bfb565b86611c24565b610d2b565b610d0885610d1a85611cea565b86611d3e565b610d2b858585611d59565b5050505050565b600081610d3e57600080fd5b6000610d4a8484611e2d565b905060016001607f1b036001600160801b03821611156101c957600080fd5b6000600f83810b9083900b0260401d60016001607f1b03198112801590610d97575060016001607f1b038113155b6101c957600080fd5b600080610db1600f85900b88610d69565b90506000610dc182600f0b611f8f565b90506000610dfe82610df5600186600f0b901d610711610ded8d8d600f0b61100990919063ffffffff16565b600f0b611fb1565b600f0b90611009565b90506000610e10600f83900b84610fa3565b90508515610e4957610e3e610e32610e2783611feb565b600f8c900b90610d69565b6106ed610e2785611feb565b945050505050610e78565b610e6f610e60610e27610e5b84614411565b611feb565b6106ed610e27610e5b86614411565b610e3e90614411565b95945050505050565b60008115610ebe5782600f0b84600f0b1315610eb957610eb685610ead86610df5600f82900b88610fa3565b600f0b9061142f565b90505b610e78565b82600f0b84600f0b1215610e7857610ee785610ede600f86900b87610fa3565b88919085611497565b9695505050505050565b60006101c983610f0284600a614394565b610d32565b80151560009081526013830160205260408120819081610f2786866120a7565b90506000610f3587876120c0565b905060008360010154118015610f4b5750825415155b8015610f58575082544210155b15610f9257610f7c81610711856001015461060e8a8c61092b90919063ffffffff16565b9350610f8b878383878a61212b565b9450610f99565b8194508093505b5050509250929050565b6000600f82810b9084900b0360016001607f1b03198112801590610d97575060016001607f1b038113156101c957600080fd5b6000600f83810b9083900b0160016001607f1b03198112801590610d97575060016001607f1b038113156101c957600080fd5b600081600f0b6000141561101c57600080fd5b600082600f0b604085600f0b901b81611037576110376143d9565b05905060016001607f1b03198112801590610d97575060016001607f1b038113156101c957600080fd5b60007f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031615610926576040516303793c8d60e11b81526001600160a01b038381166004830152610219917f0000000000000000000000000000000000000000000000000000000000000000909116906306f2791a90602401602060405180830381865afa1580156110fe573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906111229190614438565b612710610d32565b6000610219825490565b60006101c98383612222565b60006001600160a01b0383166111ac5760405162461bcd60e51b815260206004820152602b60248201527f455243313135353a2062616c616e636520717565727920666f7220746865207a60448201526a65726f206164647265737360a81b606482015260840161044d565b7f1799cf914cb0cb442ca7c7ac709ee40d0cb89e87351dc08d517fbda27d50c68b6000928352602090815260408084206001600160a01b0395909516845293905250205490565b60006011830181611206610e10856143ef565b8152602081019190915260400160002054600f0b9392505050565b6000808260030160009054906101000a90046001600160a01b03166001600160a01b03166350d25bcd6040518163ffffffff1660e01b8152600401602060405180830381865afa158015611279573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061129d9190614438565b905060008360020160009054906101000a90046001600160a01b03166001600160a01b03166350d25bcd6040518163ffffffff1660e01b8152600401602060405180830381865afa1580156112f6573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061131a9190614438565b90506103d382826122a8565b6000611334610e10846143ef565b6000818152601186016020526040902080546001600160801b0319166001600160801b038516179055905061136d60ff808316906141e3565b6001901b846012016000600884901c815260200190815260200160002060008282546113999190614210565b909155505050505050565b80151560009081526013830160205260409020805415806113c55750805442105b156113cf57505050565b60006113db84846120c0565b9050611411848261140b611400866001015461060e898b61092b90919063ffffffff16565b600f86900b90610fd6565b86612343565b50501515600090815260139091016020526040812081815560010155565b60008161143e57506000610219565b600083600f0b121561144f57600080fd5b600f83900b6001600160801b038316810260401c90608084901c026001600160c01b0381111561147e57600080fd5b60401b811981111561148f57600080fd5b019392505050565b600081156114a65750826103d3565b60006114b6600f85900b8661142f565b905060006114d5828860030160149054906101000a900460ff16610ef1565b90506114f2818860030160159054906101000a900460ff16610959565b979650505050505050565b60008167ffffffffffffffff164210156103d357600061151c856123c0565b1561153a576000858152601d87016020526040902054600f0b611543565b611543866123df565b9050610ee76301e133806115614267ffffffffffffffff87166141e3565b61156b90876143ba565b61157591906143ef565b600f83900b9061142f565b600061159887878787600001518860600151886123fb565b84606001516115a79190614210565b90506115b886868660000151610b47565b60006115c5888886612537565b1590506115f48783876040015188602001516115e191906141e3565b6115eb9190614210565b86846000610cda565b801561160f5761160a8888868860200151612571565b61165d565b84604001518211156116365761160a88888688604001518661163191906141e3565b6126a9565b818560400151111561165d5761165d88888685896040015161165891906141e3565b612571565b82156116c857866001600160a01b03167f4159772091e485ef6ade165ca7a05f8d859ded92728fe2aaa7c2371c75c0cce387876040015188602001516116a391906141e3565b88516040805193845260208401929092529082015260600160405180910390a2611730565b866001600160a01b03167f69a2ef6bf9e7ff92cbf1b71963ba1751b1abe8f99e3b3aae2ab99e416df61493878760400151886020015161170891906141e3565b8851604080519384526020840192909252908201526000606082015260800160405180910390a25b5050505050505050565b6040805160018082528183019092526060916000919060208083019080368337019050509050828160008151811061177457611774614451565b602090810291909101015292915050565b6117938686868686866127e9565b60008051602061458183398151915260005b84518110156117305760008582815181106117c2576117c2614451565b6020026020010151905060008583815181106117e0576117e0614451565b6020026020010151905080600014156117fa575050611be9565b6001600160a01b0389166118175761181560158501836129c3565b505b6001600160a01b0388161580156118345750611832826123c0565b155b156118485761184660158501836129cf565b505b7f000000000000000000000000000000000000000000000000000000000000000082148061189557507f000000000000000000000000000000000000000000000000000000000000000082145b806118bf57507f000000000000000000000000000000000000000000000000000000000000000082145b806118e957507f000000000000000000000000000000000000000000000000000000000000000082145b156119d9576001600160a01b0389161580159061190e57506001600160a01b03881615155b156119d95760007f000000000000000000000000000000000000000000000000000000000000000083148061196257507f000000000000000000000000000000000000000000000000000000000000000083145b6001600160a01b038b166000908152600d8701602090815260408083208415158452909152902054909150429061199c9062015180614210565b106119d75760405162461bcd60e51b815260206004820152600b60248201526a1b1a5c481b1bd8dac80c5960aa1b604482015260640161044d565b505b7f0000000000000000000000000000000000000000000000000000000000000000821480611a2657507f000000000000000000000000000000000000000000000000000000000000000082145b15611b9d577f000000000000000000000000000000000000000000000000000000000000000082146000611a5a86836129db565b90506001600160a01b038b1615611b52576000611a778c86611140565b90508181118015611a915750611a8d8285614210565b8111155b15611b29576001600160a01b038c166000908152601488016020908152604080832086151580855260138c0184528285205485529083528184209084529091529020548490611ae090836141e3565b1015611b1e5760405162461bcd60e51b815260206004820152600d60248201526c496e7375662062616c616e636560981b604482015260640161044d565b611b29878d856129f5565b6001600160a01b038b1615611b5057611b44878d8587612571565b611b50878c85876126a9565b505b6001600160a01b038a1615611b9a576000611b6d8b86611140565b9050818111158015611b88575081611b858583614210565b10155b15611b9857611b98878c85612aa6565b505b50505b60f882901c6005816007811115611bb657611bb66141fa565b1480611bd357506007816007811115611bd157611bd16141fa565b145b15611be557611be5858b8b8686612b4e565b5050505b80611bf381614295565b9150506117a5565b600081611c125782546001600160a01b03166101c9565b5050600101546001600160a01b031690565b80611c2e57505050565b60405163a9059cbb60e01b81526001600160a01b0384811660048301526024820183905283169063a9059cbb906044016020604051808303816000875af1158015611c7d573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611ca19190614467565b611ce55760405162461bcd60e51b8152602060048201526015602482015274115490cc8c081d1c985b9cd9995c8819985a5b1959605a1b604482015260640161044d565b505050565b600081611d17577f0000000000000000000000000000000000000000000000000000000000000000610219565b7f000000000000000000000000000000000000000000000000000000000000000092915050565b611ce583838360405180602001604052806000815250612ccb565b6000805160206145818339815191526000611d7383612de6565b90506000611d80826123c0565b1115611e2257600061010480611d9681426143ef565b611da091906143ba565b611daa9190614210565b6001600160a01b0387166000908152601485016020908152604080832084845282528083208815158452909152812080549293508792909190611dee908490614210565b90915550508315156000908152601384016020526040812060018101805491928892611e1b908490614210565b9091555050555b610d2b858286611d3e565b600081611e3957600080fd5b60006001600160c01b038411611e645782604085901b81611e5c57611e5c6143d9565b049050611f7b565b60c084811c6401000000008110611e7d576020918201911c5b620100008110611e8f576010918201911c5b6101008110611ea0576008918201911c5b60108110611eb0576004918201911c5b60048110611ec0576002918201911c5b60028110611ecf576001820191505b60bf820360018603901c6001018260ff0387901b81611ef057611ef06143d9565b0492506001600160801b03831115611f0757600080fd5b608085901c83026001600160801b038616840260c088901c604089901b82811015611f33576001820391505b608084901b92900382811015611f4a576001820391505b829003608084901c8214611f6057611f60614484565b888181611f6f57611f6f6143d9565b04870196505050505050505b6001600160801b038111156101c957600080fd5b60008082600f0b1215611fa157600080fd5b610219604083600f0b901b612e3a565b60008082600f0b13611fc257600080fd5b6080611fcd83612fa8565b600f0b6fb17217f7d1cf79abc9e3b39803f2f6af02901c9050919050565b600080611ffc600f84900b84610d69565b9050600061208661206b61203a61202b612023600f87900b68030000000000000000610fd6565b600f0b611f8f565b67d3c84b78b749bd6b90610d69565b61071161205c61204c89600f0b613083565b68019abac0ea1da6503690610d69565b679109f285df45239490610fd6565b610df5600161207986614411565b600f0b901d600f0b6130b6565b9050600084600f0b1361209957806103d3565b6103d3600160401b82610fa3565b60006120b3838361310b565b90506101c9838284613132565b6000806120dd836120d25760016120d5565b60005b600080610395565b83151560009081526013860160205260409020600101549091506103d3906000838152600080516020614561833981519152602052604090205461212191906141e3565b61060e868661092b565b60008082612140576019870154600f0b612150565b6019870154600160801b9004600f0b5b905080600f0b6000141561216857506008860154600f0b5b60405163e101a89b60e01b8152600f87810b600483015286810b602483015285810b604483015282900b606482015273c7a7275bc25a7bf07c6d0c2f8784c5450cb9b8f59063e101a89b90608401602060405180830381865af41580156121d3573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906121f79190614228565b915067b33333333333333382600f0b12156122185767b33333333333333391505b5095945050505050565b815460009082106122805760405162461bcd60e51b815260206004820152602260248201527f456e756d657261626c655365743a20696e646578206f7574206f6620626f756e604482015261647360f01b606482015260840161044d565b82600001828154811061229557612295614451565b9060005260206000200154905092915050565b6000816122b457600080fd5b6000808412156122c957836000039350600190505b60008312156122db5760009290920391155b60006122e78585611e2d565b90508115612315576001607f1b816001600160801b0316111561230957600080fd5b60000391506102199050565b60016001607f1b03816001600160801b0316111561233257600080fd5b91506102199050565b505092915050565b600061234f85836120a7565b90506000612360868387878761212b565b905061236d8682856132f8565b60408051600f83810b825287810b602083015286900b818301529051841515917f4e23621c6f591f14bf9505cb8326b45af9dc6c5569fd608de2a7a2ddd6146b2e919081900360600190a2505050505050565b6000908152600080516020614561833981519152602052604090205490565b601e810154600f0b806109265750670666666666666666919050565b6001600160a01b0385166000908152601c8701602090815260408083208784529091528120548161242c8888611140565b61243687846143ba565b61244091906143ef565b6001600160a01b0389166000908152601c8b01602090815260408083208b845290915281208054929350839290919061247a9084906141e3565b90915550612496905061248d86836141e3565b610ead8a611061565b92506000836124a587846141e3565b6124af91906141e3565b90506124e07f0000000000000000000000000000000000000000000000000000000000000000828760016000610cda565b604080516001600160a01b038b168152602081018a90529081018290527fe02f89a472c9f9be95b6093634de6f0967e703e84390d7e00d23e15ea6b997659060600160405180910390a15050509695505050505050565b6001600160a01b0382166000908152600e84016020908152604080832084151584529091528120548015806101c557504210949350505050565b6001600160a01b03831660009081526017850160209081526040808320851515845282528083205460188801909252822054909180848410156125b2578394505b6125bc85856141e3565b91506125c885846141e3565b60405163edaf7d5b60e01b81526001600160a01b0389811660048301523060248301528815156044830152606482018790526084820185905260a482018690529192507f00000000000000000000000000000000000000000000000000000000000000009091169063edaf7d5b9060c401600060405180830381600087803b15801561265357600080fd5b505af1158015612667573d6000803e3d6000fd5b505050506001600160a01b0396909616600090815260178801602090815260408083209715158352968152868220929092556018909701905250505090912055565b6001600160a01b03808416600090815260178601602090815260408083208615158452825280832054601889019092529091205490917f00000000000000000000000000000000000000000000000000000000000000001663edaf7d5b863087866127148982614210565b6040516001600160e01b031960e088901b1681526001600160a01b03958616600482015294909316602485015290151560448401526064830152608482015260a4810184905260c401600060405180830381600087803b15801561277757600080fd5b505af115801561278b573d6000803e3d6000fd5b50505050828261279b9190614210565b6001600160a01b0386166000908152601788016020908152604080832088151584529091529020556127cd8382614210565b9315156000908152601890960160205250506040909320555050565b836001600160a01b0316856001600160a01b0316146129bb576001600160a01b0385811660009081527fb31c2c74f86ca3ce94d901f5f5bbe66f7161eec2f7b5aa0b75a86371436424ec602052604080822092871682528120600080516020614561833981519152927fb31c2c74f86ca3ce94d901f5f5bbe66f7161eec2f7b5aa0b75a86371436424eb929091905b87518110156129b557600087828151811061289557612895614451565b6020026020010151905060008111156129ac5760008983815181106128bc576128bc614451565b6020026020010151905060006001600160a01b03168c6001600160a01b0316141561290a57600081815260208890526040812080548492906128ff908490614210565b909155506129409050565b816129158d83611140565b1415612940576000818152602087905260409020612933908d61334f565b5061293e85826129cf565b505b6001600160a01b038b16612977576000818152602088905260408120805484929061296c9084906141e3565b909155506129aa9050565b6129818b82611140565b6129aa57600081815260208790526040902061299d908c613364565b506129a884826129c3565b505b505b50600101612878565b50505050505b505050505050565b60006101c98383613375565b60006101c983836133c4565b6000816129ec5782600401546101c9565b50506005015490565b6001600160a01b038216612a0857600080fd5b8015156000908152600f84016020908152604080832060108701909252909120612a33848383613491565b612a3e575050505050565b6001600160a01b0393841660008181526020838152604080832080549683528184208054978a16808652838620805499909b166001600160a01b0319998a168117909b5599855295909252822080548616909717909655528054821690558254169091555050565b6001600160a01b038216612ab957600080fd5b8015156000908152600f84016020908152604080832060108701909252909120612ae4848383613491565b15612af0575050505050565b60008080526020828152604080832080546001600160a01b0390811680865296845282852080546001600160a01b03199081169a909216998a1790558885529490925282208054841690941790935580528154169092179091555050565b6001600160a01b038416158015612b6c575080612b6a836123c0565b145b15612ba957612b7a856123df565b6000838152601d87016020526040902080546001600160801b0319166001600160801b03929092169190911790555b6001600160a01b038316158015612bc65750612bc4826123c0565b155b15612bea576000828152601d86016020526040902080546001600160801b03191690555b6001600160a01b03841615801590612c0a57506001600160a01b03831615155b15610d2b5760f882901c608083901c8360006005846007811115612c3057612c306141fa565b1490506000612c418a878585611497565b90506000612c518b8984886114fd565b90506000612c638c8c8b8b86896123fb565b9050612c718c8b8b856134dd565b6000612c7e8d8d87612537565b1590508115612c9557612c958c8387846000610cda565b612cb08d8d8784612caa5761165886896141e3565b87612571565b612cbc8d8c87876126a9565b50505050505050505050505050565b6001600160a01b038416612d2b5760405162461bcd60e51b815260206004820152602160248201527f455243313135353a206d696e7420746f20746865207a65726f206164647265736044820152607360f81b606482015260840161044d565b612d4a33600086612d3b8761173a565b612d448761173a565b86611785565b60008381527f1799cf914cb0cb442ca7c7ac709ee40d0cb89e87351dc08d517fbda27d50c68b602090815260408083206001600160a01b038816845290915281208054849290612d9b908490614210565b909155505060408051848152602081018490526001600160a01b0386169160009133917fc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f629101610ccc565b600081612e13577f0000000000000000000000000000000000000000000000000000000000000000610219565b7f000000000000000000000000000000000000000000000000000000000000000092915050565b600081612e4957506000919050565b816001600160801b8210612e625760809190911c9060401b5b600160401b8210612e785760409190911c9060201b5b6401000000008210612e8f5760209190911c9060101b5b620100008210612ea45760109190911c9060081b5b6101008210612eb85760089190911c9060041b5b60108210612ecb5760049190911c9060021b5b60088210612ed75760011b5b6001818581612ee857612ee86143d9565b048201901c90506001818581612f0057612f006143d9565b048201901c90506001818581612f1857612f186143d9565b048201901c90506001818581612f3057612f306143d9565b048201901c90506001818581612f4857612f486143d9565b048201901c90506001818581612f6057612f606143d9565b048201901c90506001818581612f7857612f786143d9565b048201901c90506000818581612f9057612f906143d9565b049050808210612fa05780610e78565b509392505050565b60008082600f0b13612fb957600080fd5b6000600f83900b600160401b8112612fd3576040918201911d5b6401000000008112612fe7576020918201911d5b620100008112612ff9576010918201911d5b610100811261300a576008918201911d5b6010811261301a576004918201911d5b6004811261302a576002918201911d5b60028112613039576001820191505b603f19820160401b600f85900b607f8490031b6001603f1b5b60008113156130785790800260ff81901c8281029390930192607f011c9060011d613052565b509095945050505050565b6000600f82900b60016001607f1b0319141561309e57600080fd5b600082600f0b126130af5781610219565b5060000390565b6000600160461b82600f0b126130cb57600080fd5b683fffffffffffffffff1982600f0b12156130e857506000919050565b610219608083600f0b700171547652b82fe1777d0ffda0d23a7d1202901d613567565b600081613126576008830154600160801b9004600f0b6101c9565b505060090154600f0b90565b600080826131445784600a015461314a565b84600b01545b61315490426141e3565b905061a8c08111156131735761316c61a8c0826141e3565b905061317c565b839150506101c9565b600061318a82613840610d32565b9050600061319d856120d25760016120d5565b851515600090815260188901602090815260408083205460138c0190925282206001015492935091600084815260008051602061456183398151915260205260409020546131eb91906141e3565b9050808210156131f85750805b600061320d61320783856141e3565b84610d32565b6040805161012081018252600f88810b82528c810b602083015283900b8183015267b333333333333333606082015267e66666666666666660808201819052600160401b60a0830181905260c083015260e082015268056fc2a2c515da32ea6101008201529051634916d70d60e01b815291925073c7a7275bc25a7bf07c6d0c2f8784c5450cb9b8f591634916d70d916132a99160040161449a565b602060405180830381865af41580156132c6573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906132ea9190614228565b9a9950505050505050505050565b8015613328576009830180546001600160801b0384166001600160801b031990911617905542600b840155505050565b6008830180546001600160801b03808516600160801b02911617905542600a840155505050565b60006101c9836001600160a01b0384166133c4565b60006101c9836001600160a01b0384165b60008181526001830160205260408120546133bc57508154600181810184556000848152602080822090930184905584548482528286019093526040902091909155610219565b506000610219565b60008181526001830160205260408120548015613487578354600090859060001981019081106133f6576133f6614451565b906000526020600020015490508085600001600184038154811061341c5761341c614451565b600091825260208083209091019290925591825260018601905260409020819055835484908061344e5761344e61454a565b60019003818190600052602060002001600090559055836001016000848152602001908152602001600020600090556001915050610219565b6000915050610219565b6001600160a01b038381166000908152602084905260408120549091161515806103d357506000808052602083905260409020546001600160a01b039081169085161490509392505050565b6001600160a01b0383166000908152601c85016020908152604080832085845290915281208054839290613512908490614210565b9091555050604080516001600160a01b0385168152602081018490529081018290527f3a8ab48b47eb6f5307d9b87d560cc92cf89c416cce1d94297a70d1c82d8c84129060600160405180910390a150505050565b6000600160461b82600f0b1261357c57600080fd5b683fffffffffffffffff1982600f0b121561359957506000919050565b6001607f1b60006001603f1b8416600f0b13156135c75770016a09e667f3bcc908b2fb1366ea957d3e0260801c5b60008367400000000000000016600f0b13156135f4577001306fe0a31b7152de8d5a46305c85edec0260801c5b60008367200000000000000016600f0b1315613621577001172b83c7d517adcdf7c8c50eb14a791f0260801c5b60008367100000000000000016600f0b131561364e5770010b5586cf9890f6298b92b71842a983630260801c5b60008367080000000000000016600f0b131561367b577001059b0d31585743ae7c548eb68ca417fd0260801c5b60008367040000000000000016600f0b13156136a857700102c9a3e778060ee6f7caca4f7a29bde80260801c5b60008367020000000000000016600f0b13156136d55770010163da9fb33356d84a66ae336dcdfa3f0260801c5b60008367010000000000000016600f0b131561370257700100b1afa5abcbed6129ab13ec11dc95430260801c5b600083668000000000000016600f0b131561372e5770010058c86da1c09ea1ff19d294cf2f679b0260801c5b600083664000000000000016600f0b131561375a577001002c605e2e8cec506d21bfc89a23a00f0260801c5b600083662000000000000016600f0b131561378657700100162f3904051fa128bca9c55c31e5df0260801c5b600083661000000000000016600f0b13156137b2577001000b175effdc76ba38e31671ca9397250260801c5b600083660800000000000016600f0b13156137de57700100058ba01fb9f96d6cacd4b180917c3d0260801c5b600083660400000000000016600f0b131561380a5770010002c5cc37da9491d0985c348c68e7b30260801c5b600083660200000000000016600f0b1315613836577001000162e525ee054754457d59952920260260801c5b600083660100000000000016600f0b13156138625770010000b17255775c040618bf4a4ade83fc0260801c5b6000836580000000000016600f0b131561388d577001000058b91b5bc9ae2eed81e9b7d4cfab0260801c5b6000836540000000000016600f0b13156138b857700100002c5c89d5ec6ca4d7c8acc017b7c90260801c5b6000836520000000000016600f0b13156138e35770010000162e43f4f831060e02d839a9d16d0260801c5b6000836510000000000016600f0b131561390e57700100000b1721bcfc99d9f890ea069117630260801c5b6000836508000000000016600f0b13156139395770010000058b90cf1e6d97f9ca14dbcc16280260801c5b6000836504000000000016600f0b1315613964577001000002c5c863b73f016468f6bac5ca2b0260801c5b6000836502000000000016600f0b131561398f57700100000162e430e5a18f6119e3c02282a50260801c5b6000836501000000000016600f0b13156139ba577001000000b1721835514b86e6d96efd1bfe0260801c5b60008364800000000016600f0b13156139e457700100000058b90c0b48c6be5df846c5b2ef0260801c5b60008364400000000016600f0b1315613a0e5770010000002c5c8601cc6b9e94213c72737a0260801c5b60008364200000000016600f0b1315613a38577001000000162e42fff037df38aa2b219f060260801c5b60008364100000000016600f0b1315613a625770010000000b17217fba9c739aa5819f44f90260801c5b60008364080000000016600f0b1315613a8c577001000000058b90bfcdee5acd3c1cedc8230260801c5b60008364040000000016600f0b1315613ab657700100000002c5c85fe31f35a6a30da1be500260801c5b60008364020000000016600f0b1315613ae05770010000000162e42ff0999ce3541b9fffcf0260801c5b60008364010000000016600f0b1315613b0a57700100000000b17217f80f4ef5aadda455540260801c5b600083638000000016600f0b1315613b335770010000000058b90bfbf8479bd5a81b51ad0260801c5b600083634000000016600f0b1315613b5c577001000000002c5c85fdf84bd62ae30a74cc0260801c5b600083632000000016600f0b1315613b8557700100000000162e42fefb2fed257559bdaa0260801c5b600083631000000016600f0b1315613bae577001000000000b17217f7d5a7716bba4a9ae0260801c5b600083630800000016600f0b1315613bd757700100000000058b90bfbe9ddbac5e109cce0260801c5b600083630400000016600f0b1315613c005770010000000002c5c85fdf4b15de6f17eb0d0260801c5b600083630200000016600f0b1315613c29577001000000000162e42fefa494f1478fde050260801c5b600083630100000016600f0b1315613c525770010000000000b17217f7d20cf927c8e94c0260801c5b6000836280000016600f0b1315613c7a577001000000000058b90bfbe8f71cb4e4b33d0260801c5b6000836240000016600f0b1315613ca257700100000000002c5c85fdf477b662b269450260801c5b6000836220000016600f0b1315613cca5770010000000000162e42fefa3ae53369388c0260801c5b6000836210000016600f0b1315613cf257700100000000000b17217f7d1d351a389d400260801c5b6000836208000016600f0b1315613d1a5770010000000000058b90bfbe8e8b2d3d4ede0260801c5b6000836204000016600f0b1315613d42577001000000000002c5c85fdf4741bea6e77e0260801c5b6000836202000016600f0b1315613d6a57700100000000000162e42fefa39fe95583c20260801c5b6000836201000016600f0b1315613d92577001000000000000b17217f7d1cfb72b45e10260801c5b60008361800016600f0b1315613db957700100000000000058b90bfbe8e7cc35c3f00260801c5b60008361400016600f0b1315613de05770010000000000002c5c85fdf473e242ea380260801c5b60008361200016600f0b1315613e07577001000000000000162e42fefa39f02b772c0260801c5b60008361100016600f0b1315613e2e5770010000000000000b17217f7d1cf7d83c1a0260801c5b60008361080016600f0b1315613e55577001000000000000058b90bfbe8e7bdcbe2e0260801c5b60008361040016600f0b1315613e7c57700100000000000002c5c85fdf473dea871f0260801c5b60008361020016600f0b1315613ea35770010000000000000162e42fefa39ef44d910260801c5b60008361010016600f0b1315613eca57700100000000000000b17217f7d1cf79e9490260801c5b600083608016600f0b1315613ef05770010000000000000058b90bfbe8e7bce5440260801c5b600083604016600f0b1315613f16577001000000000000002c5c85fdf473de6eca0260801c5b600083602016600f0b1315613f3c57700100000000000000162e42fefa39ef366f0260801c5b600083601016600f0b1315613f62577001000000000000000b17217f7d1cf79afa0260801c5b600083600816600f0b1315613f8857700100000000000000058b90bfbe8e7bcd6d0260801c5b600083600416600f0b1315613fae5770010000000000000002c5c85fdf473de6b20260801c5b600083600216600f0b1315613fd4577001000000000000000162e42fefa39ef3580260801c5b600083600116600f0b1315613ffa5770010000000000000000b17217f7d1cf79ab0260801c5b600f83810b60401d603f03900b1c60016001607f1b0381111561021957600080fd5b80356001600160a01b038116811461092657600080fd5b803567ffffffffffffffff8116811461092657600080fd5b80600f0b811461405a57600080fd5b50565b801515811461405a57600080fd5b60008060008060008060c0878903121561408457600080fd5b61408d8761401c565b955061409b60208801614033565b945060408701356140ab8161404b565b935060608701356140bb8161404b565b92506080870135915060a08701356140d28161405d565b809150509295509295509295565b600080604083850312156140f357600080fd5b82356140fe8161405d565b9150602083013561410e8161405d565b809150509250929050565b60008060006060848603121561412e57600080fd5b61413784614033565b925060208401356141478161404b565b915060408401356141578161405d565b809150509250925092565b6000806040838503121561417557600080fd5b6140fe8361401c565b6000806000806080858703121561419457600080fd5b61419d85614033565b935060208501356141ad8161404b565b925060408501356141bd8161405d565b9396929550929360600135925050565b634e487b7160e01b600052601160045260246000fd5b6000828210156141f5576141f56141cd565b500390565b634e487b7160e01b600052602160045260246000fd5b60008219821115614223576142236141cd565b500190565b60006020828403121561423a57600080fd5b81516101c98161404b565b600081600f0b83600f0b600081128160016001607f1b031901831281151615614270576142706141cd565b8160016001607f1b0301831381161561428b5761428b6141cd565b5090039392505050565b60006000198214156142a9576142a96141cd565b5060010190565b600181815b808511156142eb5781600019048211156142d1576142d16141cd565b808516156142de57918102915b93841c93908002906142b5565b509250929050565b60008261430257506001610219565b8161430f57506000610219565b8160018114614325576002811461432f5761434b565b6001915050610219565b60ff841115614340576143406141cd565b50506001821b610219565b5060208310610133831016604e8410600b841016171561436e575081810a610219565b61437883836142b0565b806000190482111561438c5761438c6141cd565b029392505050565b60006101c960ff8416836142f3565b6000816143b2576143b26141cd565b506000190190565b60008160001904831182151516156143d4576143d46141cd565b500290565b634e487b7160e01b600052601260045260246000fd5b60008261440c57634e487b7160e01b600052601260045260246000fd5b500490565b600081600f0b60016001607f1b031981141561442f5761442f6141cd565b60000392915050565b60006020828403121561444a57600080fd5b5051919050565b634e487b7160e01b600052603260045260246000fd5b60006020828403121561447957600080fd5b81516101c98161405d565b634e487b7160e01b600052600160045260246000fd5b6000610120820190508251600f0b82526020830151600f0b602083015260408301516144cb6040840182600f0b9052565b5060608301516144e06060840182600f0b9052565b5060808301516144f56080840182600f0b9052565b5060a083015161450a60a0840182600f0b9052565b5060c083015161451f60c0840182600f0b9052565b5060e083015161453460e0840182600f0b9052565b506101008084015161233b82850182600f0b9052565b634e487b7160e01b600052603160045260246000fdfeb31c2c74f86ca3ce94d901f5f5bbe66f7161eec2f7b5aa0b75a86371436424eabbd6af8edd89d04327b00c29df7f272b9b1ae01bf6d9c54a784f935706df52eba2646970667358221220ce40bf29bd8a4d9f9fde186eb0462e213ab13d782ba84a2193fd7b7a9566dc5064736f6c634300080c0033000000000000000000000000c4b2c51f969e0713e799de73b7f130fb7bb604cf00000000000000000000000082af49447d8a07e3bd95bd0d56f35241523fbab1000000000000000000000000bc3c01d954282eed8433da4359c1ac1443a7d09a0000000000000000000000007bf2392bd078c8353069cffeacc67c094079be230000000000000000000000007fa86681a7c19416950bae6c04a5116f3b07116d00000000000000000000000000000000000000000000000007ae147ae147ae14000000000000000000000000d8a0d357171bebc63cea559c4e9cd182c1bf25ef
Deployed Bytecode
0x608060405234801561001057600080fd5b50600436106100575760003560e01c80635a1c4b971461005c57806380beb6f41461008e57806382b3e955146100e957806385d54ce31461010a578063ac3fa4f81461012d575b600080fd5b61006f61006a36600461406b565b610140565b60408051600f93840b81529190920b6020820152015b60405180910390f35b6100e761009c3660046140e0565b3360009081527fbbd6af8edd89d04327b00c29df7f272b9b1ae01bf6d9c54a784f935706df53056020908152604080832084151584529091529020805460ff19168315151790555050565b005b6100fc6100f7366004614119565b6101a2565b604051908152602001610085565b61011d610118366004614162565b6101d0565b6040519015158152602001610085565b6100e761013b36600461417e565b61021f565b6000806101936040518060c001604052808a6001600160a01b031681526020018967ffffffffffffffff16815260200188600f0b815260200187600f0b81526020018681526020018515158152506103db565b91509150965096945050505050565b6000806101ba6101b38460006107b2565b8686610395565b90506101c5816107e5565b9150505b9392505050565b6001600160a01b03821660009081527fbbd6af8edd89d04327b00c29df7f272b9b1ae01bf6d9c54a784f935706df530560209081526040808320841515845290915290205460ff165b92915050565b6000805160206145818339815191526000808061023b846108cb565b90506000806102906040518060c00160405280336001600160a01b031681526020018c67ffffffffffffffff1681526020018b600f0b815260200185600f0b81526020018981526020018a15158152506103db565b90925090506102a8826102a3888b61092b565b610959565b94506102b8816102a3888b61092b565b935050506102cc84898988878b6001610974565b60006102e36102dc8860016107b2565b8a8a610395565b90506102f0338288610b47565b610307336102fe85876141e3565b89600180610cda565b6103367f0000000000000000000000007bf2392bd078c8353069cffeacc67c094079be23848960016000610cda565b604080518281526020810188905290810185905260608101849052600f83900b608082015233907f2aa636510213a4ef3578d8b9fdcadcb1c62c2d77d6a452e3b60b1f59be427add9060a00160405180910390a2505050505050505050565b600081600f0b60808467ffffffffffffffff16901b60f88660078111156103be576103be6141fa565b6103c992911b614210565b6103d39190614210565b949350505050565b60008060008360400151600f0b1380156103fc575060008360600151600f0b135b801561041657506000836020015167ffffffffffffffff16115b6104565760405162461bcd60e51b815260206004820152600c60248201526b696e76616c6964206172677360a01b60448201526064015b60405180910390fd5b60006000805160206145818339815191529050600061049242866020015167ffffffffffffffff1661048891906141e3565b6301e13380610d32565b8254600184015460608801516040808a01519051631af9d50d60e11b81526001600160a01b0394851660048201529284166024840152600f91820b6044840152810b606483015283900b60848201529192506000917f000000000000000000000000c4b2c51f969e0713e799de73b7f130fb7bb604cf909116906335f3aa1a9060a401602060405180830381865afa158015610532573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906105569190614228565b9050600081600f0b136105955760405162461bcd60e51b81526020600482015260076024820152660766f6c203d20360cc1b604482015260640161044d565b60006105bd6105a8600f84900b84610d69565b88604001518960600151868b60a00151610da0565b905060006106136105e1868a608001518b606001518c604001518d60a00151610e81565b8960a001516105fe576003870154600160a81b900460ff16610ef1565b6003870154600160a01b900460ff165b610ef1565b90508760a001511561063c57610639886060015182600f0b610d6990919063ffffffff16565b90505b6000806106606106518b60a0015160016107b2565b8b602001518c60400151610395565b6000818152601b8901602052604081205460a08d0151600f9190910b94509192509061068d908990610f07565b50905082600f0b600014806106a7575082600f0b81600f0b125b156106b0578092505b5050608089015160038701546000916106d291600160a01b900460ff16610ef1565b905061071a836107116106f6826106ed600f8a900b87610d69565b600f0b90610fa3565b61070867b33333333333333387610d69565b600f0b90610d69565b600f0b90610fd6565b98508960a0015115610743576107408a606001518a600f0b61100990919063ffffffff16565b98505b610771600f8a900b7f00000000000000000000000000000000000000000000000007ae147ae147ae14610d69565b975061078e6107838b60000151611061565b600f8a900b90610d69565b6107989089614245565b97506107a4888a614245565b985050505050505050915091565b600082156107d057816107c65760056107c9565b60045b9050610219565b816107dc5760076101c9565b60069392505050565b60008181527fb31c2c74f86ca3ce94d901f5f5bbe66f7161eec2f7b5aa0b75a86371436424eb602052604081206000805160206145818339815191529060f884901c83600582600781111561083c5761083c6141fa565b149050600061084a8461112a565b905060005b818110156108c05760006108638683611134565b6001600160a01b0381166000908152601a890160209081526040808320881515845290915290205490915060ff16156108ad576108a0818a611140565b6108aa9089614210565b97505b50806108b881614295565b91505061084f565b505050505050919050565b60004282600c015414156108e35761021982426111f3565b6108ec82611221565b90506108f882426111f3565b600f0b61090a5761090a824283611326565b42600c83015561091b8260016113a4565b6109268260006113a4565b919050565b600081610946576003830154600160a81b900460ff166101c9565b505060030154600160a01b900460ff1690565b60006101c961096983600a614394565b600f85900b9061142f565b600061098b6109848460006107b2565b8888610395565b9050600061099b89878987611497565b905060006109ab8a84848c6114fd565b60008481527fb31c2c74f86ca3ce94d901f5f5bbe66f7161eec2f7b5aa0b75a86371436424eb602052604081209192506109e48261112a565b90505b8815610b39576000610a046109fb836143a3565b92508383611134565b9050868015610a3b57506001600160a01b0381166000908152601a8e01602090815260408083208b1515845290915290205460ff16155b15610a4657506109e7565b610a716040518060800160405280600081526020016000815260200160008152602001600081525090565b610a7b8288611140565b8082528b1015610a89578a81525b80518b90610a9790886143ba565b610aa191906143ef565b602082015280518b90610ab4908c6143ba565b610abe91906143ef565b604082015280518b90610ad190876143ba565b610adb91906143ef565b6060820152610aee8e8389848d8d611580565b8051610afa908c6141e3565b9a50806020015186610b0c91906141e3565b955080604001518a610b1e91906141e3565b9950806060015185610b3091906141e3565b945050506109e7565b505050505050505050505050565b6001600160a01b038316610ba95760405162461bcd60e51b815260206004820152602360248201527f455243313135353a206275726e2066726f6d20746865207a65726f206164647260448201526265737360e81b606482015260840161044d565b610bd733846000610bb98661173a565b610bc28661173a565b60405180602001604052806000815250611785565b60008281527f1799cf914cb0cb442ca7c7ac709ee40d0cb89e87351dc08d517fbda27d50c68b602090815260408083206001600160a01b03871684529182905290912054821115610c785760405162461bcd60e51b815260206004820152602560248201527f455243313135353a206275726e20616d6f756e7420657863656564732062616c604482015264616e63657360d81b606482015260840161044d565b6001600160a01b03841660008181526020838152604080832080548790039055805187815291820186905291929133917fc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f6291015b60405180910390a450505050565b8115610d20578015610d0d57610d0885610d0260008051602061458183398151915286611bfb565b86611c24565b610d2b565b610d0885610d1a85611cea565b86611d3e565b610d2b858585611d59565b5050505050565b600081610d3e57600080fd5b6000610d4a8484611e2d565b905060016001607f1b036001600160801b03821611156101c957600080fd5b6000600f83810b9083900b0260401d60016001607f1b03198112801590610d97575060016001607f1b038113155b6101c957600080fd5b600080610db1600f85900b88610d69565b90506000610dc182600f0b611f8f565b90506000610dfe82610df5600186600f0b901d610711610ded8d8d600f0b61100990919063ffffffff16565b600f0b611fb1565b600f0b90611009565b90506000610e10600f83900b84610fa3565b90508515610e4957610e3e610e32610e2783611feb565b600f8c900b90610d69565b6106ed610e2785611feb565b945050505050610e78565b610e6f610e60610e27610e5b84614411565b611feb565b6106ed610e27610e5b86614411565b610e3e90614411565b95945050505050565b60008115610ebe5782600f0b84600f0b1315610eb957610eb685610ead86610df5600f82900b88610fa3565b600f0b9061142f565b90505b610e78565b82600f0b84600f0b1215610e7857610ee785610ede600f86900b87610fa3565b88919085611497565b9695505050505050565b60006101c983610f0284600a614394565b610d32565b80151560009081526013830160205260408120819081610f2786866120a7565b90506000610f3587876120c0565b905060008360010154118015610f4b5750825415155b8015610f58575082544210155b15610f9257610f7c81610711856001015461060e8a8c61092b90919063ffffffff16565b9350610f8b878383878a61212b565b9450610f99565b8194508093505b5050509250929050565b6000600f82810b9084900b0360016001607f1b03198112801590610d97575060016001607f1b038113156101c957600080fd5b6000600f83810b9083900b0160016001607f1b03198112801590610d97575060016001607f1b038113156101c957600080fd5b600081600f0b6000141561101c57600080fd5b600082600f0b604085600f0b901b81611037576110376143d9565b05905060016001607f1b03198112801590610d97575060016001607f1b038113156101c957600080fd5b60007f0000000000000000000000007fa86681a7c19416950bae6c04a5116f3b07116d6001600160a01b031615610926576040516303793c8d60e11b81526001600160a01b038381166004830152610219917f0000000000000000000000007fa86681a7c19416950bae6c04a5116f3b07116d909116906306f2791a90602401602060405180830381865afa1580156110fe573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906111229190614438565b612710610d32565b6000610219825490565b60006101c98383612222565b60006001600160a01b0383166111ac5760405162461bcd60e51b815260206004820152602b60248201527f455243313135353a2062616c616e636520717565727920666f7220746865207a60448201526a65726f206164647265737360a81b606482015260840161044d565b7f1799cf914cb0cb442ca7c7ac709ee40d0cb89e87351dc08d517fbda27d50c68b6000928352602090815260408084206001600160a01b0395909516845293905250205490565b60006011830181611206610e10856143ef565b8152602081019190915260400160002054600f0b9392505050565b6000808260030160009054906101000a90046001600160a01b03166001600160a01b03166350d25bcd6040518163ffffffff1660e01b8152600401602060405180830381865afa158015611279573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061129d9190614438565b905060008360020160009054906101000a90046001600160a01b03166001600160a01b03166350d25bcd6040518163ffffffff1660e01b8152600401602060405180830381865afa1580156112f6573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061131a9190614438565b90506103d382826122a8565b6000611334610e10846143ef565b6000818152601186016020526040902080546001600160801b0319166001600160801b038516179055905061136d60ff808316906141e3565b6001901b846012016000600884901c815260200190815260200160002060008282546113999190614210565b909155505050505050565b80151560009081526013830160205260409020805415806113c55750805442105b156113cf57505050565b60006113db84846120c0565b9050611411848261140b611400866001015461060e898b61092b90919063ffffffff16565b600f86900b90610fd6565b86612343565b50501515600090815260139091016020526040812081815560010155565b60008161143e57506000610219565b600083600f0b121561144f57600080fd5b600f83900b6001600160801b038316810260401c90608084901c026001600160c01b0381111561147e57600080fd5b60401b811981111561148f57600080fd5b019392505050565b600081156114a65750826103d3565b60006114b6600f85900b8661142f565b905060006114d5828860030160149054906101000a900460ff16610ef1565b90506114f2818860030160159054906101000a900460ff16610959565b979650505050505050565b60008167ffffffffffffffff164210156103d357600061151c856123c0565b1561153a576000858152601d87016020526040902054600f0b611543565b611543866123df565b9050610ee76301e133806115614267ffffffffffffffff87166141e3565b61156b90876143ba565b61157591906143ef565b600f83900b9061142f565b600061159887878787600001518860600151886123fb565b84606001516115a79190614210565b90506115b886868660000151610b47565b60006115c5888886612537565b1590506115f48783876040015188602001516115e191906141e3565b6115eb9190614210565b86846000610cda565b801561160f5761160a8888868860200151612571565b61165d565b84604001518211156116365761160a88888688604001518661163191906141e3565b6126a9565b818560400151111561165d5761165d88888685896040015161165891906141e3565b612571565b82156116c857866001600160a01b03167f4159772091e485ef6ade165ca7a05f8d859ded92728fe2aaa7c2371c75c0cce387876040015188602001516116a391906141e3565b88516040805193845260208401929092529082015260600160405180910390a2611730565b866001600160a01b03167f69a2ef6bf9e7ff92cbf1b71963ba1751b1abe8f99e3b3aae2ab99e416df61493878760400151886020015161170891906141e3565b8851604080519384526020840192909252908201526000606082015260800160405180910390a25b5050505050505050565b6040805160018082528183019092526060916000919060208083019080368337019050509050828160008151811061177457611774614451565b602090810291909101015292915050565b6117938686868686866127e9565b60008051602061458183398151915260005b84518110156117305760008582815181106117c2576117c2614451565b6020026020010151905060008583815181106117e0576117e0614451565b6020026020010151905080600014156117fa575050611be9565b6001600160a01b0389166118175761181560158501836129c3565b505b6001600160a01b0388161580156118345750611832826123c0565b155b156118485761184660158501836129cf565b505b7f000000000000000000000000000000000000000000000000000000000000000082148061189557507f010000000000000000000000000000000000000000000000000000000000000082145b806118bf57507f020000000000000000000000000000000000000000000000000000000000000082145b806118e957507f030000000000000000000000000000000000000000000000000000000000000082145b156119d9576001600160a01b0389161580159061190e57506001600160a01b03881615155b156119d95760007f000000000000000000000000000000000000000000000000000000000000000083148061196257507f020000000000000000000000000000000000000000000000000000000000000083145b6001600160a01b038b166000908152600d8701602090815260408083208415158452909152902054909150429061199c9062015180614210565b106119d75760405162461bcd60e51b815260206004820152600b60248201526a1b1a5c481b1bd8dac80c5960aa1b604482015260640161044d565b505b7f0000000000000000000000000000000000000000000000000000000000000000821480611a2657507f010000000000000000000000000000000000000000000000000000000000000082145b15611b9d577f000000000000000000000000000000000000000000000000000000000000000082146000611a5a86836129db565b90506001600160a01b038b1615611b52576000611a778c86611140565b90508181118015611a915750611a8d8285614210565b8111155b15611b29576001600160a01b038c166000908152601488016020908152604080832086151580855260138c0184528285205485529083528184209084529091529020548490611ae090836141e3565b1015611b1e5760405162461bcd60e51b815260206004820152600d60248201526c496e7375662062616c616e636560981b604482015260640161044d565b611b29878d856129f5565b6001600160a01b038b1615611b5057611b44878d8587612571565b611b50878c85876126a9565b505b6001600160a01b038a1615611b9a576000611b6d8b86611140565b9050818111158015611b88575081611b858583614210565b10155b15611b9857611b98878c85612aa6565b505b50505b60f882901c6005816007811115611bb657611bb66141fa565b1480611bd357506007816007811115611bd157611bd16141fa565b145b15611be557611be5858b8b8686612b4e565b5050505b80611bf381614295565b9150506117a5565b600081611c125782546001600160a01b03166101c9565b5050600101546001600160a01b031690565b80611c2e57505050565b60405163a9059cbb60e01b81526001600160a01b0384811660048301526024820183905283169063a9059cbb906044016020604051808303816000875af1158015611c7d573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611ca19190614467565b611ce55760405162461bcd60e51b8152602060048201526015602482015274115490cc8c081d1c985b9cd9995c8819985a5b1959605a1b604482015260640161044d565b505050565b600081611d17577f0300000000000000000000000000000000000000000000000000000000000000610219565b7f020000000000000000000000000000000000000000000000000000000000000092915050565b611ce583838360405180602001604052806000815250612ccb565b6000805160206145818339815191526000611d7383612de6565b90506000611d80826123c0565b1115611e2257600061010480611d9681426143ef565b611da091906143ba565b611daa9190614210565b6001600160a01b0387166000908152601485016020908152604080832084845282528083208815158452909152812080549293508792909190611dee908490614210565b90915550508315156000908152601384016020526040812060018101805491928892611e1b908490614210565b9091555050555b610d2b858286611d3e565b600081611e3957600080fd5b60006001600160c01b038411611e645782604085901b81611e5c57611e5c6143d9565b049050611f7b565b60c084811c6401000000008110611e7d576020918201911c5b620100008110611e8f576010918201911c5b6101008110611ea0576008918201911c5b60108110611eb0576004918201911c5b60048110611ec0576002918201911c5b60028110611ecf576001820191505b60bf820360018603901c6001018260ff0387901b81611ef057611ef06143d9565b0492506001600160801b03831115611f0757600080fd5b608085901c83026001600160801b038616840260c088901c604089901b82811015611f33576001820391505b608084901b92900382811015611f4a576001820391505b829003608084901c8214611f6057611f60614484565b888181611f6f57611f6f6143d9565b04870196505050505050505b6001600160801b038111156101c957600080fd5b60008082600f0b1215611fa157600080fd5b610219604083600f0b901b612e3a565b60008082600f0b13611fc257600080fd5b6080611fcd83612fa8565b600f0b6fb17217f7d1cf79abc9e3b39803f2f6af02901c9050919050565b600080611ffc600f84900b84610d69565b9050600061208661206b61203a61202b612023600f87900b68030000000000000000610fd6565b600f0b611f8f565b67d3c84b78b749bd6b90610d69565b61071161205c61204c89600f0b613083565b68019abac0ea1da6503690610d69565b679109f285df45239490610fd6565b610df5600161207986614411565b600f0b901d600f0b6130b6565b9050600084600f0b1361209957806103d3565b6103d3600160401b82610fa3565b60006120b3838361310b565b90506101c9838284613132565b6000806120dd836120d25760016120d5565b60005b600080610395565b83151560009081526013860160205260409020600101549091506103d3906000838152600080516020614561833981519152602052604090205461212191906141e3565b61060e868661092b565b60008082612140576019870154600f0b612150565b6019870154600160801b9004600f0b5b905080600f0b6000141561216857506008860154600f0b5b60405163e101a89b60e01b8152600f87810b600483015286810b602483015285810b604483015282900b606482015273c7a7275bc25a7bf07c6d0c2f8784c5450cb9b8f59063e101a89b90608401602060405180830381865af41580156121d3573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906121f79190614228565b915067b33333333333333382600f0b12156122185767b33333333333333391505b5095945050505050565b815460009082106122805760405162461bcd60e51b815260206004820152602260248201527f456e756d657261626c655365743a20696e646578206f7574206f6620626f756e604482015261647360f01b606482015260840161044d565b82600001828154811061229557612295614451565b9060005260206000200154905092915050565b6000816122b457600080fd5b6000808412156122c957836000039350600190505b60008312156122db5760009290920391155b60006122e78585611e2d565b90508115612315576001607f1b816001600160801b0316111561230957600080fd5b60000391506102199050565b60016001607f1b03816001600160801b0316111561233257600080fd5b91506102199050565b505092915050565b600061234f85836120a7565b90506000612360868387878761212b565b905061236d8682856132f8565b60408051600f83810b825287810b602083015286900b818301529051841515917f4e23621c6f591f14bf9505cb8326b45af9dc6c5569fd608de2a7a2ddd6146b2e919081900360600190a2505050505050565b6000908152600080516020614561833981519152602052604090205490565b601e810154600f0b806109265750670666666666666666919050565b6001600160a01b0385166000908152601c8701602090815260408083208784529091528120548161242c8888611140565b61243687846143ba565b61244091906143ef565b6001600160a01b0389166000908152601c8b01602090815260408083208b845290915281208054929350839290919061247a9084906141e3565b90915550612496905061248d86836141e3565b610ead8a611061565b92506000836124a587846141e3565b6124af91906141e3565b90506124e07f0000000000000000000000007bf2392bd078c8353069cffeacc67c094079be23828760016000610cda565b604080516001600160a01b038b168152602081018a90529081018290527fe02f89a472c9f9be95b6093634de6f0967e703e84390d7e00d23e15ea6b997659060600160405180910390a15050509695505050505050565b6001600160a01b0382166000908152600e84016020908152604080832084151584529091528120548015806101c557504210949350505050565b6001600160a01b03831660009081526017850160209081526040808320851515845282528083205460188801909252822054909180848410156125b2578394505b6125bc85856141e3565b91506125c885846141e3565b60405163edaf7d5b60e01b81526001600160a01b0389811660048301523060248301528815156044830152606482018790526084820185905260a482018690529192507f000000000000000000000000bc3c01d954282eed8433da4359c1ac1443a7d09a9091169063edaf7d5b9060c401600060405180830381600087803b15801561265357600080fd5b505af1158015612667573d6000803e3d6000fd5b505050506001600160a01b0396909616600090815260178801602090815260408083209715158352968152868220929092556018909701905250505090912055565b6001600160a01b03808416600090815260178601602090815260408083208615158452825280832054601889019092529091205490917f000000000000000000000000bc3c01d954282eed8433da4359c1ac1443a7d09a1663edaf7d5b863087866127148982614210565b6040516001600160e01b031960e088901b1681526001600160a01b03958616600482015294909316602485015290151560448401526064830152608482015260a4810184905260c401600060405180830381600087803b15801561277757600080fd5b505af115801561278b573d6000803e3d6000fd5b50505050828261279b9190614210565b6001600160a01b0386166000908152601788016020908152604080832088151584529091529020556127cd8382614210565b9315156000908152601890960160205250506040909320555050565b836001600160a01b0316856001600160a01b0316146129bb576001600160a01b0385811660009081527fb31c2c74f86ca3ce94d901f5f5bbe66f7161eec2f7b5aa0b75a86371436424ec602052604080822092871682528120600080516020614561833981519152927fb31c2c74f86ca3ce94d901f5f5bbe66f7161eec2f7b5aa0b75a86371436424eb929091905b87518110156129b557600087828151811061289557612895614451565b6020026020010151905060008111156129ac5760008983815181106128bc576128bc614451565b6020026020010151905060006001600160a01b03168c6001600160a01b0316141561290a57600081815260208890526040812080548492906128ff908490614210565b909155506129409050565b816129158d83611140565b1415612940576000818152602087905260409020612933908d61334f565b5061293e85826129cf565b505b6001600160a01b038b16612977576000818152602088905260408120805484929061296c9084906141e3565b909155506129aa9050565b6129818b82611140565b6129aa57600081815260208790526040902061299d908c613364565b506129a884826129c3565b505b505b50600101612878565b50505050505b505050505050565b60006101c98383613375565b60006101c983836133c4565b6000816129ec5782600401546101c9565b50506005015490565b6001600160a01b038216612a0857600080fd5b8015156000908152600f84016020908152604080832060108701909252909120612a33848383613491565b612a3e575050505050565b6001600160a01b0393841660008181526020838152604080832080549683528184208054978a16808652838620805499909b166001600160a01b0319998a168117909b5599855295909252822080548616909717909655528054821690558254169091555050565b6001600160a01b038216612ab957600080fd5b8015156000908152600f84016020908152604080832060108701909252909120612ae4848383613491565b15612af0575050505050565b60008080526020828152604080832080546001600160a01b0390811680865296845282852080546001600160a01b03199081169a909216998a1790558885529490925282208054841690941790935580528154169092179091555050565b6001600160a01b038416158015612b6c575080612b6a836123c0565b145b15612ba957612b7a856123df565b6000838152601d87016020526040902080546001600160801b0319166001600160801b03929092169190911790555b6001600160a01b038316158015612bc65750612bc4826123c0565b155b15612bea576000828152601d86016020526040902080546001600160801b03191690555b6001600160a01b03841615801590612c0a57506001600160a01b03831615155b15610d2b5760f882901c608083901c8360006005846007811115612c3057612c306141fa565b1490506000612c418a878585611497565b90506000612c518b8984886114fd565b90506000612c638c8c8b8b86896123fb565b9050612c718c8b8b856134dd565b6000612c7e8d8d87612537565b1590508115612c9557612c958c8387846000610cda565b612cb08d8d8784612caa5761165886896141e3565b87612571565b612cbc8d8c87876126a9565b50505050505050505050505050565b6001600160a01b038416612d2b5760405162461bcd60e51b815260206004820152602160248201527f455243313135353a206d696e7420746f20746865207a65726f206164647265736044820152607360f81b606482015260840161044d565b612d4a33600086612d3b8761173a565b612d448761173a565b86611785565b60008381527f1799cf914cb0cb442ca7c7ac709ee40d0cb89e87351dc08d517fbda27d50c68b602090815260408083206001600160a01b038816845290915281208054849290612d9b908490614210565b909155505060408051848152602081018490526001600160a01b0386169160009133917fc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f629101610ccc565b600081612e13577f0100000000000000000000000000000000000000000000000000000000000000610219565b7f000000000000000000000000000000000000000000000000000000000000000092915050565b600081612e4957506000919050565b816001600160801b8210612e625760809190911c9060401b5b600160401b8210612e785760409190911c9060201b5b6401000000008210612e8f5760209190911c9060101b5b620100008210612ea45760109190911c9060081b5b6101008210612eb85760089190911c9060041b5b60108210612ecb5760049190911c9060021b5b60088210612ed75760011b5b6001818581612ee857612ee86143d9565b048201901c90506001818581612f0057612f006143d9565b048201901c90506001818581612f1857612f186143d9565b048201901c90506001818581612f3057612f306143d9565b048201901c90506001818581612f4857612f486143d9565b048201901c90506001818581612f6057612f606143d9565b048201901c90506001818581612f7857612f786143d9565b048201901c90506000818581612f9057612f906143d9565b049050808210612fa05780610e78565b509392505050565b60008082600f0b13612fb957600080fd5b6000600f83900b600160401b8112612fd3576040918201911d5b6401000000008112612fe7576020918201911d5b620100008112612ff9576010918201911d5b610100811261300a576008918201911d5b6010811261301a576004918201911d5b6004811261302a576002918201911d5b60028112613039576001820191505b603f19820160401b600f85900b607f8490031b6001603f1b5b60008113156130785790800260ff81901c8281029390930192607f011c9060011d613052565b509095945050505050565b6000600f82900b60016001607f1b0319141561309e57600080fd5b600082600f0b126130af5781610219565b5060000390565b6000600160461b82600f0b126130cb57600080fd5b683fffffffffffffffff1982600f0b12156130e857506000919050565b610219608083600f0b700171547652b82fe1777d0ffda0d23a7d1202901d613567565b600081613126576008830154600160801b9004600f0b6101c9565b505060090154600f0b90565b600080826131445784600a015461314a565b84600b01545b61315490426141e3565b905061a8c08111156131735761316c61a8c0826141e3565b905061317c565b839150506101c9565b600061318a82613840610d32565b9050600061319d856120d25760016120d5565b851515600090815260188901602090815260408083205460138c0190925282206001015492935091600084815260008051602061456183398151915260205260409020546131eb91906141e3565b9050808210156131f85750805b600061320d61320783856141e3565b84610d32565b6040805161012081018252600f88810b82528c810b602083015283900b8183015267b333333333333333606082015267e66666666666666660808201819052600160401b60a0830181905260c083015260e082015268056fc2a2c515da32ea6101008201529051634916d70d60e01b815291925073c7a7275bc25a7bf07c6d0c2f8784c5450cb9b8f591634916d70d916132a99160040161449a565b602060405180830381865af41580156132c6573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906132ea9190614228565b9a9950505050505050505050565b8015613328576009830180546001600160801b0384166001600160801b031990911617905542600b840155505050565b6008830180546001600160801b03808516600160801b02911617905542600a840155505050565b60006101c9836001600160a01b0384166133c4565b60006101c9836001600160a01b0384165b60008181526001830160205260408120546133bc57508154600181810184556000848152602080822090930184905584548482528286019093526040902091909155610219565b506000610219565b60008181526001830160205260408120548015613487578354600090859060001981019081106133f6576133f6614451565b906000526020600020015490508085600001600184038154811061341c5761341c614451565b600091825260208083209091019290925591825260018601905260409020819055835484908061344e5761344e61454a565b60019003818190600052602060002001600090559055836001016000848152602001908152602001600020600090556001915050610219565b6000915050610219565b6001600160a01b038381166000908152602084905260408120549091161515806103d357506000808052602083905260409020546001600160a01b039081169085161490509392505050565b6001600160a01b0383166000908152601c85016020908152604080832085845290915281208054839290613512908490614210565b9091555050604080516001600160a01b0385168152602081018490529081018290527f3a8ab48b47eb6f5307d9b87d560cc92cf89c416cce1d94297a70d1c82d8c84129060600160405180910390a150505050565b6000600160461b82600f0b1261357c57600080fd5b683fffffffffffffffff1982600f0b121561359957506000919050565b6001607f1b60006001603f1b8416600f0b13156135c75770016a09e667f3bcc908b2fb1366ea957d3e0260801c5b60008367400000000000000016600f0b13156135f4577001306fe0a31b7152de8d5a46305c85edec0260801c5b60008367200000000000000016600f0b1315613621577001172b83c7d517adcdf7c8c50eb14a791f0260801c5b60008367100000000000000016600f0b131561364e5770010b5586cf9890f6298b92b71842a983630260801c5b60008367080000000000000016600f0b131561367b577001059b0d31585743ae7c548eb68ca417fd0260801c5b60008367040000000000000016600f0b13156136a857700102c9a3e778060ee6f7caca4f7a29bde80260801c5b60008367020000000000000016600f0b13156136d55770010163da9fb33356d84a66ae336dcdfa3f0260801c5b60008367010000000000000016600f0b131561370257700100b1afa5abcbed6129ab13ec11dc95430260801c5b600083668000000000000016600f0b131561372e5770010058c86da1c09ea1ff19d294cf2f679b0260801c5b600083664000000000000016600f0b131561375a577001002c605e2e8cec506d21bfc89a23a00f0260801c5b600083662000000000000016600f0b131561378657700100162f3904051fa128bca9c55c31e5df0260801c5b600083661000000000000016600f0b13156137b2577001000b175effdc76ba38e31671ca9397250260801c5b600083660800000000000016600f0b13156137de57700100058ba01fb9f96d6cacd4b180917c3d0260801c5b600083660400000000000016600f0b131561380a5770010002c5cc37da9491d0985c348c68e7b30260801c5b600083660200000000000016600f0b1315613836577001000162e525ee054754457d59952920260260801c5b600083660100000000000016600f0b13156138625770010000b17255775c040618bf4a4ade83fc0260801c5b6000836580000000000016600f0b131561388d577001000058b91b5bc9ae2eed81e9b7d4cfab0260801c5b6000836540000000000016600f0b13156138b857700100002c5c89d5ec6ca4d7c8acc017b7c90260801c5b6000836520000000000016600f0b13156138e35770010000162e43f4f831060e02d839a9d16d0260801c5b6000836510000000000016600f0b131561390e57700100000b1721bcfc99d9f890ea069117630260801c5b6000836508000000000016600f0b13156139395770010000058b90cf1e6d97f9ca14dbcc16280260801c5b6000836504000000000016600f0b1315613964577001000002c5c863b73f016468f6bac5ca2b0260801c5b6000836502000000000016600f0b131561398f57700100000162e430e5a18f6119e3c02282a50260801c5b6000836501000000000016600f0b13156139ba577001000000b1721835514b86e6d96efd1bfe0260801c5b60008364800000000016600f0b13156139e457700100000058b90c0b48c6be5df846c5b2ef0260801c5b60008364400000000016600f0b1315613a0e5770010000002c5c8601cc6b9e94213c72737a0260801c5b60008364200000000016600f0b1315613a38577001000000162e42fff037df38aa2b219f060260801c5b60008364100000000016600f0b1315613a625770010000000b17217fba9c739aa5819f44f90260801c5b60008364080000000016600f0b1315613a8c577001000000058b90bfcdee5acd3c1cedc8230260801c5b60008364040000000016600f0b1315613ab657700100000002c5c85fe31f35a6a30da1be500260801c5b60008364020000000016600f0b1315613ae05770010000000162e42ff0999ce3541b9fffcf0260801c5b60008364010000000016600f0b1315613b0a57700100000000b17217f80f4ef5aadda455540260801c5b600083638000000016600f0b1315613b335770010000000058b90bfbf8479bd5a81b51ad0260801c5b600083634000000016600f0b1315613b5c577001000000002c5c85fdf84bd62ae30a74cc0260801c5b600083632000000016600f0b1315613b8557700100000000162e42fefb2fed257559bdaa0260801c5b600083631000000016600f0b1315613bae577001000000000b17217f7d5a7716bba4a9ae0260801c5b600083630800000016600f0b1315613bd757700100000000058b90bfbe9ddbac5e109cce0260801c5b600083630400000016600f0b1315613c005770010000000002c5c85fdf4b15de6f17eb0d0260801c5b600083630200000016600f0b1315613c29577001000000000162e42fefa494f1478fde050260801c5b600083630100000016600f0b1315613c525770010000000000b17217f7d20cf927c8e94c0260801c5b6000836280000016600f0b1315613c7a577001000000000058b90bfbe8f71cb4e4b33d0260801c5b6000836240000016600f0b1315613ca257700100000000002c5c85fdf477b662b269450260801c5b6000836220000016600f0b1315613cca5770010000000000162e42fefa3ae53369388c0260801c5b6000836210000016600f0b1315613cf257700100000000000b17217f7d1d351a389d400260801c5b6000836208000016600f0b1315613d1a5770010000000000058b90bfbe8e8b2d3d4ede0260801c5b6000836204000016600f0b1315613d42577001000000000002c5c85fdf4741bea6e77e0260801c5b6000836202000016600f0b1315613d6a57700100000000000162e42fefa39fe95583c20260801c5b6000836201000016600f0b1315613d92577001000000000000b17217f7d1cfb72b45e10260801c5b60008361800016600f0b1315613db957700100000000000058b90bfbe8e7cc35c3f00260801c5b60008361400016600f0b1315613de05770010000000000002c5c85fdf473e242ea380260801c5b60008361200016600f0b1315613e07577001000000000000162e42fefa39f02b772c0260801c5b60008361100016600f0b1315613e2e5770010000000000000b17217f7d1cf7d83c1a0260801c5b60008361080016600f0b1315613e55577001000000000000058b90bfbe8e7bdcbe2e0260801c5b60008361040016600f0b1315613e7c57700100000000000002c5c85fdf473dea871f0260801c5b60008361020016600f0b1315613ea35770010000000000000162e42fefa39ef44d910260801c5b60008361010016600f0b1315613eca57700100000000000000b17217f7d1cf79e9490260801c5b600083608016600f0b1315613ef05770010000000000000058b90bfbe8e7bce5440260801c5b600083604016600f0b1315613f16577001000000000000002c5c85fdf473de6eca0260801c5b600083602016600f0b1315613f3c57700100000000000000162e42fefa39ef366f0260801c5b600083601016600f0b1315613f62577001000000000000000b17217f7d1cf79afa0260801c5b600083600816600f0b1315613f8857700100000000000000058b90bfbe8e7bcd6d0260801c5b600083600416600f0b1315613fae5770010000000000000002c5c85fdf473de6b20260801c5b600083600216600f0b1315613fd4577001000000000000000162e42fefa39ef3580260801c5b600083600116600f0b1315613ffa5770010000000000000000b17217f7d1cf79ab0260801c5b600f83810b60401d603f03900b1c60016001607f1b0381111561021957600080fd5b80356001600160a01b038116811461092657600080fd5b803567ffffffffffffffff8116811461092657600080fd5b80600f0b811461405a57600080fd5b50565b801515811461405a57600080fd5b60008060008060008060c0878903121561408457600080fd5b61408d8761401c565b955061409b60208801614033565b945060408701356140ab8161404b565b935060608701356140bb8161404b565b92506080870135915060a08701356140d28161405d565b809150509295509295509295565b600080604083850312156140f357600080fd5b82356140fe8161405d565b9150602083013561410e8161405d565b809150509250929050565b60008060006060848603121561412e57600080fd5b61413784614033565b925060208401356141478161404b565b915060408401356141578161405d565b809150509250925092565b6000806040838503121561417557600080fd5b6140fe8361401c565b6000806000806080858703121561419457600080fd5b61419d85614033565b935060208501356141ad8161404b565b925060408501356141bd8161405d565b9396929550929360600135925050565b634e487b7160e01b600052601160045260246000fd5b6000828210156141f5576141f56141cd565b500390565b634e487b7160e01b600052602160045260246000fd5b60008219821115614223576142236141cd565b500190565b60006020828403121561423a57600080fd5b81516101c98161404b565b600081600f0b83600f0b600081128160016001607f1b031901831281151615614270576142706141cd565b8160016001607f1b0301831381161561428b5761428b6141cd565b5090039392505050565b60006000198214156142a9576142a96141cd565b5060010190565b600181815b808511156142eb5781600019048211156142d1576142d16141cd565b808516156142de57918102915b93841c93908002906142b5565b509250929050565b60008261430257506001610219565b8161430f57506000610219565b8160018114614325576002811461432f5761434b565b6001915050610219565b60ff841115614340576143406141cd565b50506001821b610219565b5060208310610133831016604e8410600b841016171561436e575081810a610219565b61437883836142b0565b806000190482111561438c5761438c6141cd565b029392505050565b60006101c960ff8416836142f3565b6000816143b2576143b26141cd565b506000190190565b60008160001904831182151516156143d4576143d46141cd565b500290565b634e487b7160e01b600052601260045260246000fd5b60008261440c57634e487b7160e01b600052601260045260246000fd5b500490565b600081600f0b60016001607f1b031981141561442f5761442f6141cd565b60000392915050565b60006020828403121561444a57600080fd5b5051919050565b634e487b7160e01b600052603260045260246000fd5b60006020828403121561447957600080fd5b81516101c98161405d565b634e487b7160e01b600052600160045260246000fd5b6000610120820190508251600f0b82526020830151600f0b602083015260408301516144cb6040840182600f0b9052565b5060608301516144e06060840182600f0b9052565b5060808301516144f56080840182600f0b9052565b5060a083015161450a60a0840182600f0b9052565b5060c083015161451f60c0840182600f0b9052565b5060e083015161453460e0840182600f0b9052565b506101008084015161233b82850182600f0b9052565b634e487b7160e01b600052603160045260246000fdfeb31c2c74f86ca3ce94d901f5f5bbe66f7161eec2f7b5aa0b75a86371436424eabbd6af8edd89d04327b00c29df7f272b9b1ae01bf6d9c54a784f935706df52eba2646970667358221220ce40bf29bd8a4d9f9fde186eb0462e213ab13d782ba84a2193fd7b7a9566dc5064736f6c634300080c0033
Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)
000000000000000000000000c4b2c51f969e0713e799de73b7f130fb7bb604cf00000000000000000000000082af49447d8a07e3bd95bd0d56f35241523fbab1000000000000000000000000bc3c01d954282eed8433da4359c1ac1443a7d09a0000000000000000000000007bf2392bd078c8353069cffeacc67c094079be230000000000000000000000007fa86681a7c19416950bae6c04a5116f3b07116d00000000000000000000000000000000000000000000000007ae147ae147ae14000000000000000000000000d8a0d357171bebc63cea559c4e9cd182c1bf25ef
-----Decoded View---------------
Arg [0] : ivolOracle (address): 0xC4B2C51f969e0713E799De73b7f130Fb7Bb604CF
Arg [1] : wrappedNativeToken (address): 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1
Arg [2] : premiaMining (address): 0xbC3c01D954282eEd8433da4359C1ac1443a7d09A
Arg [3] : feeReceiver (address): 0x7bf2392bd078C8353069CffeAcc67c094079be23
Arg [4] : feeDiscountAddress (address): 0x7Fa86681A7c19416950bAE6c04A5116f3b07116D
Arg [5] : feePremium64x64 (int128): 553402322211286548
Arg [6] : exchangeHelper (address): 0xD8A0D357171beBC63CeA559c4e9CD182c1bf25ef
-----Encoded View---------------
7 Constructor Arguments found :
Arg [0] : 000000000000000000000000c4b2c51f969e0713e799de73b7f130fb7bb604cf
Arg [1] : 00000000000000000000000082af49447d8a07e3bd95bd0d56f35241523fbab1
Arg [2] : 000000000000000000000000bc3c01d954282eed8433da4359c1ac1443a7d09a
Arg [3] : 0000000000000000000000007bf2392bd078c8353069cffeacc67c094079be23
Arg [4] : 0000000000000000000000007fa86681a7c19416950bae6c04a5116f3b07116d
Arg [5] : 00000000000000000000000000000000000000000000000007ae147ae147ae14
Arg [6] : 000000000000000000000000d8a0d357171bebc63cea559c4e9cd182c1bf25ef
Age | Block | Fee Address | BC Fee Address | Voting Power | Jailed | Incoming |
---|
Make sure to use the "Vote Down" button for any spammy posts, and the "Vote Up" for interesting conversations.