-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [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
1 parent
7d9bfd4
commit 7dbf825
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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,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(®istration.user_info, account)){ | ||
let user_info = table::borrow(®istration.user_info, account); | ||
(user_info.register_info, user_info.amount) | ||
}else{ | ||
(std::string::utf8(b""), 0u256) | ||
} | ||
} | ||
} | ||
|