编译器
0.8.11+commit.d7f03943
文件 1 的 16:BaseAdapter.sol
pragma solidity 0.8.11;
import { ERC20 } from "@rari-capital/solmate/src/tokens/ERC20.sol";
import { SafeTransferLib } from "@rari-capital/solmate/src/utils/SafeTransferLib.sol";
import { IERC3156FlashLender } from "../external/flashloan/IERC3156FlashLender.sol";
import { IERC3156FlashBorrower } from "../external/flashloan/IERC3156FlashBorrower.sol";
import { Divider } from "../Divider.sol";
import { Errors } from "@sense-finance/v1-utils/src/libs/Errors.sol";
abstract contract BaseAdapter is IERC3156FlashLender {
using SafeTransferLib for ERC20;
bytes32 public constant CALLBACK_SUCCESS = keccak256("ERC3156FlashBorrower.onFlashLoan");
address public immutable divider;
address public immutable target;
address public immutable underlying;
address public immutable oracle;
address public immutable stake;
uint256 public immutable stakeSize;
uint256 public immutable minm;
uint256 public immutable maxm;
uint256 public immutable mode;
uint256 public immutable ifee;
uint256 public immutable tilt;
uint256 public immutable level;
string public name;
string public symbol;
constructor(
address _divider,
address _target,
address _underlying,
address _oracle,
uint256 _ifee,
address _stake,
uint256 _stakeSize,
uint256 _minm,
uint256 _maxm,
uint256 _mode,
uint256 _tilt,
uint256 _level
) {
if (_minm >= _maxm) revert Errors.InvalidMaturityOffsets();
divider = _divider;
target = _target;
underlying = _underlying;
oracle = _oracle;
ifee = _ifee;
stake = _stake;
stakeSize = _stakeSize;
minm = _minm;
maxm = _maxm;
mode = _mode;
tilt = _tilt;
name = string(abi.encodePacked(ERC20(_target).name(), " Adapter"));
symbol = string(abi.encodePacked(ERC20(_target).symbol(), "-adapter"));
level = _level;
ERC20(_target).approve(_divider, type(uint256).max);
ERC20(_stake).approve(_divider, type(uint256).max);
}
function flashLoan(
IERC3156FlashBorrower receiver,
address,
uint256 amount,
bytes calldata data
) external returns (bool) {
if (Divider(divider).periphery() != msg.sender) revert Errors.OnlyPeriphery();
ERC20(target).safeTransfer(address(receiver), amount);
bytes32 keccak = IERC3156FlashBorrower(receiver).onFlashLoan(msg.sender, target, amount, 0, data);
if (keccak != CALLBACK_SUCCESS) revert Errors.FlashCallbackFailed();
ERC20(target).safeTransferFrom(address(receiver), address(this), amount);
return true;
}
function scale() external virtual returns (uint256);
function scaleStored() external view virtual returns (uint256);
function getUnderlyingPrice() external view virtual returns (uint256);
function wrapUnderlying(uint256 amount) external virtual returns (uint256);
function unwrapTarget(uint256 amount) external virtual returns (uint256);
function flashFee(address token, uint256) external view returns (uint256) {
if (token != target) revert Errors.TokenNotSupported();
return 0;
}
function maxFlashLoan(address token) external view override returns (uint256) {
return ERC20(token).balanceOf(address(this));
}
function notify(
address,
uint256,
bool
) public virtual {
return;
}
function onRedeem(
uint256,
uint256,
uint256,
uint256
) public virtual {
return;
}
function getMaturityBounds() external view returns (uint256, uint256) {
return (minm, maxm);
}
function getStakeAndTarget()
external
view
returns (
address,
address,
uint256
)
{
return (target, stake, stakeSize);
}
}
文件 2 的 16:Context.sol
pragma solidity ^0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
文件 3 的 16:DateTime.sol
pragma solidity 0.8.11;
library DateTime {
uint256 constant SECONDS_PER_DAY = 24 * 60 * 60;
uint256 constant SECONDS_PER_HOUR = 60 * 60;
uint256 constant SECONDS_PER_MINUTE = 60;
int256 constant OFFSET19700101 = 2440588;
function timestampToDate(uint256 timestamp)
internal
pure
returns (
uint256 year,
uint256 month,
uint256 day
)
{
(year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
}
function timestampToDateTime(uint256 timestamp)
internal
pure
returns (
uint256 year,
uint256 month,
uint256 day,
uint256 hour,
uint256 minute,
uint256 second
)
{
(year, month, day) = _daysToDate(timestamp / SECONDS_PER_DAY);
uint256 secs = timestamp % SECONDS_PER_DAY;
hour = secs / SECONDS_PER_HOUR;
secs = secs % SECONDS_PER_HOUR;
minute = secs / SECONDS_PER_MINUTE;
second = secs % SECONDS_PER_MINUTE;
}
function toDateString(uint256 _timestamp)
internal
pure
returns (
string memory d,
string memory m,
string memory y
)
{
(uint256 year, uint256 month, uint256 day) = timestampToDate(_timestamp);
d = uintToString(day);
m = uintToString(month);
y = uintToString(year);
if (day < 10) d = string(abi.encodePacked("0", d));
if (month < 10) m = string(abi.encodePacked("0", m));
}
function format(uint256 _timestamp) internal pure returns (string memory datestring) {
string[12] memory months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"June",
"July",
"Aug",
"Sept",
"Oct",
"Nov",
"Dec"
];
(uint256 year, uint256 month, uint256 day) = timestampToDate(_timestamp);
uint256 last = day % 10;
string memory suffix = "th";
if (day < 11 || day > 20) {
if (last == 1) suffix = "st";
if (last == 2) suffix = "nd";
if (last == 3) suffix = "rd";
}
return string(abi.encodePacked(uintToString(day), suffix, " ", months[month - 1], " ", uintToString(year)));
}
function getDayOfWeek(uint256 timestamp) internal pure returns (uint256 dayOfWeek) {
uint256 _days = timestamp / SECONDS_PER_DAY;
dayOfWeek = ((_days + 3) % 7) + 1;
}
function uintToString(uint256 _i) internal pure returns (string memory _uintAsString) {
if (_i == 0) return "0";
uint256 j = _i;
uint256 len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint256 k = len;
while (_i != 0) {
k = k - 1;
uint8 temp = (48 + uint8(_i - (_i / 10) * 10));
bytes1 b1 = bytes1(temp);
bstr[k] = b1;
_i /= 10;
}
return string(bstr);
}
function _daysFromDate(
uint256 year,
uint256 month,
uint256 day
) internal pure returns (uint256 _days) {
require(year >= 1970);
int256 _year = int256(year);
int256 _month = int256(month);
int256 _day = int256(day);
int256 __days = _day -
32075 +
(1461 * (_year + 4800 + (_month - 14) / 12)) /
4 +
(367 * (_month - 2 - ((_month - 14) / 12) * 12)) /
12 -
(3 * ((_year + 4900 + (_month - 14) / 12) / 100)) /
4 -
OFFSET19700101;
_days = uint256(__days);
}
function _daysToDate(uint256 _days)
internal
pure
returns (
uint256 year,
uint256 month,
uint256 day
)
{
int256 __days = int256(_days);
int256 L = __days + 68569 + OFFSET19700101;
int256 N = (4 * L) / 146097;
L = L - (146097 * N + 3) / 4;
int256 _year = (4000 * (L + 1)) / 1461001;
L = L - (1461 * _year) / 4 + 31;
int256 _month = (80 * L) / 2447;
int256 _day = L - (2447 * _month) / 80;
L = _month / 11;
_month = _month + 2 - 12 * L;
_year = 100 * (N - 49) + _year + L;
year = uint256(_year);
month = uint256(_month);
day = uint256(_day);
}
}
文件 4 的 16:Divider.sol
pragma solidity 0.8.11;
import { Pausable } from "@openzeppelin/contracts/security/Pausable.sol";
import { ERC20 } from "@rari-capital/solmate/src/tokens/ERC20.sol";
import { SafeTransferLib } from "@rari-capital/solmate/src/utils/SafeTransferLib.sol";
import { ReentrancyGuard } from "@rari-capital/solmate/src/utils/ReentrancyGuard.sol";
import { DateTime } from "./external/DateTime.sol";
import { FixedMath } from "./external/FixedMath.sol";
import { Errors } from "@sense-finance/v1-utils/src/libs/Errors.sol";
import { Levels } from "@sense-finance/v1-utils/src/libs/Levels.sol";
import { Trust } from "@sense-finance/v1-utils/src/Trust.sol";
import { YT } from "./tokens/YT.sol";
import { Token } from "./tokens/Token.sol";
import { BaseAdapter as Adapter } from "./adapters/BaseAdapter.sol";
contract Divider is Trust, ReentrancyGuard, Pausable {
using SafeTransferLib for ERC20;
using FixedMath for uint256;
using Levels for uint256;
uint256 public constant SPONSOR_WINDOW = 3 hours;
uint256 public constant SETTLEMENT_WINDOW = 3 hours;
uint256 public constant ISSUANCE_FEE_CAP = 0.05e18;
address public periphery;
address public immutable cup;
address public immutable tokenHandler;
bool public permissionless;
bool public guarded = true;
uint248 public adapterCounter;
mapping(uint256 => address) public adapterAddresses;
mapping(address => AdapterMeta) public adapterMeta;
mapping(address => mapping(uint256 => Series)) public series;
mapping(address => mapping(uint256 => mapping(address => uint256))) public lscales;
struct Series {
address pt;
uint48 issuance;
address yt;
uint96 tilt;
address sponsor;
uint256 reward;
uint256 iscale;
uint256 mscale;
uint256 maxscale;
}
struct AdapterMeta {
uint248 id;
bool enabled;
uint256 guard;
uint248 level;
}
constructor(address _cup, address _tokenHandler) Trust(msg.sender) {
cup = _cup;
tokenHandler = _tokenHandler;
}
function addAdapter(address adapter) external whenNotPaused {
if (!permissionless && msg.sender != periphery) revert Errors.OnlyPermissionless();
if (adapterMeta[adapter].id > 0 && !adapterMeta[adapter].enabled) revert Errors.InvalidAdapter();
_setAdapter(adapter, true);
}
function initSeries(
address adapter,
uint256 maturity,
address sponsor
) external nonReentrant whenNotPaused returns (address pt, address yt) {
if (periphery != msg.sender) revert Errors.OnlyPeriphery();
if (!adapterMeta[adapter].enabled) revert Errors.InvalidAdapter();
if (_exists(adapter, maturity)) revert Errors.DuplicateSeries();
if (!_isValid(adapter, maturity)) revert Errors.InvalidMaturity();
(address target, address stake, uint256 stakeSize) = Adapter(adapter).getStakeAndTarget();
(pt, yt) = TokenHandler(tokenHandler).deploy(adapter, adapterMeta[adapter].id, maturity);
uint256 scale = Adapter(adapter).scale();
series[adapter][maturity].pt = pt;
series[adapter][maturity].issuance = uint48(block.timestamp);
series[adapter][maturity].yt = yt;
series[adapter][maturity].tilt = uint96(Adapter(adapter).tilt());
series[adapter][maturity].sponsor = sponsor;
series[adapter][maturity].iscale = scale;
series[adapter][maturity].maxscale = scale;
ERC20(stake).safeTransferFrom(msg.sender, adapter, stakeSize);
emit SeriesInitialized(adapter, maturity, pt, yt, sponsor, target);
}
function settleSeries(address adapter, uint256 maturity) external nonReentrant whenNotPaused {
if (!adapterMeta[adapter].enabled) revert Errors.InvalidAdapter();
if (!_exists(adapter, maturity)) revert Errors.SeriesDoesNotExist();
if (_settled(adapter, maturity)) revert Errors.AlreadySettled();
if (!_canBeSettled(adapter, maturity)) revert Errors.OutOfWindowBoundaries();
uint256 mscale = Adapter(adapter).scale();
series[adapter][maturity].mscale = mscale;
if (mscale > series[adapter][maturity].maxscale) {
series[adapter][maturity].maxscale = mscale;
}
(address target, address stake, uint256 stakeSize) = Adapter(adapter).getStakeAndTarget();
ERC20(target).safeTransferFrom(adapter, msg.sender, series[adapter][maturity].reward);
ERC20(stake).safeTransferFrom(adapter, msg.sender, stakeSize);
emit SeriesSettled(adapter, maturity, msg.sender);
}
function issue(
address adapter,
uint256 maturity,
uint256 tBal
) external nonReentrant whenNotPaused returns (uint256 uBal) {
if (!adapterMeta[adapter].enabled) revert Errors.InvalidAdapter();
if (!_exists(adapter, maturity)) revert Errors.SeriesDoesNotExist();
if (_settled(adapter, maturity)) revert Errors.IssueOnSettle();
uint256 level = adapterMeta[adapter].level;
if (level.issueRestricted() && msg.sender != adapter) revert Errors.IssuanceRestricted();
ERC20 target = ERC20(Adapter(adapter).target());
uint256 issuanceFee = Adapter(adapter).ifee();
if (issuanceFee > ISSUANCE_FEE_CAP) revert Errors.IssuanceFeeCapExceeded();
uint256 fee = tBal.fmul(issuanceFee);
unchecked {
series[adapter][maturity].reward += fee;
}
uint256 tBalSubFee = tBal - fee;
unchecked {
if (guarded && target.balanceOf(adapter) + tBal > adapterMeta[address(adapter)].guard)
revert Errors.GuardCapReached();
}
Adapter(adapter).notify(msg.sender, tBalSubFee, true);
uint256 scale = level.collectDisabled() ? series[adapter][maturity].iscale : Adapter(adapter).scale();
uBal = tBalSubFee.fmul(scale);
lscales[adapter][maturity][msg.sender] = lscales[adapter][maturity][msg.sender] == 0
? scale
: _reweightLScale(
adapter,
maturity,
YT(series[adapter][maturity].yt).balanceOf(msg.sender),
uBal,
msg.sender,
scale
);
Token(series[adapter][maturity].pt).mint(msg.sender, uBal);
YT(series[adapter][maturity].yt).mint(msg.sender, uBal);
target.safeTransferFrom(msg.sender, adapter, tBal);
emit Issued(adapter, maturity, uBal, msg.sender);
}
function combine(
address adapter,
uint256 maturity,
uint256 uBal
) external nonReentrant whenNotPaused returns (uint256 tBal) {
if (!adapterMeta[adapter].enabled) revert Errors.InvalidAdapter();
if (!_exists(adapter, maturity)) revert Errors.SeriesDoesNotExist();
uint256 level = adapterMeta[adapter].level;
if (level.combineRestricted() && msg.sender != adapter) revert Errors.CombineRestricted();
Token(series[adapter][maturity].pt).burn(msg.sender, uBal);
uint256 collected = _collect(msg.sender, adapter, maturity, uBal, uBal, address(0));
uint256 cscale = series[adapter][maturity].mscale;
bool settled = _settled(adapter, maturity);
if (!settled) {
YT(series[adapter][maturity].yt).burn(msg.sender, uBal);
cscale = level.collectDisabled()
? series[adapter][maturity].iscale
: lscales[adapter][maturity][msg.sender];
}
tBal = uBal.fdiv(cscale);
ERC20(Adapter(adapter).target()).safeTransferFrom(adapter, msg.sender, tBal);
if (!settled) Adapter(adapter).notify(msg.sender, tBal, false);
unchecked {
tBal += collected;
}
emit Combined(adapter, maturity, tBal, msg.sender);
}
function redeem(
address adapter,
uint256 maturity,
uint256 uBal
) external nonReentrant whenNotPaused returns (uint256 tBal) {
if (!_settled(adapter, maturity)) revert Errors.NotSettled();
uint256 level = adapterMeta[adapter].level;
if (level.redeemRestricted() && msg.sender == adapter) revert Errors.RedeemRestricted();
Token(series[adapter][maturity].pt).burn(msg.sender, uBal);
uint256 zShare = FixedMath.WAD - series[adapter][maturity].tilt;
if (series[adapter][maturity].mscale.fdiv(series[adapter][maturity].maxscale) >= zShare) {
tBal = (uBal * zShare) / series[adapter][maturity].mscale;
} else {
tBal = uBal.fdiv(series[adapter][maturity].maxscale);
}
if (!level.redeemHookDisabled()) {
Adapter(adapter).onRedeem(uBal, series[adapter][maturity].mscale, series[adapter][maturity].maxscale, tBal);
}
ERC20(Adapter(adapter).target()).safeTransferFrom(adapter, msg.sender, tBal);
emit PTRedeemed(adapter, maturity, tBal);
}
function collect(
address usr,
address adapter,
uint256 maturity,
uint256 uBalTransfer,
address to
) external nonReentrant onlyYT(adapter, maturity) whenNotPaused returns (uint256 collected) {
uint256 uBal = YT(msg.sender).balanceOf(usr);
return _collect(usr, adapter, maturity, uBal, uBalTransfer > 0 ? uBalTransfer : uBal, to);
}
function _collect(
address usr,
address adapter,
uint256 maturity,
uint256 uBal,
uint256 uBalTransfer,
address to
) internal returns (uint256 collected) {
if (!_exists(adapter, maturity)) revert Errors.SeriesDoesNotExist();
if (!adapterMeta[adapter].enabled && !_settled(adapter, maturity)) revert Errors.InvalidAdapter();
Series memory _series = series[adapter][maturity];
uint256 lscale = lscales[adapter][maturity][usr];
uint256 level = adapterMeta[adapter].level;
if (level.collectDisabled()) {
if (_settled(adapter, maturity)) {
lscale = series[adapter][maturity].iscale;
} else {
return 0;
}
}
if (_settled(adapter, maturity)) {
_redeemYT(usr, adapter, maturity, uBal);
} else {
if (block.timestamp > maturity + SPONSOR_WINDOW) {
revert Errors.CollectNotSettled();
} else {
uint256 cscale = Adapter(adapter).scale();
if (cscale > _series.maxscale) {
_series.maxscale = cscale;
lscales[adapter][maturity][usr] = cscale;
} else {
lscales[adapter][maturity][usr] = _series.maxscale;
}
}
}
uint256 tBalNow = uBal.fdivUp(_series.maxscale);
uint256 tBalPrev = uBal.fdiv(lscale);
unchecked {
collected = tBalPrev > tBalNow ? tBalPrev - tBalNow : 0;
}
ERC20(Adapter(adapter).target()).safeTransferFrom(adapter, usr, collected);
Adapter(adapter).notify(usr, collected, false);
if (to != address(0)) {
uint256 ytBal = YT(_series.yt).balanceOf(to);
lscales[adapter][maturity][to] = ytBal > 0
? _reweightLScale(adapter, maturity, ytBal, uBalTransfer, to, _series.maxscale)
: _series.maxscale;
uint256 tBalTransfer = uBalTransfer.fdiv(_series.maxscale);
Adapter(adapter).notify(usr, tBalTransfer, false);
Adapter(adapter).notify(to, tBalTransfer, true);
}
series[adapter][maturity] = _series;
emit Collected(adapter, maturity, collected);
}
function _reweightLScale(
address adapter,
uint256 maturity,
uint256 ytBal,
uint256 uBal,
address receiver,
uint256 scale
) internal view returns (uint256) {
return (ytBal + uBal).fdiv((ytBal.fdiv(lscales[adapter][maturity][receiver]) + uBal.fdiv(scale)));
}
function _redeemYT(
address usr,
address adapter,
uint256 maturity,
uint256 uBal
) internal {
YT(series[adapter][maturity].yt).burn(usr, uBal);
uint256 tBal = 0;
uint256 zShare = FixedMath.WAD - series[adapter][maturity].tilt;
if (series[adapter][maturity].mscale.fdiv(series[adapter][maturity].maxscale) >= zShare) {
tBal = uBal.fdiv(series[adapter][maturity].maxscale) - (uBal * zShare) / series[adapter][maturity].mscale;
ERC20(Adapter(adapter).target()).safeTransferFrom(adapter, usr, tBal);
}
Adapter(adapter).notify(usr, uBal.fdivUp(series[adapter][maturity].maxscale), false);
emit YTRedeemed(adapter, maturity, tBal);
}
function setAdapter(address adapter, bool isOn) public requiresTrust {
_setAdapter(adapter, isOn);
}
function setGuard(address adapter, uint256 cap) external requiresTrust {
adapterMeta[adapter].guard = cap;
emit GuardChanged(adapter, cap);
}
function setGuarded(bool _guarded) external requiresTrust {
guarded = _guarded;
emit GuardedChanged(_guarded);
}
function setPeriphery(address _periphery) external requiresTrust {
periphery = _periphery;
emit PeripheryChanged(_periphery);
}
function setPaused(bool _paused) external requiresTrust {
_paused ? _pause() : _unpause();
}
function setPermissionless(bool _permissionless) external requiresTrust {
permissionless = _permissionless;
emit PermissionlessChanged(_permissionless);
}
function backfillScale(
address adapter,
uint256 maturity,
uint256 mscale,
address[] calldata _usrs,
uint256[] calldata _lscales
) external requiresTrust {
if (!_exists(adapter, maturity)) revert Errors.SeriesDoesNotExist();
uint256 cutoff = maturity + SPONSOR_WINDOW + SETTLEMENT_WINDOW;
if (block.timestamp <= cutoff) revert Errors.OutOfWindowBoundaries();
for (uint256 i = 0; i < _usrs.length; i++) {
lscales[adapter][maturity][_usrs[i]] = _lscales[i];
}
if (mscale > 0) {
Series memory _series = series[adapter][maturity];
series[adapter][maturity].mscale = mscale;
if (mscale > _series.maxscale) {
series[adapter][maturity].maxscale = mscale;
}
(address target, address stake, uint256 stakeSize) = Adapter(adapter).getStakeAndTarget();
address stakeDst = adapterMeta[adapter].enabled ? cup : _series.sponsor;
ERC20(target).safeTransferFrom(adapter, cup, _series.reward);
series[adapter][maturity].reward = 0;
ERC20(stake).safeTransferFrom(adapter, stakeDst, stakeSize);
}
emit Backfilled(adapter, maturity, mscale, _usrs, _lscales);
}
function _exists(address adapter, uint256 maturity) internal view returns (bool) {
return series[adapter][maturity].pt != address(0);
}
function _settled(address adapter, uint256 maturity) internal view returns (bool) {
return series[adapter][maturity].mscale > 0;
}
function _canBeSettled(address adapter, uint256 maturity) internal view returns (bool) {
uint256 cutoff = maturity + SPONSOR_WINDOW + SETTLEMENT_WINDOW;
if (msg.sender == series[adapter][maturity].sponsor) {
return maturity - SPONSOR_WINDOW <= block.timestamp && cutoff >= block.timestamp;
} else {
return maturity + SPONSOR_WINDOW < block.timestamp && cutoff >= block.timestamp;
}
}
function _isValid(address adapter, uint256 maturity) internal view returns (bool) {
(uint256 minm, uint256 maxm) = Adapter(adapter).getMaturityBounds();
if (maturity < block.timestamp + minm || maturity > block.timestamp + maxm) return false;
(, , uint256 day, uint256 hour, uint256 minute, uint256 second) = DateTime.timestampToDateTime(maturity);
if (hour != 0 || minute != 0 || second != 0) return false;
uint256 mode = Adapter(adapter).mode();
if (mode == 0) {
return day == 1;
}
if (mode == 1) {
return DateTime.getDayOfWeek(maturity) == 1;
}
return false;
}
function _setAdapter(address adapter, bool isOn) internal {
AdapterMeta memory am = adapterMeta[adapter];
if (am.enabled == isOn) revert Errors.ExistingValue();
am.enabled = isOn;
if (isOn && am.id == 0) {
am.id = ++adapterCounter;
adapterAddresses[am.id] = adapter;
}
am.level = uint248(Adapter(adapter).level());
adapterMeta[adapter] = am;
emit AdapterChanged(adapter, am.id, isOn);
}
function pt(address adapter, uint256 maturity) public view returns (address) {
return series[adapter][maturity].pt;
}
function yt(address adapter, uint256 maturity) public view returns (address) {
return series[adapter][maturity].yt;
}
function mscale(address adapter, uint256 maturity) public view returns (uint256) {
return series[adapter][maturity].mscale;
}
modifier onlyYT(address adapter, uint256 maturity) {
if (series[adapter][maturity].yt != msg.sender) revert Errors.OnlyYT();
_;
}
event Backfilled(
address indexed adapter,
uint256 indexed maturity,
uint256 mscale,
address[] _usrs,
uint256[] _lscales
);
event GuardChanged(address indexed adapter, uint256 cap);
event AdapterChanged(address indexed adapter, uint256 indexed id, bool indexed isOn);
event PeripheryChanged(address indexed periphery);
event SeriesInitialized(
address adapter,
uint256 indexed maturity,
address pt,
address yt,
address indexed sponsor,
address indexed target
);
event Issued(address indexed adapter, uint256 indexed maturity, uint256 balance, address indexed sender);
event Combined(address indexed adapter, uint256 indexed maturity, uint256 balance, address indexed sender);
event Collected(address indexed adapter, uint256 indexed maturity, uint256 collected);
event SeriesSettled(address indexed adapter, uint256 indexed maturity, address indexed settler);
event PTRedeemed(address indexed adapter, uint256 indexed maturity, uint256 redeemed);
event YTRedeemed(address indexed adapter, uint256 indexed maturity, uint256 redeemed);
event GuardedChanged(bool indexed guarded);
event PermissionlessChanged(bool indexed permissionless);
}
contract TokenHandler is Trust {
address public divider;
constructor() Trust(msg.sender) {}
function init(address _divider) external requiresTrust {
if (divider != address(0)) revert Errors.AlreadyInitialized();
divider = _divider;
}
function deploy(
address adapter,
uint248 id,
uint256 maturity
) external returns (address pt, address yt) {
if (msg.sender != divider) revert Errors.OnlyDivider();
ERC20 target = ERC20(Adapter(adapter).target());
uint8 decimals = target.decimals();
string memory symbol = target.symbol();
(string memory d, string memory m, string memory y) = DateTime.toDateString(maturity);
string memory date = DateTime.format(maturity);
string memory datestring = string(abi.encodePacked(d, "-", m, "-", y));
string memory adapterId = DateTime.uintToString(id);
pt = address(
new Token(
string(abi.encodePacked(date, " ", symbol, " Sense Principal Token, A", adapterId)),
string(abi.encodePacked("sP-", symbol, ":", datestring, ":", adapterId)),
decimals,
divider
)
);
yt = address(
new YT(
adapter,
maturity,
string(abi.encodePacked(date, " ", symbol, " Sense Yield Token, A", adapterId)),
string(abi.encodePacked("sY-", symbol, ":", datestring, ":", adapterId)),
decimals,
divider
)
);
}
}
文件 5 的 16:ERC20.sol
pragma solidity >=0.8.0;
abstract contract ERC20 {
event Transfer(address indexed from, address indexed to, uint256 amount);
event Approval(address indexed owner, address indexed spender, uint256 amount);
string public name;
string public symbol;
uint8 public immutable decimals;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
bytes32 public constant PERMIT_TYPEHASH =
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
uint256 internal immutable INITIAL_CHAIN_ID;
bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;
mapping(address => uint256) public nonces;
constructor(
string memory _name,
string memory _symbol,
uint8 _decimals
) {
name = _name;
symbol = _symbol;
decimals = _decimals;
INITIAL_CHAIN_ID = block.chainid;
INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
}
function approve(address spender, uint256 amount) public virtual returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
function transfer(address to, uint256 amount) public virtual returns (bool) {
balanceOf[msg.sender] -= amount;
unchecked {
balanceOf[to] += amount;
}
emit Transfer(msg.sender, to, amount);
return true;
}
function transferFrom(
address from,
address to,
uint256 amount
) public virtual returns (bool) {
uint256 allowed = allowance[from][msg.sender];
if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - amount;
balanceOf[from] -= amount;
unchecked {
balanceOf[to] += amount;
}
emit Transfer(from, to, amount);
return true;
}
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public virtual {
require(deadline >= block.timestamp, "PERMIT_DEADLINE_EXPIRED");
unchecked {
bytes32 digest = keccak256(
abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR(),
keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, nonces[owner]++, deadline))
)
);
address recoveredAddress = ecrecover(digest, v, r, s);
require(recoveredAddress != address(0) && recoveredAddress == owner, "INVALID_SIGNER");
allowance[recoveredAddress][spender] = value;
}
emit Approval(owner, spender, value);
}
function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
}
function computeDomainSeparator() internal view virtual returns (bytes32) {
return
keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes(name)),
keccak256("1"),
block.chainid,
address(this)
)
);
}
function _mint(address to, uint256 amount) internal virtual {
totalSupply += amount;
unchecked {
balanceOf[to] += amount;
}
emit Transfer(address(0), to, amount);
}
function _burn(address from, uint256 amount) internal virtual {
balanceOf[from] -= amount;
unchecked {
totalSupply -= amount;
}
emit Transfer(from, address(0), amount);
}
}
文件 6 的 16:Errors.sol
pragma solidity >=0.8.4;
library Errors {
error CombineRestricted();
error IssuanceRestricted();
error NotAuthorized();
error OnlyYT();
error OnlyDivider();
error OnlyPeriphery();
error OnlyPermissionless();
error RedeemRestricted();
error Untrusted();
error TokenNotSupported();
error FlashCallbackFailed();
error InvalidMaturityOffsets();
error SenderNotEligible();
error TargetMismatch();
error TargetNotSupported();
error AlreadySettled();
error CollectNotSettled();
error GuardCapReached();
error IssuanceFeeCapExceeded();
error IssueOnSettle();
error NotSettled();
error AlreadyInitialized();
error DuplicateSeries();
error ExistingValue();
error InvalidAdapter();
error InvalidMaturity();
error InvalidParam();
error OutOfWindowBoundaries();
error SeriesDoesNotExist();
error SwapTooSmall();
error TargetParamsNotSet();
error PoolParamsNotSet();
error PTParamsNotSet();
error FactoryNotSupported();
error FlashBorrowFailed();
error FlashUntrustedBorrower();
error FlashUntrustedLoanInitiator();
error UnexpectedSwapAmount();
error AdapterNotSet();
error FailedBecomeAdmin();
error FailedAddTargetMarket();
error FailedToAddPTMarket();
error FailedAddLpMarket();
error OracleNotReady();
error PoolAlreadyDeployed();
error PoolNotDeployed();
error PoolNotSet();
error SeriesNotQueued();
error TargetExists();
error TargetNotInFuse();
error MintFailed();
error RedeemFailed();
error TransferFailed();
}
文件 7 的 16:FixedMath.sol
pragma solidity 0.8.11;
library FixedMath {
uint256 internal constant WAD = 1e18;
uint256 internal constant RAY = 1e27;
function fmul(
uint256 x,
uint256 y,
uint256 baseUnit
) internal pure returns (uint256) {
return mulDivDown(x, y, baseUnit);
}
function fmul(uint256 x, uint256 y) internal pure returns (uint256) {
return mulDivDown(x, y, WAD);
}
function fmulUp(
uint256 x,
uint256 y,
uint256 baseUnit
) internal pure returns (uint256) {
return mulDivUp(x, y, baseUnit);
}
function fmulUp(uint256 x, uint256 y) internal pure returns (uint256) {
return mulDivUp(x, y, WAD);
}
function fdiv(
uint256 x,
uint256 y,
uint256 baseUnit
) internal pure returns (uint256) {
return mulDivDown(x, baseUnit, y);
}
function fdiv(uint256 x, uint256 y) internal pure returns (uint256) {
return mulDivDown(x, WAD, y);
}
function fdivUp(
uint256 x,
uint256 y,
uint256 baseUnit
) internal pure returns (uint256) {
return mulDivUp(x, baseUnit, y);
}
function fdivUp(uint256 x, uint256 y) internal pure returns (uint256) {
return mulDivUp(x, WAD, y);
}
function mulDivDown(
uint256 x,
uint256 y,
uint256 denominator
) internal pure returns (uint256 z) {
assembly {
z := mul(x, y)
if iszero(and(iszero(iszero(denominator)), or(iszero(x), eq(div(z, x), y)))) {
revert(0, 0)
}
z := div(z, denominator)
}
}
function mulDivUp(
uint256 x,
uint256 y,
uint256 denominator
) internal pure returns (uint256 z) {
assembly {
z := mul(x, y)
if iszero(and(iszero(iszero(denominator)), or(iszero(x), eq(div(z, x), y)))) {
revert(0, 0)
}
z := mul(iszero(iszero(z)), add(div(sub(z, 1), denominator), 1))
}
}
}
文件 8 的 16:IERC3156FlashBorrower.sol
pragma solidity ^0.8.0;
interface IERC3156FlashBorrower {
function onFlashLoan(
address initiator,
address token,
uint256 amount,
uint256 fee,
bytes calldata data
) external returns (bytes32);
}
文件 9 的 16:IERC3156FlashLender.sol
pragma solidity ^0.8.0;
import "./IERC3156FlashBorrower.sol";
interface IERC3156FlashLender {
function maxFlashLoan(address token) external view returns (uint256);
function flashFee(address token, uint256 amount) external view returns (uint256);
function flashLoan(
IERC3156FlashBorrower receiver,
address token,
uint256 amount,
bytes calldata data
) external returns (bool);
}
文件 10 的 16:Levels.sol
pragma solidity >=0.7.0;
library Levels {
uint256 private constant _INIT_BIT = 0x1;
uint256 private constant _ISSUE_BIT = 0x2;
uint256 private constant _COMBINE_BIT = 0x4;
uint256 private constant _COLLECT_BIT = 0x8;
uint256 private constant _REDEEM_BIT = 0x10;
uint256 private constant _REDEEM_HOOK_BIT = 0x20;
function initRestricted(uint256 level) internal pure returns (bool) {
return level & _INIT_BIT != _INIT_BIT;
}
function issueRestricted(uint256 level) internal pure returns (bool) {
return level & _ISSUE_BIT != _ISSUE_BIT;
}
function combineRestricted(uint256 level) internal pure returns (bool) {
return level & _COMBINE_BIT != _COMBINE_BIT;
}
function collectDisabled(uint256 level) internal pure returns (bool) {
return level & _COLLECT_BIT != _COLLECT_BIT;
}
function redeemRestricted(uint256 level) internal pure returns (bool) {
return level & _REDEEM_BIT != _REDEEM_BIT;
}
function redeemHookDisabled(uint256 level) internal pure returns (bool) {
return level & _REDEEM_HOOK_BIT != _REDEEM_HOOK_BIT;
}
}
文件 11 的 16:Pausable.sol
pragma solidity ^0.8.0;
import "../utils/Context.sol";
abstract contract Pausable is Context {
event Paused(address account);
event Unpaused(address account);
bool private _paused;
constructor() {
_paused = false;
}
function paused() public view virtual returns (bool) {
return _paused;
}
modifier whenNotPaused() {
require(!paused(), "Pausable: paused");
_;
}
modifier whenPaused() {
require(paused(), "Pausable: not paused");
_;
}
function _pause() internal virtual whenNotPaused {
_paused = true;
emit Paused(_msgSender());
}
function _unpause() internal virtual whenPaused {
_paused = false;
emit Unpaused(_msgSender());
}
}
文件 12 的 16:ReentrancyGuard.sol
pragma solidity >=0.8.0;
abstract contract ReentrancyGuard {
uint256 private reentrancyStatus = 1;
modifier nonReentrant() {
require(reentrancyStatus == 1, "REENTRANCY");
reentrancyStatus = 2;
_;
reentrancyStatus = 1;
}
}
文件 13 的 16:SafeTransferLib.sol
pragma solidity >=0.8.0;
import {ERC20} from "../tokens/ERC20.sol";
library SafeTransferLib {
function safeTransferETH(address to, uint256 amount) internal {
bool callStatus;
assembly {
callStatus := call(gas(), to, amount, 0, 0, 0, 0)
}
require(callStatus, "ETH_TRANSFER_FAILED");
}
function safeTransferFrom(
ERC20 token,
address from,
address to,
uint256 amount
) internal {
bool callStatus;
assembly {
let freeMemoryPointer := mload(0x40)
mstore(freeMemoryPointer, 0x23b872dd00000000000000000000000000000000000000000000000000000000)
mstore(add(freeMemoryPointer, 4), and(from, 0xffffffffffffffffffffffffffffffffffffffff))
mstore(add(freeMemoryPointer, 36), and(to, 0xffffffffffffffffffffffffffffffffffffffff))
mstore(add(freeMemoryPointer, 68), amount)
callStatus := call(gas(), token, 0, freeMemoryPointer, 100, 0, 0)
}
require(didLastOptionalReturnCallSucceed(callStatus), "TRANSFER_FROM_FAILED");
}
function safeTransfer(
ERC20 token,
address to,
uint256 amount
) internal {
bool callStatus;
assembly {
let freeMemoryPointer := mload(0x40)
mstore(freeMemoryPointer, 0xa9059cbb00000000000000000000000000000000000000000000000000000000)
mstore(add(freeMemoryPointer, 4), and(to, 0xffffffffffffffffffffffffffffffffffffffff))
mstore(add(freeMemoryPointer, 36), amount)
callStatus := call(gas(), token, 0, freeMemoryPointer, 68, 0, 0)
}
require(didLastOptionalReturnCallSucceed(callStatus), "TRANSFER_FAILED");
}
function safeApprove(
ERC20 token,
address to,
uint256 amount
) internal {
bool callStatus;
assembly {
let freeMemoryPointer := mload(0x40)
mstore(freeMemoryPointer, 0x095ea7b300000000000000000000000000000000000000000000000000000000)
mstore(add(freeMemoryPointer, 4), and(to, 0xffffffffffffffffffffffffffffffffffffffff))
mstore(add(freeMemoryPointer, 36), amount)
callStatus := call(gas(), token, 0, freeMemoryPointer, 68, 0, 0)
}
require(didLastOptionalReturnCallSucceed(callStatus), "APPROVE_FAILED");
}
function didLastOptionalReturnCallSucceed(bool callStatus) private pure returns (bool success) {
assembly {
let returnDataSize := returndatasize()
if iszero(callStatus) {
returndatacopy(0, 0, returnDataSize)
revert(0, returnDataSize)
}
switch returnDataSize
case 32 {
returndatacopy(0, 0, returnDataSize)
success := iszero(iszero(mload(0)))
}
case 0 {
success := 1
}
default {
success := 0
}
}
}
}
文件 14 的 16:Token.sol
pragma solidity 0.8.11;
import { ERC20 } from "@rari-capital/solmate/src/tokens/ERC20.sol";
import { Trust } from "@sense-finance/v1-utils/src/Trust.sol";
contract Token is ERC20, Trust {
constructor(
string memory _name,
string memory _symbol,
uint8 _decimals,
address _trusted
) ERC20(_name, _symbol, _decimals) Trust(_trusted) {}
function mint(address usr, uint256 amount) public requiresTrust {
_mint(usr, amount);
}
function burn(address usr, uint256 amount) public requiresTrust {
_burn(usr, amount);
}
}
文件 15 的 16:Trust.sol
pragma solidity >=0.7.0;
abstract contract Trust {
event UserTrustUpdated(address indexed user, bool trusted);
mapping(address => bool) public isTrusted;
constructor(address initialUser) {
isTrusted[initialUser] = true;
emit UserTrustUpdated(initialUser, true);
}
function setIsTrusted(address user, bool trusted) public virtual requiresTrust {
isTrusted[user] = trusted;
emit UserTrustUpdated(user, trusted);
}
modifier requiresTrust() {
require(isTrusted[msg.sender], "UNTRUSTED");
_;
}
}
文件 16 的 16:YT.sol
pragma solidity 0.8.11;
import { Divider } from "../Divider.sol";
import { Token } from "./Token.sol";
contract YT is Token {
address public immutable adapter;
address public immutable divider;
uint256 public immutable maturity;
constructor(
address _adapter,
uint256 _maturity,
string memory _name,
string memory _symbol,
uint8 _decimals,
address _divider
) Token(_name, _symbol, _decimals, _divider) {
adapter = _adapter;
maturity = _maturity;
divider = _divider;
}
function collect() external returns (uint256 _collected) {
return Divider(divider).collect(msg.sender, adapter, maturity, 0, address(0));
}
function transfer(address to, uint256 value) public override returns (bool) {
Divider(divider).collect(msg.sender, adapter, maturity, value, to);
return super.transfer(to, value);
}
function transferFrom(
address from,
address to,
uint256 value
) public override returns (bool) {
if (value > 0) Divider(divider).collect(from, adapter, maturity, value, to);
return super.transferFrom(from, to, value);
}
}
{
"compilationTarget": {
"@sense-finance/v1-core/src/tokens/YT.sol": "YT"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 1500
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_adapter","type":"address"},{"internalType":"uint256","name":"_maturity","type":"uint256"},{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"uint8","name":"_decimals","type":"uint8"},{"internalType":"address","name":"_divider","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"bool","name":"trusted","type":"bool"}],"name":"UserTrustUpdated","type":"event"},{"inputs":[],"name":"DOMAIN_SEPARATOR","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"PERMIT_TYPEHASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"adapter","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"usr","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"burn","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"collect","outputs":[{"internalType":"uint256","name":"_collected","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"divider","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"isTrusted","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maturity","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"usr","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"nonces","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"permit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"},{"internalType":"bool","name":"trusted","type":"bool"}],"name":"setIsTrusted","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]