Contract Name:
SolvBTCRouter
Contract Source Code:
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (proxy/utils/Initializable.sol)
pragma solidity ^0.8.20;
/**
* @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
* behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
* external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
* function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
*
* The initialization functions use a version number. Once a version number is used, it is consumed and cannot be
* reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in
* case an upgrade adds a module that needs to be initialized.
*
* For example:
*
* [.hljs-theme-light.nopadding]
* ```solidity
* contract MyToken is ERC20Upgradeable {
* function initialize() initializer public {
* __ERC20_init("MyToken", "MTK");
* }
* }
*
* contract MyTokenV2 is MyToken, ERC20PermitUpgradeable {
* function initializeV2() reinitializer(2) public {
* __ERC20Permit_init("MyToken");
* }
* }
* ```
*
* TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
* possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
*
* CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
* that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
*
* [CAUTION]
* ====
* Avoid leaving a contract uninitialized.
*
* An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
* contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke
* the {_disableInitializers} function in the constructor to automatically lock it when it is deployed:
*
* [.hljs-theme-light.nopadding]
* ```
* /// @custom:oz-upgrades-unsafe-allow constructor
* constructor() {
* _disableInitializers();
* }
* ```
* ====
*/
abstract contract Initializable {
/**
* @dev Storage of the initializable contract.
*
* It's implemented on a custom ERC-7201 namespace to reduce the risk of storage collisions
* when using with upgradeable contracts.
*
* @custom:storage-location erc7201:openzeppelin.storage.Initializable
*/
struct InitializableStorage {
/**
* @dev Indicates that the contract has been initialized.
*/
uint64 _initialized;
/**
* @dev Indicates that the contract is in the process of being initialized.
*/
bool _initializing;
}
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.Initializable")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant INITIALIZABLE_STORAGE = 0xf0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00;
/**
* @dev The contract is already initialized.
*/
error InvalidInitialization();
/**
* @dev The contract is not initializing.
*/
error NotInitializing();
/**
* @dev Triggered when the contract has been initialized or reinitialized.
*/
event Initialized(uint64 version);
/**
* @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope,
* `onlyInitializing` functions can be used to initialize parent contracts.
*
* Similar to `reinitializer(1)`, except that in the context of a constructor an `initializer` may be invoked any
* number of times. This behavior in the constructor can be useful during testing and is not expected to be used in
* production.
*
* Emits an {Initialized} event.
*/
modifier initializer() {
// solhint-disable-next-line var-name-mixedcase
InitializableStorage storage $ = _getInitializableStorage();
// Cache values to avoid duplicated sloads
bool isTopLevelCall = !$._initializing;
uint64 initialized = $._initialized;
// Allowed calls:
// - initialSetup: the contract is not in the initializing state and no previous version was
// initialized
// - construction: the contract is initialized at version 1 (no reininitialization) and the
// current contract is just being deployed
bool initialSetup = initialized == 0 && isTopLevelCall;
bool construction = initialized == 1 && address(this).code.length == 0;
if (!initialSetup && !construction) {
revert InvalidInitialization();
}
$._initialized = 1;
if (isTopLevelCall) {
$._initializing = true;
}
_;
if (isTopLevelCall) {
$._initializing = false;
emit Initialized(1);
}
}
/**
* @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the
* contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be
* used to initialize parent contracts.
*
* A reinitializer may be used after the original initialization step. This is essential to configure modules that
* are added through upgrades and that require initialization.
*
* When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer`
* cannot be nested. If one is invoked in the context of another, execution will revert.
*
* Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in
* a contract, executing them in the right order is up to the developer or operator.
*
* WARNING: Setting the version to 2**64 - 1 will prevent any future reinitialization.
*
* Emits an {Initialized} event.
*/
modifier reinitializer(uint64 version) {
// solhint-disable-next-line var-name-mixedcase
InitializableStorage storage $ = _getInitializableStorage();
if ($._initializing || $._initialized >= version) {
revert InvalidInitialization();
}
$._initialized = version;
$._initializing = true;
_;
$._initializing = false;
emit Initialized(version);
}
/**
* @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
* {initializer} and {reinitializer} modifiers, directly or indirectly.
*/
modifier onlyInitializing() {
_checkInitializing();
_;
}
/**
* @dev Reverts if the contract is not in an initializing state. See {onlyInitializing}.
*/
function _checkInitializing() internal view virtual {
if (!_isInitializing()) {
revert NotInitializing();
}
}
/**
* @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call.
* Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized
* to any version. It is recommended to use this to lock implementation contracts that are designed to be called
* through proxies.
*
* Emits an {Initialized} event the first time it is successfully executed.
*/
function _disableInitializers() internal virtual {
// solhint-disable-next-line var-name-mixedcase
InitializableStorage storage $ = _getInitializableStorage();
if ($._initializing) {
revert InvalidInitialization();
}
if ($._initialized != type(uint64).max) {
$._initialized = type(uint64).max;
emit Initialized(type(uint64).max);
}
}
/**
* @dev Returns the highest version that has been initialized. See {reinitializer}.
*/
function _getInitializedVersion() internal view returns (uint64) {
return _getInitializableStorage()._initialized;
}
/**
* @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}.
*/
function _isInitializing() internal view returns (bool) {
return _getInitializableStorage()._initializing;
}
/**
* @dev Returns a pointer to the storage namespace.
*/
// solhint-disable-next-line var-name-mixedcase
function _getInitializableStorage() private pure returns (InitializableStorage storage $) {
assembly {
$.slot := INITIALIZABLE_STORAGE
}
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/ReentrancyGuard.sol)
pragma solidity ^0.8.20;
import {Initializable} from "../proxy/utils/Initializable.sol";
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuardUpgradeable is Initializable {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant NOT_ENTERED = 1;
uint256 private constant ENTERED = 2;
/// @custom:storage-location erc7201:openzeppelin.storage.ReentrancyGuard
struct ReentrancyGuardStorage {
uint256 _status;
}
// keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.ReentrancyGuard")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant ReentrancyGuardStorageLocation = 0x9b779b17422d0df92223018b32b4d1fa46e071723d6817e2486d003becc55f00;
function _getReentrancyGuardStorage() private pure returns (ReentrancyGuardStorage storage $) {
assembly {
$.slot := ReentrancyGuardStorageLocation
}
}
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
function __ReentrancyGuard_init() internal onlyInitializing {
__ReentrancyGuard_init_unchained();
}
function __ReentrancyGuard_init_unchained() internal onlyInitializing {
ReentrancyGuardStorage storage $ = _getReentrancyGuardStorage();
$._status = NOT_ENTERED;
}
/**
* @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 making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
ReentrancyGuardStorage storage $ = _getReentrancyGuardStorage();
// On the first call to nonReentrant, _status will be NOT_ENTERED
if ($._status == ENTERED) {
revert ReentrancyGuardReentrantCall();
}
// Any calls to nonReentrant after this point will fail
$._status = ENTERED;
}
function _nonReentrantAfter() private {
ReentrancyGuardStorage storage $ = _getReentrancyGuardStorage();
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
$._status = NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
ReentrancyGuardStorage storage $ = _getReentrancyGuardStorage();
return $._status == ENTERED;
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/introspection/IERC165.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[EIP].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import "./external/IERC721Receiver.sol";
import "./external/IERC3525Receiver.sol";
interface ISftWrapRouter is IERC721Receiver, IERC3525Receiver, IERC165 {
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
interface ISolvBTCMultiAssetPool {
function deposit(address sft_, uint256 sftId_, uint256 value_) external;
function withdraw(address sft, uint256 slot, uint256 sftId, uint256 value) external returns (uint256 toSftId_);
function isSftSlotDepositAllowed(address sft_, uint256 slot_) external view returns (bool);
function isSftSlotWithdrawAllowed(address sft_, uint256 slot_) external view returns (bool);
function getERC20(address sft_, uint256 slot_) external view returns (address);
function getHoldingValueSftId(address sft_, uint256 slot_) external view returns (uint256);
function getSftSlotBalance(address sft_, uint256 slot_) external view returns (uint256);
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import "@openzeppelin/contracts-upgradeable/utils/ReentrancyGuardUpgradeable.sol";
import "./access/AdminControlUpgradeable.sol";
import "./access/GovernorControlUpgradeable.sol";
import "./utils/ERC20TransferHelper.sol";
import "./utils/ERC3525TransferHelper.sol";
import "./external/IERC3525.sol";
import "./external/IOpenFundMarket.sol";
import "./ISftWrapRouter.sol";
import "./ISolvBTCMultiAssetPool.sol";
contract SolvBTCRouter is
ISftWrapRouter,
ReentrancyGuardUpgradeable,
AdminControlUpgradeable,
GovernorControlUpgradeable
{
event Stake(
address indexed solvBTC, address indexed staker, address sft, uint256 sftSlot, uint256 sftId, uint256 amount
);
event Unstake(
address indexed solvBTC, address indexed unstaker, address sft, uint256 sftSlot, uint256 sftId, uint256 amount
);
event CreateSubscription(
bytes32 indexed poolId,
address indexed subscriber,
address solvBTC,
uint256 subscribeAmount,
address currency,
uint256 currencyAmount
);
event CreateRedemption(
bytes32 indexed poolId,
address indexed redeemer,
address indexed solvBTC,
uint256 redeemAmount,
uint256 redemptionId
);
event CancelRedemption(
bytes32 indexed poolId,
address indexed owner,
address indexed solvBTC,
uint256 redemptionId,
uint256 cancelAmount
);
event SetOpenFundMarket(address indexed previousOpenFundMarket, address indexed newOpenFundMarket);
event SetSolvBTCMultiAssetPool(
address indexed previousSolvBTCMultiAssetPool, address indexed newSolvBTCMultiAssetPool
);
address public openFundMarket;
address public solvBTCMultiAssetPool;
// sft address => sft slot => holding sft id
mapping(address => mapping(uint256 => uint256)) public holdingSftIds;
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
function initialize(address governor_, address openFundMarket_, address solvBTCMultiAssetPool_)
external
initializer
{
require(governor_ != address(0), "SolvBTCRouter: invalid governor");
require(openFundMarket_ != address(0), "SolvBTCRouter: invalid openFundMarket");
require(solvBTCMultiAssetPool_ != address(0), "SolvBTCRouter: invalid solvBTCMultiAssetPool");
AdminControlUpgradeable.__AdminControl_init(msg.sender);
GovernorControlUpgradeable.__GovernorControl_init(governor_);
ReentrancyGuardUpgradeable.__ReentrancyGuard_init();
_setOpenFundMarket(openFundMarket_);
_setSolvBTCMultiAssetPool(solvBTCMultiAssetPool_);
}
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == type(IERC3525Receiver).interfaceId || interfaceId == type(IERC721Receiver).interfaceId
|| interfaceId == type(IERC165).interfaceId;
}
function onERC3525Received(
address, /* operator_ */
uint256 fromSftId_,
uint256 toSftId_,
uint256 value_,
bytes calldata /* data_ */
) external virtual override returns (bytes4) {
IERC3525 openFundShare = IERC3525(msg.sender);
uint256 openFundShareSlot = openFundShare.slotOf(toSftId_);
require(
ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).isSftSlotDepositAllowed(
address(openFundShare), openFundShareSlot
),
"SolvBTCRouter: sft slot not allowed"
);
require(value_ > 0, "SolvBTCRouter: stake amount cannot be 0");
address fromSftIdOwner = openFundShare.ownerOf(fromSftId_);
if (
fromSftIdOwner == openFundMarket || fromSftIdOwner == solvBTCMultiAssetPool
|| fromSftIdOwner == address(this)
) {
return IERC3525Receiver.onERC3525Received.selector;
}
address toSftIdOwner = openFundShare.ownerOf(toSftId_);
require(toSftIdOwner == address(this), "SolvBTCRouter: not owned sft id");
{
if (holdingSftIds[address(openFundShare)][openFundShareSlot] == 0) {
holdingSftIds[address(openFundShare)][openFundShareSlot] = toSftId_;
} else {
require(
toSftId_ == holdingSftIds[address(openFundShare)][openFundShareSlot],
"SolvBTCRouter: not holding sft id"
);
}
uint256 newSftId = openFundShare.transferFrom(toSftId_, address(this), value_);
openFundShare.approve(solvBTCMultiAssetPool, newSftId);
ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).deposit(address(openFundShare), newSftId, value_);
}
address solvBTC =
ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).getERC20(address(openFundShare), openFundShareSlot);
ERC20TransferHelper.doTransferOut(solvBTC, payable(fromSftIdOwner), value_);
emit Stake(solvBTC, fromSftIdOwner, address(openFundShare), openFundShareSlot, fromSftId_, value_);
return IERC3525Receiver.onERC3525Received.selector;
}
function onERC721Received(address, /* operator_ */ address from_, uint256 sftId_, bytes calldata /* data_ */ )
external
virtual
override
returns (bytes4)
{
IERC3525 openFundShare = IERC3525(msg.sender);
uint256 openFundShareSlot = openFundShare.slotOf(sftId_);
require(
ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).isSftSlotDepositAllowed(
address(openFundShare), openFundShareSlot
),
"SolvBTCRouter: sft slot not allowed"
);
if (from_ == openFundMarket || from_ == solvBTCMultiAssetPool) {
return IERC721Receiver.onERC721Received.selector;
}
address sftIdOwner = openFundShare.ownerOf(sftId_);
require(sftIdOwner == address(this), "SolvBTCRouter: not owned sft id");
uint256 openFundShareValue = openFundShare.balanceOf(sftId_);
require(openFundShareValue > 0, "SolvBTCRouter: stake amount cannot be 0");
openFundShare.approve(solvBTCMultiAssetPool, sftId_);
ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).deposit(address(openFundShare), sftId_, openFundShareValue);
address solvBTC =
ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).getERC20(address(openFundShare), openFundShareSlot);
ERC20TransferHelper.doTransferOut(solvBTC, payable(from_), openFundShareValue);
emit Stake(solvBTC, from_, address(openFundShare), openFundShareSlot, sftId_, openFundShareValue);
return IERC721Receiver.onERC721Received.selector;
}
function stake(address sftAddress_, uint256 sftId_, uint256 amount_) external virtual nonReentrant {
IERC3525 openFundShare = IERC3525(sftAddress_);
uint256 openFundShareSlot = openFundShare.slotOf(sftId_);
require(
ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).isSftSlotDepositAllowed(
address(openFundShare), openFundShareSlot
),
"SolvBTCRouter: sft slot not allowed"
);
require(msg.sender == openFundShare.ownerOf(sftId_), "SolvBTCRouter: caller is not sft owner");
require(amount_ > 0, "SolvBTCRouter: stake amount cannot be 0");
uint256 sftBalance = openFundShare.balanceOf(sftId_);
if (amount_ == sftBalance) {
ERC3525TransferHelper.doSafeTransferIn(sftAddress_, msg.sender, sftId_);
} else if (amount_ < sftBalance) {
uint256 holdingSftId = holdingSftIds[sftAddress_][openFundShareSlot];
if (holdingSftId == 0) {
ERC3525TransferHelper.doTransferIn(sftAddress_, sftId_, amount_);
} else {
ERC3525TransferHelper.doTransfer(sftAddress_, sftId_, holdingSftId, amount_);
}
} else {
revert("SolvBTCRouter: stake amount exceeds sft balance");
}
}
function unstake(address solvBTCAddress_, uint256 amount_, address sft_, uint256 slot_, uint256 sftId_)
external
virtual
nonReentrant
returns (uint256 toSftId_)
{
require(
ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).isSftSlotWithdrawAllowed(sft_, slot_),
"SolvBTCRouter: sft slot not allowed"
);
require(
solvBTCAddress_ == ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).getERC20(sft_, slot_),
"SolvBTCRouter: solvBTC address not matched"
);
require(amount_ > 0, "SolvBTCRouter: unstake amount cannot be 0");
ERC20TransferHelper.doTransferIn(solvBTCAddress_, msg.sender, amount_);
if (holdingSftIds[sft_][slot_] == 0) {
holdingSftIds[sft_][slot_] = ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).withdraw(sft_, slot_, 0, amount_);
} else {
ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).withdraw(sft_, slot_, holdingSftIds[sft_][slot_], amount_);
}
if (sftId_ == 0) {
toSftId_ = ERC3525TransferHelper.doTransferOut(sft_, holdingSftIds[sft_][slot_], msg.sender, amount_);
} else {
require(slot_ == IERC3525(sft_).slotOf(sftId_), "SolvBTCRouter: sftId slot not matched");
require(msg.sender == IERC3525(sft_).ownerOf(sftId_), "SolvBTCRouter: not sft owner");
ERC3525TransferHelper.doTransfer(sft_, holdingSftIds[sft_][slot_], sftId_, amount_);
toSftId_ = sftId_;
}
emit Unstake(solvBTCAddress_, msg.sender, sft_, slot_, toSftId_, amount_);
}
function createSubscription(bytes32 poolId_, uint256 currencyAmount_)
external
virtual
nonReentrant
returns (uint256 shareValue_)
{
require(checkPoolPermission(poolId_), "SolvBTCRouter: pool permission denied");
PoolInfo memory poolInfo = IOpenFundMarket(openFundMarket).poolInfos(poolId_);
IERC3525 openFundShare = IERC3525(poolInfo.poolSFTInfo.openFundShare);
uint256 openFundShareSlot = poolInfo.poolSFTInfo.openFundShareSlot;
ERC20TransferHelper.doTransferIn(poolInfo.currency, msg.sender, currencyAmount_);
ERC20TransferHelper.doApprove(poolInfo.currency, openFundMarket, currencyAmount_);
shareValue_ =
IOpenFundMarket(openFundMarket).subscribe(poolId_, currencyAmount_, 0, uint64(block.timestamp + 300));
uint256 shareCount = openFundShare.balanceOf(address(this));
uint256 shareId = openFundShare.tokenOfOwnerByIndex(address(this), shareCount - 1);
require(openFundShare.slotOf(shareId) == openFundShareSlot, "SolvBTCRouter: incorrect share slot");
require(openFundShare.balanceOf(shareId) == shareValue_, "SolvBTCRouter: incorrect share value");
openFundShare.approve(solvBTCMultiAssetPool, shareId);
ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).deposit(address(openFundShare), shareId, shareValue_);
address solvBTC =
ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).getERC20(address(openFundShare), openFundShareSlot);
ERC20TransferHelper.doTransferOut(solvBTC, payable(msg.sender), shareValue_);
emit CreateSubscription(poolId_, msg.sender, solvBTC, shareValue_, poolInfo.currency, currencyAmount_);
}
function createRedemption(bytes32 poolId_, uint256 redeemAmount_)
external
virtual
nonReentrant
returns (uint256 redemptionId_)
{
PoolInfo memory poolInfo = IOpenFundMarket(openFundMarket).poolInfos(poolId_);
IERC3525 openFundShare = IERC3525(poolInfo.poolSFTInfo.openFundShare);
IERC3525 openFundRedemption = IERC3525(poolInfo.poolSFTInfo.openFundRedemption);
uint256 openFundShareSlot = poolInfo.poolSFTInfo.openFundShareSlot;
require(
ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).isSftSlotWithdrawAllowed(
address(openFundShare), openFundShareSlot
),
"SolvBTCRouter: sft slot not allowed"
);
address solvBTC =
ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).getERC20(address(openFundShare), openFundShareSlot);
ERC20TransferHelper.doTransferIn(solvBTC, msg.sender, redeemAmount_);
uint256 shareId = ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).withdraw(
address(openFundShare), openFundShareSlot, 0, redeemAmount_
);
ERC3525TransferHelper.doApproveId(address(openFundShare), openFundMarket, shareId);
IOpenFundMarket(openFundMarket).requestRedeem(poolId_, shareId, 0, redeemAmount_);
uint256 redemptionBalance = openFundRedemption.balanceOf(address(this));
redemptionId_ = openFundRedemption.tokenOfOwnerByIndex(address(this), redemptionBalance - 1);
require(
openFundRedemption.balanceOf(redemptionId_) == redeemAmount_, "SolvBTCRouter: incorrect redemption value"
);
ERC3525TransferHelper.doTransferOut(address(openFundRedemption), payable(msg.sender), redemptionId_);
emit CreateRedemption(poolId_, msg.sender, solvBTC, redeemAmount_, redemptionId_);
}
function cancelRedemption(bytes32 poolId_, uint256 openFundRedemptionId_) external virtual nonReentrant {
PoolInfo memory poolInfo = IOpenFundMarket(openFundMarket).poolInfos(poolId_);
IERC3525 openFundShare = IERC3525(poolInfo.poolSFTInfo.openFundShare);
IERC3525 openFundRedemption = IERC3525(poolInfo.poolSFTInfo.openFundRedemption);
uint256 openFundShareSlot = poolInfo.poolSFTInfo.openFundShareSlot;
ERC3525TransferHelper.doTransferIn(address(openFundRedemption), msg.sender, openFundRedemptionId_);
ERC3525TransferHelper.doApproveId(address(openFundRedemption), openFundMarket, openFundRedemptionId_);
IOpenFundMarket(openFundMarket).revokeRedeem(poolId_, openFundRedemptionId_);
uint256 shareBalance = openFundShare.balanceOf(address(this));
uint256 shareId = openFundShare.tokenOfOwnerByIndex(address(this), shareBalance - 1);
uint256 shareValue = openFundShare.balanceOf(shareId);
openFundShare.approve(solvBTCMultiAssetPool, shareId);
ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).deposit(address(openFundShare), shareId, shareValue);
address solvBTC =
ISolvBTCMultiAssetPool(solvBTCMultiAssetPool).getERC20(address(openFundShare), openFundShareSlot);
ERC20TransferHelper.doTransferOut(solvBTC, payable(msg.sender), shareValue);
emit CancelRedemption(poolId_, msg.sender, solvBTC, openFundRedemptionId_, shareValue);
}
function checkPoolPermission(bytes32 poolId_) public view virtual returns (bool) {
PoolInfo memory poolInfo = IOpenFundMarket(openFundMarket).poolInfos(poolId_);
if (poolInfo.permissionless) {
return true;
}
address whiteListManager = IOpenFundMarket(openFundMarket).getAddress("OFMWhitelistStrategyManager");
return IOFMWhitelistStrategyManager(whiteListManager).isWhitelisted(poolId_, msg.sender);
}
function setOpenFundMarket(address openFundMarket_) external virtual onlyAdmin {
_setOpenFundMarket(openFundMarket_);
}
function _setOpenFundMarket(address openFundMarket_) internal virtual {
require(openFundMarket_ != address(0), "SolvBTCRouter: invalid openFundMarket");
emit SetOpenFundMarket(openFundMarket, openFundMarket_);
openFundMarket = openFundMarket_;
}
function setSolvBTCMultiAssetPool(address solvBTCMultiAssetPool_) external virtual onlyAdmin {
_setSolvBTCMultiAssetPool(solvBTCMultiAssetPool_);
}
function _setSolvBTCMultiAssetPool(address solvBTCMultiAssetPool_) internal virtual {
require(solvBTCMultiAssetPool_ != address(0), "SolvBTCRouter: invalid solvBTCMultiAssetPool");
emit SetSolvBTCMultiAssetPool(solvBTCMultiAssetPool, solvBTCMultiAssetPool_);
solvBTCMultiAssetPool = solvBTCMultiAssetPool_;
}
uint256[47] private __gap;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
abstract contract AdminControlUpgradeable is Initializable {
event NewAdmin(address oldAdmin, address newAdmin);
event NewPendingAdmin(address oldPendingAdmin, address newPendingAdmin);
address public admin;
address public pendingAdmin;
modifier onlyAdmin() {
require(msg.sender == admin, "only admin");
_;
}
modifier onlyPendingAdmin() {
require(msg.sender == pendingAdmin, "only pending admin");
_;
}
function __AdminControl_init(address admin_) internal onlyInitializing {
__AdminControl_init_unchained(admin_);
}
function __AdminControl_init_unchained(address admin_) internal onlyInitializing {
admin = admin_;
emit NewAdmin(address(0), admin_);
}
function transferAdmin(address newPendingAdmin_) external virtual onlyAdmin {
emit NewPendingAdmin(pendingAdmin, newPendingAdmin_);
pendingAdmin = newPendingAdmin_;
}
function acceptAdmin() external virtual onlyPendingAdmin {
emit NewAdmin(admin, pendingAdmin);
admin = pendingAdmin;
delete pendingAdmin;
}
uint256[48] private __gap;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
abstract contract GovernorControlUpgradeable is Initializable {
event NewGovernor(address oldGovernor, address newGovernor);
event NewPendingGovernor(address oldPendingGovernor, address newPendingGovernor);
address public governor;
address public pendingGovernor;
modifier onlyGovernor() {
require(governor == msg.sender, "only governor");
_;
}
modifier onlyPendingGovernor() {
require(pendingGovernor == msg.sender, "only governor");
_;
}
function __GovernorControl_init(address governor_) internal onlyInitializing {
__GovernorControl_init_unchained(governor_);
}
function __GovernorControl_init_unchained(address governor_) internal onlyInitializing {
governor = governor_;
emit NewGovernor(address(0), governor_);
}
function transferGovernance(address newPendingGovernor_) external virtual onlyGovernor {
emit NewPendingGovernor(pendingGovernor, newPendingGovernor_);
pendingGovernor = newPendingGovernor_;
}
function acceptGovernance() external virtual onlyPendingGovernor {
emit NewGovernor(governor, pendingGovernor);
governor = pendingGovernor;
delete pendingGovernor;
}
uint256[48] private __gap;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import "./IERC721.sol";
interface IERC3525 is IERC721 {
function valueDecimals() external view returns (uint8);
function balanceOf(uint256 tokenId) external view returns (uint256);
function slotOf(uint256 tokenId) external view returns (uint256);
function allowance(uint256 tokenId, address operator) external view returns (uint256);
function approve(address operator, uint256 tokenId) external payable;
function approve(uint256 tokenId, address operator, uint256 value) external payable;
function transferFrom(uint256 fromTokenId, uint256 toTokenId, uint256 value) external payable;
function transferFrom(uint256 fromTokenId, address to, uint256 value) external payable returns (uint256);
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
interface IERC3525Receiver {
function onERC3525Received(address operator, uint256 fromTokenId, uint256 toTokenId, uint256 value, bytes calldata data) external returns (bytes4);
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
interface IERC721 {
function balanceOf(address owner) external view returns (uint256);
function ownerOf(uint256 tokenId) external view returns (address);
function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256);
function getApproved(uint256 tokenId) external view returns (address);
function isApprovedForAll(address owner, address operator) external view returns (bool);
function approve(address approved, uint256 tokenId) external payable;
function setApprovalForAll(address operator, bool approved) external;
function transferFrom(address from, address to, uint256 tokenId) external payable;
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external payable;
function safeTransferFrom(address from, address to, uint256 tokenId) external payable;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
interface IERC721Receiver {
function onERC721Received(address operator, address from, uint256 tokenId, bytes calldata data) external returns (bytes4);
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
struct SubscribeLimitInfo {
uint256 hardCap;
uint256 subscribeMin;
uint256 subscribeMax;
uint64 fundraisingStartTime;
uint64 fundraisingEndTime;
}
struct PoolSFTInfo {
address openFundShare;
address openFundRedemption;
uint256 openFundShareSlot;
uint256 latestRedeemSlot;
}
struct PoolFeeInfo {
uint16 carryRate;
address carryCollector;
uint64 latestProtocolFeeSettleTime;
}
struct ManagerInfo {
address poolManager;
address subscribeNavManager;
address redeemNavManager;
}
struct PoolInfo {
PoolSFTInfo poolSFTInfo;
PoolFeeInfo poolFeeInfo;
ManagerInfo managerInfo;
SubscribeLimitInfo subscribeLimitInfo;
address vault;
address currency;
address navOracle;
uint64 valueDate;
bool permissionless;
uint256 fundraisingAmount;
}
interface IOpenFundMarket {
function subscribe(bytes32 poolId, uint256 currencyAmount, uint256 openFundShareId, uint64 expireTime)
external
returns (uint256 value_);
function requestRedeem(bytes32 poolId, uint256 openFundShareId, uint256 openFundRedemptionId, uint256 redeemValue)
external;
function revokeRedeem(bytes32 poolId, uint256 openFundRedemptionId) external;
function poolInfos(bytes32 poolId) external view returns (PoolInfo memory);
function getAddress(bytes32 name) external view returns (address);
function purchasedRecords(bytes32 poolId, address buyer) external view returns (uint256);
}
interface IOFMWhitelistStrategyManager {
function isWhitelisted(bytes32 poolId_, address buyer_) external view returns (bool);
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
interface ERC20Interface {
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
}
// helper methods for interacting with ERC20 tokens and sending ETH that do not consistently return true/false
library ERC20TransferHelper {
address internal constant ETH_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
function doApprove(address underlying, address spender, uint256 amount) internal {
require(underlying.code.length > 0, "invalid underlying");
(bool success, bytes memory data) = underlying.call(
abi.encodeWithSelector(
ERC20Interface.approve.selector,
spender,
amount
)
);
require(success && (data.length == 0 || abi.decode(data, (bool))), "SAF");
}
function doTransferIn(address underlying, address from, uint256 amount) internal {
if (underlying == ETH_ADDRESS) {
// Sanity checks
require(tx.origin == from || msg.sender == from, "sender mismatch");
require(msg.value >= amount, "value mismatch");
} else {
require(underlying.code.length > 0, "invalid underlying");
(bool success, bytes memory data) = underlying.call(
abi.encodeWithSelector(
ERC20Interface.transferFrom.selector,
from,
address(this),
amount
)
);
require(success && (data.length == 0 || abi.decode(data, (bool))), "STF");
}
}
function doTransferOut(address underlying, address payable to, uint256 amount) internal {
if (underlying == ETH_ADDRESS) {
(bool success, ) = to.call{value: amount}(new bytes(0));
require(success, "STE");
} else {
require(underlying.code.length > 0, "invalid underlying");
(bool success, bytes memory data) = underlying.call(
abi.encodeWithSelector(
ERC20Interface.transfer.selector,
to,
amount
)
);
require(success && (data.length == 0 || abi.decode(data, (bool))), "ST");
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
interface ERC721Interface {
function approve(address to, uint256 tokenId) external;
function transferFrom(address from, address to, uint256 tokenId) external;
function safeTransferFrom(address from, address to, uint256 tokenId) external;
}
interface ERC3525Interface {
function approve(uint256 tokenId, address to, uint256 allowance) external payable;
function transferFrom(uint256 fromTokenId, uint256 toTokenId, uint256 value) external payable;
function transferFrom(uint256 fromTokenId, address to, uint256 value) external payable returns (uint256);
}
library ERC3525TransferHelper {
function doApproveId(address underlying, address to, uint256 tokenId) internal {
ERC721Interface token = ERC721Interface(underlying);
token.approve(to, tokenId);
}
function doApproveValue(address underlying, uint256 tokenId, address to, uint256 allowance) internal {
ERC3525Interface token = ERC3525Interface(underlying);
token.approve(tokenId, to, allowance);
}
function doTransferIn(address underlying, address from, uint256 tokenId) internal {
ERC721Interface token = ERC721Interface(underlying);
token.transferFrom(from, address(this), tokenId);
}
function doSafeTransferIn(address underlying, address from, uint256 tokenId) internal {
ERC721Interface token = ERC721Interface(underlying);
token.safeTransferFrom(from, address(this), tokenId);
}
function doSafeTransferOut(address underlying, address to, uint256 tokenId) internal {
ERC721Interface token = ERC721Interface(underlying);
token.safeTransferFrom(address(this), to, tokenId);
}
function doTransferOut(address underlying, address to, uint256 tokenId) internal {
ERC721Interface token = ERC721Interface(underlying);
token.transferFrom(address(this), to, tokenId);
}
function doTransferIn(address underlying, uint256 fromTokenId, uint256 value) internal returns (uint256 newTokenId) {
ERC3525Interface token = ERC3525Interface(underlying);
return token.transferFrom(fromTokenId, address(this), value);
}
function doTransferOut(address underlying, uint256 fromTokenId, address to, uint256 value) internal returns (uint256 newTokenId) {
ERC3525Interface token = ERC3525Interface(underlying);
newTokenId = token.transferFrom(fromTokenId, to, value);
}
function doTransfer(address underlying, address from, address to, uint256 tokenId) internal {
ERC721Interface token = ERC721Interface(underlying);
token.transferFrom(from, to, tokenId);
}
function doTransfer(address underlying, uint256 fromTokenId, uint256 toTokenId, uint256 value) internal {
ERC3525Interface token = ERC3525Interface(underlying);
token.transferFrom(fromTokenId, toTokenId, value);
}
}