Skip to content

check if object exists before borrow #366

Discussion options

You must be logged in to vote

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 a borrow_option or borrow_with_default function

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()
      }
  }
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dancespiele
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants