Reading Resource Data with borrow_global
borrow_global allows you to read an existing resource struct from a specific address without needing a signer, but the function must declare that it accesses the resource using acquires ResourceName.
borrow_global: read an existing resource struct
borrow_global: read an existing resource structmodule 0xcafe::Dinosaur_nest {
use std::vector;
struct DinosaurGendna has key {
Gendna_digits: u64,
Gendna_modulus: u64,
}
struct Dinosaur has store {
Gendna: u64,
}
struct DinosaurSwarm has key {
Dinosaurs: vector<Dinosaur>,
}
fun init_module(cafe_signer: &signer) {
let Gendna_digits = 10;
let Gendna_modulus = 10 ^ Gendna_digits;
move_to(cafe_signer, DinosaurGendna {
Gendna_digits,
Gendna_modulus: (Gendna_modulus as u256),
});
move_to(cafe_signer, DinosaurSwarm {
Dinosaurs: vector[],
});
}
public fun spawn_Dinosaur(Gendna: u64): Dinosaur {
Dinosaur {
Gendna,
}
}
public fun get_Gendna_digits(): u64 acquires DinosaurGendna {
borrow_global<DinosaurGendna>(@0xcafe).Gendna_digits
}
}