Created
May 24, 2022 15:26
-
-
Save lucas-manuel/0d78e33c8a75554d57958ec3efe136ed to your computer and use it in GitHub Desktop.
Flattened xMPL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: AGPL-3.0-or-later | |
pragma solidity 0.8.7; | |
/// @title Interface of the ERC20 standard as defined in the EIP, including EIP-2612 permit functionality. | |
interface IERC20 { | |
/**************/ | |
/*** Events ***/ | |
/**************/ | |
/** | |
* @dev Emitted when one account has set the allowance of another account over their tokens. | |
* @param owner_ Account that tokens are approved from. | |
* @param spender_ Account that tokens are approved for. | |
* @param amount_ Amount of tokens that have been approved. | |
*/ | |
event Approval(address indexed owner_, address indexed spender_, uint256 amount_); | |
/** | |
* @dev Emitted when tokens have moved from one account to another. | |
* @param owner_ Account that tokens have moved from. | |
* @param recipient_ Account that tokens have moved to. | |
* @param amount_ Amount of tokens that have been transferred. | |
*/ | |
event Transfer(address indexed owner_, address indexed recipient_, uint256 amount_); | |
/**************************/ | |
/*** External Functions ***/ | |
/**************************/ | |
/** | |
* @dev Function that allows one account to set the allowance of another account over their tokens. | |
* Emits an {Approval} event. | |
* @param spender_ Account that tokens are approved for. | |
* @param amount_ Amount of tokens that have been approved. | |
* @return success_ Boolean indicating whether the operation succeeded. | |
*/ | |
function approve(address spender_, uint256 amount_) external returns (bool success_); | |
/** | |
* @dev Function that allows one account to decrease the allowance of another account over their tokens. | |
* Emits an {Approval} event. | |
* @param spender_ Account that tokens are approved for. | |
* @param subtractedAmount_ Amount to decrease approval by. | |
* @return success_ Boolean indicating whether the operation succeeded. | |
*/ | |
function decreaseAllowance(address spender_, uint256 subtractedAmount_) external returns (bool success_); | |
/** | |
* @dev Function that allows one account to increase the allowance of another account over their tokens. | |
* Emits an {Approval} event. | |
* @param spender_ Account that tokens are approved for. | |
* @param addedAmount_ Amount to increase approval by. | |
* @return success_ Boolean indicating whether the operation succeeded. | |
*/ | |
function increaseAllowance(address spender_, uint256 addedAmount_) external returns (bool success_); | |
/** | |
* @dev Approve by signature. | |
* @param owner_ Owner address that signed the permit. | |
* @param spender_ Spender of the permit. | |
* @param amount_ Permit approval spend limit. | |
* @param deadline_ Deadline after which the permit is invalid. | |
* @param v_ ECDSA signature v component. | |
* @param r_ ECDSA signature r component. | |
* @param s_ ECDSA signature s component. | |
*/ | |
function permit(address owner_, address spender_, uint amount_, uint deadline_, uint8 v_, bytes32 r_, bytes32 s_) external; | |
/** | |
* @dev Moves an amount of tokens from `msg.sender` to a specified account. | |
* Emits a {Transfer} event. | |
* @param recipient_ Account that receives tokens. | |
* @param amount_ Amount of tokens that are transferred. | |
* @return success_ Boolean indicating whether the operation succeeded. | |
*/ | |
function transfer(address recipient_, uint256 amount_) external returns (bool success_); | |
/** | |
* @dev Moves a pre-approved amount of tokens from a sender to a specified account. | |
* Emits a {Transfer} event. | |
* Emits an {Approval} event. | |
* @param owner_ Account that tokens are moving from. | |
* @param recipient_ Account that receives tokens. | |
* @param amount_ Amount of tokens that are transferred. | |
* @return success_ Boolean indicating whether the operation succeeded. | |
*/ | |
function transferFrom(address owner_, address recipient_, uint256 amount_) external returns (bool success_); | |
/**********************/ | |
/*** View Functions ***/ | |
/**********************/ | |
/** | |
* @dev Returns the allowance that one account has given another over their tokens. | |
* @param owner_ Account that tokens are approved from. | |
* @param spender_ Account that tokens are approved for. | |
* @return allowance_ Allowance that one account has given another over their tokens. | |
*/ | |
function allowance(address owner_, address spender_) external view returns (uint256 allowance_); | |
/** | |
* @dev Returns the amount of tokens owned by a given account. | |
* @param account_ Account that owns the tokens. | |
* @return balance_ Amount of tokens owned by a given account. | |
*/ | |
function balanceOf(address account_) external view returns (uint256 balance_); | |
/** | |
* @dev Returns the decimal precision used by the token. | |
* @return decimals_ The decimal precision used by the token. | |
*/ | |
function decimals() external view returns (uint8 decimals_); | |
/** | |
* @dev Returns the signature domain separator. | |
* @return domainSeparator_ The signature domain separator. | |
*/ | |
function DOMAIN_SEPARATOR() external view returns (bytes32 domainSeparator_); | |
/** | |
* @dev Returns the name of the token. | |
* @return name_ The name of the token. | |
*/ | |
function name() external view returns (string memory name_); | |
/** | |
* @dev Returns the nonce for the given owner. | |
* @param owner_ The address of the owner account. | |
* @return nonce_ The nonce for the given owner. | |
*/ | |
function nonces(address owner_) external view returns (uint256 nonce_); | |
/** | |
* @dev Returns the permit type hash. | |
* @return permitTypehash_ The permit type hash. | |
*/ | |
function PERMIT_TYPEHASH() external view returns (bytes32 permitTypehash_); | |
/** | |
* @dev Returns the symbol of the token. | |
* @return symbol_ The symbol of the token. | |
*/ | |
function symbol() external view returns (string memory symbol_); | |
/** | |
* @dev Returns the total amount of tokens in existence. | |
* @return totalSupply_ The total amount of tokens in existence. | |
*/ | |
function totalSupply() external view returns (uint256 totalSupply_); | |
} | |
/** | |
* @title Small Library to standardize erc20 token interactions. | |
*/ | |
library ERC20Helper { | |
/**************************/ | |
/*** Internal Functions ***/ | |
/**************************/ | |
function transfer(address token_, address to_, uint256 amount_) internal returns (bool success_) { | |
return _call(token_, abi.encodeWithSelector(IERC20.transfer.selector, to_, amount_)); | |
} | |
function transferFrom(address token_, address from_, address to_, uint256 amount_) internal returns (bool success_) { | |
return _call(token_, abi.encodeWithSelector(IERC20.transferFrom.selector, from_, to_, amount_)); | |
} | |
function approve(address token_, address spender_, uint256 amount_) internal returns (bool success_) { | |
// If setting approval to zero fails, return false. | |
if (!_call(token_, abi.encodeWithSelector(IERC20.approve.selector, spender_, uint256(0)))) return false; | |
// If `amount_` is zero, return true as the previous step already did this. | |
if (amount_ == uint256(0)) return true; | |
// Return the result of setting the approval to `amount_`. | |
return _call(token_, abi.encodeWithSelector(IERC20.approve.selector, spender_, amount_)); | |
} | |
function _call(address token_, bytes memory data_) private returns (bool success_) { | |
if (token_.code.length == uint256(0)) return false; | |
bytes memory returnData; | |
( success_, returnData ) = token_.call(data_); | |
return success_ && (returnData.length == uint256(0) || abi.decode(returnData, (bool))); | |
} | |
} | |
contract Migrator { | |
address public immutable newToken; | |
address public immutable oldToken; | |
constructor(address oldToken_, address newToken_) { | |
require(IERC20(newToken_).decimals() == IERC20(oldToken_).decimals(), "M:C:DECIMAL_MISMATCH"); | |
oldToken = oldToken_; | |
newToken = newToken_; | |
} | |
function migrate(uint256 amount_) external { | |
migrate(msg.sender, amount_); | |
} | |
function migrate(address owner_, uint256 amount_) public { | |
require(amount_ != uint256(0), "M:M:ZERO_AMOUNT"); | |
require(ERC20Helper.transferFrom(oldToken, owner_, address(this), amount_), "M:M:TRANSFER_FROM_FAILED"); | |
require(ERC20Helper.transfer(newToken, owner_, amount_), "M:M:TRANSFER_FAILED"); | |
} | |
} | |
/* | |
███████╗██████╗ ██████╗ ██████╗ ██████╗ | |
██╔════╝██╔══██╗██╔════╝ ╚════██╗██╔═████╗ | |
█████╗ ██████╔╝██║ █████╔╝██║██╔██║ | |
██╔══╝ ██╔══██╗██║ ██╔═══╝ ████╔╝██║ | |
███████╗██║ ██║╚██████╗ ███████╗╚██████╔╝ | |
╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝ ╚═════╝ | |
*/ | |
/** | |
* @title Modern ERC-20 implementation. | |
* @dev Acknowledgements to Solmate, OpenZeppelin, and DSS for inspiring this code. | |
*/ | |
contract ERC20 is IERC20 { | |
/**************/ | |
/*** ERC-20 ***/ | |
/**************/ | |
string public override name; | |
string public override symbol; | |
uint8 public immutable override decimals; | |
uint256 public override totalSupply; | |
mapping(address => uint256) public override balanceOf; | |
mapping(address => mapping(address => uint256)) public override allowance; | |
/****************/ | |
/*** ERC-2612 ***/ | |
/****************/ | |
// PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); | |
bytes32 public constant override PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9; | |
mapping(address => uint256) public override nonces; | |
/** | |
* @param name_ The name of the token. | |
* @param symbol_ The symbol of the token. | |
* @param decimals_ The decimal precision used by the token. | |
*/ | |
constructor(string memory name_, string memory symbol_, uint8 decimals_) { | |
name = name_; | |
symbol = symbol_; | |
decimals = decimals_; | |
} | |
/**************************/ | |
/*** External Functions ***/ | |
/**************************/ | |
function approve(address spender_, uint256 amount_) external override returns (bool success_) { | |
_approve(msg.sender, spender_, amount_); | |
return true; | |
} | |
function decreaseAllowance(address spender_, uint256 subtractedAmount_) external override returns (bool success_) { | |
_decreaseAllowance(msg.sender, spender_, subtractedAmount_); | |
return true; | |
} | |
function increaseAllowance(address spender_, uint256 addedAmount_) external override returns (bool success_) { | |
_approve(msg.sender, spender_, allowance[msg.sender][spender_] + addedAmount_); | |
return true; | |
} | |
function permit(address owner_, address spender_, uint256 amount_, uint256 deadline_, uint8 v_, bytes32 r_, bytes32 s_) external override { | |
require(deadline_ >= block.timestamp, "ERC20:P:EXPIRED"); | |
// Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines | |
// the valid range for s in (301): 0 < s < secp256k1n ÷ 2 + 1, and for v in (302): v ∈ {27, 28}. | |
require( | |
uint256(s_) <= uint256(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) && | |
(v_ == 27 || v_ == 28), | |
"ERC20:P:MALLEABLE" | |
); | |
// Nonce realistically cannot overflow. | |
unchecked { | |
bytes32 digest = keccak256( | |
abi.encodePacked( | |
"\x19\x01", | |
DOMAIN_SEPARATOR(), | |
keccak256(abi.encode(PERMIT_TYPEHASH, owner_, spender_, amount_, nonces[owner_]++, deadline_)) | |
) | |
); | |
address recoveredAddress = ecrecover(digest, v_, r_, s_); | |
require(recoveredAddress == owner_ && owner_ != address(0), "ERC20:P:INVALID_SIGNATURE"); | |
} | |
_approve(owner_, spender_, amount_); | |
} | |
function transfer(address recipient_, uint256 amount_) external override returns (bool success_) { | |
_transfer(msg.sender, recipient_, amount_); | |
return true; | |
} | |
function transferFrom(address owner_, address recipient_, uint256 amount_) external override returns (bool success_) { | |
_decreaseAllowance(owner_, msg.sender, amount_); | |
_transfer(owner_, recipient_, amount_); | |
return true; | |
} | |
/**********************/ | |
/*** View Functions ***/ | |
/**********************/ | |
function DOMAIN_SEPARATOR() public view override returns (bytes32 domainSeparator_) { | |
return keccak256( | |
abi.encode( | |
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"), | |
keccak256(bytes(name)), | |
keccak256(bytes("1")), | |
block.chainid, | |
address(this) | |
) | |
); | |
} | |
/**************************/ | |
/*** Internal Functions ***/ | |
/**************************/ | |
function _approve(address owner_, address spender_, uint256 amount_) internal { | |
emit Approval(owner_, spender_, allowance[owner_][spender_] = amount_); | |
} | |
function _burn(address owner_, uint256 amount_) internal { | |
balanceOf[owner_] -= amount_; | |
// Cannot underflow because a user's balance will never be larger than the total supply. | |
unchecked { totalSupply -= amount_; } | |
emit Transfer(owner_, address(0), amount_); | |
} | |
function _decreaseAllowance(address owner_, address spender_, uint256 subtractedAmount_) internal { | |
uint256 spenderAllowance = allowance[owner_][spender_]; // Cache to memory. | |
if (spenderAllowance != type(uint256).max) { | |
_approve(owner_, spender_, spenderAllowance - subtractedAmount_); | |
} | |
} | |
function _mint(address recipient_, uint256 amount_) internal { | |
totalSupply += amount_; | |
// Cannot overflow because totalSupply would first overflow in the statement above. | |
unchecked { balanceOf[recipient_] += amount_; } | |
emit Transfer(address(0), recipient_, amount_); | |
} | |
function _transfer(address owner_, address recipient_, uint256 amount_) internal { | |
balanceOf[owner_] -= amount_; | |
// Cannot overflow because minting prevents overflow of totalSupply, and sum of user balances == totalSupply. | |
unchecked { balanceOf[recipient_] += amount_; } | |
emit Transfer(owner_, recipient_, amount_); | |
} | |
} | |
/// @title A standard for tokenized Vaults with a single underlying ERC-20 token. | |
interface IERC4626 is IERC20 { | |
/**************/ | |
/*** Events ***/ | |
/**************/ | |
/** | |
* @dev `caller_` has exchanged `assets_` for `shares_` and transferred them to `owner_`. | |
* MUST be emitted when assets are deposited via the `deposit` or `mint` methods. | |
* @param caller_ The caller of the function that emitted the `Deposit` event. | |
* @param owner_ The owner of the shares. | |
* @param assets_ The amount of assets deposited. | |
* @param shares_ The amount of shares minted. | |
*/ | |
event Deposit(address indexed caller_, address indexed owner_, uint256 assets_, uint256 shares_); | |
/** | |
* @dev `caller_` has exchanged `shares_`, owned by `owner_`, for `assets_`, and transferred them to `receiver_`. | |
* MUST be emitted when assets are withdrawn via the `withdraw` or `redeem` methods. | |
* @param caller_ The caller of the function that emitted the `Withdraw` event. | |
* @param receiver_ The receiver of the assets. | |
* @param owner_ The owner of the shares. | |
* @param assets_ The amount of assets withdrawn. | |
* @param shares_ The amount of shares burned. | |
*/ | |
event Withdraw(address indexed caller_, address indexed receiver_, address indexed owner_, uint256 assets_, uint256 shares_); | |
/***********************/ | |
/*** State Variables ***/ | |
/***********************/ | |
/** | |
* @dev The address of the underlying asset used by the Vault. | |
* MUST be a contract that implements the ERC-20 standard. | |
* MUST NOT revert. | |
* @return asset_ The address of the underlying asset. | |
*/ | |
function asset() external view returns (address asset_); | |
/********************************/ | |
/*** State Changing Functions ***/ | |
/********************************/ | |
/** | |
* @dev Mints `shares_` to `receiver_` by depositing `assets_` into the Vault. | |
* MUST emit the {Deposit} event. | |
* MUST revert if all of the assets cannot be deposited (due to insufficient approval, deposit limits, slippage, etc). | |
* @param assets_ The amount of assets to deposit. | |
* @param receiver_ The receiver of the shares. | |
* @return shares_ The amount of shares minted. | |
*/ | |
function deposit(uint256 assets_, address receiver_) external returns (uint256 shares_); | |
/** | |
* @dev Mints `shares_` to `receiver_` by depositing `assets_` into the Vault. | |
* MUST emit the {Deposit} event. | |
* MUST revert if all of shares cannot be minted (due to insufficient approval, deposit limits, slippage, etc). | |
* @param shares_ The amount of shares to mint. | |
* @param receiver_ The receiver of the shares. | |
* @return assets_ The amount of assets deposited. | |
*/ | |
function mint(uint256 shares_, address receiver_) external returns (uint256 assets_); | |
/** | |
* @dev Burns `shares_` from `owner_` and sends `assets_` to `receiver_`. | |
* MUST emit the {Withdraw} event. | |
* MUST revert if all of the shares cannot be redeemed (due to insufficient shares, withdrawal limits, slippage, etc). | |
* @param shares_ The amount of shares to redeem. | |
* @param receiver_ The receiver of the assets. | |
* @param owner_ The owner of the shares. | |
* @return assets_ The amount of assets sent to the receiver. | |
*/ | |
function redeem(uint256 shares_, address receiver_, address owner_) external returns (uint256 assets_); | |
/** | |
* @dev Burns `shares_` from `owner_` and sends `assets_` to `receiver_`. | |
* MUST emit the {Withdraw} event. | |
* MUST revert if all of the assets cannot be withdrawn (due to insufficient assets, withdrawal limits, slippage, etc). | |
* @param assets_ The amount of assets to withdraw. | |
* @param receiver_ The receiver of the assets. | |
* @param owner_ The owner of the assets. | |
* @return shares_ The amount of shares burned from the owner. | |
*/ | |
function withdraw(uint256 assets_, address receiver_, address owner_) external returns (uint256 shares_); | |
/**********************/ | |
/*** View Functions ***/ | |
/**********************/ | |
/** | |
* @dev The amount of `assets_` the `shares_` are currently equivalent to. | |
* MUST NOT be inclusive of any fees that are charged against assets in the Vault. | |
* MUST NOT reflect slippage or other on-chain conditions when performing the actual exchange. | |
* MUST NOT show any variations depending on the caller. | |
* MUST NOT revert. | |
* @param shares_ The amount of shares to convert. | |
* @return assets_ The amount of equivalent assets. | |
*/ | |
function convertToAssets(uint256 shares_) external view returns (uint256 assets_); | |
/** | |
* @dev The amount of `shares_` the `assets_` are currently equivalent to. | |
* MUST NOT be inclusive of any fees that are charged against assets in the Vault. | |
* MUST NOT reflect slippage or other on-chain conditions when performing the actual exchange. | |
* MUST NOT show any variations depending on the caller. | |
* MUST NOT revert. | |
* @param assets_ The amount of assets to convert. | |
* @return shares_ The amount of equivalent shares. | |
*/ | |
function convertToShares(uint256 assets_) external view returns (uint256 shares_); | |
/** | |
* @dev Maximum amount of `assets_` that can be deposited on behalf of the `receiver_` through a `deposit` call. | |
* MUST return a limited value if the receiver is subject to any limits, or the maximum value otherwise. | |
* MUST NOT revert. | |
* @param receiver_ The receiver of the assets. | |
* @return assets_ The maximum amount of assets that can be deposited. | |
*/ | |
function maxDeposit(address receiver_) external view returns (uint256 assets_); | |
/** | |
* @dev Maximum amount of `shares_` that can be minted on behalf of the `receiver_` through a `mint` call. | |
* MUST return a limited value if the receiver is subject to any limits, or the maximum value otherwise. | |
* MUST NOT revert. | |
* @param receiver_ The receiver of the shares. | |
* @return shares_ The maximum amount of shares that can be minted. | |
*/ | |
function maxMint(address receiver_) external view returns (uint256 shares_); | |
/** | |
* @dev Maximum amount of `shares_` that can be redeemed from the `owner_` through a `redeem` call. | |
* MUST return a limited value if the owner is subject to any limits, or the total amount of owned shares otherwise. | |
* MUST NOT revert. | |
* @param owner_ The owner of the shares. | |
* @return shares_ The maximum amount of shares that can be redeemed. | |
*/ | |
function maxRedeem(address owner_) external view returns (uint256 shares_); | |
/** | |
* @dev Maximum amount of `assets_` that can be withdrawn from the `owner_` through a `withdraw` call. | |
* MUST return a limited value if the owner is subject to any limits, or the total amount of owned assets otherwise. | |
* MUST NOT revert. | |
* @param owner_ The owner of the assets. | |
* @return assets_ The maximum amount of assets that can be withdrawn. | |
*/ | |
function maxWithdraw(address owner_) external view returns (uint256 assets_); | |
/** | |
* @dev Allows an on-chain or off-chain user to simulate the effects of their deposit at the current block, given current on-chain conditions. | |
* MUST return as close to and no more than the exact amount of shares that would be minted in a `deposit` call in the same transaction. | |
* MUST NOT account for deposit limits like those returned from `maxDeposit` and should always act as though the deposit would be accepted. | |
* MUST NOT revert. | |
* @param assets_ The amount of assets to deposit. | |
* @return shares_ The amount of shares that would be minted. | |
*/ | |
function previewDeposit(uint256 assets_) external view returns (uint256 shares_); | |
/** | |
* @dev Allows an on-chain or off-chain user to simulate the effects of their mint at the current block, given current on-chain conditions. | |
* MUST return as close to and no fewer than the exact amount of assets that would be deposited in a `mint` call in the same transaction. | |
* MUST NOT account for mint limits like those returned from `maxMint` and should always act as though the minting would be accepted. | |
* MUST NOT revert. | |
* @param shares_ The amount of shares to mint. | |
* @return assets_ The amount of assets that would be deposited. | |
*/ | |
function previewMint(uint256 shares_) external view returns (uint256 assets_); | |
/** | |
* @dev Allows an on-chain or off-chain user to simulate the effects of their redemption at the current block, given current on-chain conditions. | |
* MUST return as close to and no more than the exact amount of assets that would be withdrawn in a `redeem` call in the same transaction. | |
* MUST NOT account for redemption limits like those returned from `maxRedeem` and should always act as though the redemption would be accepted. | |
* MUST NOT revert. | |
* @param shares_ The amount of shares to redeem. | |
* @return assets_ The amount of assets that would be withdrawn. | |
*/ | |
function previewRedeem(uint256 shares_) external view returns (uint256 assets_); | |
/** | |
* @dev Allows an on-chain or off-chain user to simulate the effects of their withdrawal at the current block, given current on-chain conditions. | |
* MUST return as close to and no fewer than the exact amount of shares that would be burned in a `withdraw` call in the same transaction. | |
* MUST NOT account for withdrawal limits like those returned from `maxWithdraw` and should always act as though the withdrawal would be accepted. | |
* MUST NOT revert. | |
* @param assets_ The amount of assets to withdraw. | |
* @return shares_ The amount of shares that would be redeemed. | |
*/ | |
function previewWithdraw(uint256 assets_) external view returns (uint256 shares_); | |
/** | |
* @dev Total amount of the underlying asset that is managed by the Vault. | |
* SHOULD include compounding that occurs from any yields. | |
* MUST NOT revert. | |
* @return totalAssets_ The total amount of assets the Vault manages. | |
*/ | |
function totalAssets() external view returns (uint256 totalAssets_); | |
} | |
/// @title A token that represents ownership of future revenues distributed linearly over time. | |
interface IRevenueDistributionToken is IERC20, IERC4626 { | |
/**************/ | |
/*** Events ***/ | |
/**************/ | |
/** | |
* @dev Issuance parameters have been updated after a `_mint` or `_burn`. | |
* @param freeAssets_ Resulting `freeAssets` (y-intercept) value after accounting update. | |
* @param issuanceRate_ The new issuance rate of `asset` until `vestingPeriodFinish_`. | |
*/ | |
event IssuanceParamsUpdated(uint256 freeAssets_, uint256 issuanceRate_); | |
/** | |
* @dev `newOwner_` has accepted the transferral of RDT ownership from `previousOwner_`. | |
* @param previousOwner_ The previous RDT owner. | |
* @param newOwner_ The new RDT owner. | |
*/ | |
event OwnershipAccepted(address indexed previousOwner_, address indexed newOwner_); | |
/** | |
* @dev `owner_` has set the new pending owner of RDT to `pendingOwner_`. | |
* @param owner_ The current RDT owner. | |
* @param pendingOwner_ The new pending RDT owner. | |
*/ | |
event PendingOwnerSet(address indexed owner_, address indexed pendingOwner_); | |
/** | |
* @dev `owner_` has updated the RDT vesting schedule to end at `vestingPeriodFinish_`. | |
* @param owner_ The current RDT owner. | |
* @param vestingPeriodFinish_ When the unvested balance will finish vesting. | |
*/ | |
event VestingScheduleUpdated(address indexed owner_, uint256 vestingPeriodFinish_); | |
/***********************/ | |
/*** State Variables ***/ | |
/***********************/ | |
/** | |
* @dev The total amount of the underlying asset that is currently unlocked and is not time-dependent. | |
* Analogous to the y-intercept in a linear function. | |
*/ | |
function freeAssets() external view returns (uint256 freeAssets_); | |
/** | |
* @dev The rate of issuance of the vesting schedule that is currently active. | |
* Denominated as the amount of underlying assets vesting per second. | |
*/ | |
function issuanceRate() external view returns (uint256 issuanceRate_); | |
/** | |
* @dev The timestamp of when the linear function was last recalculated. | |
* Analogous to t0 in a linear function. | |
*/ | |
function lastUpdated() external view returns (uint256 lastUpdated_); | |
/** | |
* @dev The address of the account that is allowed to update the vesting schedule. | |
*/ | |
function owner() external view returns (address owner_); | |
/** | |
* @dev The next owner, nominated by the current owner. | |
*/ | |
function pendingOwner() external view returns (address pendingOwner_); | |
/** | |
* @dev The precision at which the issuance rate is measured. | |
*/ | |
function precision() external view returns (uint256 precision_); | |
/** | |
* @dev The end of the current vesting schedule. | |
*/ | |
function vestingPeriodFinish() external view returns (uint256 vestingPeriodFinish_); | |
/********************************/ | |
/*** Administrative Functions ***/ | |
/********************************/ | |
/** | |
* @dev Sets the pending owner as the new owner. | |
* Can be called only by the pending owner, and only after their nomination by the current owner. | |
*/ | |
function acceptOwnership() external; | |
/** | |
* @dev Sets a new address as the pending owner. | |
* @param pendingOwner_ The address of the next potential owner. | |
*/ | |
function setPendingOwner(address pendingOwner_) external; | |
/** | |
* @dev Updates the current vesting formula based on the amount of total unvested funds in the contract and the new `vestingPeriod_`. | |
* @param vestingPeriod_ The amount of time over which all currently unaccounted underlying assets will be vested over. | |
* @return issuanceRate_ The new issuance rate. | |
* @return freeAssets_ The new amount of underlying assets that are unlocked. | |
*/ | |
function updateVestingSchedule(uint256 vestingPeriod_) external returns (uint256 issuanceRate_, uint256 freeAssets_); | |
/************************/ | |
/*** Staker Functions ***/ | |
/************************/ | |
/** | |
* @dev Does a ERC4626 `deposit` with a ERC-2612 `permit`. | |
* @param assets_ The amount of `asset` to deposit. | |
* @param receiver_ The receiver of the shares. | |
* @param deadline_ The timestamp after which the `permit` signature is no longer valid. | |
* @param v_ ECDSA signature v component. | |
* @param r_ ECDSA signature r component. | |
* @param s_ ECDSA signature s component. | |
* @return shares_ The amount of shares minted. | |
*/ | |
function depositWithPermit(uint256 assets_, address receiver_, uint256 deadline_, uint8 v_, bytes32 r_, bytes32 s_) external returns (uint256 shares_); | |
/** | |
* @dev Does a ERC4626 `mint` with a ERC-2612 `permit`. | |
* @param shares_ The amount of `shares` to mint. | |
* @param receiver_ The receiver of the shares. | |
* @param maxAssets_ The maximum amount of assets that can be taken, as per the permit. | |
* @param deadline_ The timestamp after which the `permit` signature is no longer valid. | |
* @param v_ ECDSA signature v component. | |
* @param r_ ECDSA signature r component. | |
* @param s_ ECDSA signature s component. | |
* @return assets_ The amount of shares deposited. | |
*/ | |
function mintWithPermit(uint256 shares_, address receiver_, uint256 maxAssets_, uint256 deadline_, uint8 v_, bytes32 r_, bytes32 s_) external returns (uint256 assets_); | |
/**********************/ | |
/*** View Functions ***/ | |
/**********************/ | |
/** | |
* @dev Returns the amount of underlying assets owned by the specified account. | |
* @param account_ Address of the account. | |
* @return assets_ Amount of assets owned. | |
*/ | |
function balanceOfAssets(address account_) external view returns (uint256 assets_); | |
} | |
/* | |
██████╗ ██████╗ ████████╗ | |
██╔══██╗██╔══██╗╚══██╔══╝ | |
██████╔╝██║ ██║ ██║ | |
██╔══██╗██║ ██║ ██║ | |
██║ ██║██████╔╝ ██║ | |
╚═╝ ╚═╝╚═════╝ ╚═╝ | |
*/ | |
contract RevenueDistributionToken is IRevenueDistributionToken, ERC20 { | |
uint256 public immutable override precision; // Precision of rates, equals max deposit amounts before rounding errors occur | |
address public override asset; // Underlying ERC-20 asset used by ERC-4626 functionality. | |
address public override owner; // Current owner of the contract, able to update the vesting schedule. | |
address public override pendingOwner; // Pending owner of the contract, able to accept ownership. | |
uint256 public override freeAssets; // Amount of assets unlocked regardless of time passed. | |
uint256 public override issuanceRate; // asset/second rate dependent on aggregate vesting schedule. | |
uint256 public override lastUpdated; // Timestamp of when issuance equation was last updated. | |
uint256 public override vestingPeriodFinish; // Timestamp when current vesting schedule ends. | |
uint256 private locked = 1; // Used in reentrancy check. | |
/*****************/ | |
/*** Modifiers ***/ | |
/*****************/ | |
modifier nonReentrant() { | |
require(locked == 1, "RDT:LOCKED"); | |
locked = 2; | |
_; | |
locked = 1; | |
} | |
constructor(string memory name_, string memory symbol_, address owner_, address asset_, uint256 precision_) | |
ERC20(name_, symbol_, ERC20(asset_).decimals()) | |
{ | |
require((owner = owner_) != address(0), "RDT:C:OWNER_ZERO_ADDRESS"); | |
asset = asset_; // Don't need to check zero address as ERC20(asset_).decimals() will fail in ERC20 constructor. | |
precision = precision_; | |
} | |
/********************************/ | |
/*** Administrative Functions ***/ | |
/********************************/ | |
function acceptOwnership() external virtual override { | |
require(msg.sender == pendingOwner, "RDT:AO:NOT_PO"); | |
emit OwnershipAccepted(owner, msg.sender); | |
owner = msg.sender; | |
pendingOwner = address(0); | |
} | |
function setPendingOwner(address pendingOwner_) external virtual override { | |
require(msg.sender == owner, "RDT:SPO:NOT_OWNER"); | |
pendingOwner = pendingOwner_; | |
emit PendingOwnerSet(msg.sender, pendingOwner_); | |
} | |
function updateVestingSchedule(uint256 vestingPeriod_) external virtual override returns (uint256 issuanceRate_, uint256 freeAssets_) { | |
require(msg.sender == owner, "RDT:UVS:NOT_OWNER"); | |
require(totalSupply != 0, "RDT:UVS:ZERO_SUPPLY"); | |
// Update "y-intercept" to reflect current available asset. | |
freeAssets_ = freeAssets = totalAssets(); | |
// Calculate slope. | |
issuanceRate_ = issuanceRate = ((ERC20(asset).balanceOf(address(this)) - freeAssets_) * precision) / vestingPeriod_; | |
// Update timestamp and period finish. | |
vestingPeriodFinish = (lastUpdated = block.timestamp) + vestingPeriod_; | |
emit IssuanceParamsUpdated(freeAssets_, issuanceRate_); | |
emit VestingScheduleUpdated(msg.sender, vestingPeriodFinish); | |
} | |
/************************/ | |
/*** Staker Functions ***/ | |
/************************/ | |
function deposit(uint256 assets_, address receiver_) external virtual override nonReentrant returns (uint256 shares_) { | |
_mint(shares_ = previewDeposit(assets_), assets_, receiver_, msg.sender); | |
} | |
function depositWithPermit( | |
uint256 assets_, | |
address receiver_, | |
uint256 deadline_, | |
uint8 v_, | |
bytes32 r_, | |
bytes32 s_ | |
) | |
external virtual override nonReentrant returns (uint256 shares_) | |
{ | |
ERC20(asset).permit(msg.sender, address(this), assets_, deadline_, v_, r_, s_); | |
_mint(shares_ = previewDeposit(assets_), assets_, receiver_, msg.sender); | |
} | |
function mint(uint256 shares_, address receiver_) external virtual override nonReentrant returns (uint256 assets_) { | |
_mint(shares_, assets_ = previewMint(shares_), receiver_, msg.sender); | |
} | |
function mintWithPermit( | |
uint256 shares_, | |
address receiver_, | |
uint256 maxAssets_, | |
uint256 deadline_, | |
uint8 v_, | |
bytes32 r_, | |
bytes32 s_ | |
) | |
external virtual override nonReentrant returns (uint256 assets_) | |
{ | |
require((assets_ = previewMint(shares_)) <= maxAssets_, "RDT:MWP:INSUFFICIENT_PERMIT"); | |
ERC20(asset).permit(msg.sender, address(this), maxAssets_, deadline_, v_, r_, s_); | |
_mint(shares_, assets_, receiver_, msg.sender); | |
} | |
function redeem(uint256 shares_, address receiver_, address owner_) external virtual override nonReentrant returns (uint256 assets_) { | |
_burn(shares_, assets_ = previewRedeem(shares_), receiver_, owner_, msg.sender); | |
} | |
function withdraw(uint256 assets_, address receiver_, address owner_) external virtual override nonReentrant returns (uint256 shares_) { | |
_burn(shares_ = previewWithdraw(assets_), assets_, receiver_, owner_, msg.sender); | |
} | |
/**************************/ | |
/*** Internal Functions ***/ | |
/**************************/ | |
function _mint(uint256 shares_, uint256 assets_, address receiver_, address caller_) internal { | |
require(receiver_ != address(0), "RDT:M:ZERO_RECEIVER"); | |
require(shares_ != uint256(0), "RDT:M:ZERO_SHARES"); | |
require(assets_ != uint256(0), "RDT:M:ZERO_ASSETS"); | |
_mint(receiver_, shares_); | |
uint256 freeAssetsCache = freeAssets = totalAssets() + assets_; | |
uint256 issuanceRate_ = _updateIssuanceParams(); | |
emit Deposit(caller_, receiver_, assets_, shares_); | |
emit IssuanceParamsUpdated(freeAssetsCache, issuanceRate_); | |
require(ERC20Helper.transferFrom(asset, caller_, address(this), assets_), "RDT:M:TRANSFER_FROM"); | |
} | |
function _burn(uint256 shares_, uint256 assets_, address receiver_, address owner_, address caller_) internal { | |
require(receiver_ != address(0), "RDT:B:ZERO_RECEIVER"); | |
require(shares_ != uint256(0), "RDT:B:ZERO_SHARES"); | |
require(assets_ != uint256(0), "RDT:B:ZERO_ASSETS"); | |
if (caller_ != owner_) { | |
_decreaseAllowance(owner_, caller_, shares_); | |
} | |
_burn(owner_, shares_); | |
uint256 freeAssetsCache = freeAssets = totalAssets() - assets_; | |
uint256 issuanceRate_ = _updateIssuanceParams(); | |
emit Withdraw(caller_, receiver_, owner_, assets_, shares_); | |
emit IssuanceParamsUpdated(freeAssetsCache, issuanceRate_); | |
require(ERC20Helper.transfer(asset, receiver_, assets_), "RDT:B:TRANSFER"); | |
} | |
function _updateIssuanceParams() internal returns (uint256 issuanceRate_) { | |
return issuanceRate = (lastUpdated = block.timestamp) > vestingPeriodFinish ? 0 : issuanceRate; | |
} | |
/**********************/ | |
/*** View Functions ***/ | |
/**********************/ | |
function balanceOfAssets(address account_) public view virtual override returns (uint256 balanceOfAssets_) { | |
return convertToAssets(balanceOf[account_]); | |
} | |
function convertToAssets(uint256 shares_) public view virtual override returns (uint256 assets_) { | |
uint256 supply = totalSupply; // Cache to stack. | |
assets_ = supply == 0 ? shares_ : (shares_ * totalAssets()) / supply; | |
} | |
function convertToShares(uint256 assets_) public view virtual override returns (uint256 shares_) { | |
uint256 supply = totalSupply; // Cache to stack. | |
shares_ = supply == 0 ? assets_ : (assets_ * supply) / totalAssets(); | |
} | |
function maxDeposit(address receiver_) external pure virtual override returns (uint256 maxAssets_) { | |
receiver_; // Silence warning | |
maxAssets_ = type(uint256).max; | |
} | |
function maxMint(address receiver_) external pure virtual override returns (uint256 maxShares_) { | |
receiver_; // Silence warning | |
maxShares_ = type(uint256).max; | |
} | |
function maxRedeem(address owner_) external view virtual override returns (uint256 maxShares_) { | |
maxShares_ = balanceOf[owner_]; | |
} | |
function maxWithdraw(address owner_) external view virtual override returns (uint256 maxAssets_) { | |
maxAssets_ = balanceOfAssets(owner_); | |
} | |
function previewDeposit(uint256 assets_) public view virtual override returns (uint256 shares_) { | |
// As per https://eips.ethereum.org/EIPS/eip-4626#security-considerations, | |
// it should round DOWN if it’s calculating the amount of shares to issue to a user, given an amount of assets provided. | |
shares_ = convertToShares(assets_); | |
} | |
function previewMint(uint256 shares_) public view virtual override returns (uint256 assets_) { | |
uint256 supply = totalSupply; // Cache to stack. | |
// As per https://eips.ethereum.org/EIPS/eip-4626#security-considerations, | |
// it should round UP if it’s calculating the amount of assets a user must provide, to be issued a given amount of shares. | |
assets_ = supply == 0 ? shares_ : _divRoundUp(shares_ * totalAssets(), supply); | |
} | |
function previewRedeem(uint256 shares_) public view virtual override returns (uint256 assets_) { | |
// As per https://eips.ethereum.org/EIPS/eip-4626#security-considerations, | |
// it should round DOWN if it’s calculating the amount of assets to send to a user, given amount of shares returned. | |
assets_ = convertToAssets(shares_); | |
} | |
function previewWithdraw(uint256 assets_) public view virtual override returns (uint256 shares_) { | |
uint256 supply = totalSupply; // Cache to stack. | |
// As per https://eips.ethereum.org/EIPS/eip-4626#security-considerations, | |
// it should round UP if it’s calculating the amount of shares a user must return, to be sent a given amount of assets. | |
shares_ = supply == 0 ? assets_ : _divRoundUp(assets_ * supply, totalAssets()); | |
} | |
function totalAssets() public view virtual override returns (uint256 totalManagedAssets_) { | |
uint256 issuanceRate_ = issuanceRate; | |
if (issuanceRate_ == 0) return freeAssets; | |
uint256 vestingPeriodFinish_ = vestingPeriodFinish; | |
uint256 lastUpdated_ = lastUpdated; | |
uint256 vestingTimePassed = | |
block.timestamp > vestingPeriodFinish_ ? | |
vestingPeriodFinish_ - lastUpdated_ : | |
block.timestamp - lastUpdated_; | |
return ((issuanceRate_ * vestingTimePassed) / precision) + freeAssets; | |
} | |
/**************************/ | |
/*** Internal Functions ***/ | |
/**************************/ | |
function _divRoundUp(uint256 numerator_, uint256 divisor_) internal pure returns (uint256 result_) { | |
return (numerator_ / divisor_) + (numerator_ % divisor_ > 0 ? 1 : 0); | |
} | |
} | |
interface IxMPL is IRevenueDistributionToken { | |
/**************/ | |
/*** Events ***/ | |
/**************/ | |
/** | |
* @dev Notifies that a scheduled migration was cancelled. | |
*/ | |
event MigrationCancelled(); | |
/** | |
* @dev Notifies that a scheduled migration was executed. | |
* @param fromAsset_ The address of the old asset. | |
* @param toAsset_ The address of new asset migrated to. | |
* @param amount_ The amount of tokens migrated. | |
*/ | |
event MigrationPerformed(address indexed fromAsset_, address indexed toAsset_, uint256 amount_); | |
/** | |
* @dev Notifies that migration was scheduled. | |
* @param fromAsset_ The current asset address. | |
* @param toAsset_ The address of the asset to be migrated to. | |
* @param migrator_ The address of the migrator contract. | |
* @param migrationTime_ The earliest time the migration is scheduled for. | |
*/ | |
event MigrationScheduled(address indexed fromAsset_, address indexed toAsset_, address indexed migrator_, uint256 migrationTime_); | |
/********************************/ | |
/*** Administrative Functions ***/ | |
/********************************/ | |
/** | |
* @dev Cancel the scheduled migration | |
*/ | |
function cancelMigration() external; | |
/** | |
* @dev Perform a migration of the asset. | |
*/ | |
function performMigration() external; | |
/** | |
* @dev Schedule a migration to be executed after a delay. | |
* @param migrator_ The address of the migrator contract. | |
* @param newAsset_ The address of the new asset token. | |
*/ | |
function scheduleMigration(address migrator_, address newAsset_) external; | |
/**********************/ | |
/*** View Functions ***/ | |
/**********************/ | |
/** | |
* @dev Get the minimum delay that a scheduled transaction needs in order to be executed. | |
* @return minimumMigrationDelay_ The delay in seconds. | |
*/ | |
function MINIMUM_MIGRATION_DELAY() external pure returns (uint256 minimumMigrationDelay_); | |
/** | |
* @dev Get the timestamp that a migration is scheduled for. | |
* @return scheduledMigrationTimestamp_ The timestamp of the migration. | |
*/ | |
function scheduledMigrationTimestamp() external view returns (uint256 scheduledMigrationTimestamp_); | |
/** | |
* @dev The address of the migrator contract to be used during the scheduled migration. | |
* @return scheduledMigrator_ The address of the migrator. | |
*/ | |
function scheduledMigrator() external view returns (address scheduledMigrator_); | |
/** | |
* @dev The address of the new asset token to be migrated to during the scheduled migration. | |
* @return scheduledNewAsset_ The address of the new asset token. | |
*/ | |
function scheduledNewAsset() external view returns (address scheduledNewAsset_); | |
} | |
/* | |
██╗ ██╗███╗ ███╗██████╗ ██╗ | |
╚██╗██╔╝████╗ ████║██╔══██╗██║ | |
╚███╔╝ ██╔████╔██║██████╔╝██║ | |
██╔██╗ ██║╚██╔╝██║██╔═══╝ ██║ | |
██╔╝ ██╗██║ ╚═╝ ██║██║ ███████╗ | |
╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚══════╝ | |
*/ | |
contract xMPL is IxMPL, RevenueDistributionToken { | |
uint256 public constant override MINIMUM_MIGRATION_DELAY = 10 days; | |
address public override scheduledMigrator; | |
address public override scheduledNewAsset; | |
uint256 public override scheduledMigrationTimestamp; | |
constructor(string memory name_, string memory symbol_, address owner_, address asset_, uint256 precision_) | |
RevenueDistributionToken(name_, symbol_, owner_, asset_, precision_) { } | |
/*****************/ | |
/*** Modifiers ***/ | |
/*****************/ | |
modifier onlyOwner { | |
require(msg.sender == owner, "xMPL:NOT_OWNER"); | |
_; | |
} | |
/********************************/ | |
/*** Administrative Functions ***/ | |
/********************************/ | |
function cancelMigration() external override onlyOwner { | |
require(scheduledMigrationTimestamp != 0, "xMPL:CM:NOT_SCHEDULED"); | |
_cleanupMigration(); | |
emit MigrationCancelled(); | |
} | |
function performMigration() external override onlyOwner { | |
uint256 migrationTimestamp = scheduledMigrationTimestamp; | |
address migrator = scheduledMigrator; | |
address oldAsset = asset; | |
address newAsset = scheduledNewAsset; | |
require(migrationTimestamp != 0, "xMPL:PM:NOT_SCHEDULED"); | |
require(block.timestamp >= migrationTimestamp, "xMPL:PM:TOO_EARLY"); | |
uint256 oldAssetBalanceBeforeMigration = ERC20(oldAsset).balanceOf(address(this)); | |
uint256 newAssetBalanceBeforeMigration = ERC20(newAsset).balanceOf(address(this)); | |
require(ERC20(oldAsset).approve(migrator, oldAssetBalanceBeforeMigration), "xMPL:PM:APPROVAL_FAILED"); | |
Migrator(migrator).migrate(oldAssetBalanceBeforeMigration); | |
require(ERC20(newAsset).balanceOf(address(this)) - newAssetBalanceBeforeMigration == oldAssetBalanceBeforeMigration, "xMPL:PM:WRONG_AMOUNT"); | |
emit MigrationPerformed(oldAsset, newAsset, oldAssetBalanceBeforeMigration); | |
asset = newAsset; | |
_cleanupMigration(); | |
} | |
function scheduleMigration(address migrator_, address newAsset_) external override onlyOwner { | |
require(migrator_ != address(0), "xMPL:SM:INVALID_MIGRATOR"); | |
require(newAsset_ != address(0), "xMPL:SM:INVALID_NEW_ASSET"); | |
scheduledMigrationTimestamp = block.timestamp + MINIMUM_MIGRATION_DELAY; | |
scheduledMigrator = migrator_; | |
scheduledNewAsset = newAsset_; | |
emit MigrationScheduled(asset, newAsset_, migrator_, scheduledMigrationTimestamp); | |
} | |
/*************************/ | |
/*** Utility Functions ***/ | |
/*************************/ | |
function _cleanupMigration() internal { | |
delete scheduledMigrationTimestamp; | |
delete scheduledMigrator; | |
delete scheduledNewAsset; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment