Skip to content
Merged
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
4 changes: 4 additions & 0 deletions contracts/geev-core/src/giveaway.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::profile::ProfileContract;
use crate::types::{DataKey, Error, Giveaway, GiveawayStatus};
use crate::utils::with_reentrancy_guard;
use soroban_sdk::{
Expand Down Expand Up @@ -211,6 +212,9 @@ impl GiveawayContract {

giveaway.status = GiveawayStatus::Completed;
env.storage().persistent().set(&giveaway_key, &giveaway);

// Increment creator's reputation — internal call, not user-accessible.
ProfileContract::increment_reputation(&env, giveaway.creator.clone());
})
}

Expand Down
21 changes: 21 additions & 0 deletions contracts/geev-core/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,25 @@ impl ProfileContract {
.persistent()
.get::<DataKey, Address>(&DataKey::Username(username))
}

/// Read the reputation score for a given address (defaults to 0).
pub fn get_reputation(env: Env, user: Address) -> u64 {
env.storage()
.persistent()
.get(&DataKey::Reputation(user))
.unwrap_or(0)
}
}

impl ProfileContract {
/// Increment `user`'s reputation by 1.
/// Private — only callable from within this crate (e.g. `distribute_prize`).
/// Never exposed in the contract ABI.
pub(crate) fn increment_reputation(env: &Env, user: Address) {
let key = DataKey::Reputation(user);
let score: u64 = env.storage().persistent().get(&key).unwrap_or(0);
env.storage()
.persistent()
.set(&key, &score.saturating_add(1));
}
}
112 changes: 112 additions & 0 deletions contracts/geev-core/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1632,3 +1632,115 @@ fn test_donate_to_suspended_request_fails() {
// Should panic with InvalidStatus.
aid_client.donate(&donor, &request_id, &100);
}

// ── reputation tests ──────────────────────────────────────────────────────────

#[test]
fn test_reputation_starts_at_zero() {
let env = Env::default();
env.mock_all_auths();

let contract_id = env.register(ProfileContract, ());
let client = ProfileContractClient::new(&env, &contract_id);

let user = Address::generate(&env);
assert_eq!(client.get_reputation(&user), 0);
}

#[test]
fn test_reputation_increments_after_distribute_prize() {
let env = Env::default();
env.mock_all_auths();

let contract_id = env.register(GiveawayContract, ());
let client = GiveawayContractClient::new(&env, &contract_id);

let token_admin = Address::generate(&env);
let mock_token = env
.register_stellar_asset_contract_v2(token_admin.clone())
.address();
let token_admin_client = token::StellarAssetClient::new(&env, &mock_token);

let creator = Address::generate(&env);
let participant = Address::generate(&env);
token_admin_client.mint(&creator, &1000);

env.as_contract(&contract_id, || {
env.storage()
.instance()
.set(&DataKey::AllowedToken(mock_token.clone()), &true);
});

let giveaway_id = client.create_giveaway(
&creator,
&mock_token,
&500,
&String::from_str(&env, "Rep Test"),
&60,
);

client.enter_giveaway(&participant, &giveaway_id);

env.ledger().with_mut(|li| li.timestamp += 100);
client.pick_winner(&giveaway_id);
client.distribute_prize(&giveaway_id);

// Creator's reputation should now be 1.
env.as_contract(&contract_id, || {
let score: u64 = env
.storage()
.persistent()
.get(&DataKey::Reputation(creator.clone()))
.unwrap_or(0);
assert_eq!(score, 1);
});
}

#[test]
fn test_reputation_accumulates_across_giveaways() {
let env = Env::default();
env.mock_all_auths();

let contract_id = env.register(GiveawayContract, ());
let client = GiveawayContractClient::new(&env, &contract_id);

let token_admin = Address::generate(&env);
let mock_token = env
.register_stellar_asset_contract_v2(token_admin.clone())
.address();
let token_admin_client = token::StellarAssetClient::new(&env, &mock_token);

let creator = Address::generate(&env);
let participant = Address::generate(&env);
token_admin_client.mint(&creator, &2000);

env.as_contract(&contract_id, || {
env.storage()
.instance()
.set(&DataKey::AllowedToken(mock_token.clone()), &true);
});

// Complete two giveaways with the same creator.
for _ in 0..2u32 {
let giveaway_id = client.create_giveaway(
&creator,
&mock_token,
&500,
&String::from_str(&env, "Rep Test"),
&60,
);
client.enter_giveaway(&participant, &giveaway_id);
env.ledger().with_mut(|li| li.timestamp += 100);
client.pick_winner(&giveaway_id);
client.distribute_prize(&giveaway_id);
}

env.as_contract(&contract_id, || {
let score: u64 = env
.storage()
.persistent()
.get(&DataKey::Reputation(creator.clone()))
.unwrap_or(0);
assert_eq!(score, 2);
});
}
1 change: 1 addition & 0 deletions contracts/geev-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub enum DataKey {
Username(String),
FlagRecord(u64, Address),
FlagCount(u64),
Reputation(Address),
}

#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,51 @@
4095
]
],
[
{
"contract_data": {
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
"key": {
"vec": [
{
"symbol": "Reputation"
},
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAITA4"
}
]
},
"durability": "persistent"
}
},
[
{
"last_modified_ledger_seq": 0,
"data": {
"contract_data": {
"ext": "v0",
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
"key": {
"vec": [
{
"symbol": "Reputation"
},
{
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAITA4"
}
]
},
"durability": "persistent",
"val": {
"u64": "1"
}
}
},
"ext": "v0"
},
4095
]
],
[
{
"contract_data": {
Expand Down
Loading
Loading