Search code examples
onflow-cadence

Pass a copy of an object in storage without the reference to avoid users from modifying the object in storage



pub fun getAddresses(): {Address: Bool} {
    return Contract.account.borrow<&{Address: Bool}>(from: storage/dict) as! {Address: Bool}

}

In other words, I don’t want to pass back to the caller the reference to the dictionary because then they will be able to modify the actual dictionary object in storage. I just want a copy of the dictionary object returned, but don’t want to create a copy through looping over the address dictionary as that will be computationally expensive… Is there any way to remove the reference?


Solution

  • Have you consider storing a struct in your storage that has this as a field? Then you can get a reference to the struct and then send this field back?