Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add get_scores view function #175

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Binary file modified precompile/binaries/minlib/bigdecimal.mv
Binary file not shown.
Binary file modified precompile/binaries/minlib/ed25519.mv
Binary file not shown.
Binary file modified precompile/binaries/minlib/multisig.mv
Binary file not shown.
Binary file modified precompile/binaries/minlib/simple_map.mv
Binary file not shown.
Binary file modified precompile/binaries/minlib/table.mv
Binary file not shown.
Binary file modified precompile/binaries/minlib/vip_score.mv
Binary file not shown.
Binary file modified precompile/binaries/stdlib/bigdecimal.mv
Binary file not shown.
Binary file modified precompile/binaries/stdlib/ed25519.mv
Binary file not shown.
Binary file modified precompile/binaries/stdlib/multisig.mv
Binary file not shown.
Binary file modified precompile/binaries/stdlib/simple_map.mv
Binary file not shown.
Binary file modified precompile/binaries/stdlib/table.mv
Binary file not shown.
53 changes: 53 additions & 0 deletions precompile/modules/minitia_stdlib/sources/vip/score.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module minitia_std::vip_score {
use std::vector;
use std::event;
use std::option::{Self, Option};

use minitia_std::signer;
use minitia_std::table;
Expand All @@ -20,6 +21,12 @@ module minitia_std::vip_score {
score: table::Table<address /* user */, u64>
}

//
// Constants
//

const MAX_LIMIT: u16 = 1000;

//
// Errors
//
Expand Down Expand Up @@ -157,6 +164,52 @@ module minitia_std::vip_score {
// View functions
//

struct GetScoresResponse {
stage: u64,
scores: vector<Score>
}

struct Score {
addr: address,
score: u64
}

#[view]
public fun get_scores(
stage: u64, limit: u16, start_after: Option<address>
): GetScoresResponse acquires ModuleStore {
let module_store = borrow_global<ModuleStore>(@minitia_std);
let scores: vector<Score> = vector[];

// check stage exists
if (!table::contains(&module_store.scores, stage)) {
return GetScoresResponse { stage, scores }
};

// check max limit
if (limit > MAX_LIMIT) {
limit = MAX_LIMIT
};

// collect scores
let score = table::borrow(&module_store.scores, stage);
let iter = table::iter(
&score.score,
option::none(),
start_after, // exclusive
2
);

while (table::prepare<address, u64>(iter)
&& vector::length(&scores) < (limit as u64)) {
let (addr, score) = table::next<address, u64>(iter);

vector::push_back(&mut scores, Score { addr, score: *score });
};

return GetScoresResponse { stage, scores }
}

#[view]
public fun get_score(account: address, stage: u64): u64 acquires ModuleStore {
let module_store = borrow_global<ModuleStore>(@minitia_std);
Expand Down
Loading