Skip to content

Commit

Permalink
[apps] grow registration (#3150)
Browse files Browse the repository at this point in the history
* [example] grow registration

* add batch function and fix build

* add entry for function

* move grow_registration to grow_bitcoin

* [release] Release grow bitcoin

* [apps] Release grow bitcoin v8

---------

Co-authored-by: jolestar <[email protected]>
  • Loading branch information
mx819812523 and jolestar authored Jan 6, 2025
1 parent 7d9bfd4 commit 7dbf825
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
Binary file added apps/grow_bitcoin/released/8/package.rpd
Binary file not shown.
85 changes: 85 additions & 0 deletions apps/grow_bitcoin/sources/grow_registration.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
module grow_bitcoin::grow_registration {

use std::signer::address_of;
use std::string::String;
use std::vector;
use std::vector::length;
use grow_bitcoin::grow_point_v3::{PointBox, timestamp, value};
use app_admin::admin::AdminCap;
use moveos_std::object;
use moveos_std::table;
use moveos_std::object::{ObjectID, to_shared, Object};
use moveos_std::table::Table;


const ErrorInvalidVoteTime: u64 = 1;

struct Registration has key, store{
project_id: String,
register_point_box: Table<ObjectID, bool>,
user_info: Table<address, UserInfo>,
start_time: u64,
end_time: u64,
}

struct UserInfo has store, drop{
register_info: String,
amount: u256
}

public entry fun create_registration(project_id: String, start_time: u64, end_time: u64, _admin: &mut Object<AdminCap>) {
let registration = Registration {
project_id,
register_point_box: table::new(),
user_info: table::new(),
start_time,
end_time
};
to_shared(object::new_with_id(project_id, registration));
}

public entry fun update_registration_time(registration_obj: &mut Object<Registration>, start_time: u64, end_time: u64, _admin: &mut Object<AdminCap>) {
let registration = object::borrow_mut(registration_obj);
registration.start_time = start_time;
registration.end_time = end_time;
}

public entry fun register_batch(signer: &signer, registration_obj: &mut Object<Registration>, point_box_objs: vector<ObjectID>, register_info: String) {
let i = 0;
while (i < length(&point_box_objs)) {
let point_box_id = *vector::borrow(&point_box_objs, i);
let point_box = object::borrow_mut_object<PointBox>(signer, point_box_id);
register(signer, registration_obj, point_box, register_info);
i = i + 1;
}
}

public entry fun register(signer: &signer, registration_obj: &mut Object<Registration>, point_box_obj: &mut Object<PointBox>, register_info: String) {
let registration = object::borrow_mut(registration_obj);
let vote_time = timestamp(point_box_obj);
assert!(vote_time >= registration.start_time && vote_time <= registration.end_time, ErrorInvalidVoteTime);
let user_info = table::borrow_mut_with_default(&mut registration.user_info, address_of(signer), UserInfo{
register_info,
amount: 0
});
user_info.amount = user_info.amount + value(point_box_obj);
table::add(&mut registration.register_point_box, object::id(point_box_obj), true);
}

public entry fun update_register_info(signer: &signer, registration_obj: &mut Object<Registration>, register_info: String) {
let registration = object::borrow_mut(registration_obj);
let user_info = table::borrow_mut(&mut registration.user_info, address_of(signer));
user_info.register_info = register_info
}

public fun get_user_info(registration_obj: &Object<Registration>, account: address): (String, u256) {
let registration = object::borrow(registration_obj);
if(table::contains(&registration.user_info, account)){
let user_info = table::borrow(&registration.user_info, account);
(user_info.register_info, user_info.amount)
}else{
(std::string::utf8(b""), 0u256)
}
}
}

0 comments on commit 7dbf825

Please sign in to comment.