Verified contractExactBUSL-1.1
CrossChainMasterStrategy
Address0x2567fc741c6b9efe589f2583e4431ac4e69d8b8d
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.28
- Same code
Only this contract
Details
- Compiler
- solc 0.8.28+commit.7893614a
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- paris
- License
- BUSL-1.1
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 24,427,345
- Creation transaction
- 0xf1d87c5d5a…877ee76512
19 files
- contracts/strategies/crosschain/CrossChainMasterStrategy.solmain13.03 KB
- @openzeppelin/contracts/token/ERC20/IERC20.sol2.78 KB
- @openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol683 B
- @openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol3.92 KB
- @openzeppelin/contracts/utils/Address.sol7.91 KB
- @openzeppelin/contracts/utils/math/SafeCast.sol7.82 KB
- contracts/governance/Governable.sol5.54 KB
- contracts/interfaces/IBasicToken.sol200 B
- contracts/interfaces/IStrategy.sol1.72 KB
- contracts/interfaces/IVault.sol6.85 KB
- contracts/interfaces/cctp/ICCTP.sol2.55 KB
- contracts/strategies/crosschain/AbstractCCTPIntegrator.sol23.8 KB
- contracts/strategies/crosschain/CrossChainStrategyHelper.sol8.56 KB
- contracts/token/OUSD.sol26.28 KB
- contracts/utils/BytesHelper.sol3.53 KB
- contracts/utils/Helpers.sol1.13 KB
- contracts/utils/Initializable.sol998 B
- contracts/utils/InitializableAbstractStrategy.sol11.97 KB
- contracts/vault/VaultStorage.sol9.49 KB
contracts/strategies/crosschain/CrossChainMasterStrategy.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
/**
* @title OUSD Yearn V3 Master Strategy - the Mainnet part
* @author Origin Protocol Inc
*
* @dev This strategy can only perform 1 deposit or withdrawal at a time. For that
* reason it shouldn't be configured as an asset default strategy.
*/
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { IERC20, InitializableAbstractStrategy } from "../../utils/InitializableAbstractStrategy.sol";
import { AbstractCCTPIntegrator } from "./AbstractCCTPIntegrator.sol";
import { CrossChainStrategyHelper } from "./CrossChainStrategyHelper.sol";
contract CrossChainMasterStrategy is
AbstractCCTPIntegrator,
InitializableAbstractStrategy
{
using SafeERC20 for IERC20;
using CrossChainStrategyHelper for bytes;
/**
* @notice Remote strategy balance
* @dev The remote balance is cached and might not reflect the actual
* real-time balance of the remote strategy.
*/
uint256 public remoteStrategyBalance;
/// @notice Amount that's bridged due to a pending Deposit process
/// but with no acknowledgement from the remote strategy yet
uint256 public pendingAmount;
uint256 internal constant MAX_BALANCE_CHECK_AGE = 1 days;
event RemoteStrategyBalanceUpdated(uint256 balance);
event WithdrawRequested(address indexed asset, uint256 amount);
event WithdrawAllSkipped();
event BalanceCheckIgnored(uint64 nonce, uint256 timestamp, bool isTooOld);
/**
* @param _stratConfig The platform and OToken vault addresses
*/
constructor(
BaseStrategyConfig memory _stratConfig,
CCTPIntegrationConfig memory _cctpConfig
)
InitializableAbstractStrategy(_stratConfig)
AbstractCCTPIntegrator(_cctpConfig)
{
require(
_stratConfig.platformAddress == address(0),
"Invalid platform address"
);
require(
_stratConfig.vaultAddress != address(0),
"Invalid Vault address"
);
}
/**
* @dev Initialize the strategy implementation
* @param _operator Address of the operator
* @param _minFinalityThreshold Minimum finality threshold
* @param _feePremiumBps Fee premium in basis points
*/
function initialize(
address _operator,
uint16 _minFinalityThreshold,
uint16 _feePremiumBps
) external virtual onlyGovernor initializer {
_initialize(_operator, _minFinalityThreshold, _feePremiumBps);
address[] memory rewardTokens = new address[](0);
address[] memory assets = new address[](0);
address[] memory pTokens = new address[](0);
InitializableAbstractStrategy._initialize(
rewardTokens,
assets,
pTokens
);
}
/// @inheritdoc InitializableAbstractStrategy
function deposit(address _asset, uint256 _amount)
external
override
onlyVault
nonReentrant
{
_deposit(_asset, _amount);
}
/// @inheritdoc InitializableAbstractStrategy
function depositAll() external override onlyVault nonReentrant {
uint256 balance = IERC20(usdcToken).balanceOf(address(this));
// Deposit if balance is greater than 1 USDC
if (balance >= MIN_TRANSFER_AMOUNT) {
_deposit(usdcToken, balance);
}
}
/// @inheritdoc InitializableAbstractStrategy
function withdraw(
address _recipient,
address _asset,
uint256 _amount
) external override onlyVault nonReentrant {
require(_recipient == vaultAddress, "Only Vault can withdraw");
_withdraw(_asset, _amount);
}
/// @inheritdoc InitializableAbstractStrategy
function withdrawAll() external override onlyVaultOrGovernor nonReentrant {
if (isTransferPending()) {
// Do nothing if there is a pending transfer
// Note: We never want withdrawAll to fail, so
// emit an event to indicate that the withdrawal was skipped
emit WithdrawAllSkipped();
return;
}
// Withdraw everything in Remote strategy
uint256 _remoteBalance = remoteStrategyBalance;
if (_remoteBalance < MIN_TRANSFER_AMOUNT) {
// Do nothing if there is less than 1 USDC in the Remote strategy
return;
}
_withdraw(
usdcToken,
_remoteBalance > MAX_TRANSFER_AMOUNT
? MAX_TRANSFER_AMOUNT
: _remoteBalance
);
}
/**
* @notice Check the balance of the strategy that includes
* the balance of the asset on this contract,
* the amount of the asset being bridged,
* and the balance reported by the Remote strategy.
* @param _asset Address of the asset to check
* @return balance Total balance of the asset
*/
function checkBalance(address _asset)
public
view
override
returns (uint256 balance)
{
require(_asset == usdcToken, "Unsupported asset");
// USDC balance on this contract
// + USDC being bridged
// + USDC cached in the corresponding Remote part of this contract
return
IERC20(usdcToken).balanceOf(address(this)) +
pendingAmount +
remoteStrategyBalance;
}
/// @inheritdoc InitializableAbstractStrategy
function supportsAsset(address _asset) public view override returns (bool) {
return _asset == usdcToken;
}
/// @inheritdoc InitializableAbstractStrategy
function safeApproveAllTokens()
external
override
onlyGovernor
nonReentrant
{}
/// @inheritdoc InitializableAbstractStrategy
function _abstractSetPToken(address, address) internal override {}
/// @inheritdoc InitializableAbstractStrategy
function collectRewardTokens()
external
override
onlyHarvester
nonReentrant
{}
/// @inheritdoc AbstractCCTPIntegrator
function _onMessageReceived(bytes memory payload) internal override {
if (
payload.getMessageType() ==
CrossChainStrategyHelper.BALANCE_CHECK_MESSAGE
) {
// Received when Remote strategy checks the balance
_processBalanceCheckMessage(payload);
return;
}
revert("Unknown message type");
}
/// @inheritdoc AbstractCCTPIntegrator
function _onTokenReceived(
uint256 tokenAmount,
// solhint-disable-next-line no-unused-vars
uint256 feeExecuted,
bytes memory payload
) internal override {
uint64 _nonce = lastTransferNonce;
// Should be expecting an acknowledgement
require(!isNonceProcessed(_nonce), "Nonce already processed");
// Now relay to the regular flow
// NOTE: Calling _onMessageReceived would mean that we are bypassing a
// few checks that the regular flow does (like sourceDomainID check
// and sender check in `handleReceiveFinalizedMessage`). However,
// CCTPMessageRelayer relays the message first (which will go through
// all the checks) and not update balance and then finally calls this
// `_onTokenReceived` which will update the balance.
// So, if any of the checks fail during the first no-balance-update flow,
// this won't happen either, since the tx would revert.
_onMessageReceived(payload);
// Send any tokens in the contract to the Vault
uint256 usdcBalance = IERC20(usdcToken).balanceOf(address(this));
// Should always have enough tokens
require(usdcBalance >= tokenAmount, "Insufficient balance");
// Transfer all tokens to the Vault to not leave any dust
IERC20(usdcToken).safeTransfer(vaultAddress, usdcBalance);
// Emit withdrawal amount
emit Withdrawal(usdcToken, usdcToken, usdcBalance);
}
/**
* @dev Bridge and deposit asset into the remote strategy
* @param _asset Address of the asset to deposit
* @param depositAmount Amount of the asset to deposit
*/
function _deposit(address _asset, uint256 depositAmount) internal virtual {
require(_asset == usdcToken, "Unsupported asset");
require(pendingAmount == 0, "Unexpected pending amount");
// Deposit at least 1 USDC
require(
depositAmount >= MIN_TRANSFER_AMOUNT,
"Deposit amount too small"
);
require(
depositAmount <= MAX_TRANSFER_AMOUNT,
"Deposit amount too high"
);
// Get the next nonce
// Note: reverts if a transfer is pending
uint64 nonce = _getNextNonce();
// Set pending amount
pendingAmount = depositAmount;
// Build deposit message payload
bytes memory message = CrossChainStrategyHelper.encodeDepositMessage(
nonce,
depositAmount
);
// Send deposit message to the remote strategy
_sendTokens(depositAmount, message);
// Emit deposit event
emit Deposit(_asset, _asset, depositAmount);
}
/**
* @dev Send a withdraw request to the remote strategy
* @param _asset Address of the asset to withdraw
* @param _amount Amount of the asset to withdraw
*/
function _withdraw(address _asset, uint256 _amount) internal virtual {
require(_asset == usdcToken, "Unsupported asset");
// Withdraw at least 1 USDC
require(_amount >= MIN_TRANSFER_AMOUNT, "Withdraw amount too small");
require(
_amount <= remoteStrategyBalance,
"Withdraw amount exceeds remote strategy balance"
);
require(
_amount <= MAX_TRANSFER_AMOUNT,
"Withdraw amount exceeds max transfer amount"
);
// Get the next nonce
// Note: reverts if a transfer is pending
uint64 nonce = _getNextNonce();
// Build and send withdrawal message with payload
bytes memory message = CrossChainStrategyHelper.encodeWithdrawMessage(
nonce,
_amount
);
_sendMessage(message);
// Emit WithdrawRequested event here,
// Withdraw will be emitted in _onTokenReceived
emit WithdrawRequested(usdcToken, _amount);
}
/**
* @dev Process balance check:
* - Confirms a deposit to the remote strategy
* - Skips balance update if there's a pending withdrawal
* - Updates the remote strategy balance
* @param message The message containing the nonce and balance
*/
function _processBalanceCheckMessage(bytes memory message)
internal
virtual
{
// Decode the message
// When transferConfirmation is true, it means that the message is a result of a deposit or a withdrawal
// process.
(
uint64 nonce,
uint256 balance,
bool transferConfirmation,
uint256 timestamp
) = message.decodeBalanceCheckMessage();
// Get the last cached nonce
uint64 _lastCachedNonce = lastTransferNonce;
if (nonce != _lastCachedNonce) {
// If nonce is not the last cached nonce, it is an outdated message
// Ignore it
return;
}
// A received message nonce not yet processed indicates there is a
// deposit or withdrawal in progress.
bool transferInProgress = isTransferPending();
if (transferInProgress) {
if (transferConfirmation) {
// Apply the effects of the deposit / withdrawal completion
_markNonceAsProcessed(nonce);
pendingAmount = 0;
} else {
// A balanceCheck arrived that is not part of the deposit / withdrawal process
// that has been generated on the Remote contract after the deposit / withdrawal which is
// still pending. This can happen when the CCTP bridge delivers the messages out of order.
// Ignore it, since the pending deposit / withdrawal must first be cofirmed.
emit BalanceCheckIgnored(nonce, timestamp, false);
return;
}
} else {
if (block.timestamp > timestamp + MAX_BALANCE_CHECK_AGE) {
// Balance check is too old, ignore it
emit BalanceCheckIgnored(nonce, timestamp, true);
return;
}
}
// At this point update the strategy balance the balanceCheck message is either:
// - a confirmation of a deposit / withdrawal
// - a message that updates balances when no deposit / withdrawal is in progress
remoteStrategyBalance = balance;
emit RemoteStrategyBalanceUpdated(balance);
}
}
@openzeppelin/contracts/token/ERC20/IERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `recipient`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address recipient, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `sender` to `recipient` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
}
@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)
pragma solidity ^0.8.0;
import "../IERC20.sol";
/**
* @dev Interface for the optional metadata functions from the ERC20 standard.
*
* _Available since v4.1._
*/
interface IERC20Metadata is IERC20 {
/**
* @dev Returns the name of the token.
*/
function name() external view returns (string memory);
/**
* @dev Returns the symbol of the token.
*/
function symbol() external view returns (string memory);
/**
* @dev Returns the decimals places of the token.
*/
function decimals() external view returns (uint8);
}
@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.0;
import "../IERC20.sol";
import "../../../utils/Address.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20 {
using Address 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 Deprecated. This function has issues similar to the ones found in
* {IERC20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/
function safeApprove(
IERC20 token,
address spender,
uint256 value
) internal {
// safeApprove should only be called when setting an initial allowance,
// or when resetting it to zero. To increase and decrease it, use
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
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));
}
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/
function _callOptionalReturn(IERC20 token, bytes memory data) private {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
// Return data is optional
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
@openzeppelin/contracts/utils/Address.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Address.sol)
pragma solidity ^0.8.0;
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*/
function isContract(address account) internal view returns (bool) {
// This method relies on extcodesize, which returns 0 for contracts in
// construction, since the code is only stored at the end of the
// constructor execution.
uint256 size;
assembly {
size := extcodesize(account)
}
return size > 0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/
function functionCallWithValue(
address target,
bytes memory data,
uint256 value
) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
require(isContract(target), "Address: delegate call to non-contract");
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResult(success, returndata, errorMessage);
}
/**
* @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason using the provided one.
*
* _Available since v4.3._
*/
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
@openzeppelin/contracts/utils/math/SafeCast.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/math/SafeCast.sol)
pragma solidity ^0.8.0;
/**
* @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
* checks.
*
* Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
* easily result in undesired exploitation or bugs, since developers usually
* assume that overflows raise errors. `SafeCast` restores this intuition by
* reverting the transaction when such an operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*
* Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing
* all math on `uint256` and `int256` and then downcasting.
*/
library SafeCast {
/**
* @dev Returns the downcasted uint224 from uint256, reverting on
* overflow (when the input is greater than largest uint224).
*
* Counterpart to Solidity's `uint224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*/
function toUint224(uint256 value) internal pure returns (uint224) {
require(value <= type(uint224).max, "SafeCast: value doesn't fit in 224 bits");
return uint224(value);
}
/**
* @dev Returns the downcasted uint128 from uint256, reverting on
* overflow (when the input is greater than largest uint128).
*
* Counterpart to Solidity's `uint128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*/
function toUint128(uint256 value) internal pure returns (uint128) {
require(value <= type(uint128).max, "SafeCast: value doesn't fit in 128 bits");
return uint128(value);
}
/**
* @dev Returns the downcasted uint96 from uint256, reverting on
* overflow (when the input is greater than largest uint96).
*
* Counterpart to Solidity's `uint96` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*/
function toUint96(uint256 value) internal pure returns (uint96) {
require(value <= type(uint96).max, "SafeCast: value doesn't fit in 96 bits");
return uint96(value);
}
/**
* @dev Returns the downcasted uint64 from uint256, reverting on
* overflow (when the input is greater than largest uint64).
*
* Counterpart to Solidity's `uint64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*/
function toUint64(uint256 value) internal pure returns (uint64) {
require(value <= type(uint64).max, "SafeCast: value doesn't fit in 64 bits");
return uint64(value);
}
/**
* @dev Returns the downcasted uint32 from uint256, reverting on
* overflow (when the input is greater than largest uint32).
*
* Counterpart to Solidity's `uint32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*/
function toUint32(uint256 value) internal pure returns (uint32) {
require(value <= type(uint32).max, "SafeCast: value doesn't fit in 32 bits");
return uint32(value);
}
/**
* @dev Returns the downcasted uint16 from uint256, reverting on
* overflow (when the input is greater than largest uint16).
*
* Counterpart to Solidity's `uint16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*/
function toUint16(uint256 value) internal pure returns (uint16) {
require(value <= type(uint16).max, "SafeCast: value doesn't fit in 16 bits");
return uint16(value);
}
/**
* @dev Returns the downcasted uint8 from uint256, reverting on
* overflow (when the input is greater than largest uint8).
*
* Counterpart to Solidity's `uint8` operator.
*
* Requirements:
*
* - input must fit into 8 bits.
*/
function toUint8(uint256 value) internal pure returns (uint8) {
require(value <= type(uint8).max, "SafeCast: value doesn't fit in 8 bits");
return uint8(value);
}
/**
* @dev Converts a signed int256 into an unsigned uint256.
*
* Requirements:
*
* - input must be greater than or equal to 0.
*/
function toUint256(int256 value) internal pure returns (uint256) {
require(value >= 0, "SafeCast: value must be positive");
return uint256(value);
}
/**
* @dev Returns the downcasted int128 from int256, reverting on
* overflow (when the input is less than smallest int128 or
* greater than largest int128).
*
* Counterpart to Solidity's `int128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*
* _Available since v3.1._
*/
function toInt128(int256 value) internal pure returns (int128) {
require(value >= type(int128).min && value <= type(int128).max, "SafeCast: value doesn't fit in 128 bits");
return int128(value);
}
/**
* @dev Returns the downcasted int64 from int256, reverting on
* overflow (when the input is less than smallest int64 or
* greater than largest int64).
*
* Counterpart to Solidity's `int64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*
* _Available since v3.1._
*/
function toInt64(int256 value) internal pure returns (int64) {
require(value >= type(int64).min && value <= type(int64).max, "SafeCast: value doesn't fit in 64 bits");
return int64(value);
}
/**
* @dev Returns the downcasted int32 from int256, reverting on
* overflow (when the input is less than smallest int32 or
* greater than largest int32).
*
* Counterpart to Solidity's `int32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*
* _Available since v3.1._
*/
function toInt32(int256 value) internal pure returns (int32) {
require(value >= type(int32).min && value <= type(int32).max, "SafeCast: value doesn't fit in 32 bits");
return int32(value);
}
/**
* @dev Returns the downcasted int16 from int256, reverting on
* overflow (when the input is less than smallest int16 or
* greater than largest int16).
*
* Counterpart to Solidity's `int16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*
* _Available since v3.1._
*/
function toInt16(int256 value) internal pure returns (int16) {
require(value >= type(int16).min && value <= type(int16).max, "SafeCast: value doesn't fit in 16 bits");
return int16(value);
}
/**
* @dev Returns the downcasted int8 from int256, reverting on
* overflow (when the input is less than smallest int8 or
* greater than largest int8).
*
* Counterpart to Solidity's `int8` operator.
*
* Requirements:
*
* - input must fit into 8 bits.
*
* _Available since v3.1._
*/
function toInt8(int256 value) internal pure returns (int8) {
require(value >= type(int8).min && value <= type(int8).max, "SafeCast: value doesn't fit in 8 bits");
return int8(value);
}
/**
* @dev Converts an unsigned uint256 into a signed int256.
*
* Requirements:
*
* - input must be less than or equal to maxInt256.
*/
function toInt256(uint256 value) internal pure returns (int256) {
// Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
require(value <= uint256(type(int256).max), "SafeCast: value doesn't fit in an int256");
return int256(value);
}
}
contracts/governance/Governable.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
/**
* @title Base for contracts that are managed by the Origin Protocol's Governor.
* @dev Copy of the openzeppelin Ownable.sol contract with nomenclature change
* from owner to governor and renounce methods removed. Does not use
* Context.sol like Ownable.sol does for simplification.
* @author Origin Protocol Inc
*/
abstract contract Governable {
// Storage position of the owner and pendingOwner of the contract
// keccak256("OUSD.governor");
bytes32 private constant governorPosition =
0x7bea13895fa79d2831e0a9e28edede30099005a50d652d8957cf8a607ee6ca4a;
// keccak256("OUSD.pending.governor");
bytes32 private constant pendingGovernorPosition =
0x44c4d30b2eaad5130ad70c3ba6972730566f3e6359ab83e800d905c61b1c51db;
// keccak256("OUSD.reentry.status");
bytes32 private constant reentryStatusPosition =
0x53bf423e48ed90e97d02ab0ebab13b2a235a6bfbe9c321847d5c175333ac4535;
// See OpenZeppelin ReentrancyGuard implementation
uint256 constant _NOT_ENTERED = 1;
uint256 constant _ENTERED = 2;
event PendingGovernorshipTransfer(
address indexed previousGovernor,
address indexed newGovernor
);
event GovernorshipTransferred(
address indexed previousGovernor,
address indexed newGovernor
);
/**
* @notice Returns the address of the current Governor.
*/
function governor() public view returns (address) {
return _governor();
}
/**
* @dev Returns the address of the current Governor.
*/
function _governor() internal view returns (address governorOut) {
bytes32 position = governorPosition;
// solhint-disable-next-line no-inline-assembly
assembly {
governorOut := sload(position)
}
}
/**
* @dev Returns the address of the pending Governor.
*/
function _pendingGovernor()
internal
view
returns (address pendingGovernor)
{
bytes32 position = pendingGovernorPosition;
// solhint-disable-next-line no-inline-assembly
assembly {
pendingGovernor := sload(position)
}
}
/**
* @dev Throws if called by any account other than the Governor.
*/
modifier onlyGovernor() {
require(isGovernor(), "Caller is not the Governor");
_;
}
/**
* @notice Returns true if the caller is the current Governor.
*/
function isGovernor() public view returns (bool) {
return msg.sender == _governor();
}
function _setGovernor(address newGovernor) internal {
emit GovernorshipTransferred(_governor(), newGovernor);
bytes32 position = governorPosition;
// solhint-disable-next-line no-inline-assembly
assembly {
sstore(position, newGovernor)
}
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and make it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
bytes32 position = reentryStatusPosition;
uint256 _reentry_status;
// solhint-disable-next-line no-inline-assembly
assembly {
_reentry_status := sload(position)
}
// On the first call to nonReentrant, _notEntered will be true
require(_reentry_status != _ENTERED, "Reentrant call");
// Any calls to nonReentrant after this point will fail
// solhint-disable-next-line no-inline-assembly
assembly {
sstore(position, _ENTERED)
}
_;
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
// solhint-disable-next-line no-inline-assembly
assembly {
sstore(position, _NOT_ENTERED)
}
}
function _setPendingGovernor(address newGovernor) internal {
bytes32 position = pendingGovernorPosition;
// solhint-disable-next-line no-inline-assembly
assembly {
sstore(position, newGovernor)
}
}
/**
* @notice Transfers Governance of the contract to a new account (`newGovernor`).
* Can only be called by the current Governor. Must be claimed for this to complete
* @param _newGovernor Address of the new Governor
*/
function transferGovernance(address _newGovernor) external onlyGovernor {
_setPendingGovernor(_newGovernor);
emit PendingGovernorshipTransfer(_governor(), _newGovernor);
}
/**
* @notice Claim Governance of the contract to a new account (`newGovernor`).
* Can only be called by the new Governor.
*/
function claimGovernance() external {
require(
msg.sender == _pendingGovernor(),
"Only the pending Governor can complete the claim"
);
_changeGovernor(msg.sender);
}
/**
* @dev Change Governance of the contract to a new account (`newGovernor`).
* @param _newGovernor Address of the new Governor
*/
function _changeGovernor(address _newGovernor) internal {
require(_newGovernor != address(0), "New Governor is address(0)");
_setGovernor(_newGovernor);
}
}
contracts/interfaces/IBasicToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IBasicToken {
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
}
contracts/interfaces/IStrategy.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
/**
* @title Platform interface to integrate with lending platform like Compound, AAVE etc.
*/
interface IStrategy {
/**
* @dev Deposit the given asset to platform
* @param _asset asset address
* @param _amount Amount to deposit
*/
function deposit(address _asset, uint256 _amount) external;
/**
* @dev Deposit the entire balance of all supported assets in the Strategy
* to the platform
*/
function depositAll() external;
/**
* @dev Withdraw given asset from Lending platform
*/
function withdraw(
address _recipient,
address _asset,
uint256 _amount
) external;
/**
* @dev Liquidate all assets in strategy and return them to Vault.
*/
function withdrawAll() external;
/**
* @dev Returns the current balance of the given asset.
*/
function checkBalance(address _asset)
external
view
returns (uint256 balance);
/**
* @dev Returns bool indicating whether strategy supports asset.
*/
function supportsAsset(address _asset) external view returns (bool);
/**
* @dev Collect reward tokens from the Strategy.
*/
function collectRewardTokens() external;
/**
* @dev The address array of the reward tokens for the Strategy.
*/
function getRewardTokenAddresses() external view returns (address[] memory);
function harvesterAddress() external view returns (address);
function transferToken(address token, uint256 amount) external;
function setRewardTokenAddresses(address[] calldata _rewardTokenAddresses)
external;
}
contracts/interfaces/IVault.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
import { VaultStorage } from "../vault/VaultStorage.sol";
interface IVault {
// slither-disable-start constable-states
event AssetAllocated(address _asset, address _strategy, uint256 _amount);
event StrategyApproved(address _addr);
event StrategyRemoved(address _addr);
event Mint(address _addr, uint256 _value);
event Redeem(address _addr, uint256 _value);
event CapitalPaused();
event CapitalUnpaused();
event DefaultStrategyUpdated(address _strategy);
event RebasePaused();
event RebaseUnpaused();
event VaultBufferUpdated(uint256 _vaultBuffer);
event AllocateThresholdUpdated(uint256 _threshold);
event RebaseThresholdUpdated(uint256 _threshold);
event StrategistUpdated(address _address);
event MaxSupplyDiffChanged(uint256 maxSupplyDiff);
event YieldDistribution(address _to, uint256 _yield, uint256 _fee);
event TrusteeFeeBpsChanged(uint256 _basis);
event TrusteeAddressChanged(address _address);
event StrategyAddedToMintWhitelist(address indexed strategy);
event StrategyRemovedFromMintWhitelist(address indexed strategy);
event RebasePerSecondMaxChanged(uint256 rebaseRatePerSecond);
event DripDurationChanged(uint256 dripDuration);
event WithdrawalRequested(
address indexed _withdrawer,
uint256 indexed _requestId,
uint256 _amount,
uint256 _queued
);
event WithdrawalClaimed(
address indexed _withdrawer,
uint256 indexed _requestId,
uint256 _amount
);
event WithdrawalClaimable(uint256 _claimable, uint256 _newClaimable);
event WithdrawalClaimDelayUpdated(uint256 _newDelay);
// Governable.sol
function transferGovernance(address _newGovernor) external;
function claimGovernance() external;
function governor() external view returns (address);
// VaultAdmin.sol
function setVaultBuffer(uint256 _vaultBuffer) external;
function vaultBuffer() external view returns (uint256);
function setAutoAllocateThreshold(uint256 _threshold) external;
function autoAllocateThreshold() external view returns (uint256);
function setRebaseThreshold(uint256 _threshold) external;
function rebaseThreshold() external view returns (uint256);
function setStrategistAddr(address _address) external;
function strategistAddr() external view returns (address);
function setMaxSupplyDiff(uint256 _maxSupplyDiff) external;
function maxSupplyDiff() external view returns (uint256);
function setTrusteeAddress(address _address) external;
function trusteeAddress() external view returns (address);
function setTrusteeFeeBps(uint256 _basis) external;
function trusteeFeeBps() external view returns (uint256);
function approveStrategy(address _addr) external;
function removeStrategy(address _addr) external;
function setDefaultStrategy(address _strategy) external;
function defaultStrategy() external view returns (address);
function pauseRebase() external;
function unpauseRebase() external;
function rebasePaused() external view returns (bool);
function pauseCapital() external;
function unpauseCapital() external;
function capitalPaused() external view returns (bool);
function transferToken(address _asset, uint256 _amount) external;
function withdrawAllFromStrategy(address _strategyAddr) external;
function withdrawAllFromStrategies() external;
function withdrawFromStrategy(
address _strategyFromAddress,
address[] calldata _assets,
uint256[] calldata _amounts
) external;
function depositToStrategy(
address _strategyToAddress,
address[] calldata _assets,
uint256[] calldata _amounts
) external;
// VaultCore.sol
function mint(
address _asset,
uint256 _amount,
uint256 _minimumOusdAmount
) external;
function mintForStrategy(uint256 _amount) external;
function redeem(uint256 _amount, uint256 _minimumUnitAmount) external;
function burnForStrategy(uint256 _amount) external;
function allocate() external;
function rebase() external;
function totalValue() external view returns (uint256 value);
function checkBalance(address _asset) external view returns (uint256);
/// @notice Deprecated: use calculateRedeemOutput
function calculateRedeemOutputs(uint256 _amount)
external
view
returns (uint256[] memory);
function calculateRedeemOutput(uint256 _amount)
external
view
returns (uint256);
function getAssetCount() external view returns (uint256);
function getAllAssets() external view returns (address[] memory);
function getStrategyCount() external view returns (uint256);
function getAllStrategies() external view returns (address[] memory);
/// @notice Deprecated.
function isSupportedAsset(address _asset) external view returns (bool);
function dripper() external view returns (address);
function asset() external view returns (address);
function initialize(address) external;
function addWithdrawalQueueLiquidity() external;
function requestWithdrawal(uint256 _amount)
external
returns (uint256 requestId, uint256 queued);
function claimWithdrawal(uint256 requestId)
external
returns (uint256 amount);
function claimWithdrawals(uint256[] memory requestIds)
external
returns (uint256[] memory amounts, uint256 totalAmount);
function withdrawalQueueMetadata()
external
view
returns (VaultStorage.WithdrawalQueueMetadata memory);
function withdrawalRequests(uint256 requestId)
external
view
returns (VaultStorage.WithdrawalRequest memory);
function addStrategyToMintWhitelist(address strategyAddr) external;
function removeStrategyFromMintWhitelist(address strategyAddr) external;
function isMintWhitelistedStrategy(address strategyAddr)
external
view
returns (bool);
function withdrawalClaimDelay() external view returns (uint256);
function setWithdrawalClaimDelay(uint256 newDelay) external;
function lastRebase() external view returns (uint64);
function dripDuration() external view returns (uint64);
function setDripDuration(uint256 _dripDuration) external;
function rebasePerSecondMax() external view returns (uint64);
function setRebaseRateMax(uint256 yearlyApr) external;
function rebasePerSecondTarget() external view returns (uint64);
function previewYield() external view returns (uint256 yield);
function weth() external view returns (address);
// slither-disable-end constable-states
}
contracts/interfaces/cctp/ICCTP.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
interface ICCTPTokenMessenger {
function depositForBurn(
uint256 amount,
uint32 destinationDomain,
bytes32 mintRecipient,
address burnToken,
bytes32 destinationCaller,
uint256 maxFee,
uint32 minFinalityThreshold
) external;
function depositForBurnWithHook(
uint256 amount,
uint32 destinationDomain,
bytes32 mintRecipient,
address burnToken,
bytes32 destinationCaller,
uint256 maxFee,
uint32 minFinalityThreshold,
bytes memory hookData
) external;
function getMinFeeAmount(uint256 amount) external view returns (uint256);
}
interface ICCTPMessageTransmitter {
function sendMessage(
uint32 destinationDomain,
bytes32 recipient,
bytes32 destinationCaller,
uint32 minFinalityThreshold,
bytes memory messageBody
) external;
function receiveMessage(bytes calldata message, bytes calldata attestation)
external
returns (bool);
}
interface IMessageHandlerV2 {
/**
* @notice Handles an incoming finalized message from an IReceiverV2
* @dev Finalized messages have finality threshold values greater than or equal to 2000
* @param sourceDomain The source domain of the message
* @param sender The sender of the message
* @param finalityThresholdExecuted the finality threshold at which the message was attested to
* @param messageBody The raw bytes of the message body
* @return success True, if successful; false, if not.
*/
function handleReceiveFinalizedMessage(
uint32 sourceDomain,
bytes32 sender,
uint32 finalityThresholdExecuted,
bytes calldata messageBody
) external returns (bool);
/**
* @notice Handles an incoming unfinalized message from an IReceiverV2
* @dev Unfinalized messages have finality threshold values less than 2000
* @param sourceDomain The source domain of the message
* @param sender The sender of the message
* @param finalityThresholdExecuted The finality threshold at which the message was attested to
* @param messageBody The raw bytes of the message body
* @return success True, if successful; false, if not.
*/
function handleReceiveUnfinalizedMessage(
uint32 sourceDomain,
bytes32 sender,
uint32 finalityThresholdExecuted,
bytes calldata messageBody
) external returns (bool);
}
contracts/strategies/crosschain/AbstractCCTPIntegrator.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
/**
* @title AbstractCCTPIntegrator
* @author Origin Protocol Inc
*
* @dev Abstract contract that contains all the logic used to integrate with CCTP.
*/
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { IERC20 } from "../../utils/InitializableAbstractStrategy.sol";
import { ICCTPTokenMessenger, ICCTPMessageTransmitter, IMessageHandlerV2 } from "../../interfaces/cctp/ICCTP.sol";
import { CrossChainStrategyHelper } from "./CrossChainStrategyHelper.sol";
import { Governable } from "../../governance/Governable.sol";
import { BytesHelper } from "../../utils/BytesHelper.sol";
import "../../utils/Helpers.sol";
abstract contract AbstractCCTPIntegrator is Governable, IMessageHandlerV2 {
using SafeERC20 for IERC20;
using BytesHelper for bytes;
using CrossChainStrategyHelper for bytes;
event LastTransferNonceUpdated(uint64 lastTransferNonce);
event NonceProcessed(uint64 nonce);
event CCTPMinFinalityThresholdSet(uint16 minFinalityThreshold);
event CCTPFeePremiumBpsSet(uint16 feePremiumBps);
event OperatorChanged(address operator);
event TokensBridged(
uint32 destinationDomain,
address peerStrategy,
address tokenAddress,
uint256 tokenAmount,
uint256 maxFee,
uint32 minFinalityThreshold,
bytes hookData
);
event MessageTransmitted(
uint32 destinationDomain,
address peerStrategy,
uint32 minFinalityThreshold,
bytes message
);
// Message body V2 fields
// Ref: https://developers.circle.com/cctp/technical-guide#message-body
// Ref: https://github.com/circlefin/evm-cctp-contracts/blob/master/src/messages/v2/BurnMessageV2.sol
uint8 private constant BURN_MESSAGE_V2_VERSION_INDEX = 0;
uint8 private constant BURN_MESSAGE_V2_BURN_TOKEN_INDEX = 4;
uint8 private constant BURN_MESSAGE_V2_RECIPIENT_INDEX = 36;
uint8 private constant BURN_MESSAGE_V2_AMOUNT_INDEX = 68;
uint8 private constant BURN_MESSAGE_V2_MESSAGE_SENDER_INDEX = 100;
uint8 private constant BURN_MESSAGE_V2_FEE_EXECUTED_INDEX = 164;
uint8 private constant BURN_MESSAGE_V2_HOOK_DATA_INDEX = 228;
/**
* @notice Max transfer threshold imposed by the CCTP
* Ref: https://developers.circle.com/cctp/evm-smart-contracts#depositforburn
* @dev 10M USDC limit applies to both standard and fast transfer modes. The fast transfer mode has
* an additional limitation that is not present on-chain and Circle may alter that amount off-chain
* at their preference. The amount available for fast transfer can be queried here:
* https://iris-api.circle.com/v2/fastBurn/USDC/allowance .
* If a fast transfer token transaction has been issued and there is not enough allowance for it
* the off-chain Iris component will re-attempt the transaction and if it fails it will fallback
* to a standard transfer. Reference section 4.3 in the whitepaper:
* https://6778953.fs1.hubspotusercontent-na1.net/hubfs/6778953/PDFs/Whitepapers/CCTPV2_White_Paper.pdf
*/
uint256 public constant MAX_TRANSFER_AMOUNT = 10_000_000 * 10**6; // 10M USDC
/// @notice Minimum transfer amount to avoid zero or dust transfers
uint256 public constant MIN_TRANSFER_AMOUNT = 10**6;
// CCTP contracts
// This implementation assumes that remote and local chains have these contracts
// deployed on the same addresses.
/// @notice CCTP message transmitter contract
ICCTPMessageTransmitter public immutable cctpMessageTransmitter;
/// @notice CCTP token messenger contract
ICCTPTokenMessenger public immutable cctpTokenMessenger;
/// @notice USDC address on local chain
address public immutable usdcToken;
/// @notice USDC address on remote chain
address public immutable peerUsdcToken;
/// @notice Domain ID of the chain from which messages are accepted
uint32 public immutable peerDomainID;
/// @notice Strategy address on other chain
address public immutable peerStrategy;
/**
* @notice Minimum finality threshold
* Can be 1000 (safe, after 1 epoch) or 2000 (finalized, after 2 epochs).
* Ref: https://developers.circle.com/cctp/technical-guide#finality-thresholds
* @dev When configuring the contract for fast transfer we should check the available
* allowance of USDC that can be bridged using fast mode:
* wget https://iris-api.circle.com/v2/fastBurn/USDC/allowance
*/
uint16 public minFinalityThreshold;
/// @notice Fee premium in basis points
uint16 public feePremiumBps;
/// @notice Nonce of the last known deposit or withdrawal
uint64 public lastTransferNonce;
/// @notice Operator address: Can relay CCTP messages
address public operator;
/// @notice Mapping of processed nonces
mapping(uint64 => bool) private nonceProcessed;
// For future use
uint256[48] private __gap;
modifier onlyCCTPMessageTransmitter() {
require(
msg.sender == address(cctpMessageTransmitter),
"Caller is not CCTP transmitter"
);
_;
}
modifier onlyOperator() {
require(msg.sender == operator, "Caller is not the Operator");
_;
}
/**
* @notice Configuration for CCTP integration
* @param cctpTokenMessenger Address of the CCTP token messenger contract
* @param cctpMessageTransmitter Address of the CCTP message transmitter contract
* @param peerDomainID Domain ID of the chain from which messages are accepted.
* 0 for Ethereum, 6 for Base, etc.
* Ref: https://developers.circle.com/cctp/cctp-supported-blockchains
* @param peerStrategy Address of the master or remote strategy on the other chain
* @param usdcToken USDC address on local chain
*/
struct CCTPIntegrationConfig {
address cctpTokenMessenger;
address cctpMessageTransmitter;
uint32 peerDomainID;
address peerStrategy;
address usdcToken;
address peerUsdcToken;
}
constructor(CCTPIntegrationConfig memory _config) {
require(_config.usdcToken != address(0), "Invalid USDC address");
require(
_config.peerUsdcToken != address(0),
"Invalid peer USDC address"
);
require(
_config.cctpTokenMessenger != address(0),
"Invalid CCTP config"
);
require(
_config.cctpMessageTransmitter != address(0),
"Invalid CCTP config"
);
require(
_config.peerStrategy != address(0),
"Invalid peer strategy address"
);
cctpMessageTransmitter = ICCTPMessageTransmitter(
_config.cctpMessageTransmitter
);
cctpTokenMessenger = ICCTPTokenMessenger(_config.cctpTokenMessenger);
// Domain ID of the chain from which messages are accepted
peerDomainID = _config.peerDomainID;
// Strategy address on other chain, should
// always be same as the proxy of this strategy
peerStrategy = _config.peerStrategy;
// USDC address on local chain
usdcToken = _config.usdcToken;
// Just a sanity check to ensure the base token is USDC
uint256 _usdcTokenDecimals = Helpers.getDecimals(_config.usdcToken);
string memory _usdcTokenSymbol = Helpers.getSymbol(_config.usdcToken);
require(_usdcTokenDecimals == 6, "Base token decimals must be 6");
require(
keccak256(abi.encodePacked(_usdcTokenSymbol)) ==
keccak256(abi.encodePacked("USDC")),
"Token symbol must be USDC"
);
// USDC address on remote chain
peerUsdcToken = _config.peerUsdcToken;
}
/**
* @dev Initialize the implementation contract
* @param _operator Operator address
* @param _minFinalityThreshold Minimum finality threshold
* @param _feePremiumBps Fee premium in basis points
*/
function _initialize(
address _operator,
uint16 _minFinalityThreshold,
uint16 _feePremiumBps
) internal {
_setOperator(_operator);
_setMinFinalityThreshold(_minFinalityThreshold);
_setFeePremiumBps(_feePremiumBps);
// Nonce starts at 1, so assume nonce 0 as processed.
// NOTE: This will cause the deposit/withdraw to fail if the
// strategy is not initialized properly (which is expected).
nonceProcessed[0] = true;
}
/***************************************
Settings
****************************************/
/**
* @dev Set the operator address
* @param _operator Operator address
*/
function setOperator(address _operator) external onlyGovernor {
_setOperator(_operator);
}
/**
* @dev Set the operator address
* @param _operator Operator address
*/
function _setOperator(address _operator) internal {
operator = _operator;
emit OperatorChanged(_operator);
}
/**
* @dev Set the minimum finality threshold at which
* the message is considered to be finalized to relay.
* Only accepts a value of 1000 (Safe, after 1 epoch) or
* 2000 (Finalized, after 2 epochs).
* @param _minFinalityThreshold Minimum finality threshold
*/
function setMinFinalityThreshold(uint16 _minFinalityThreshold)
external
onlyGovernor
{
_setMinFinalityThreshold(_minFinalityThreshold);
}
/**
* @dev Set the minimum finality threshold
* @param _minFinalityThreshold Minimum finality threshold
*/
function _setMinFinalityThreshold(uint16 _minFinalityThreshold) internal {
// 1000 for fast transfer and 2000 for standard transfer
require(
_minFinalityThreshold == 1000 || _minFinalityThreshold == 2000,
"Invalid threshold"
);
minFinalityThreshold = _minFinalityThreshold;
emit CCTPMinFinalityThresholdSet(_minFinalityThreshold);
}
/**
* @dev Set the fee premium in basis points.
* Cannot be higher than 30% (3000 basis points).
* @param _feePremiumBps Fee premium in basis points
*/
function setFeePremiumBps(uint16 _feePremiumBps) external onlyGovernor {
_setFeePremiumBps(_feePremiumBps);
}
/**
* @dev Set the fee premium in basis points
* Cannot be higher than 30% (3000 basis points).
* Ref: https://developers.circle.com/cctp/technical-guide#fees
* @param _feePremiumBps Fee premium in basis points
*/
function _setFeePremiumBps(uint16 _feePremiumBps) internal {
require(_feePremiumBps <= 3000, "Fee premium too high"); // 30%
feePremiumBps = _feePremiumBps;
emit CCTPFeePremiumBpsSet(_feePremiumBps);
}
/***************************************
CCTP message handling
****************************************/
/**
* @dev Handles a finalized CCTP message
* @param sourceDomain Source domain of the message
* @param sender Sender of the message
* @param finalityThresholdExecuted Fidelity threshold executed
* @param messageBody Message body
*/
function handleReceiveFinalizedMessage(
uint32 sourceDomain,
bytes32 sender,
uint32 finalityThresholdExecuted,
bytes memory messageBody
) external override onlyCCTPMessageTransmitter returns (bool) {
// Make sure the finality threshold at execution is at least 2000
require(
finalityThresholdExecuted >= 2000,
"Finality threshold too low"
);
return _handleReceivedMessage(sourceDomain, sender, messageBody);
}
/**
* @dev Handles an unfinalized but safe CCTP message
* @param sourceDomain Source domain of the message
* @param sender Sender of the message
* @param finalityThresholdExecuted Fidelity threshold executed
* @param messageBody Message body
*/
function handleReceiveUnfinalizedMessage(
uint32 sourceDomain,
bytes32 sender,
uint32 finalityThresholdExecuted,
bytes memory messageBody
) external override onlyCCTPMessageTransmitter returns (bool) {
// Make sure the contract is configured to handle unfinalized messages
require(
minFinalityThreshold == 1000,
"Unfinalized messages are not supported"
);
// Make sure the finality threshold at execution is at least 1000
require(
finalityThresholdExecuted >= 1000,
"Finality threshold too low"
);
return _handleReceivedMessage(sourceDomain, sender, messageBody);
}
/**
* @dev Handles a CCTP message
* @param sourceDomain Source domain of the message
* @param sender Sender of the message
* @param messageBody Message body
*/
function _handleReceivedMessage(
uint32 sourceDomain,
bytes32 sender,
bytes memory messageBody
) internal returns (bool) {
require(sourceDomain == peerDomainID, "Unknown Source Domain");
// Extract address from bytes32 (CCTP stores addresses as right-padded bytes32)
address senderAddress = address(uint160(uint256(sender)));
require(senderAddress == peerStrategy, "Unknown Sender");
_onMessageReceived(messageBody);
return true;
}
/**
* @dev Sends tokens to the peer strategy using CCTP Token Messenger
* @param tokenAmount Amount of tokens to send
* @param hookData Hook data
*/
function _sendTokens(uint256 tokenAmount, bytes memory hookData)
internal
virtual
{
// CCTP has a maximum transfer amount of 10M USDC per tx
require(tokenAmount <= MAX_TRANSFER_AMOUNT, "Token amount too high");
// Approve only what needs to be transferred
IERC20(usdcToken).safeApprove(address(cctpTokenMessenger), tokenAmount);
// Compute the max fee to be paid.
// Ref: https://developers.circle.com/cctp/evm-smart-contracts#getminfeeamount
// The right way to compute fees would be to use CCTP's getMinFeeAmount function.
// The issue is that the getMinFeeAmount is not present on v2.0 contracts, but is on
// v2.1. Some of CCTP's deployed contracts are v2.0, some are v2.1.
// We will only be using standard transfers and fee on those is 0 for now. If they
// ever start implementing fee for standard transfers or if we decide to use fast
// trasnfer, we can use feePremiumBps as a workaround.
uint256 maxFee = feePremiumBps > 0
? (tokenAmount * feePremiumBps) / 10000
: 0;
// Send tokens to the peer strategy using CCTP Token Messenger
cctpTokenMessenger.depositForBurnWithHook(
tokenAmount,
peerDomainID,
bytes32(uint256(uint160(peerStrategy))),
address(usdcToken),
bytes32(uint256(uint160(peerStrategy))),
maxFee,
uint32(minFinalityThreshold),
hookData
);
emit TokensBridged(
peerDomainID,
peerStrategy,
usdcToken,
tokenAmount,
maxFee,
uint32(minFinalityThreshold),
hookData
);
}
/**
* @dev Sends a message to the peer strategy using CCTP Message Transmitter
* @param message Payload of the message to send
*/
function _sendMessage(bytes memory message) internal virtual {
cctpMessageTransmitter.sendMessage(
peerDomainID,
bytes32(uint256(uint160(peerStrategy))),
bytes32(uint256(uint160(peerStrategy))),
uint32(minFinalityThreshold),
message
);
emit MessageTransmitted(
peerDomainID,
peerStrategy,
uint32(minFinalityThreshold),
message
);
}
/**
* @dev Receives a message from the peer strategy on the other chain,
* does some basic checks and relays it to the local MessageTransmitterV2.
* If the message is a burn message, it will also handle the hook data
* and call the _onTokenReceived function.
* @param message Payload of the message to send
* @param attestation Attestation of the message
*/
function relay(bytes memory message, bytes memory attestation)
external
onlyOperator
{
(
uint32 version,
uint32 sourceDomainID,
address sender,
address recipient,
bytes memory messageBody
) = message.decodeMessageHeader();
// Ensure that it's a CCTP message
require(
version == CrossChainStrategyHelper.CCTP_MESSAGE_VERSION,
"Invalid CCTP message version"
);
// Ensure that the source domain is the peer domain
require(sourceDomainID == peerDomainID, "Unknown Source Domain");
// Ensure message body version
version = messageBody.extractUint32(BURN_MESSAGE_V2_VERSION_INDEX);
// NOTE: There's a possibility that the CCTP Token Messenger might
// send other types of messages in future, not just the burn message.
// If it ever comes to that, this shouldn't cause us any problems
// because it has to still go through the followign checks:
// - version check
// - message body length check
// - sender and recipient (which should be in the same slots and same as address(this))
// - hook data handling (which will revert even if all the above checks pass)
bool isBurnMessageV1 = sender == address(cctpTokenMessenger);
if (isBurnMessageV1) {
// Handle burn message
require(
version == 1 &&
messageBody.length >= BURN_MESSAGE_V2_HOOK_DATA_INDEX,
"Invalid burn message"
);
// Ensure the burn token is USDC
address burnToken = messageBody.extractAddress(
BURN_MESSAGE_V2_BURN_TOKEN_INDEX
);
require(burnToken == peerUsdcToken, "Invalid burn token");
// Address of caller of depositForBurn (or depositForBurnWithCaller) on source domain
sender = messageBody.extractAddress(
BURN_MESSAGE_V2_MESSAGE_SENDER_INDEX
);
recipient = messageBody.extractAddress(
BURN_MESSAGE_V2_RECIPIENT_INDEX
);
} else {
// We handle only Burn message or our custom messagee
require(
version == CrossChainStrategyHelper.ORIGIN_MESSAGE_VERSION,
"Unsupported message version"
);
}
// Ensure the recipient is this contract
// Both sender and recipient should be deployed to same address on both chains.
require(address(this) == recipient, "Unexpected recipient address");
require(sender == peerStrategy, "Incorrect sender/recipient address");
// Relay the message
// This step also mints USDC and transfers it to the recipient wallet
bool relaySuccess = cctpMessageTransmitter.receiveMessage(
message,
attestation
);
require(relaySuccess, "Receive message failed");
if (isBurnMessageV1) {
// Extract the hook data from the message body
bytes memory hookData = messageBody.extractSlice(
BURN_MESSAGE_V2_HOOK_DATA_INDEX,
messageBody.length
);
// Extract the token amount from the message body
uint256 tokenAmount = messageBody.extractUint256(
BURN_MESSAGE_V2_AMOUNT_INDEX
);
// Extract the fee executed from the message body
uint256 feeExecuted = messageBody.extractUint256(
BURN_MESSAGE_V2_FEE_EXECUTED_INDEX
);
// Call the _onTokenReceived function
_onTokenReceived(tokenAmount - feeExecuted, feeExecuted, hookData);
}
}
/***************************************
Message utils
****************************************/
/***************************************
Nonce Handling
****************************************/
/**
* @dev Checks if the last known transfer is pending.
* Nonce starts at 1, so 0 is disregarded.
* @return True if a transfer is pending, false otherwise
*/
function isTransferPending() public view returns (bool) {
return !nonceProcessed[lastTransferNonce];
}
/**
* @dev Checks if a given nonce is processed.
* Nonce starts at 1, so 0 is disregarded.
* @param nonce Nonce to check
* @return True if the nonce is processed, false otherwise
*/
function isNonceProcessed(uint64 nonce) public view returns (bool) {
return nonceProcessed[nonce];
}
/**
* @dev Marks a given nonce as processed.
* Can only mark nonce as processed once. New nonce should
* always be greater than the last known nonce. Also updates
* the last known nonce.
* @param nonce Nonce to mark as processed
*/
function _markNonceAsProcessed(uint64 nonce) internal {
uint64 lastNonce = lastTransferNonce;
// Can only mark latest nonce as processed
// Master strategy when receiving a message from the remote strategy
// will have lastNone == nonce, as the nonce is increase at the start
// of deposit / withdrawal flow.
// Remote strategy will have lastNonce < nonce, as a new nonce initiated
// from master will be greater than the last one.
require(nonce >= lastNonce, "Nonce too low");
// Can only mark nonce as processed once
require(!nonceProcessed[nonce], "Nonce already processed");
nonceProcessed[nonce] = true;
emit NonceProcessed(nonce);
if (nonce != lastNonce) {
// Update last known nonce
lastTransferNonce = nonce;
emit LastTransferNonceUpdated(nonce);
}
}
/**
* @dev Gets the next nonce to use.
* Nonce starts at 1, so 0 is disregarded.
* Reverts if last nonce hasn't been processed yet.
* @return Next nonce
*/
function _getNextNonce() internal returns (uint64) {
uint64 nonce = lastTransferNonce;
require(nonceProcessed[nonce], "Pending token transfer");
nonce = nonce + 1;
lastTransferNonce = nonce;
emit LastTransferNonceUpdated(nonce);
return nonce;
}
/***************************************
Inheritence overrides
****************************************/
/**
* @dev Called when the USDC is received from the CCTP
* @param tokenAmount The actual amount of USDC received (amount sent - fee executed)
* @param feeExecuted The fee executed
* @param payload The payload of the message (hook data)
*/
function _onTokenReceived(
uint256 tokenAmount,
uint256 feeExecuted,
bytes memory payload
) internal virtual;
/**
* @dev Called when the message is received
* @param payload The payload of the message
*/
function _onMessageReceived(bytes memory payload) internal virtual;
}
contracts/strategies/crosschain/CrossChainStrategyHelper.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
/**
* @title CrossChainStrategyHelper
* @author Origin Protocol Inc
* @dev This library is used to encode and decode the messages for the cross-chain strategy.
* It is used to ensure that the messages are valid and to get the message version and type.
*/
import { BytesHelper } from "../../utils/BytesHelper.sol";
library CrossChainStrategyHelper {
using BytesHelper for bytes;
uint32 public constant DEPOSIT_MESSAGE = 1;
uint32 public constant WITHDRAW_MESSAGE = 2;
uint32 public constant BALANCE_CHECK_MESSAGE = 3;
uint32 public constant CCTP_MESSAGE_VERSION = 1;
uint32 public constant ORIGIN_MESSAGE_VERSION = 1010;
// CCTP Message Header fields
// Ref: https://developers.circle.com/cctp/technical-guide#message-header
uint8 private constant VERSION_INDEX = 0;
uint8 private constant SOURCE_DOMAIN_INDEX = 4;
uint8 private constant SENDER_INDEX = 44;
uint8 private constant RECIPIENT_INDEX = 76;
uint8 private constant MESSAGE_BODY_INDEX = 148;
/**
* @dev Get the message version from the message.
* It should always be 4 bytes long,
* starting from the 0th index.
* @param message The message to get the version from
* @return The message version
*/
function getMessageVersion(bytes memory message)
internal
pure
returns (uint32)
{
// uint32 bytes 0 to 4 is Origin message version
// uint32 bytes 4 to 8 is Message type
return message.extractUint32(0);
}
/**
* @dev Get the message type from the message.
* It should always be 4 bytes long,
* starting from the 4th index.
* @param message The message to get the type from
* @return The message type
*/
function getMessageType(bytes memory message)
internal
pure
returns (uint32)
{
// uint32 bytes 0 to 4 is Origin message version
// uint32 bytes 4 to 8 is Message type
return message.extractUint32(4);
}
/**
* @dev Verify the message version and type.
* The message version should be the same as the Origin message version,
* and the message type should be the same as the expected message type.
* @param _message The message to verify
* @param _type The expected message type
*/
function verifyMessageVersionAndType(bytes memory _message, uint32 _type)
internal
pure
{
require(
getMessageVersion(_message) == ORIGIN_MESSAGE_VERSION,
"Invalid Origin Message Version"
);
require(getMessageType(_message) == _type, "Invalid Message type");
}
/**
* @dev Get the message payload from the message.
* The payload starts at the 8th byte.
* @param message The message to get the payload from
* @return The message payload
*/
function getMessagePayload(bytes memory message)
internal
pure
returns (bytes memory)
{
// uint32 bytes 0 to 4 is Origin message version
// uint32 bytes 4 to 8 is Message type
// Payload starts at byte 8
return message.extractSlice(8, message.length);
}
/**
* @dev Encode the deposit message.
* The message version and type are always encoded in the message.
* @param nonce The nonce of the deposit
* @param depositAmount The amount of the deposit
* @return The encoded deposit message
*/
function encodeDepositMessage(uint64 nonce, uint256 depositAmount)
internal
pure
returns (bytes memory)
{
return
abi.encodePacked(
ORIGIN_MESSAGE_VERSION,
DEPOSIT_MESSAGE,
abi.encode(nonce, depositAmount)
);
}
/**
* @dev Decode the deposit message.
* The message version and type are verified in the message.
* @param message The message to decode
* @return The nonce and the amount of the deposit
*/
function decodeDepositMessage(bytes memory message)
internal
pure
returns (uint64, uint256)
{
verifyMessageVersionAndType(message, DEPOSIT_MESSAGE);
(uint64 nonce, uint256 depositAmount) = abi.decode(
getMessagePayload(message),
(uint64, uint256)
);
return (nonce, depositAmount);
}
/**
* @dev Encode the withdrawal message.
* The message version and type are always encoded in the message.
* @param nonce The nonce of the withdrawal
* @param withdrawAmount The amount of the withdrawal
* @return The encoded withdrawal message
*/
function encodeWithdrawMessage(uint64 nonce, uint256 withdrawAmount)
internal
pure
returns (bytes memory)
{
return
abi.encodePacked(
ORIGIN_MESSAGE_VERSION,
WITHDRAW_MESSAGE,
abi.encode(nonce, withdrawAmount)
);
}
/**
* @dev Decode the withdrawal message.
* The message version and type are verified in the message.
* @param message The message to decode
* @return The nonce and the amount of the withdrawal
*/
function decodeWithdrawMessage(bytes memory message)
internal
pure
returns (uint64, uint256)
{
verifyMessageVersionAndType(message, WITHDRAW_MESSAGE);
(uint64 nonce, uint256 withdrawAmount) = abi.decode(
getMessagePayload(message),
(uint64, uint256)
);
return (nonce, withdrawAmount);
}
/**
* @dev Encode the balance check message.
* The message version and type are always encoded in the message.
* @param nonce The nonce of the balance check
* @param balance The balance to check
* @param transferConfirmation Indicates if the message is a transfer confirmation. This is true
* when the message is a result of a deposit or a withdrawal.
* @return The encoded balance check message
*/
function encodeBalanceCheckMessage(
uint64 nonce,
uint256 balance,
bool transferConfirmation,
uint256 timestamp
) internal pure returns (bytes memory) {
return
abi.encodePacked(
ORIGIN_MESSAGE_VERSION,
BALANCE_CHECK_MESSAGE,
abi.encode(nonce, balance, transferConfirmation, timestamp)
);
}
/**
* @dev Decode the balance check message.
* The message version and type are verified in the message.
* @param message The message to decode
* @return The nonce, the balance and indicates if the message is a transfer confirmation
*/
function decodeBalanceCheckMessage(bytes memory message)
internal
pure
returns (
uint64,
uint256,
bool,
uint256
)
{
verifyMessageVersionAndType(message, BALANCE_CHECK_MESSAGE);
(
uint64 nonce,
uint256 balance,
bool transferConfirmation,
uint256 timestamp
) = abi.decode(
getMessagePayload(message),
(uint64, uint256, bool, uint256)
);
return (nonce, balance, transferConfirmation, timestamp);
}
/**
* @dev Decode the CCTP message header
* @param message Message to decode
* @return version Version of the message
* @return sourceDomainID Source domain ID
* @return sender Sender of the message
* @return recipient Recipient of the message
* @return messageBody Message body
*/
function decodeMessageHeader(bytes memory message)
internal
pure
returns (
uint32 version,
uint32 sourceDomainID,
address sender,
address recipient,
bytes memory messageBody
)
{
version = message.extractUint32(VERSION_INDEX);
sourceDomainID = message.extractUint32(SOURCE_DOMAIN_INDEX);
// Address of MessageTransmitterV2 caller on source domain
sender = message.extractAddress(SENDER_INDEX);
// Address to handle message body on destination domain
recipient = message.extractAddress(RECIPIENT_INDEX);
messageBody = message.extractSlice(MESSAGE_BODY_INDEX, message.length);
}
}
contracts/token/OUSD.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
/**
* @title OUSD Token Contract
* @dev ERC20 compatible contract for OUSD
* @dev Implements an elastic supply
* @author Origin Protocol Inc
*/
import { IVault } from "../interfaces/IVault.sol";
import { Governable } from "../governance/Governable.sol";
import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol";
contract OUSD is Governable {
using SafeCast for int256;
using SafeCast for uint256;
/// @dev Event triggered when the supply changes
/// @param totalSupply Updated token total supply
/// @param rebasingCredits Updated token rebasing credits
/// @param rebasingCreditsPerToken Updated token rebasing credits per token
event TotalSupplyUpdatedHighres(
uint256 totalSupply,
uint256 rebasingCredits,
uint256 rebasingCreditsPerToken
);
/// @dev Event triggered when an account opts in for rebasing
/// @param account Address of the account
event AccountRebasingEnabled(address account);
/// @dev Event triggered when an account opts out of rebasing
/// @param account Address of the account
event AccountRebasingDisabled(address account);
/// @dev Emitted when `value` tokens are moved from one account `from` to
/// another `to`.
/// @param from Address of the account tokens are moved from
/// @param to Address of the account tokens are moved to
/// @param value Amount of tokens transferred
event Transfer(address indexed from, address indexed to, uint256 value);
/// @dev Emitted when the allowance of a `spender` for an `owner` is set by
/// a call to {approve}. `value` is the new allowance.
/// @param owner Address of the owner approving allowance
/// @param spender Address of the spender allowance is granted to
/// @param value Amount of tokens spender can transfer
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
/// @dev Yield resulting from {changeSupply} that a `source` account would
/// receive is directed to `target` account.
/// @param source Address of the source forwarding the yield
/// @param target Address of the target receiving the yield
event YieldDelegated(address source, address target);
/// @dev Yield delegation from `source` account to the `target` account is
/// suspended.
/// @param source Address of the source suspending yield forwarding
/// @param target Address of the target no longer receiving yield from `source`
/// account
event YieldUndelegated(address source, address target);
enum RebaseOptions {
NotSet,
StdNonRebasing,
StdRebasing,
YieldDelegationSource,
YieldDelegationTarget
}
uint256[154] private _gap; // Slots to align with deployed contract
uint256 private constant MAX_SUPPLY = type(uint128).max;
/// @dev The amount of tokens in existence
uint256 public totalSupply;
mapping(address => mapping(address => uint256)) private allowances;
/// @dev The vault with privileges to execute {mint}, {burn}
/// and {changeSupply}
address public vaultAddress;
mapping(address => uint256) internal creditBalances;
// the 2 storage variables below need trailing underscores to not name collide with public functions
uint256 private rebasingCredits_; // Sum of all rebasing credits (creditBalances for rebasing accounts)
uint256 private rebasingCreditsPerToken_;
/// @dev The amount of tokens that are not rebasing - receiving yield
uint256 public nonRebasingSupply;
mapping(address => uint256) internal alternativeCreditsPerToken;
/// @dev A map of all addresses and their respective RebaseOptions
mapping(address => RebaseOptions) public rebaseState;
mapping(address => uint256) private __deprecated_isUpgraded;
/// @dev A map of addresses that have yields forwarded to. This is an
/// inverse mapping of {yieldFrom}
/// Key Account forwarding yield
/// Value Account receiving yield
mapping(address => address) public yieldTo;
/// @dev A map of addresses that are receiving the yield. This is an
/// inverse mapping of {yieldTo}
/// Key Account receiving yield
/// Value Account forwarding yield
mapping(address => address) public yieldFrom;
uint256 private constant RESOLUTION_INCREASE = 1e9;
uint256[34] private __gap; // including below gap totals up to 200
/// @dev Verifies that the caller is the Governor or Strategist.
modifier onlyGovernorOrStrategist() {
require(
isGovernor() || msg.sender == IVault(vaultAddress).strategistAddr(),
"Caller is not the Strategist or Governor"
);
_;
}
/// @dev Initializes the contract and sets necessary variables.
/// @param _vaultAddress Address of the vault contract
/// @param _initialCreditsPerToken The starting rebasing credits per token.
function initialize(address _vaultAddress, uint256 _initialCreditsPerToken)
external
onlyGovernor
{
require(_vaultAddress != address(0), "Zero vault address");
require(vaultAddress == address(0), "Already initialized");
rebasingCreditsPerToken_ = _initialCreditsPerToken;
vaultAddress = _vaultAddress;
}
/// @dev Returns the symbol of the token, a shorter version
/// of the name.
function symbol() external pure virtual returns (string memory) {
return "OUSD";
}
/// @dev Returns the name of the token.
function name() external pure virtual returns (string memory) {
return "Origin Dollar";
}
/// @dev Returns the number of decimals used to get its user representation.
function decimals() external pure virtual returns (uint8) {
return 18;
}
/**
* @dev Verifies that the caller is the Vault contract
*/
modifier onlyVault() {
require(vaultAddress == msg.sender, "Caller is not the Vault");
_;
}
/**
* @return High resolution rebasingCreditsPerToken
*/
function rebasingCreditsPerTokenHighres() external view returns (uint256) {
return rebasingCreditsPerToken_;
}
/**
* @return Low resolution rebasingCreditsPerToken
*/
function rebasingCreditsPerToken() external view returns (uint256) {
return rebasingCreditsPerToken_ / RESOLUTION_INCREASE;
}
/**
* @return High resolution total number of rebasing credits
*/
function rebasingCreditsHighres() external view returns (uint256) {
return rebasingCredits_;
}
/**
* @return Low resolution total number of rebasing credits
*/
function rebasingCredits() external view returns (uint256) {
return rebasingCredits_ / RESOLUTION_INCREASE;
}
/**
* @notice Gets the balance of the specified address.
* @param _account Address to query the balance of.
* @return A uint256 representing the amount of base units owned by the
* specified address.
*/
function balanceOf(address _account) public view returns (uint256) {
RebaseOptions state = rebaseState[_account];
if (state == RebaseOptions.YieldDelegationSource) {
// Saves a slot read when transferring to or from a yield delegating source
// since we know creditBalances equals the balance.
return creditBalances[_account];
}
uint256 baseBalance = (creditBalances[_account] * 1e18) /
_creditsPerToken(_account);
if (state == RebaseOptions.YieldDelegationTarget) {
// creditBalances of yieldFrom accounts equals token balances
return baseBalance - creditBalances[yieldFrom[_account]];
}
return baseBalance;
}
/**
* @notice Gets the credits balance of the specified address.
* @dev Backwards compatible with old low res credits per token.
* @param _account The address to query the balance of.
* @return (uint256, uint256) Credit balance and credits per token of the
* address
*/
function creditsBalanceOf(address _account)
external
view
returns (uint256, uint256)
{
uint256 cpt = _creditsPerToken(_account);
if (cpt == 1e27) {
// For a period before the resolution upgrade, we created all new
// contract accounts at high resolution. Since they are not changing
// as a result of this upgrade, we will return their true values
return (creditBalances[_account], cpt);
} else {
return (
creditBalances[_account] / RESOLUTION_INCREASE,
cpt / RESOLUTION_INCREASE
);
}
}
/**
* @notice Gets the credits balance of the specified address.
* @param _account The address to query the balance of.
* @return (uint256, uint256, bool) Credit balance, credits per token of the
* address, and isUpgraded
*/
function creditsBalanceOfHighres(address _account)
external
view
returns (
uint256,
uint256,
bool
)
{
return (
creditBalances[_account],
_creditsPerToken(_account),
true // all accounts have their resolution "upgraded"
);
}
// Backwards compatible view
function nonRebasingCreditsPerToken(address _account)
external
view
returns (uint256)
{
return alternativeCreditsPerToken[_account];
}
/**
* @notice Transfer tokens to a specified address.
* @param _to the address to transfer to.
* @param _value the amount to be transferred.
* @return true on success.
*/
function transfer(address _to, uint256 _value) external returns (bool) {
require(_to != address(0), "Transfer to zero address");
_executeTransfer(msg.sender, _to, _value);
emit Transfer(msg.sender, _to, _value);
return true;
}
/**
* @notice Transfer tokens from one address to another.
* @param _from The address you want to send tokens from.
* @param _to The address you want to transfer to.
* @param _value The amount of tokens to be transferred.
* @return true on success.
*/
function transferFrom(
address _from,
address _to,
uint256 _value
) external returns (bool) {
require(_to != address(0), "Transfer to zero address");
uint256 userAllowance = allowances[_from][msg.sender];
require(_value <= userAllowance, "Allowance exceeded");
unchecked {
allowances[_from][msg.sender] = userAllowance - _value;
}
_executeTransfer(_from, _to, _value);
emit Transfer(_from, _to, _value);
return true;
}
function _executeTransfer(
address _from,
address _to,
uint256 _value
) internal {
(
int256 fromRebasingCreditsDiff,
int256 fromNonRebasingSupplyDiff
) = _adjustAccount(_from, -_value.toInt256());
(
int256 toRebasingCreditsDiff,
int256 toNonRebasingSupplyDiff
) = _adjustAccount(_to, _value.toInt256());
_adjustGlobals(
fromRebasingCreditsDiff + toRebasingCreditsDiff,
fromNonRebasingSupplyDiff + toNonRebasingSupplyDiff
);
}
function _adjustAccount(address _account, int256 _balanceChange)
internal
returns (int256 rebasingCreditsDiff, int256 nonRebasingSupplyDiff)
{
RebaseOptions state = rebaseState[_account];
int256 currentBalance = balanceOf(_account).toInt256();
if (currentBalance + _balanceChange < 0) {
revert("Transfer amount exceeds balance");
}
uint256 newBalance = (currentBalance + _balanceChange).toUint256();
if (state == RebaseOptions.YieldDelegationSource) {
address target = yieldTo[_account];
uint256 targetOldBalance = balanceOf(target);
uint256 targetNewCredits = _balanceToRebasingCredits(
targetOldBalance + newBalance
);
rebasingCreditsDiff =
targetNewCredits.toInt256() -
creditBalances[target].toInt256();
creditBalances[_account] = newBalance;
creditBalances[target] = targetNewCredits;
} else if (state == RebaseOptions.YieldDelegationTarget) {
uint256 newCredits = _balanceToRebasingCredits(
newBalance + creditBalances[yieldFrom[_account]]
);
rebasingCreditsDiff =
newCredits.toInt256() -
creditBalances[_account].toInt256();
creditBalances[_account] = newCredits;
} else {
_autoMigrate(_account);
uint256 alternativeCreditsPerTokenMem = alternativeCreditsPerToken[
_account
];
if (alternativeCreditsPerTokenMem > 0) {
nonRebasingSupplyDiff = _balanceChange;
if (alternativeCreditsPerTokenMem != 1e18) {
alternativeCreditsPerToken[_account] = 1e18;
}
creditBalances[_account] = newBalance;
} else {
uint256 newCredits = _balanceToRebasingCredits(newBalance);
rebasingCreditsDiff =
newCredits.toInt256() -
creditBalances[_account].toInt256();
creditBalances[_account] = newCredits;
}
}
}
function _adjustGlobals(
int256 _rebasingCreditsDiff,
int256 _nonRebasingSupplyDiff
) internal {
if (_rebasingCreditsDiff != 0) {
rebasingCredits_ = (rebasingCredits_.toInt256() +
_rebasingCreditsDiff).toUint256();
}
if (_nonRebasingSupplyDiff != 0) {
nonRebasingSupply = (nonRebasingSupply.toInt256() +
_nonRebasingSupplyDiff).toUint256();
}
}
/**
* @notice Function to check the amount of tokens that _owner has allowed
* to `_spender`.
* @param _owner The address which owns the funds.
* @param _spender The address which will spend the funds.
* @return The number of tokens still available for the _spender.
*/
function allowance(address _owner, address _spender)
external
view
returns (uint256)
{
return allowances[_owner][_spender];
}
/**
* @notice Approve the passed address to spend the specified amount of
* tokens on behalf of msg.sender.
* @param _spender The address which will spend the funds.
* @param _value The amount of tokens to be spent.
* @return true on success.
*/
function approve(address _spender, uint256 _value) external returns (bool) {
allowances[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
/**
* @notice Creates `_amount` tokens and assigns them to `_account`,
* increasing the total supply.
*/
function mint(address _account, uint256 _amount) external onlyVault {
require(_account != address(0), "Mint to the zero address");
// Account
(
int256 toRebasingCreditsDiff,
int256 toNonRebasingSupplyDiff
) = _adjustAccount(_account, _amount.toInt256());
// Globals
_adjustGlobals(toRebasingCreditsDiff, toNonRebasingSupplyDiff);
totalSupply = totalSupply + _amount;
require(totalSupply < MAX_SUPPLY, "Max supply");
emit Transfer(address(0), _account, _amount);
}
/**
* @notice Destroys `_amount` tokens from `_account`,
* reducing the total supply.
*/
function burn(address _account, uint256 _amount) external onlyVault {
require(_account != address(0), "Burn from the zero address");
if (_amount == 0) {
return;
}
// Account
(
int256 toRebasingCreditsDiff,
int256 toNonRebasingSupplyDiff
) = _adjustAccount(_account, -_amount.toInt256());
// Globals
_adjustGlobals(toRebasingCreditsDiff, toNonRebasingSupplyDiff);
totalSupply = totalSupply - _amount;
emit Transfer(_account, address(0), _amount);
}
/**
* @dev Get the credits per token for an account. Returns a fixed amount
* if the account is non-rebasing.
* @param _account Address of the account.
*/
function _creditsPerToken(address _account)
internal
view
returns (uint256)
{
uint256 alternativeCreditsPerTokenMem = alternativeCreditsPerToken[
_account
];
if (alternativeCreditsPerTokenMem != 0) {
return alternativeCreditsPerTokenMem;
} else {
return rebasingCreditsPerToken_;
}
}
/**
* @dev Auto migrate contracts to be non rebasing,
* unless they have opted into yield.
* @param _account Address of the account.
*/
function _autoMigrate(address _account) internal {
uint256 codeLen = _account.code.length;
bool isEOA = (codeLen == 0) ||
(codeLen == 23 && bytes3(_account.code) == 0xef0100);
// In previous code versions, contracts would not have had their
// rebaseState[_account] set to RebaseOptions.NonRebasing when migrated
// therefore we check the actual accounting used on the account as well.
if (
(!isEOA) &&
rebaseState[_account] == RebaseOptions.NotSet &&
alternativeCreditsPerToken[_account] == 0
) {
_rebaseOptOut(_account);
}
}
/**
* @dev Calculates credits from contract's global rebasingCreditsPerToken_, and
* also balance that corresponds to those credits. The latter is important
* when adjusting the contract's global nonRebasingSupply to circumvent any
* possible rounding errors.
*
* @param _balance Balance of the account.
*/
function _balanceToRebasingCredits(uint256 _balance)
internal
view
returns (uint256 rebasingCredits)
{
// Rounds up, because we need to ensure that accounts always have
// at least the balance that they should have.
// Note this should always be used on an absolute account value,
// not on a possibly negative diff, because then the rounding would be wrong.
return ((_balance) * rebasingCreditsPerToken_ + 1e18 - 1) / 1e18;
}
/**
* @notice The calling account will start receiving yield after a successful call.
* @param _account Address of the account.
*/
function governanceRebaseOptIn(address _account) external onlyGovernor {
require(_account != address(0), "Zero address not allowed");
_rebaseOptIn(_account);
}
/**
* @notice The calling account will start receiving yield after a successful call.
*/
function rebaseOptIn() external {
_rebaseOptIn(msg.sender);
}
function _rebaseOptIn(address _account) internal {
uint256 balance = balanceOf(_account);
// prettier-ignore
require(
alternativeCreditsPerToken[_account] > 0 ||
// Accounts may explicitly `rebaseOptIn` regardless of
// accounting if they have a 0 balance.
creditBalances[_account] == 0
,
"Account must be non-rebasing"
);
RebaseOptions state = rebaseState[_account];
// prettier-ignore
require(
state == RebaseOptions.StdNonRebasing ||
state == RebaseOptions.NotSet,
"Only standard non-rebasing accounts can opt in"
);
uint256 newCredits = _balanceToRebasingCredits(balance);
// Account
rebaseState[_account] = RebaseOptions.StdRebasing;
alternativeCreditsPerToken[_account] = 0;
creditBalances[_account] = newCredits;
// Globals
_adjustGlobals(newCredits.toInt256(), -balance.toInt256());
emit AccountRebasingEnabled(_account);
}
/**
* @notice The calling account will no longer receive yield
*/
function rebaseOptOut() external {
_rebaseOptOut(msg.sender);
}
function _rebaseOptOut(address _account) internal {
require(
alternativeCreditsPerToken[_account] == 0,
"Account must be rebasing"
);
RebaseOptions state = rebaseState[_account];
require(
state == RebaseOptions.StdRebasing || state == RebaseOptions.NotSet,
"Only standard rebasing accounts can opt out"
);
uint256 oldCredits = creditBalances[_account];
uint256 balance = balanceOf(_account);
// Account
rebaseState[_account] = RebaseOptions.StdNonRebasing;
alternativeCreditsPerToken[_account] = 1e18;
creditBalances[_account] = balance;
// Globals
_adjustGlobals(-oldCredits.toInt256(), balance.toInt256());
emit AccountRebasingDisabled(_account);
}
/**
* @notice Distribute yield to users. This changes the exchange rate
* between "credits" and OUSD tokens to change rebasing user's balances.
* @param _newTotalSupply New total supply of OUSD.
*/
function changeSupply(uint256 _newTotalSupply) external onlyVault {
require(totalSupply > 0, "Cannot increase 0 supply");
if (totalSupply == _newTotalSupply) {
emit TotalSupplyUpdatedHighres(
totalSupply,
rebasingCredits_,
rebasingCreditsPerToken_
);
return;
}
totalSupply = _newTotalSupply > MAX_SUPPLY
? MAX_SUPPLY
: _newTotalSupply;
uint256 rebasingSupply = totalSupply - nonRebasingSupply;
// round up in the favour of the protocol
rebasingCreditsPerToken_ =
(rebasingCredits_ * 1e18 + rebasingSupply - 1) /
rebasingSupply;
require(rebasingCreditsPerToken_ > 0, "Invalid change in supply");
emit TotalSupplyUpdatedHighres(
totalSupply,
rebasingCredits_,
rebasingCreditsPerToken_
);
}
/*
* @notice Send the yield from one account to another account.
* Each account keeps its own balances.
*/
function delegateYield(address _from, address _to)
external
onlyGovernorOrStrategist
{
require(_from != address(0), "Zero from address not allowed");
require(_to != address(0), "Zero to address not allowed");
require(_from != _to, "Cannot delegate to self");
require(
yieldFrom[_to] == address(0) &&
yieldTo[_to] == address(0) &&
yieldFrom[_from] == address(0) &&
yieldTo[_from] == address(0),
"Blocked by existing yield delegation"
);
RebaseOptions stateFrom = rebaseState[_from];
RebaseOptions stateTo = rebaseState[_to];
require(
stateFrom == RebaseOptions.NotSet ||
stateFrom == RebaseOptions.StdNonRebasing ||
stateFrom == RebaseOptions.StdRebasing,
"Invalid rebaseState from"
);
require(
stateTo == RebaseOptions.NotSet ||
stateTo == RebaseOptions.StdNonRebasing ||
stateTo == RebaseOptions.StdRebasing,
"Invalid rebaseState to"
);
if (alternativeCreditsPerToken[_from] == 0) {
_rebaseOptOut(_from);
}
if (alternativeCreditsPerToken[_to] > 0) {
_rebaseOptIn(_to);
}
uint256 fromBalance = balanceOf(_from);
uint256 toBalance = balanceOf(_to);
uint256 oldToCredits = creditBalances[_to];
uint256 newToCredits = _balanceToRebasingCredits(
fromBalance + toBalance
);
// Set up the bidirectional links
yieldTo[_from] = _to;
yieldFrom[_to] = _from;
// Local
rebaseState[_from] = RebaseOptions.YieldDelegationSource;
alternativeCreditsPerToken[_from] = 1e18;
creditBalances[_from] = fromBalance;
rebaseState[_to] = RebaseOptions.YieldDelegationTarget;
creditBalances[_to] = newToCredits;
// Global
int256 creditsChange = newToCredits.toInt256() -
oldToCredits.toInt256();
_adjustGlobals(creditsChange, -(fromBalance).toInt256());
emit YieldDelegated(_from, _to);
}
/*
* @notice Stop sending the yield from one account to another account.
*/
function undelegateYield(address _from) external onlyGovernorOrStrategist {
// Require a delegation, which will also ensure a valid delegation
require(yieldTo[_from] != address(0), "Zero address not allowed");
address to = yieldTo[_from];
uint256 fromBalance = balanceOf(_from);
uint256 toBalance = balanceOf(to);
uint256 oldToCredits = creditBalances[to];
uint256 newToCredits = _balanceToRebasingCredits(toBalance);
// Remove the bidirectional links
yieldFrom[to] = address(0);
yieldTo[_from] = address(0);
// Local
rebaseState[_from] = RebaseOptions.StdNonRebasing;
// alternativeCreditsPerToken[from] already 1e18 from `delegateYield()`
creditBalances[_from] = fromBalance;
rebaseState[to] = RebaseOptions.StdRebasing;
// alternativeCreditsPerToken[to] already 0 from `delegateYield()`
creditBalances[to] = newToCredits;
// Global
int256 creditsChange = newToCredits.toInt256() -
oldToCredits.toInt256();
_adjustGlobals(creditsChange, fromBalance.toInt256());
emit YieldUndelegated(_from, to);
}
}
contracts/utils/BytesHelper.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
uint256 constant UINT32_LENGTH = 4;
uint256 constant UINT64_LENGTH = 8;
uint256 constant UINT256_LENGTH = 32;
// Address is 20 bytes, but we expect the data to be padded with 0s to 32 bytes
uint256 constant ADDRESS_LENGTH = 32;
library BytesHelper {
/**
* @dev Extract a slice from bytes memory
* @param data The bytes memory to slice
* @param start The start index (inclusive)
* @param end The end index (exclusive)
* @return result A new bytes memory containing the slice
*/
function extractSlice(
bytes memory data,
uint256 start,
uint256 end
) internal pure returns (bytes memory) {
require(end >= start, "Invalid slice range");
require(end <= data.length, "Slice end exceeds data length");
uint256 length = end - start;
bytes memory result = new bytes(length);
// Simple byte-by-byte copy
for (uint256 i = 0; i < length; i++) {
result[i] = data[start + i];
}
return result;
}
/**
* @dev Decode a uint32 from a bytes memory
* @param data The bytes memory to decode
* @return uint32 The decoded uint32
*/
function decodeUint32(bytes memory data) internal pure returns (uint32) {
require(data.length == 4, "Invalid data length");
return uint32(uint256(bytes32(data)) >> 224);
}
/**
* @dev Extract a uint32 from a bytes memory
* @param data The bytes memory to extract from
* @param start The start index (inclusive)
* @return uint32 The extracted uint32
*/
function extractUint32(bytes memory data, uint256 start)
internal
pure
returns (uint32)
{
return decodeUint32(extractSlice(data, start, start + UINT32_LENGTH));
}
/**
* @dev Decode an address from a bytes memory.
* Expects the data to be padded with 0s to 32 bytes.
* @param data The bytes memory to decode
* @return address The decoded address
*/
function decodeAddress(bytes memory data) internal pure returns (address) {
// We expect the data to be padded with 0s, so length is 32 not 20
require(data.length == 32, "Invalid data length");
return abi.decode(data, (address));
}
/**
* @dev Extract an address from a bytes memory
* @param data The bytes memory to extract from
* @param start The start index (inclusive)
* @return address The extracted address
*/
function extractAddress(bytes memory data, uint256 start)
internal
pure
returns (address)
{
return decodeAddress(extractSlice(data, start, start + ADDRESS_LENGTH));
}
/**
* @dev Decode a uint256 from a bytes memory
* @param data The bytes memory to decode
* @return uint256 The decoded uint256
*/
function decodeUint256(bytes memory data) internal pure returns (uint256) {
require(data.length == 32, "Invalid data length");
return abi.decode(data, (uint256));
}
/**
* @dev Extract a uint256 from a bytes memory
* @param data The bytes memory to extract from
* @param start The start index (inclusive)
* @return uint256 The extracted uint256
*/
function extractUint256(bytes memory data, uint256 start)
internal
pure
returns (uint256)
{
return decodeUint256(extractSlice(data, start, start + UINT256_LENGTH));
}
}
contracts/utils/Helpers.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
import { IBasicToken } from "../interfaces/IBasicToken.sol";
library Helpers {
/**
* @notice Fetch the `symbol()` from an ERC20 token
* @dev Grabs the `symbol()` from a contract
* @param _token Address of the ERC20 token
* @return string Symbol of the ERC20 token
*/
function getSymbol(address _token) internal view returns (string memory) {
string memory symbol = IBasicToken(_token).symbol();
return symbol;
}
/**
* @notice Fetch the `decimals()` from an ERC20 token
* @dev Grabs the `decimals()` from a contract and fails if
* the decimal value does not live within a certain range
* @param _token Address of the ERC20 token
* @return uint256 Decimals of the ERC20 token
*/
function getDecimals(address _token) internal view returns (uint256) {
uint256 decimals = IBasicToken(_token).decimals();
require(
decimals >= 4 && decimals <= 18,
"Token must have sufficient decimal places"
);
return decimals;
}
}
contracts/utils/Initializable.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
/**
* @title Base contract any contracts that need to initialize state after deployment.
* @author Origin Protocol Inc
*/
abstract contract Initializable {
/**
* @dev Indicates that the contract has been initialized.
*/
bool private initialized;
/**
* @dev Indicates that the contract is in the process of being initialized.
*/
bool private initializing;
/**
* @dev Modifier to protect an initializer function from being invoked twice.
*/
modifier initializer() {
require(
initializing || !initialized,
"Initializable: contract is already initialized"
);
bool isTopLevelCall = !initializing;
if (isTopLevelCall) {
initializing = true;
initialized = true;
}
_;
if (isTopLevelCall) {
initializing = false;
}
}
uint256[50] private ______gap;
}
contracts/utils/InitializableAbstractStrategy.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
/**
* @title Base contract for vault strategies.
* @author Origin Protocol Inc
*/
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { Initializable } from "../utils/Initializable.sol";
import { Governable } from "../governance/Governable.sol";
import { IVault } from "../interfaces/IVault.sol";
abstract contract InitializableAbstractStrategy is Initializable, Governable {
using SafeERC20 for IERC20;
event PTokenAdded(address indexed _asset, address _pToken);
event PTokenRemoved(address indexed _asset, address _pToken);
event Deposit(address indexed _asset, address _pToken, uint256 _amount);
event Withdrawal(address indexed _asset, address _pToken, uint256 _amount);
event RewardTokenCollected(
address recipient,
address rewardToken,
uint256 amount
);
event RewardTokenAddressesUpdated(
address[] _oldAddresses,
address[] _newAddresses
);
event HarvesterAddressesUpdated(
address _oldHarvesterAddress,
address _newHarvesterAddress
);
/// @notice Address of the underlying platform
address public immutable platformAddress;
/// @notice Address of the OToken vault
address public immutable vaultAddress;
/// @dev Replaced with an immutable variable
// slither-disable-next-line constable-states
address private _deprecated_platformAddress;
/// @dev Replaced with an immutable
// slither-disable-next-line constable-states
address private _deprecated_vaultAddress;
/// @notice asset => pToken (Platform Specific Token Address)
mapping(address => address) public assetToPToken;
/// @notice Full list of all assets supported by the strategy
address[] internal assetsMapped;
// Deprecated: Reward token address
// slither-disable-next-line constable-states
address private _deprecated_rewardTokenAddress;
// Deprecated: now resides in Harvester's rewardTokenConfigs
// slither-disable-next-line constable-states
uint256 private _deprecated_rewardLiquidationThreshold;
/// @notice Address of the Harvester contract allowed to collect reward tokens
address public harvesterAddress;
/// @notice Address of the reward tokens. eg CRV, BAL, CVX, AURA
address[] public rewardTokenAddresses;
/* Reserved for future expansion. Used to be 100 storage slots
* and has decreased to accommodate:
* - harvesterAddress
* - rewardTokenAddresses
*/
int256[98] private _reserved;
struct BaseStrategyConfig {
address platformAddress; // Address of the underlying platform
address vaultAddress; // Address of the OToken's Vault
}
/**
* @dev Verifies that the caller is the Governor or Strategist.
*/
modifier onlyGovernorOrStrategist() virtual {
require(
isGovernor() || msg.sender == IVault(vaultAddress).strategistAddr(),
"Caller is not the Strategist or Governor"
);
_;
}
/**
* @param _config The platform and OToken vault addresses
*/
constructor(BaseStrategyConfig memory _config) {
platformAddress = _config.platformAddress;
vaultAddress = _config.vaultAddress;
}
/**
* @dev Internal initialize function, to set up initial internal state
* @param _rewardTokenAddresses Address of reward token for platform
* @param _assets Addresses of initial supported assets
* @param _pTokens Platform Token corresponding addresses
*/
function _initialize(
address[] memory _rewardTokenAddresses,
address[] memory _assets,
address[] memory _pTokens
) internal {
rewardTokenAddresses = _rewardTokenAddresses;
uint256 assetCount = _assets.length;
require(assetCount == _pTokens.length, "Invalid input arrays");
for (uint256 i = 0; i < assetCount; ++i) {
_setPTokenAddress(_assets[i], _pTokens[i]);
}
}
/**
* @notice Collect accumulated reward token and send to Vault.
*/
function collectRewardTokens() external virtual onlyHarvester nonReentrant {
_collectRewardTokens();
}
/**
* @dev Default implementation that transfers reward tokens to the Harvester
* Implementing strategies need to add custom logic to collect the rewards.
*/
function _collectRewardTokens() internal virtual {
uint256 rewardTokenCount = rewardTokenAddresses.length;
for (uint256 i = 0; i < rewardTokenCount; ++i) {
IERC20 rewardToken = IERC20(rewardTokenAddresses[i]);
uint256 balance = rewardToken.balanceOf(address(this));
if (balance > 0) {
emit RewardTokenCollected(
harvesterAddress,
address(rewardToken),
balance
);
rewardToken.safeTransfer(harvesterAddress, balance);
}
}
}
/**
* @dev Verifies that the caller is the Vault.
*/
modifier onlyVault() {
require(msg.sender == vaultAddress, "Caller is not the Vault");
_;
}
/**
* @dev Verifies that the caller is the Harvester.
*/
modifier onlyHarvester() {
require(msg.sender == harvesterAddress, "Caller is not the Harvester");
_;
}
/**
* @dev Verifies that the caller is the Vault or Governor.
*/
modifier onlyVaultOrGovernor() {
require(
msg.sender == vaultAddress || msg.sender == governor(),
"Caller is not the Vault or Governor"
);
_;
}
/**
* @dev Verifies that the caller is the Vault, Governor, or Strategist.
*/
modifier onlyVaultOrGovernorOrStrategist() {
require(
msg.sender == vaultAddress ||
msg.sender == governor() ||
msg.sender == IVault(vaultAddress).strategistAddr(),
"Caller is not the Vault, Governor, or Strategist"
);
_;
}
/**
* @notice Set the reward token addresses. Any old addresses will be overwritten.
* @param _rewardTokenAddresses Array of reward token addresses
*/
function setRewardTokenAddresses(address[] calldata _rewardTokenAddresses)
external
onlyGovernor
{
uint256 rewardTokenCount = _rewardTokenAddresses.length;
for (uint256 i = 0; i < rewardTokenCount; ++i) {
require(
_rewardTokenAddresses[i] != address(0),
"Can not set an empty address as a reward token"
);
}
emit RewardTokenAddressesUpdated(
rewardTokenAddresses,
_rewardTokenAddresses
);
rewardTokenAddresses = _rewardTokenAddresses;
}
/**
* @notice Get the reward token addresses.
* @return address[] the reward token addresses.
*/
function getRewardTokenAddresses()
external
view
returns (address[] memory)
{
return rewardTokenAddresses;
}
/**
* @notice Provide support for asset by passing its pToken address.
* This method can only be called by the system Governor
* @param _asset Address for the asset
* @param _pToken Address for the corresponding platform token
*/
function setPTokenAddress(address _asset, address _pToken)
external
virtual
onlyGovernor
{
_setPTokenAddress(_asset, _pToken);
}
/**
* @notice Remove a supported asset by passing its index.
* This method can only be called by the system Governor
* @param _assetIndex Index of the asset to be removed
*/
function removePToken(uint256 _assetIndex) external virtual onlyGovernor {
require(_assetIndex < assetsMapped.length, "Invalid index");
address asset = assetsMapped[_assetIndex];
address pToken = assetToPToken[asset];
if (_assetIndex < assetsMapped.length - 1) {
assetsMapped[_assetIndex] = assetsMapped[assetsMapped.length - 1];
}
assetsMapped.pop();
assetToPToken[asset] = address(0);
emit PTokenRemoved(asset, pToken);
}
/**
* @notice Provide support for asset by passing its pToken address.
* Add to internal mappings and execute the platform specific,
* abstract method `_abstractSetPToken`
* @param _asset Address for the asset
* @param _pToken Address for the corresponding platform token
*/
function _setPTokenAddress(address _asset, address _pToken) internal {
require(assetToPToken[_asset] == address(0), "pToken already set");
require(
_asset != address(0) && _pToken != address(0),
"Invalid addresses"
);
assetToPToken[_asset] = _pToken;
assetsMapped.push(_asset);
emit PTokenAdded(_asset, _pToken);
_abstractSetPToken(_asset, _pToken);
}
/**
* @notice Transfer token to governor. Intended for recovering tokens stuck in
* strategy contracts, i.e. mistaken sends.
* @param _asset Address for the asset
* @param _amount Amount of the asset to transfer
*/
function transferToken(address _asset, uint256 _amount)
public
virtual
onlyGovernor
{
require(!supportsAsset(_asset), "Cannot transfer supported asset");
IERC20(_asset).safeTransfer(governor(), _amount);
}
/**
* @notice Set the Harvester contract that can collect rewards.
* @param _harvesterAddress Address of the harvester contract.
*/
function setHarvesterAddress(address _harvesterAddress)
external
onlyGovernor
{
emit HarvesterAddressesUpdated(harvesterAddress, _harvesterAddress);
harvesterAddress = _harvesterAddress;
}
/***************************************
Abstract
****************************************/
function _abstractSetPToken(address _asset, address _pToken)
internal
virtual;
function safeApproveAllTokens() external virtual;
/**
* @notice Deposit an amount of assets into the platform
* @param _asset Address for the asset
* @param _amount Units of asset to deposit
*/
function deposit(address _asset, uint256 _amount) external virtual;
/**
* @notice Deposit all supported assets in this strategy contract to the platform
*/
function depositAll() external virtual;
/**
* @notice Withdraw an `amount` of assets from the platform and
* send to the `_recipient`.
* @param _recipient Address to which the asset should be sent
* @param _asset Address of the asset
* @param _amount Units of asset to withdraw
*/
function withdraw(
address _recipient,
address _asset,
uint256 _amount
) external virtual;
/**
* @notice Withdraw all supported assets from platform and
* sends to the OToken's Vault.
*/
function withdrawAll() external virtual;
/**
* @notice Get the total asset value held in the platform.
* This includes any interest that was generated since depositing.
* @param _asset Address of the asset
* @return balance Total value of the asset in the platform
*/
function checkBalance(address _asset)
external
view
virtual
returns (uint256 balance);
/**
* @notice Check if an asset is supported.
* @param _asset Address of the asset
* @return bool Whether asset is supported
*/
function supportsAsset(address _asset) public view virtual returns (bool);
}
contracts/vault/VaultStorage.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
/**
* @title OToken VaultStorage contract
* @notice The VaultStorage contract defines the storage for the Vault contracts
* @author Origin Protocol Inc
*/
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { Address } from "@openzeppelin/contracts/utils/Address.sol";
import { IStrategy } from "../interfaces/IStrategy.sol";
import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import { Governable } from "../governance/Governable.sol";
import { OUSD } from "../token/OUSD.sol";
import { Initializable } from "../utils/Initializable.sol";
import "../utils/Helpers.sol";
abstract contract VaultStorage is Initializable, Governable {
using SafeERC20 for IERC20;
event AssetAllocated(address _asset, address _strategy, uint256 _amount);
event StrategyApproved(address _addr);
event StrategyRemoved(address _addr);
event Mint(address _addr, uint256 _value);
event Redeem(address _addr, uint256 _value);
event CapitalPaused();
event CapitalUnpaused();
event DefaultStrategyUpdated(address _strategy);
event RebasePaused();
event RebaseUnpaused();
event VaultBufferUpdated(uint256 _vaultBuffer);
event AllocateThresholdUpdated(uint256 _threshold);
event RebaseThresholdUpdated(uint256 _threshold);
event StrategistUpdated(address _address);
event MaxSupplyDiffChanged(uint256 maxSupplyDiff);
event YieldDistribution(address _to, uint256 _yield, uint256 _fee);
event TrusteeFeeBpsChanged(uint256 _basis);
event TrusteeAddressChanged(address _address);
event StrategyAddedToMintWhitelist(address indexed strategy);
event StrategyRemovedFromMintWhitelist(address indexed strategy);
event RebasePerSecondMaxChanged(uint256 rebaseRatePerSecond);
event DripDurationChanged(uint256 dripDuration);
event WithdrawalRequested(
address indexed _withdrawer,
uint256 indexed _requestId,
uint256 _amount,
uint256 _queued
);
event WithdrawalClaimed(
address indexed _withdrawer,
uint256 indexed _requestId,
uint256 _amount
);
event WithdrawalClaimable(uint256 _claimable, uint256 _newClaimable);
event WithdrawalClaimDelayUpdated(uint256 _newDelay);
// Since we are proxy, all state should be uninitalized.
// Since this storage contract does not have logic directly on it
// we should not be checking for to see if these variables can be constant.
// slither-disable-start uninitialized-state
// slither-disable-start constable-states
/// @dev mapping of supported vault assets to their configuration
uint256 private _deprecated_assets;
/// @dev list of all assets supported by the vault.
address[] private _deprecated_allAssets;
// Strategies approved for use by the Vault
struct Strategy {
bool isSupported;
uint256 _deprecated; // Deprecated storage slot
}
/// @dev mapping of strategy contracts to their configuration
mapping(address => Strategy) public strategies;
/// @dev list of all vault strategies
address[] internal allStrategies;
/// @notice Address of the Oracle price provider contract
address private _deprecated_priceProvider;
/// @notice pause rebasing if true
bool public rebasePaused;
/// @notice pause operations that change the OToken supply.
/// eg mint, redeem, allocate, mint/burn for strategy
bool public capitalPaused;
/// @notice Redemption fee in basis points. eg 50 = 0.5%
uint256 private _deprecated_redeemFeeBps;
/// @notice Percentage of assets to keep in Vault to handle (most) withdrawals. 100% = 1e18.
uint256 public vaultBuffer;
/// @notice OToken mints over this amount automatically allocate funds. 18 decimals.
uint256 public autoAllocateThreshold;
/// @notice OToken mints over this amount automatically rebase. 18 decimals.
uint256 public rebaseThreshold;
/// @dev Address of the OToken token. eg OUSD or OETH.
OUSD public oToken;
/// @dev Address of the contract responsible for post rebase syncs with AMMs
address private _deprecated_rebaseHooksAddr = address(0);
/// @dev Deprecated: Address of Uniswap
address private _deprecated_uniswapAddr = address(0);
/// @notice Address of the Strategist
address public strategistAddr = address(0);
/// @notice Mapping of asset address to the Strategy that they should automatically
// be allocated to
uint256 private _deprecated_assetDefaultStrategies;
/// @notice Max difference between total supply and total value of assets. 18 decimals.
uint256 public maxSupplyDiff;
/// @notice Trustee contract that can collect a percentage of yield
address public trusteeAddress;
/// @notice Amount of yield collected in basis points. eg 2000 = 20%
uint256 public trusteeFeeBps;
/// @dev Deprecated: Tokens that should be swapped for stablecoins
address[] private _deprecated_swapTokens;
/// @notice Metapool strategy that is allowed to mint/burn OTokens without changing collateral
address private _deprecated_ousdMetaStrategy;
/// @notice How much OTokens are currently minted by the strategy
int256 private _deprecated_netOusdMintedForStrategy;
/// @notice How much net total OTokens are allowed to be minted by all strategies
uint256 private _deprecated_netOusdMintForStrategyThreshold;
uint256 private _deprecated_swapConfig;
// List of strategies that can mint oTokens directly
// Used in OETHBaseVaultCore
mapping(address => bool) public isMintWhitelistedStrategy;
/// @notice Address of the Dripper contract that streams harvested rewards to the Vault
/// @dev The vault is proxied so needs to be set with setDripper against the proxy contract.
address private _deprecated_dripper;
/// Withdrawal Queue Storage /////
struct WithdrawalQueueMetadata {
// cumulative total of all withdrawal requests included the ones that have already been claimed
uint128 queued;
// cumulative total of all the requests that can be claimed including the ones that have already been claimed
uint128 claimable;
// total of all the requests that have been claimed
uint128 claimed;
// index of the next withdrawal request starting at 0
uint128 nextWithdrawalIndex;
}
/// @notice Global metadata for the withdrawal queue including:
/// queued - cumulative total of all withdrawal requests included the ones that have already been claimed
/// claimable - cumulative total of all the requests that can be claimed including the ones already claimed
/// claimed - total of all the requests that have been claimed
/// nextWithdrawalIndex - index of the next withdrawal request starting at 0
WithdrawalQueueMetadata public withdrawalQueueMetadata;
struct WithdrawalRequest {
address withdrawer;
bool claimed;
uint40 timestamp; // timestamp of the withdrawal request
// Amount of oTokens to redeem. eg OETH
uint128 amount;
// cumulative total of all withdrawal requests including this one.
// this request can be claimed when this queued amount is less than or equal to the queue's claimable amount.
uint128 queued;
}
/// @notice Mapping of withdrawal request indices to the user withdrawal request data
mapping(uint256 => WithdrawalRequest) public withdrawalRequests;
/// @notice Sets a minimum delay that is required to elapse between
/// requesting async withdrawals and claiming the request.
/// When set to 0 async withdrawals are disabled.
uint256 public withdrawalClaimDelay;
/// @notice Time in seconds that the vault last rebased yield.
uint64 public lastRebase;
/// @notice Automatic rebase yield calculations. In seconds. Set to 0 or 1 to disable.
uint64 public dripDuration;
/// @notice max rebase percentage per second
/// Can be used to set maximum yield of the protocol,
/// spreading out yield over time
uint64 public rebasePerSecondMax;
/// @notice target rebase rate limit, based on past rates and funds available.
uint64 public rebasePerSecondTarget;
uint256 internal constant MAX_REBASE = 0.02 ether;
uint256 internal constant MAX_REBASE_PER_SECOND =
uint256(0.05 ether) / 1 days;
/// @notice Default strategy for asset
address public defaultStrategy;
// For future use
uint256[42] private __gap;
/// @notice Index of WETH asset in allAssets array
/// Legacy OETHVaultCore code, relocated here for vault consistency.
uint256 private _deprecated_wethAssetIndex;
/// @dev Address of the asset (eg. WETH or USDC)
address public immutable asset;
uint8 internal immutable assetDecimals;
// slither-disable-end constable-states
// slither-disable-end uninitialized-state
constructor(address _asset) {
uint8 _decimals = IERC20Metadata(_asset).decimals();
require(_decimals <= 18, "invalid asset decimals");
asset = _asset;
assetDecimals = _decimals;
}
/// @notice Deprecated: use `oToken()` instead.
function oUSD() external view returns (OUSD) {
return oToken;
}
}
Compiler settingsShow
{
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "paris",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.