Skip to content

Commit

Permalink
feat: add get_scores view function (#175)
Browse files Browse the repository at this point in the history
* feat: add `get_scores` view function

* chore: add comments
  • Loading branch information
ALPAC-4 authored Feb 10, 2025
1 parent f90d5dc commit c639f88
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 0 deletions.
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.
60 changes: 60 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,59 @@ 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(
// The stage number
stage: u64,
// Number of results to return (Max: 1000)
limit: u16,
// Pagination key. If None, start from the beginning.
// If provided, return results after this key in descending order.
// Use the last returned address to fetch the next page.
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

0 comments on commit c639f88

Please sign in to comment.