-
Notifications
You must be signed in to change notification settings - Fork 63
feat: add rVirtual conversion via RVirtualConverter #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
koo-virtuals
wants to merge
13
commits into
main
Choose a base branch
from
feat/vp-2434
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b98ca31
feat: add rVirtual conversion via RVirtualConverter
koo-virtuals 0d4be6e
fix(M-01): revert on rVirtual under-delivery in convertVirtualToRVirtual
koo-virtuals 747389b
docs(M-02): document the 18-decimals guarantee for VIRTUAL/rVirtual
koo-virtuals 3e1bc13
feat(L-02): route incoming VIRTUAL directly to a treasury multisig
koo-virtuals 61c57dd
fix(L-04): use SafeERC20.forceApprove instead of raw approve()
koo-virtuals cefee98
fix(L-09): validate token identity at converter init and wiring time
koo-virtuals 5ac0cd9
chore: track test-only mocks used by the rVirtual conversion test suite
koo-virtuals 617035c
fix(L-10): add previous value + indexed topics to AdminWalletUpdated
koo-virtuals 546ec73
chore(I-05): move RVirtualConverterV2Mock.sol into contracts/token/mo…
koo-virtuals 6f1901f
fix(I-06): remove withdrawVirtual()/adminWallet - superseded by L-02
koo-virtuals b9a2ea1
fix(L-12): disable initializers on the raw implementation contracts
koo-virtuals 9139a68
test(L-12): prove an upgrade adding the constructor preserves proxy s…
koo-virtuals 5a85940
update .gitignore
koo-virtuals File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,4 +22,5 @@ fireblocks_secret.key | |
| .cursor/ | ||
| env* | ||
|
|
||
| lib/ | ||
| lib/ | ||
| docs/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.20; | ||
|
|
||
| interface IRVirtualConverter { | ||
| function convertVirtualToRVirtual( | ||
| uint256 amount, | ||
| address rVirtualReceiver | ||
| ) external; | ||
|
|
||
| /// @notice The VIRTUAL token this converter accepts as input. Exposed so callers | ||
| /// (e.g. veVirtual.setRVirtualConverter) can assert their own base token | ||
| /// matches this converter's before wiring it in (see audit L-09). | ||
| function virtualToken() external view returns (address); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // 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; | ||
| /// @notice Treasury multisig that every conversion's incoming VIRTUAL is sent to | ||
| /// directly. Set once at initialize() and never changed at runtime - VIRTUAL | ||
| /// is never custodied by this contract, so there is no accumulated balance | ||
| /// for a compromised or malicious admin key to sweep (see audit L-02). This | ||
| /// is also why there is no adminWallet/withdrawVirtual() sweep mechanism here | ||
| /// anymore (see audit I-06) - there is nothing left for it to sweep. | ||
| address public treasury; | ||
|
|
||
| /// @custom:oz-upgrades-unsafe-allow constructor | ||
| constructor() { | ||
| _disableInitializers(); | ||
| } | ||
|
|
||
| event ConvertedVirtualToRVirtual( | ||
| address indexed caller, | ||
| address indexed rVirtualReceiver, | ||
| uint256 amount | ||
| ); | ||
|
|
||
| function initialize( | ||
| address virtualToken_, | ||
| address rVirtualToken_, | ||
| address treasury_ | ||
| ) external initializer { | ||
| __ReentrancyGuard_init(); | ||
| __AccessControl_init(); | ||
| __UUPSUpgradeable_init(); | ||
|
|
||
| require(virtualToken_ != address(0), "Invalid virtual token"); | ||
| require(rVirtualToken_ != address(0), "Invalid rVirtual token"); | ||
| require(treasury_ != address(0), "Invalid treasury"); | ||
| require(virtualToken_ != rVirtualToken_, "Tokens must differ"); | ||
| // NOTE (audit M-02): the 1:1 conversion below is a raw-integer transfer with no | ||
| // decimals rescaling. This is safe only because VIRTUAL and rVirtual are both | ||
| // guaranteed by protocol design to use 18 decimals - if either token is ever | ||
| // redeployed/migrated to a different decimals value, this invariant must be | ||
| // re-verified (or an explicit decimals() equivalence check added) before wiring | ||
| // it in here. | ||
| virtualToken = virtualToken_; | ||
| rVirtualToken = rVirtualToken_; | ||
| treasury = treasury_; | ||
|
|
||
| _grantRole(DEFAULT_ADMIN_ROLE, _msgSender()); | ||
| _grantRole(ADMIN_ROLE, _msgSender()); | ||
| } | ||
|
|
||
| /// @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(), | ||
| treasury, | ||
| amount | ||
| ); | ||
|
|
||
| uint256 balanceBefore = IERC20(rVirtualToken).balanceOf(rVirtualReceiver); | ||
| IERC20(rVirtualToken).safeTransfer(rVirtualReceiver, amount); | ||
| uint256 delivered = IERC20(rVirtualToken).balanceOf(rVirtualReceiver) - balanceBefore; | ||
| require(delivered == amount, "rVirtual delivery mismatch"); | ||
|
|
||
| emit ConvertedVirtualToRVirtual(_msgSender(), rVirtualReceiver, delivered); | ||
| } | ||
|
|
||
| function _authorizeUpgrade( | ||
| address newImplementation | ||
| ) internal override onlyRole(ADMIN_ROLE) {} | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.20; | ||
|
|
||
| import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
|
|
||
| /// @notice TEST-ONLY mock modeling a fee-on-transfer / tax token, used to simulate the | ||
| /// real rVirtual (GodToken)'s configurable transfer tax for security PoC purposes | ||
| /// (see verify_H-2.js). NOT used in production - lives under contracts/token/mocks | ||
| /// solely to be reachable by Hardhat's compiler for the test. | ||
| /// | ||
| /// Deducts `feeBps` (out of 10_000) from every transfer/transferFrom, sending the | ||
| /// fee portion to a burn/dead sink so the recipient always receives strictly less | ||
| /// than the nominal transferred amount whenever feeBps > 0. | ||
| contract FeeOnTransferMock is ERC20 { | ||
| uint256 public immutable feeBps; // e.g. 1000 = 10% | ||
| address public immutable feeSink; | ||
|
|
||
| constructor( | ||
| string memory name_, | ||
| string memory symbol_, | ||
| address initialAccount, | ||
| uint256 initialBalance, | ||
| uint256 feeBps_, | ||
| address feeSink_ | ||
| ) ERC20(name_, symbol_) { | ||
| require(feeBps_ <= 10_000, "fee too high"); | ||
| feeBps = feeBps_; | ||
| feeSink = feeSink_; | ||
| _mint(initialAccount, initialBalance); | ||
| } | ||
|
|
||
| function _update(address from, address to, uint256 value) internal override { | ||
| // Mint (from == address(0)) and burn (to == address(0)) pass through untaxed - | ||
| // only regular transfers between two live accounts are taxed, matching typical | ||
| // fee-on-transfer token behavior. | ||
| if (from == address(0) || to == address(0) || feeBps == 0) { | ||
| super._update(from, to, value); | ||
| return; | ||
| } | ||
|
|
||
| uint256 fee = (value * feeBps) / 10_000; | ||
| uint256 net = value - fee; | ||
|
|
||
| super._update(from, to, net); | ||
| if (fee > 0) { | ||
| super._update(from, feeSink, fee); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.20; | ||
|
|
||
| import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
| import "../IRVirtualConverter.sol"; | ||
|
|
||
| /// @notice TEST-ONLY mock simulating a malicious/repointed `rVirtualConverter` (see H-4 PoC, | ||
| /// verify_H-4.js). Implements the exact `IRVirtualConverter` interface that | ||
| /// `veVirtual.convertVeVirtualToRVirtual()` calls, but instead of delivering rVirtual | ||
| /// 1:1, it pulls the approved VIRTUAL via `transferFrom` and routes it to an | ||
| /// attacker-controlled address, delivering ZERO rVirtual back to the victim. It does | ||
| /// NOT revert - the call succeeds silently from veVirtual's perspective, so the | ||
| /// lock deletion (which already happened before this external call) is never rolled | ||
| /// back. | ||
| contract MaliciousConverterMock is IRVirtualConverter { | ||
| address public immutable virtualToken; | ||
| address public immutable attacker; | ||
|
|
||
| constructor(address virtualToken_, address attacker_) { | ||
| virtualToken = virtualToken_; | ||
| attacker = attacker_; | ||
| } | ||
|
|
||
| function convertVirtualToRVirtual( | ||
| uint256 amount, | ||
| address /* rVirtualReceiver */ | ||
| ) external override { | ||
| // Steal the approved VIRTUAL; deliver no rVirtual, and do not revert. | ||
| IERC20(virtualToken).transferFrom(msg.sender, attacker, amount); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.20; | ||
|
|
||
| import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
|
|
||
| /// @notice Minimal mock ERC20 with 6 decimals (like USDC), used ONLY to verify H-3 | ||
| /// (RVirtualConverter never asserts rVirtualToken.decimals() == 18). | ||
| contract MockERC20SixDecimals is ERC20 { | ||
| constructor( | ||
| string memory name, | ||
| string memory symbol, | ||
| address initialAccount, | ||
| uint256 initialBalance | ||
| ) ERC20(name, symbol) { | ||
| _mint(initialAccount, initialBalance); | ||
| } | ||
|
|
||
| function decimals() public pure override returns (uint8) { | ||
| return 6; | ||
| } | ||
|
|
||
| function mint(address to, uint256 amount) public { | ||
| _mint(to, amount); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.20; | ||
|
|
||
| import "../IRVirtualConverter.sol"; | ||
|
|
||
| /// @notice TEST-ONLY mock for verify_H-10.js. Implements IRVirtualConverter but does NOT call | ||
| /// transferFrom at all - simulates the specific scenario Chain Agent 2 analyzed where | ||
| /// the converter call succeeds (no-op) without pulling the approved VIRTUAL, leaving | ||
| /// veVirtual's raw `approve()` allowance (L345) standing after the call returns. | ||
| /// Distinct from MaliciousConverterMock (H-4), which DOES pull funds via transferFrom | ||
| /// and therefore leaves zero residual allowance. | ||
| contract NoOpConverterMock is IRVirtualConverter { | ||
| address public virtualToken; | ||
|
|
||
| constructor(address virtualToken_) { | ||
| virtualToken = virtualToken_; | ||
| } | ||
|
|
||
| function convertVirtualToRVirtual( | ||
| uint256 /* amount */, | ||
| address /* rVirtualReceiver */ | ||
| ) external override { | ||
| // Intentionally does nothing - no transferFrom, no revert. | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.