Допис
Діліться своїми знаннями.
Returning User List from a Move Module and Clarification on Gas Fees
I have a Move module where I want to return a list of users from a function instead of using debug::print. Here is the updated code snippet:
public fun get_users(acc: &signer): SimpleMap<address,User> acquires Users{
address = signer::address_of(acc);
assert_is_owner(signer_address);
let users = borrow_global<Users>(signer_address);
return users.list_of_users;
}
In this function, I return the list_of_users
instead of printing it. Does this approach work correctly for returning data from a Move function?
Additionally, when I call a function using a signer, does it incur transaction fees? I want to confirm if there are any gas fees associated with calling functions using a signer.
Full Code:
module my_addrx::TRADE{
use std::simple_map;
use std::simple_map::SimpleMap;
use std::string::{String,utf8};
use std::signer;
use std::account;
use std::debug;
struct Users has key, drop{
list_of_users: SimpleMap<address,User>
}
struct User has copy, drop, store{
name: String
}
public fun assert_is_owner(addr: address){
assert!(addr==@my_addrx, 0);
}
public fun assert_is_initialized(addr: address){
assert!(exists<Users>(addr), 1);
}
public fun assert_uninititalized(addr: address){
assert!(!exists<Users>(addr), 2);
}
public fun initialize(acc: &signer, newUser: User) acquires Users{
let signer_address = signer::address_of(acc);
assert_is_owner(signer_address);
assert_uninititalized(signer_address);
let new_users = Users{
list_of_users: simple_map::create()
};
move_to(acc, new_users);
let users = borrow_global_mut<Users>(signer_address);
simple_map::add(&mut users.list_of_users, signer_address, newUser);
}
public fun get_users(acc: &signer) acquires Users{
let signer_address = signer::address_of(acc);
assert_is_owner(signer_address);
let users = borrow_global<Users>(signer_address);
debug::print(&users.list_of_users);
}
#[test(admin = @my_addrx)]
public fun testing(admin: signer) acquires Users{
let acc = account::create_account_for_test(signer::address_of(&admin));
let user = User{
name: utf8(b"strong")
};
initialize(&acc, user);
get_users(&acc);
// debug::print(&check_users);
}
}
- Move Module
- Move Script
Відповіді
2Yes, your approach to returning list_of_users from the Move function is correct. The updated get_users
function you provided will return the SimpleMap containing the list of users.
Regarding transaction fees, when you call a function using a signer, it does indeed involve transaction fees. In the context of Move smart contract language, transaction fees, also known as gas fees, are incurred for executing transactions on the blockchain. Functions that modify the blockchain state, such as initialize and get_users
in your case, typically require transaction fees to cover the computational resources used.
Therefore, whenever you invoke a function that involves state changes or read operations from the blockchain, you can expect transaction fees to be incurred. It's important to consider these fees while designing and interacting with your smart contracts on the blockchain.
module my_addrx::TRADE{
use std::simple_map;
use std::simple_map::SimpleMap;
use std::string::{String,utf8};
use std::signer;
use std::account;
use std::debug;
struct Users has key, drop {
list_of_users: SimpleMap<address,User>
}
struct User has copy, drop, store{
name: String
}
public fun assert_is_owner(addr: address){
assert!(addr==@my_addrx, 0);
}
public fun assert_is_initialized(addr: address){
assert!(exists<Users>(addr), 1);
}
public fun assert_uninititalized(addr: address){
assert!(!exists<Users>(addr), 2);
}
public fun initialize(acc: &signer, newUser: User) acquires Users{
let signer_address = signer::address_of(acc);
assert_is_owner(signer_address);
assert_uninititalized(signer_address);
let new_users = Users{
list_of_users: simple_map::create()
};
move_to(acc, new_users);
let users = borrow_global_mut<Users>(signer_address);
simple_map::add(&mut users.list_of_users, signer_address, newUser);
}
public fun get_users(acc: &signer): SimpleMap<address,User> acquires Users{
let signer_address = signer::address_of(acc);
assert_is_owner(signer_address);
let users = borrow_global<Users>(signer_address);
// debug::print(&users.list_of_users);
return users.list_of_users
}
#[test(admin = @my_addrx)]
public fun testing(admin: signer) acquires Users{
let acc = account::create_account_for_test(signer::address_of(&admin));
let user = User{
name: utf8(b"strong")
};
initialize(&acc, user);
let user = get_users(&acc);
debug::print(&user);
}
}
Just made a minor change , add a return statement and also change the definition of the function
I think when you call a function using signer than it will take some txn fee. If you have further questions or need assistance with a different approach, feel free to ask them here;
The function get_users
in your code is designed to return the list of users, which is a SimpleMap<address, User>
. This approach should work correctly for returning data from a Move function. In Move, functions can return values of any type, including complex types like SimpleMap. When you return a value from a function, it is moved out of the function's local scope and into the calling function's scope. This is similar to how Rust handles function return values. Here's a simplified example:
public fun return_a_value(): u64 {
let x = 10;
return x;
}
In this example, the u64 value 10
is returned from the function return_a_value
. The value 10 is moved out of the function's scope and into the scope of the calling function move-language.github.io. As for your second question, in Move, there are indeed transaction fees associated with executing transactions, which includes calling functions. These fees are charged in gas, a measure of computational work. The cost of a transaction depends on the complexity of the computation, the amount of data being processed, and the current gas price set by the network. Therefore, calling a function using a signer would incur transaction fees.
Ви знаєте відповідь?
Будь ласка, увійдіть та поділіться нею.