forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Reviews Rating (MystenLabs#16520)
## Description Describe the changes or additions included in this PR. ## Test Plan How did you test the new or updated feature? --- If your changes are not user-facing and do not break anything, you can skip the following section. Otherwise, please briefly describe what has changed under the Release Notes section. ### Type of Change (Check all that apply) - [ ] protocol change - [ ] user-visible impact - [ ] breaking change for a client SDKs - [ ] breaking change for FNs (FN binary must upgrade) - [ ] breaking change for validators or node operators (must upgrade binaries) - [ ] breaking change for on-chain data layout - [ ] necessitate either a data wipe or data migration ### Release notes --------- Co-authored-by: Ronny Roland <ronmilton@mystenlabs.com> Co-authored-by: Ashok Menon <amenon94@gmail.com> Co-authored-by: ronny-mysten <118224482+ronny-mysten@users.noreply.github.com>
- Loading branch information
1 parent
622f7d0
commit edd3ece
Showing
8 changed files
with
923 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
453 changes: 453 additions & 0 deletions
453
docs/content/guides/developer/app-examples/reviews-rating.mdx
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "reviews_rating" | ||
version = "0.0.1" | ||
edition = "2024.beta" | ||
|
||
[dependencies] | ||
Sui = { local = "../../../crates/sui-framework/packages/sui-framework", override = true } | ||
|
||
[addresses] | ||
reviews_rating = "0x0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
module reviews_rating::dashboard { | ||
use std::string::String; | ||
|
||
use sui::dynamic_field as df; | ||
|
||
/// Dashboard is a collection of services | ||
public struct Dashboard has key, store { | ||
id: UID, | ||
service_type: String | ||
} | ||
|
||
/// Create a new dashboard | ||
public fun create_dashboard( | ||
service_type: String, | ||
ctx: &mut TxContext, | ||
) { | ||
let db = Dashboard { | ||
id: object::new(ctx), | ||
service_type | ||
}; | ||
transfer::share_object(db); | ||
} | ||
|
||
public fun register_service(db: &mut Dashboard, service_id: ID) { | ||
df::add(&mut db.id, service_id, service_id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
module reviews_rating::moderator { | ||
use sui::tx_context::{sender}; | ||
|
||
/// Represents a moderator that can be used to delete reviews | ||
public struct Moderator has key { | ||
id: UID, | ||
} | ||
|
||
/// A capability that can be used to setup moderators | ||
public struct ModCap has key, store { | ||
id: UID | ||
} | ||
|
||
fun init(ctx: &mut TxContext) { | ||
let mod_cap = ModCap { | ||
id: object::new(ctx) | ||
}; | ||
transfer::transfer(mod_cap, sender(ctx)); | ||
} | ||
|
||
/// Adds a moderator | ||
public fun add_moderator( | ||
_: &ModCap, | ||
recipient: address, | ||
ctx: &mut TxContext | ||
) { | ||
// generate an NFT and transfer it to moderator who may use it to delete reviews | ||
let mod = Moderator { | ||
id: object::new(ctx) | ||
}; | ||
transfer::transfer(mod, recipient); | ||
} | ||
|
||
/// Deletes a moderator | ||
public fun delete_moderator( | ||
mod: Moderator | ||
) { | ||
let Moderator { id } = mod; | ||
object::delete(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
module reviews_rating::review { | ||
use std::string::String; | ||
|
||
use sui::clock::Clock; | ||
use sui::math; | ||
|
||
const EInvalidContentLen: u64 = 1; | ||
|
||
const MIN_REVIEW_CONTENT_LEN: u64 = 5; | ||
const MAX_REVIEW_CONTENT_LEN: u64 = 1000; | ||
|
||
/// Represents a review of a service | ||
public struct Review has key, store { | ||
id: UID, | ||
owner: address, | ||
service_id: ID, | ||
content: String, | ||
// intrinsic score | ||
len: u64, | ||
// extrinsic score | ||
votes: u64, | ||
time_issued: u64, | ||
// proof of experience | ||
has_poe: bool, | ||
// total score | ||
total_score: u64, | ||
// overall rating value; max=5 | ||
overall_rate: u8, | ||
} | ||
|
||
/// Creates a new review | ||
public(package) fun new_review( | ||
owner: address, | ||
service_id: ID, | ||
content: String, | ||
has_poe: bool, | ||
overall_rate: u8, | ||
clock: &Clock, | ||
ctx: &mut TxContext | ||
): Review { | ||
let len = content.length(); | ||
assert!(len > MIN_REVIEW_CONTENT_LEN && len <= MAX_REVIEW_CONTENT_LEN, EInvalidContentLen); | ||
let mut new_review = Review { | ||
id: object::new(ctx), | ||
owner, | ||
service_id, | ||
content, | ||
len, | ||
votes: 0, | ||
time_issued: clock.timestamp_ms(), | ||
has_poe, | ||
total_score: 0, | ||
overall_rate, | ||
}; | ||
new_review.total_score = new_review.calculate_total_score(); | ||
new_review | ||
} | ||
|
||
/// Deletes a review | ||
public(package) fun delete_review(rev: Review) { | ||
let Review { | ||
id, owner: _, service_id: _, content: _, len: _, votes: _, time_issued: _, | ||
has_poe: _, total_score: _, overall_rate: _ | ||
} = rev; | ||
object::delete(id); | ||
} | ||
|
||
/// Calculates the total score of a review | ||
fun calculate_total_score(rev: &Review): u64 { | ||
let mut intrinsic_score: u64 = rev.len; | ||
intrinsic_score = math::min(intrinsic_score, 150); | ||
let extrinsic_score: u64 = 10 * rev.votes; | ||
let vm: u64 = if (rev.has_poe) { 2 } else { 1 }; | ||
(intrinsic_score + extrinsic_score) * vm | ||
} | ||
|
||
/// Updates the total score of a review | ||
fun update_total_score(rev: &mut Review) { | ||
rev.total_score = rev.calculate_total_score(); | ||
} | ||
|
||
/// Upvotes a review | ||
public fun upvote(rev: &mut Review) { | ||
rev.votes = rev.votes + 1; | ||
rev.update_total_score(); | ||
} | ||
|
||
public fun get_id(rev: &Review): ID { | ||
rev.id.to_inner() | ||
} | ||
|
||
public fun get_total_score(rev: &Review): u64 { | ||
rev.total_score | ||
} | ||
|
||
public fun get_time_issued(rev: &Review): u64 { | ||
rev.time_issued | ||
} | ||
} |
Oops, something went wrong.