Skip to content
Closed
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
7 changes: 4 additions & 3 deletions quid-contract/contracts/quid-reputation/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use soroban_sdk::contracterror;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum ReputationError {
NotAuthorized = 1,
InvalidInput = 2,
AlreadyRevoked = 3,
ProfileNotFound = 2,
InvalidInput = 3,
AttestationNotFound = 4,
ProfileNotFound = 5,
AlreadyRevoked = 5,
InvalidRewardAmount = 6,
}
51 changes: 35 additions & 16 deletions quid-contract/contracts/quid-reputation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ pub struct AttestationRevokedEvent {
pub revoked_by: Address,
}

#[contractevent(topics = ["profile", "updated"])]
pub struct ProfileUpdatedEvent {
pub subject: Address,
}

#[contract]
pub struct QuidReputationContract;

#[contractimpl]
impl QuidReputationContract {
// -------------------------------------------------------------------------
// Admin bootstrap
// -------------------------------------------------------------------------

pub fn initialize(env: Env, admin: Address) -> Result<(), ReputationError> {
admin.require_auth();

Expand All @@ -42,10 +43,6 @@ impl QuidReputationContract {
.ok_or(ReputationError::NotAuthorized)
}

// -------------------------------------------------------------------------
// Attestations
// -------------------------------------------------------------------------

pub fn issue_attestation(
env: Env,
issuer: Address,
Expand Down Expand Up @@ -139,10 +136,6 @@ impl QuidReputationContract {
.has(&DataKey::Attestation(attestation_id))
}

// -------------------------------------------------------------------------
// Profiles
// -------------------------------------------------------------------------

pub fn get_profile(env: Env, subject: Address) -> Result<Profile, ReputationError> {
env.storage()
.persistent()
Expand Down Expand Up @@ -170,6 +163,36 @@ impl QuidReputationContract {
env.storage().persistent().has(&DataKey::Profile(subject))
}

/// Admin-only upsert: write a full profile snapshot for a subject.
pub fn upsert_profile(
env: Env,
subject: Address,
score: i64,
missions_completed: u32,
missions_created: u32,
total_earnings: i128,
) -> Result<(), ReputationError> {
let admin = Self::get_admin(env.clone())?;
admin.require_auth();

if total_earnings < 0 {
return Err(ReputationError::InvalidRewardAmount);
}

let profile = Profile {
subject: subject.clone(),
score,
missions_completed,
missions_created,
};

Self::store_profile(&env, &profile);

ProfileUpdatedEvent { subject }.publish(&env);

Ok(())
}

fn get_next_attestation_id(env: &Env) -> u64 {
let mut count: u64 = env
.storage()
Expand All @@ -184,10 +207,6 @@ impl QuidReputationContract {
}
}

// -------------------------------------------------------------------------
// Internal helpers used by tests
// -------------------------------------------------------------------------

#[allow(dead_code)]
impl QuidReputationContract {
pub(crate) fn require_admin(env: &Env, caller: &Address) -> Result<(), ReputationError> {
Expand Down
Loading
Loading