USDC+ Exchange Rate Library
A Rust library for computing real-time exchange rates between USDC and USDC+ tokens in the Reflect Protocol, incorporating live yield from lending.
Overview
This crate provides four core functions for calculating USDC ↔ USDC+ conversions:
-
Deposit (USDC → USDC+)
exchange_rate_usdc_input– raw-data versionexchange_rate_usdc_accounts– AnchorAccountInfovalidated version
-
Redeem (USDC+ → USDC)
exchange_rate_receipt_input– raw-data versionexchange_rate_receipt_accounts– AnchorAccountInfovalidated version
All functions automatically incorporates pending yield before computing the exchange rate.
Usage
Deposit Example (USDC → USDC+)
use ;
// With raw account bytes:
let tokens = exchange_rate_usdc_input?;
// With Anchor AccountInfo (validates account addresses):
let tokens_validated = exchange_rate_usdc_accounts?;
Redeem Example (USDC+ → USDC)
use ;
// With raw account bytes:
let usdc = exchange_rate_receipt_input?;
// With Anchor AccountInfo (validates account addresses):
let usdc_validated = exchange_rate_receipt_accounts?;
Function Details
1. exchange_rate_usdc_input
Calculates how many USDC+ tokens you receive for a USDC deposit using raw account data.
- Raw account data only (faster, no address validation)
- Incorporate pending yield computing the deposit exchange rate
- Returns USDC+ amount with 6 decimal places
2. exchange_rate_usdc_accounts
Validates accounts before calling exchange_rate_usdc_input.
- Validates each account matches Reflect Protocol addresses (
require!) - Prevents account substitution attacks
- Performs the same calculation as
exchange_rate_usdc_inputbut with account address validation."
3. exchange_rate_receipt_input
Calculates how much USDC you receive for redeeming USDC+ tokens using raw account data.
- Raw account data only (no validation)
- Incorporates pending yield before computing redemption rate
- Returns USDC amount with 6 decimal places
4. exchange_rate_receipt_accounts
Validates accounts before calling exchange_rate_receipt_input.
- Validates all account addresses
- Performs the same calculation as
exchange_rate_receipt_inputbut with account address validation."
Equations
Deposit: USDC+ = USDC × effective_supply / deposited_vault_value
Redeem: USDC = USDC+ × deposited_vault_value / effective_supply
Where:
effective_supply= total user sharesdeposited_vault_value= vault’s actual USDC value
Security
- Use
*_accountsvariants when running inside an Anchor program for account validation. - Raw-data variants are faster but assume you trust the account data source.
Error Handling
All functions return Result<u64> and may fail with:
InvalidAccount– if account validation failsMathError– division by zero or overflowDeserializationError– invalid or truncated account dataInsufficientData– provided account data is too short