Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions contracts/token/IRVirtualConverter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface IRVirtualConverter {
function convertVirtualToRVirtual(
uint256 amount,
address rVirtualReceiver
) external;
}
93 changes: 93 additions & 0 deletions contracts/token/RVirtualConverter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/ReentrancyGuardUpgradeable.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

/// @notice Open, permissionless 1:1 converter from VIRTUAL to rVirtual.
///
/// Pre-funded with the full rVirtual supply before launch - conversions draw down that
/// balance rather than minting on demand. Any caller (a regular wallet, or veVirtual's
/// convertVeVirtualToRVirtual()) uses the exact same convertVirtualToRVirtual() entrypoint;
/// there is no privileged "veVirtual-only" path here.
contract RVirtualConverter is
Initializable,
ReentrancyGuardUpgradeable,
AccessControlUpgradeable,
UUPSUpgradeable
{
using SafeERC20 for IERC20;

bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");

address public virtualToken;
address public rVirtualToken;
address public adminWallet;

event ConvertedVirtualToRVirtual(
address indexed caller,
address indexed rVirtualReceiver,
uint256 amount
);
event AdminWalletUpdated(address adminWallet);
event VirtualWithdrawn(address adminWallet, uint256 amount);

function initialize(
address virtualToken_,
address rVirtualToken_
) external initializer {
__ReentrancyGuard_init();
__AccessControl_init();
__UUPSUpgradeable_init();

require(virtualToken_ != address(0), "Invalid virtual token");
require(rVirtualToken_ != address(0), "Invalid rVirtual token");
virtualToken = virtualToken_;
rVirtualToken = rVirtualToken_;

_grantRole(DEFAULT_ADMIN_ROLE, _msgSender());
_grantRole(ADMIN_ROLE, _msgSender());
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation initializers left enabled

Medium Severity

RVirtualConverter is UUPS-upgradeable but never disables initializers on the implementation. Unlike other upgradeable contracts here (for example Minter), anyone can call initialize on the implementation itself, take over its admin roles, and empty any tokens sent to that address.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b98ca31. Configure here.


/// @notice Convert `amount` VIRTUAL (pulled from the caller) into `amount` rVirtual,
/// sent to `rVirtualReceiver`. Fully open - no allowlist, no cap.
function convertVirtualToRVirtual(
uint256 amount,
address rVirtualReceiver
) external nonReentrant {
require(amount > 0, "Amount must be greater than 0");
require(rVirtualReceiver != address(0), "Invalid receiver");

IERC20(virtualToken).safeTransferFrom(
_msgSender(),
address(this),
amount
);
IERC20(rVirtualToken).safeTransfer(rVirtualReceiver, amount);

emit ConvertedVirtualToRVirtual(_msgSender(), rVirtualReceiver, amount);
}

function setAdminWallet(address adminWallet_) external onlyRole(ADMIN_ROLE) {
require(adminWallet_ != address(0), "Invalid admin wallet");
adminWallet = adminWallet_;
emit AdminWalletUpdated(adminWallet_);
}

/// @notice Withdraw accumulated VIRTUAL out of this contract. Only VIRTUAL - there is
/// intentionally no withdrawal path for rVirtual or any other token here, so
/// adminWallet is never exposed to rVirtual's transfer tax.
function withdrawVirtual(uint256 amount) external nonReentrant {
require(_msgSender() == adminWallet, "Only admin wallet");
IERC20(virtualToken).safeTransfer(adminWallet, amount);
emit VirtualWithdrawn(adminWallet, amount);
}

function _authorizeUpgrade(
address newImplementation
) internal override onlyRole(ADMIN_ROLE) {}
}
16 changes: 16 additions & 0 deletions contracts/token/RVirtualConverterV2Mock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "./RVirtualConverter.sol";

/// @notice Test-only V2 used to verify RVirtualConverter's UUPS upgrade path round-trips
/// cleanly. Adds one new event + trigger function on top of V1; never deployed to
/// production - exists purely so a test can upgrade forward, prove the new code is
/// live, then upgrade back and confirm the final bytecode matches the original V1.
contract RVirtualConverterV2Mock is RVirtualConverter {
event V2UpgradeMarker(string message);

function triggerV2Marker() external {
emit V2UpgradeMarker("upgraded");
}
}
53 changes: 53 additions & 0 deletions contracts/token/veVirtual.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/structs/Checkpoints.sol";
import "@openzeppelin/contracts-upgradeable/governance/utils/VotesUpgradeable.sol";
import "./IRVirtualConverter.sol";

contract veVirtual is
Initializable,
Expand Down Expand Up @@ -47,6 +48,14 @@ contract veVirtual is
event AdminUnlocked(bool adminUnlocked);
bool public adminUnlocked;

address public rVirtualConverter;
event RVirtualConverterUpdated(address rVirtualConverter);
event ConvertedVeVirtualToRVirtual(
address indexed user,
uint256 id,
uint256 amount
);

function initialize(
address baseToken_,
uint8 maxWeeks_
Expand Down Expand Up @@ -298,4 +307,48 @@ contract veVirtual is
}
return amount;
}

/**
* @notice Set the RVirtualConverter contract that convertVeVirtualToRVirtual() forwards to.
*/
function setRVirtualConverter(
address rVirtualConverter_
) external onlyRole(ADMIN_ROLE) {
require(rVirtualConverter_ != address(0), "Invalid converter");
rVirtualConverter = rVirtualConverter_;
emit RVirtualConverterUpdated(rVirtualConverter_);
}

/**
* @notice Voluntarily give up a lock's underlying VIRTUAL (and its voting power) in
* exchange for an equal amount of rVirtual. Unlike withdraw(), this does not
* require the lock to be matured - the user is explicitly forfeiting the
* remaining lock time. The lock is deleted regardless of its autoRenew state.
* @dev Approves RVirtualConverter for exactly this lock's amount and calls its
* convertVirtualToRVirtual() - the same open entrypoint any wallet can call directly.
* There is no veVirtual-specific path on the converter side.
*/
function convertVeVirtualToRVirtual(uint256 id) external nonReentrant {
require(rVirtualConverter != address(0), "Converter not set");
address account = _msgSender();
uint256 index = _indexOf(account, id);
Lock memory lock = locks[account][index];

uint256 amount = lock.amount;

uint256 lastIndex = locks[account].length - 1;
if (index != lastIndex) {
locks[account][index] = locks[account][lastIndex];
}
locks[account].pop();

IERC20(baseToken).approve(rVirtualConverter, amount);
IRVirtualConverter(rVirtualConverter).convertVirtualToRVirtual(
amount,
account
);

emit ConvertedVeVirtualToRVirtual(account, id, amount);
_transferVotingUnits(account, address(0), amount);
}
}
Loading