|
| 1 | +/// Multi-signature arbitration for high-value trades. |
| 2 | +/// |
| 3 | +/// Flow: |
| 4 | +/// 1. Seller calls `create_multisig_trade` with a `MultiSigConfig` |
| 5 | +/// (arbitrators list, threshold, voting_timeout_seconds). |
| 6 | +/// 2. Buyer or seller raises a dispute — voting window opens automatically. |
| 7 | +/// 3. Each panel arbitrator calls `cast_vote` with their preferred resolution. |
| 8 | +/// 4. Once `threshold` arbitrators agree on the same outcome, consensus is |
| 9 | +/// reached and `resolve_dispute` executes it. |
| 10 | +/// 5. If the window expires without consensus, admin calls |
| 11 | +/// `resolve_expired_dispute` — defaults to refunding the buyer. |
| 12 | +
|
| 13 | +use soroban_sdk::{Address, Env, Vec}; |
| 14 | + |
| 15 | +use crate::errors::ContractError; |
| 16 | +use crate::events; |
| 17 | +use crate::storage::{ |
| 18 | + get_all_votes_for_trade, get_trade, has_arbitrator, has_arbitrator_voted, |
| 19 | + save_arbitrator_vote, |
| 20 | +}; |
| 21 | +use crate::types::{ |
| 22 | + ArbitrationConfig, ArbitratorVote, DisputeResolution, MultiSigConfig, Trade, |
| 23 | + TradeStatus, VotingSummary, |
| 24 | +}; |
| 25 | + |
| 26 | +// --------------------------------------------------------------------------- |
| 27 | +// Cast vote |
| 28 | +// --------------------------------------------------------------------------- |
| 29 | + |
| 30 | +pub fn cast_vote( |
| 31 | + env: &Env, |
| 32 | + trade_id: u64, |
| 33 | + arbitrator: &Address, |
| 34 | + resolution: DisputeResolution, |
| 35 | +) -> Result<(), ContractError> { |
| 36 | + let trade = get_trade(env, trade_id)?; |
| 37 | + if trade.status != TradeStatus::Disputed { |
| 38 | + return Err(ContractError::InvalidStatus); |
| 39 | + } |
| 40 | + let config = extract_multisig_config(&trade)?; |
| 41 | + |
| 42 | + if !is_panel_member(arbitrator, &config.arbitrators) { |
| 43 | + return Err(ContractError::Unauthorized); |
| 44 | + } |
| 45 | + if !has_arbitrator(env, arbitrator) { |
| 46 | + return Err(ContractError::ArbitratorNotRegistered); |
| 47 | + } |
| 48 | + if has_arbitrator_voted(env, trade_id, arbitrator) { |
| 49 | + return Err(ContractError::AlreadyVoted); |
| 50 | + } |
| 51 | + if is_voting_expired(env, &config) { |
| 52 | + return Err(ContractError::VotingExpired); |
| 53 | + } |
| 54 | + |
| 55 | + arbitrator.require_auth(); |
| 56 | + |
| 57 | + save_arbitrator_vote( |
| 58 | + env, |
| 59 | + trade_id, |
| 60 | + arbitrator, |
| 61 | + &ArbitratorVote { |
| 62 | + arbitrator: arbitrator.clone(), |
| 63 | + resolution: resolution.clone(), |
| 64 | + timestamp: env.ledger().timestamp(), |
| 65 | + }, |
| 66 | + ); |
| 67 | + |
| 68 | + events::emit_arbitrator_vote_cast(env, trade_id, arbitrator.clone(), resolution); |
| 69 | + Ok(()) |
| 70 | +} |
| 71 | + |
| 72 | +// --------------------------------------------------------------------------- |
| 73 | +// Voting summary / consensus |
| 74 | +// --------------------------------------------------------------------------- |
| 75 | + |
| 76 | +pub fn voting_summary(env: &Env, trade_id: u64) -> Result<VotingSummary, ContractError> { |
| 77 | + let trade = get_trade(env, trade_id)?; |
| 78 | + let config = extract_multisig_config(&trade)?; |
| 79 | + |
| 80 | + let votes = get_all_votes_for_trade(env, trade_id, &config.arbitrators); |
| 81 | + let votes_cast = votes.len() as u32; |
| 82 | + let total_arbitrators = config.arbitrators.len() as u32; |
| 83 | + let expired = is_voting_expired(env, &config); |
| 84 | + let threshold = config.threshold; |
| 85 | + |
| 86 | + // Tally votes per resolution variant |
| 87 | + let mut release_to_buyer: u32 = 0; |
| 88 | + let mut release_to_seller: u32 = 0; |
| 89 | + // Partial votes: Vec of (buyer_bps, count) |
| 90 | + let mut partial: Vec<(u32, u32)> = Vec::new(env); |
| 91 | + |
| 92 | + for i in 0..votes.len() { |
| 93 | + match votes.get(i).unwrap().resolution { |
| 94 | + DisputeResolution::ReleaseToBuyer => release_to_buyer += 1, |
| 95 | + DisputeResolution::ReleaseToSeller => release_to_seller += 1, |
| 96 | + DisputeResolution::Partial { buyer_bps } => { |
| 97 | + let mut found = false; |
| 98 | + for j in 0..partial.len() { |
| 99 | + let (bps, cnt) = partial.get(j).unwrap(); |
| 100 | + if bps == buyer_bps { |
| 101 | + partial.set(j, (bps, cnt + 1)); |
| 102 | + found = true; |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + if !found { |
| 107 | + partial.push_back((buyer_bps, 1)); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + let consensus: Option<DisputeResolution> = if release_to_buyer >= threshold { |
| 114 | + Some(DisputeResolution::ReleaseToBuyer) |
| 115 | + } else if release_to_seller >= threshold { |
| 116 | + Some(DisputeResolution::ReleaseToSeller) |
| 117 | + } else { |
| 118 | + let mut found: Option<DisputeResolution> = None; |
| 119 | + for i in 0..partial.len() { |
| 120 | + let (bps, cnt) = partial.get(i).unwrap(); |
| 121 | + if cnt >= threshold { |
| 122 | + found = Some(DisputeResolution::Partial { buyer_bps: bps }); |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + found |
| 127 | + }; |
| 128 | + |
| 129 | + if consensus.is_some() { |
| 130 | + events::emit_multisig_consensus(env, trade_id); |
| 131 | + } |
| 132 | + |
| 133 | + Ok(VotingSummary { |
| 134 | + total_arbitrators, |
| 135 | + votes_cast, |
| 136 | + threshold, |
| 137 | + has_consensus: consensus.is_some(), |
| 138 | + consensus_resolution: consensus, |
| 139 | + voting_expired: expired, |
| 140 | + }) |
| 141 | +} |
| 142 | + |
| 143 | +// --------------------------------------------------------------------------- |
| 144 | +// Expired-dispute resolution |
| 145 | +// --------------------------------------------------------------------------- |
| 146 | + |
| 147 | +/// Force-resolve after the voting window expires without consensus. |
| 148 | +/// Safe default: refund the buyer. Admin-only. |
| 149 | +pub fn resolve_expired_dispute( |
| 150 | + env: &Env, |
| 151 | + trade_id: u64, |
| 152 | + admin: &Address, |
| 153 | +) -> Result<DisputeResolution, ContractError> { |
| 154 | + let trade = get_trade(env, trade_id)?; |
| 155 | + if trade.status != TradeStatus::Disputed { |
| 156 | + return Err(ContractError::InvalidStatus); |
| 157 | + } |
| 158 | + let config = extract_multisig_config(&trade)?; |
| 159 | + if !is_voting_expired(env, &config) { |
| 160 | + return Err(ContractError::VotingNotExpired); |
| 161 | + } |
| 162 | + admin.require_auth(); |
| 163 | + events::emit_multisig_expired(env, trade_id); |
| 164 | + Ok(DisputeResolution::ReleaseToBuyer) |
| 165 | +} |
| 166 | + |
| 167 | +// --------------------------------------------------------------------------- |
| 168 | +// Helpers |
| 169 | +// --------------------------------------------------------------------------- |
| 170 | + |
| 171 | +fn extract_multisig_config(trade: &Trade) -> Result<MultiSigConfig, ContractError> { |
| 172 | + match &trade.arbitrator { |
| 173 | + Some(ArbitrationConfig::MultiSig(cfg)) => Ok(cfg.clone()), |
| 174 | + _ => Err(ContractError::InvalidStatus), |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +fn is_panel_member(arbitrator: &Address, panel: &Vec<Address>) -> bool { |
| 179 | + for i in 0..panel.len() { |
| 180 | + if panel.get(i).unwrap() == *arbitrator { |
| 181 | + return true; |
| 182 | + } |
| 183 | + } |
| 184 | + false |
| 185 | +} |
| 186 | + |
| 187 | +fn is_voting_expired(env: &Env, config: &MultiSigConfig) -> bool { |
| 188 | + match config.voting_started_at { |
| 189 | + Some(started) => { |
| 190 | + env.ledger().timestamp() > started.saturating_add(config.voting_timeout_seconds) |
| 191 | + } |
| 192 | + None => false, |
| 193 | + } |
| 194 | +} |
0 commit comments