F
Flow14mo ago
joshua

joshua | Flow (2023-11-21)

Anyone know where the documentation for authorized references to primitive types in Cadence 1.0 is? I'm working on updating the latest changes to the epoch smart contracts to Cadence 1.0 and running into a lot of issues that I'm very confused about, like references needing to be auth (Insert, Mutate, or Remove)
13 Replies
Needle
Needle14mo ago
I've created a thread for your message. Please continue any relevant discussion in this thread. You can rename this thread using /title <new title> If this is a technical question that others may benefit from, considering also asking it on Stackoverflow: https://stackoverflow.com/questions/ask?tags=onflow-cadence
turbolent
turbolent14mo ago
The documentation moved to the website, https://cadence-lang.org, and you can select 1.0 from the top-right The documentation for built-in entitlements can be found here: https://cadence-lang.org/docs/1.0/language/access-control#built-in-mutability-entitlements and (authorized) references are documented here: https://cadence-lang.org/docs/1.0/language/references what kinds of issues are you running into? what are you confused about? please let the team know if you find gaps in the documentation, or have suggestions! There's also a vision document explaining how the individual FLIPs tie in with each other: https://github.com/onflow/flips/blob/main/cadence/vision/mutability-restrictions.md
joshua
joshuaOP14mo ago
thank you! I guess I missed that there was a 1.0 section of the docs site now. Is a dictionary considered a primitive type? I'm trying to borrow references to dictionaries of dictionaries (&{String: {UInt32: Bool}}) that are stored in account storage. I understand I have to borrow them as auth(Insert, Mutate, Remove), but when I try to access a value in the inner dictionary, it returns a reference to it, but it isn't an entitled reference, so I can't do anything do it, but I'm not sure how I can create an entitled reference to it. it looks like normally I would use entitlement mappings, but I haven't figured out how to use an entitlement mapping on a dictionary type that I store in account storage instead of a field on a composite type this is an example of what I'm trying to do:
// borrow a reference to the dictionary in account storage
let movesPendingList = existingList ?? (self.account.storage.borrow<auth(Mutate, Insert, Remove) &{String: {UInt32: Bool}}>(from: /storage/idTableMovesPendingList)
?? panic("No moves pending list in account storage"))

// Get the delegator list for the node id, set the delegator's
// record to true, then save the delegator list back to the
// node's moves pending record
//
if let existingDelegatorList = movesPendingList[nodeID] {

existingDelegatorList[unwrappedDelegatorID] = true

movesPendingList[nodeID] = existingDelegatorList
}
// borrow a reference to the dictionary in account storage
let movesPendingList = existingList ?? (self.account.storage.borrow<auth(Mutate, Insert, Remove) &{String: {UInt32: Bool}}>(from: /storage/idTableMovesPendingList)
?? panic("No moves pending list in account storage"))

// Get the delegator list for the node id, set the delegator's
// record to true, then save the delegator list back to the
// node's moves pending record
//
if let existingDelegatorList = movesPendingList[nodeID] {

existingDelegatorList[unwrappedDelegatorID] = true

movesPendingList[nodeID] = existingDelegatorList
}
But I get the error that I can't assign to the non-auth reference, since existingDelegatorList is a reference to a dictionary now instead of just a dictionary
can only assign to a reference with (Mutate) or (Insert, Remove) access, but found a non-auth reference
--> f8d6e0586b0a20c7.FlowIDTableStaking:1725:37
|
1725 | existingDelegatorList[unwrappedDelegatorID] = true
| ^^^^^^^^^^^^^^^^^^^^^^ consider taking a reference with `Mutate` or `Insert, Remove` access
can only assign to a reference with (Mutate) or (Insert, Remove) access, but found a non-auth reference
--> f8d6e0586b0a20c7.FlowIDTableStaking:1725:37
|
1725 | existingDelegatorList[unwrappedDelegatorID] = true
| ^^^^^^^^^^^^^^^^^^^^^^ consider taking a reference with `Mutate` or `Insert, Remove` access
but I don't think I can do this, right?
if let existingDelegatorList = movesPendingList[nodeID] as auth(Mutate, Insert, Remove) &{UInt32: Bool} {
if let existingDelegatorList = movesPendingList[nodeID] as auth(Mutate, Insert, Remove) &{UInt32: Bool} {
since only the owner can create authorized references? so if I can't create the authorized reference there, then it seems like an entitlement mapping is the way to go, but I don't know how to specify or create that for a value in account storage
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
joshua
joshuaOP14mo ago
that is way too expensive though Or do you mean copy the inner dictionary?
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
joshua
joshuaOP14mo ago
How?
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
joshua
joshuaOP14mo ago
I'll try remove, that might work yeah, seems like the built in entitlements for built in types should apply to child dictionaries and arrays it looks like remove works for now, but it still would be good to know if what I am trying to do with only references works
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
joshua
joshuaOP14mo ago
thank you! So is there no way to get around this with the current behavior, like with using entitlement mappings? or is the only way to just use .remove?
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server