Before performing operations, load encrypted values from their references:
Copy
use privora_sdk_program::prelude::*;// From hash reference stored in accountlet price_ref: EncryptedRef<u8> = account.price_ref;// Load the actual encrypted valuelet price: Encrypted<u8> = price_ref.load()?;
The load() method:
Takes the hash from EncryptedRef
Fetches the ciphertext from the content store via syscall
// Calculate: (a + b) * clet result = a.add(&b)?.mul(&c)?;// With operators (use parentheses carefully)let result = &(&a + &b) * &c;// More readable with intermediate variableslet sum = &a + &b;let result = &sum * &c;
After computation, store results back to the content store:
Copy
// Compute resultlet result: Encrypted<u8> = a.add(&b)?;// Store and get hash referencelet result_ref: EncryptedRef<u8> = result.store()?;// Save reference to accountaccount.result_ref = result_ref;
use privora_sdk_program::prelude::*;pub fn match_orders( buy_price_ref: EncryptedRef<u8>, buy_qty_ref: EncryptedRef<u8>, sell_price_ref: EncryptedRef<u8>, sell_qty_ref: EncryptedRef<u8>,) -> Result<(EncryptedRef<u8>, EncryptedRef<u8>), ProgramError> { // 1. Load all encrypted values let buy_price = buy_price_ref.load()?; let buy_qty = buy_qty_ref.load()?; let sell_price = sell_price_ref.load()?; let sell_qty = sell_qty_ref.load()?; // 2. Check if orders can match (buy >= sell) let can_match: EncryptedBool = buy_price.ge(&sell_price)?; // 3. Calculate fill quantity (minimum of both) let fill_qty: Encrypted<u8> = buy_qty.min(&sell_qty)?; // 4. Use sell price as fill price (standard matching) // The fill price is the sell_price (taker price) // 5. Store results let fill_price_ref = sell_price.store()?; let fill_qty_ref = fill_qty.store()?; Ok((fill_price_ref, fill_qty_ref))}