|
| 1 | +//! # Votes Module |
| 2 | +//! |
| 3 | +//! This module provides utilities for tracking voting power per account with |
| 4 | +//! historical checkpoints. It supports delegation (an account can delegate its |
| 5 | +//! voting power to another account) and provides historical vote queries at any |
| 6 | +//! past timestamp. |
| 7 | +//! |
| 8 | +//! # Core Concepts |
| 9 | +//! |
| 10 | +//! - **Voting Units**: The base unit of voting power, typically 1:1 with token |
| 11 | +//! balance |
| 12 | +//! - **Delegation**: Accounts can delegate their voting power to another |
| 13 | +//! account (delegatee). **Only delegated voting power counts as votes** while |
| 14 | +//! undelegated voting units are not counted. Self-delegation is required for |
| 15 | +//! an account to use its own voting power. |
| 16 | +//! - **Checkpoints**: Historical snapshots of voting power at specific |
| 17 | +//! timestamps |
| 18 | +//! |
| 19 | +//! # Usage |
| 20 | +//! |
| 21 | +//! This module is to be integrated into a token contract and is responsible |
| 22 | +//! for: |
| 23 | +//! - Overriding the transfer method to call `transfer_voting_units` on every |
| 24 | +//! balance change (mint/burn/transfer), as shown in the example below |
| 25 | +//! - Exposing delegation functionality to users |
| 26 | +//! |
| 27 | +//! # Example |
| 28 | +//! |
| 29 | +//! ```ignore |
| 30 | +//! use stellar_governance::votes::{ |
| 31 | +//! delegate, get_votes, get_votes_at_checkpoint, transfer_voting_units, |
| 32 | +//! }; |
| 33 | +//! |
| 34 | +//! // Override your token contract's transfer to update voting units: |
| 35 | +//! pub fn transfer(e: &Env, from: Address, to: Address, amount: i128) { |
| 36 | +//! // ... perform transfer logic ... |
| 37 | +//! transfer_voting_units(e, Some(&from), Some(&to), amount as u128); |
| 38 | +//! } |
| 39 | +//! |
| 40 | +//! // Expose delegation: |
| 41 | +//! pub fn delegate(e: &Env, account: Address, delegatee: Address) { |
| 42 | +//! votes::delegate(e, &account, &delegatee); |
| 43 | +//! } |
| 44 | +//! ``` |
| 45 | +
|
| 46 | +mod storage; |
| 47 | + |
| 48 | +#[cfg(test)] |
| 49 | +mod test; |
| 50 | + |
| 51 | +use soroban_sdk::{contracterror, contractevent, contracttrait, Address, Env}; |
| 52 | + |
| 53 | +pub use crate::votes::storage::{ |
| 54 | + delegate, get_checkpoint, get_delegate, get_total_supply, get_total_supply_at_checkpoint, |
| 55 | + get_votes, get_votes_at_checkpoint, get_voting_units, num_checkpoints, transfer_voting_units, |
| 56 | + Checkpoint, CheckpointType, VotesStorageKey, |
| 57 | +}; |
| 58 | + |
| 59 | +/// Trait for contracts that support vote tracking with delegation. |
| 60 | +/// |
| 61 | +/// This trait defines the interface for vote tracking functionality. |
| 62 | +/// Contracts implementing this trait can be used in governance systems |
| 63 | +/// that require historical vote queries and delegation. |
| 64 | +/// |
| 65 | +/// # Implementation Notes |
| 66 | +/// |
| 67 | +/// The implementing contract must: |
| 68 | +/// - Call `transfer_voting_units` on every balance change |
| 69 | +/// - Expose `delegate` functionality to users |
| 70 | +#[contracttrait] |
| 71 | +pub trait Votes { |
| 72 | + /// Returns the current voting power (delegated votes) of an account. |
| 73 | + /// |
| 74 | + /// Returns `0` if the account has no delegated voting power or does not |
| 75 | + /// exist in the contract. |
| 76 | + /// |
| 77 | + /// # Arguments |
| 78 | + /// |
| 79 | + /// * `e` - Access to the Soroban environment. |
| 80 | + /// * `account` - The address to query voting power for. |
| 81 | + fn get_votes(e: &Env, account: Address) -> u128 { |
| 82 | + get_votes(e, &account) |
| 83 | + } |
| 84 | + |
| 85 | + /// Returns the voting power (delegated votes) of an account at a specific |
| 86 | + /// past timestamp. |
| 87 | + /// |
| 88 | + /// Returns `0` if the account had no delegated voting power at the given |
| 89 | + /// timepoint or does not exist in the contract. |
| 90 | + /// |
| 91 | + /// # Arguments |
| 92 | + /// |
| 93 | + /// * `e` - Access to the Soroban environment. |
| 94 | + /// * `account` - The address to query voting power for. |
| 95 | + /// * `timepoint` - The timestamp to query (must be in the past). |
| 96 | + /// |
| 97 | + /// # Errors |
| 98 | + /// |
| 99 | + /// * [`VotesError::FutureLookup`] - If `timepoint` >= current timestamp. |
| 100 | + fn get_votes_at_checkpoint(e: &Env, account: Address, timepoint: u64) -> u128 { |
| 101 | + get_votes_at_checkpoint(e, &account, timepoint) |
| 102 | + } |
| 103 | + |
| 104 | + /// Returns the current total supply of voting units. |
| 105 | + /// |
| 106 | + /// This tracks all voting units in circulation (regardless of delegation |
| 107 | + /// status), not just delegated votes. |
| 108 | + /// |
| 109 | + /// Returns `0` if no voting units exist. |
| 110 | + /// |
| 111 | + /// # Arguments |
| 112 | + /// |
| 113 | + /// * `e` - Access to the Soroban environment. |
| 114 | + fn get_total_supply(e: &Env) -> u128 { |
| 115 | + get_total_supply(e) |
| 116 | + } |
| 117 | + |
| 118 | + /// Returns the total supply of voting units at a specific past timestamp. |
| 119 | + /// |
| 120 | + /// This tracks all voting units in circulation (regardless of delegation |
| 121 | + /// status), not just delegated votes. |
| 122 | + /// |
| 123 | + /// Returns `0` if there were no voting units at the given timepoint. |
| 124 | + /// |
| 125 | + /// # Arguments |
| 126 | + /// |
| 127 | + /// * `e` - Access to the Soroban environment. |
| 128 | + /// * `timepoint` - The timestamp to query (must be in the past). |
| 129 | + /// |
| 130 | + /// # Errors |
| 131 | + /// |
| 132 | + /// * [`VotesError::FutureLookup`] - If `timepoint` >= current timestamp. |
| 133 | + fn get_total_supply_at_checkpoint(e: &Env, timepoint: u64) -> u128 { |
| 134 | + get_total_supply_at_checkpoint(e, timepoint) |
| 135 | + } |
| 136 | + |
| 137 | + /// Returns the current delegate for an account. |
| 138 | + /// |
| 139 | + /// # Arguments |
| 140 | + /// |
| 141 | + /// * `e` - Access to the Soroban environment. |
| 142 | + /// * `account` - The address to query the delegate for. |
| 143 | + /// |
| 144 | + /// # Returns |
| 145 | + /// |
| 146 | + /// * `Some(Address)` - The delegate address if delegation is set. |
| 147 | + /// * `None` - If the account has not delegated. |
| 148 | + fn get_delegate(e: &Env, account: Address) -> Option<Address> { |
| 149 | + get_delegate(e, &account) |
| 150 | + } |
| 151 | + |
| 152 | + /// Delegates voting power from `account` to `delegatee`. |
| 153 | + /// |
| 154 | + /// # Arguments |
| 155 | + /// |
| 156 | + /// * `e` - Access to the Soroban environment. |
| 157 | + /// * `account` - The account delegating its voting power. |
| 158 | + /// * `delegatee` - The account receiving the delegated voting power. |
| 159 | + /// |
| 160 | + /// # Events |
| 161 | + /// |
| 162 | + /// * topics - `["delegate_changed", delegator: Address]` |
| 163 | + /// * data - `[from_delegate: Option<Address>, to_delegate: Address]` |
| 164 | + /// |
| 165 | + /// * topics - `["delegate_votes_changed", delegate: Address]` |
| 166 | + /// * data - `[previous_votes: u128, new_votes: u128]` |
| 167 | + /// |
| 168 | + /// # Notes |
| 169 | + /// |
| 170 | + /// Authorization for `account` is required. |
| 171 | + fn delegate(e: &Env, account: Address, delegatee: Address) { |
| 172 | + delegate(e, &account, &delegatee); |
| 173 | + } |
| 174 | +} |
| 175 | +// ################## ERRORS ################## |
| 176 | + |
| 177 | +/// Errors that can occur in votes operations. |
| 178 | +#[contracterror] |
| 179 | +#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 180 | +#[repr(u32)] |
| 181 | +pub enum VotesError { |
| 182 | + /// The timepoint is in the future |
| 183 | + FutureLookup = 4100, |
| 184 | + /// Arithmetic overflow occurred |
| 185 | + MathOverflow = 4101, |
| 186 | + /// Attempting to transfer more voting units than available |
| 187 | + InsufficientVotingUnits = 4102, |
| 188 | + /// Attempting to delegate to the same delegate that is already set |
| 189 | + SameDelegate = 4103, |
| 190 | + /// A checkpoint that was expected to exist was not found in storage |
| 191 | + CheckpointNotFound = 4104, |
| 192 | +} |
| 193 | + |
| 194 | +// ################## CONSTANTS ################## |
| 195 | + |
| 196 | +const DAY_IN_LEDGERS: u32 = 17280; |
| 197 | + |
| 198 | +/// TTL extension amount for storage entries (in ledgers) |
| 199 | +pub const VOTES_EXTEND_AMOUNT: u32 = 30 * DAY_IN_LEDGERS; |
| 200 | + |
| 201 | +/// TTL threshold for extending storage entries (in ledgers) |
| 202 | +pub const VOTES_TTL_THRESHOLD: u32 = VOTES_EXTEND_AMOUNT - DAY_IN_LEDGERS; |
| 203 | + |
| 204 | +// ################## EVENTS ################## |
| 205 | + |
| 206 | +/// Event emitted when an account changes its delegate. |
| 207 | +#[contractevent] |
| 208 | +#[derive(Clone, Debug, Eq, PartialEq)] |
| 209 | +pub struct DelegateChanged { |
| 210 | + /// The account that changed its delegate |
| 211 | + #[topic] |
| 212 | + pub delegator: Address, |
| 213 | + /// The previous delegate (if any) |
| 214 | + pub from_delegate: Option<Address>, |
| 215 | + /// The new delegate |
| 216 | + pub to_delegate: Address, |
| 217 | +} |
| 218 | + |
| 219 | +/// Emits an event when an account changes its delegate. |
| 220 | +/// |
| 221 | +/// # Arguments |
| 222 | +/// |
| 223 | +/// * `e` - Access to Soroban environment. |
| 224 | +/// * `delegator` - The account that changed its delegate. |
| 225 | +/// * `from_delegate` - The previous delegate (if any). |
| 226 | +/// * `to_delegate` - The new delegate. |
| 227 | +pub fn emit_delegate_changed( |
| 228 | + e: &Env, |
| 229 | + delegator: &Address, |
| 230 | + from_delegate: Option<Address>, |
| 231 | + to_delegate: &Address, |
| 232 | +) { |
| 233 | + DelegateChanged { |
| 234 | + delegator: delegator.clone(), |
| 235 | + from_delegate, |
| 236 | + to_delegate: to_delegate.clone(), |
| 237 | + } |
| 238 | + .publish(e); |
| 239 | +} |
| 240 | + |
| 241 | +/// Event emitted when a delegate's voting power changes. |
| 242 | +#[contractevent] |
| 243 | +#[derive(Clone, Debug, Eq, PartialEq)] |
| 244 | +pub struct DelegateVotesChanged { |
| 245 | + /// The delegate whose voting power changed |
| 246 | + #[topic] |
| 247 | + pub delegate: Address, |
| 248 | + /// The previous voting power |
| 249 | + pub previous_votes: u128, |
| 250 | + /// The new voting power |
| 251 | + pub new_votes: u128, |
| 252 | +} |
| 253 | + |
| 254 | +/// Emits an event when a delegate's voting power changes. |
| 255 | +/// |
| 256 | +/// # Arguments |
| 257 | +/// |
| 258 | +/// * `e` - Access to Soroban environment. |
| 259 | +/// * `delegate` - The delegate whose voting power changed. |
| 260 | +/// * `previous_votes` - The previous voting power. |
| 261 | +/// * `new_votes` - The new voting power. |
| 262 | +pub fn emit_delegate_votes_changed( |
| 263 | + e: &Env, |
| 264 | + delegate: &Address, |
| 265 | + previous_votes: u128, |
| 266 | + new_votes: u128, |
| 267 | +) { |
| 268 | + DelegateVotesChanged { delegate: delegate.clone(), previous_votes, new_votes }.publish(e); |
| 269 | +} |
0 commit comments