check if object exists before borrow #366
Answered
by
gregnazario
dancespiele
asked this question in
Questions
-
Discord user IDspielcrypto Describe your question in detail.I have this view function: #[view]
public fun get_balance_to_claim(account_addr: address): u64 acquires FeesToClaim {
let fees_to_claim = borrow_global<FeesToClaim>(account_addr);
fees_to_claim.amount
} I think if What error, if any, are you getting?No response What have you tried or looked at? Or how can we reproduce the error?No response Which operating system are you using?Linux (Ubuntu, Fedora, Windows WSL, etc.) Which SDK or tool are you using? (if any)Aptos CLI Describe your environment or tooling in detailNo response |
Beta Was this translation helpful? Give feedback.
Answered by
gregnazario
Aug 15, 2024
Replies: 1 comment
-
This is a very common pattern with the resource functions. You can use module 0x42::example {
#[view]
public fun get_balance_to_claim(account_addr: address): Option<u64> acquires FeesToClaim {
if (exists<FeesToClaim>(account_addr) {
let fees_to_claim = borrow_global<FeesToClaim>(account_addr);
option::some(fees_to_claim.amount)
} else {
option::none()
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
dancespiele
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a very common pattern with the resource functions. You can use
exists<>
. This does bring a good point though, probably something that could be good to add aborrow_option
orborrow_with_default
function