Thank you very much for your answer. It helped me a lot.
I’ve summarized several resource usage scenarios as follows:
- access a field (
u64
) in a resource
module T
{
resource T1
{
a: u64
}
resource T2
{
b: u64,
t1: Self.T1
}
public fun1(): u64 acquires T1
{
let add: address;
let ref: &mut Self.T1;
let x: u64;
add = get_txn_sender();
ref = borrow_global_mut<T1>(move(add));
x = *&move(ref).a;
return move(x);
}
}
- modify a field(
u64
) in a resource
module T
{
resource T1
{
a: u64
}
resource T2
{
b: u64,
t1: Self.T1
}
public fun2() acquires T1
{
let add: address;
let ref: &mut Self.T1;
let x: u64;
add = get_txn_sender();
ref = borrow_global_mut<T1>(move(add));
x = *©(ref).a;
*(&mut move(ref).a) = move(x) + 1;
return;
}
}
- access a field (
u64
) in a child resource of the resource
module T
{
resource T1
{
a: u64
}
resource T2
{
b: u64,
t1: Self.T1
}
public fun3(): u64 acquires T2
{
let add: address;
let ref: &mut Self.T2;
let t: &mut Self.T1;
let x: u64;
add = get_txn_sender();
ref = borrow_global_mut<T2>(move(add));
t = &mut move(ref).t1;
x = *©(t).a;
return move(x);
}
}
- modify a field (
u64
) in a child resource of a parent resource
module T
{
resource T1
{
a: u64
}
resource T2
{
b: u64,
t1: Self.T1
}
public fun4() acquires T2
{
let add: address;
let ref: &mut Self.T2;
let t: &mut Self.T1;
let x: u64;
add = get_txn_sender();
ref = borrow_global_mut<T2>(move(add));
t = &mut move(ref).t1;
x = *©(t).a;
*(&mut move(t).a) = move(x) + 1;
return;
}
}
Is my method correct?
And when the child resource isn’t in the same module with the parent resource, like follow, what could I do to access the child resource and its member variables?
module Tt
{
resource T3
{
c: u64
}
}
module T
{
import Transaction.Tt;
resource T1
{
a: u64
}
resource T2
{
b: u64,
t1: Self.T1,
t3: Tt.T3
}
public fun5()
{
//I want to access or modify the value of c in resource T3
}
}