Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions contract/contracts/predifi-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,63 @@ impl PredifiContract {
pool.state == MarketState::Active
}

/// Validate custom token transfer constraints before executing a transfer.
/// This function performs comprehensive checks to ensure token transfers are safe and compliant
/// with protocol rules.
///
/// # Checks performed:
/// 1. Token address validation (not null/default)
/// 2. Amount validation (positive, not zero)
/// 3. Sender/recipient validation (not same, not null)
/// 4. Token contract callable check (via contract invocation)
///
/// # Returns:
/// - Ok(()) if all validation checks pass
/// - Err(PredifiError::TokenError) if transfer is deemed unsafe
/// - Err(PredifiError::InvalidAmount) if amount is invalid
/// - Err(PredifiError::InvalidAddressOrToken) if addresses are invalid
fn validate_token_transfer(
env: &Env,
token: &Address,
from: &Address,
to: &Address,
amount: i128,
) -> Result<(), PredifiError> {
// Validate amount: must be positive and non-zero
if amount <= 0 {
return Err(PredifiError::InvalidAmount);
}

// Validate token address is not null/default
let zero_addr = Address::from_contract_id(
env,
&BytesN::<32>::from_array(env, &[0u8; 32]),
);
if token == &zero_addr {
return Err(PredifiError::InvalidAddressOrToken);
}

// Validate sender and recipient are distinct
if from == to {
return Err(PredifiError::InvalidAddressOrToken);
}

// Validate sender and recipient are not null
if from == &zero_addr || to == &zero_addr {
return Err(PredifiError::InvalidAddressOrToken);
}

// Verify token contract is callable by attempting to get its balance
// This ensures the token contract is valid and responsive
let token_client = token::Client::new(env, token);
match token_client.balance(from) {
_ => {
// If balance check succeeds, token contract is callable and valid
Ok(())
}
}
}

/// Pure: Initialize outcome stakes vector with zeros.
/// Used for markets with many outcomes (e.g., 32+ teams tournament).
#[allow(dead_code)]
Expand Down Expand Up @@ -2350,6 +2407,15 @@ impl PredifiContract {

Self::enter_reentrancy_guard(&env);

// Validate token transfer before withdrawal
Self::validate_token_transfer(
&env,
&token,
&env.current_contract_address(),
&recipient,
amount,
)?;

// Transfer tokens to recipient
token_client.transfer(&env.current_contract_address(), &recipient, &amount);

Expand Down Expand Up @@ -3393,6 +3459,15 @@ impl PredifiContract {

// --- INTERACTIONS ---

// Validate token transfer safety before executing
Self::validate_token_transfer(
&env,
&pool.token,
&user,
&env.current_contract_address(),
amount,
)?;

let token_client = token::Client::new(&env, &pool.token);
token_client.transfer(&user, &env.current_contract_address(), &amount);

Expand Down Expand Up @@ -3483,6 +3558,15 @@ impl PredifiContract {
Self::bump_ttl(env, &claimed_key);

if pool.state == MarketState::Canceled {
// Validate token transfer before sending refund
Self::validate_token_transfer(
env,
&pool.token,
&env.current_contract_address(),
user,
prediction.amount,
)?;

let token_client = token::Client::new(env, &pool.token);
token_client.transfer(&env.current_contract_address(), user, &prediction.amount);

Expand Down Expand Up @@ -3559,6 +3643,15 @@ impl PredifiContract {
)
.map_err(|_| PredifiError::InvalidAmount)?;
if referral_amount > 0 {
// Validate referral token transfer before execution
Self::validate_token_transfer(
env,
&pool.token,
&env.current_contract_address(),
&referrer,
referral_amount,
)?;

token_client.transfer(
&env.current_contract_address(),
&referrer,
Expand All @@ -3575,6 +3668,15 @@ impl PredifiContract {
}
}

// Validate main winnings transfer before execution
Self::validate_token_transfer(
env,
&pool.token,
&env.current_contract_address(),
user,
winnings,
)?;

token_client.transfer(&env.current_contract_address(), user, &winnings);

WinningsClaimedEvent {
Expand Down Expand Up @@ -3716,6 +3818,15 @@ impl PredifiContract {

// --- INTERACTIONS ---

// Validate token transfer before sending refund
Self::validate_token_transfer(
&env,
&pool.token,
&env.current_contract_address(),
&user,
refund_amount,
)?;

let token_client = token::Client::new(&env, &pool.token);
token_client.transfer(&env.current_contract_address(), &user, &refund_amount);

Expand Down Expand Up @@ -4619,6 +4730,15 @@ impl PredifiContract {
admin.require_auth();
Self::require_admin_role(&env, &admin, "emergency_withdraw")?;

// Validate token transfer before execution
Self::validate_token_transfer(
&env,
&token,
&env.current_contract_address(),
&destination,
amount,
)?;

let token_client = token::Client::new(&env, &token);

Self::enter_reentrancy_guard(&env);
Expand Down
Loading