Skip to content

Commit

Permalink
sync new nft standard and public capability retrieval changes
Browse files Browse the repository at this point in the history
  • Loading branch information
austinkline committed May 4, 2024
1 parent 0c0243b commit 1346ae1
Show file tree
Hide file tree
Showing 47 changed files with 81 additions and 97 deletions.
14 changes: 7 additions & 7 deletions contracts/HybridCustody.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ access(all) contract HybridCustody {
/// Adds a ChildAccount Capability to this Manager. If a default Filter is set in the manager, it will also be
/// added to the ChildAccount
///
access(Manage | Insert) fun addAccount(cap: Capability<auth(Child) &{AccountPrivate, AccountPublic, ViewResolver.Resolver}>) {
access(Manage) fun addAccount(cap: Capability<auth(Child) &{AccountPrivate, AccountPublic, ViewResolver.Resolver}>) {
pre {
self.childAccounts[cap.address] == nil: "There is already a child account with this address"
}
Expand Down Expand Up @@ -336,7 +336,7 @@ access(all) contract HybridCustody {
/// Removes specified child account from the Manager's child accounts. Callbacks to the child account remove
/// any associated resources and Capabilities
///
access(Manage | Remove) fun removeChild(addr: Address) {
access(Manage) fun removeChild(addr: Address) {
let cap = self.childAccounts.remove(key: addr)
?? panic("child account not found")

Expand Down Expand Up @@ -370,7 +370,7 @@ access(all) contract HybridCustody {
/// Adds an owned account to the Manager's list of owned accounts, setting the Manager account as the owner of
/// the given account
///
access(Manage | Insert) fun addOwnedAccount(cap: Capability<auth(Owner) &{OwnedAccountPrivate, OwnedAccountPublic, ViewResolver.Resolver}>) {
access(Manage) fun addOwnedAccount(cap: Capability<auth(Owner) &{OwnedAccountPrivate, OwnedAccountPublic, ViewResolver.Resolver}>) {
pre {
self.ownedAccounts[cap.address] == nil: "There is already an owned account with this address"
}
Expand Down Expand Up @@ -424,7 +424,7 @@ access(all) contract HybridCustody {
/// Removes specified child account from the Manager's child accounts. Callbacks to the child account remove
/// any associated resources and Capabilities
///
access(Manage | Remove) fun removeOwned(addr: Address) {
access(Manage) fun removeOwned(addr: Address) {
if let acct = self.ownedAccounts.remove(key: addr) {
if acct.check() {
acct.borrow()!.seal()
Expand Down Expand Up @@ -954,7 +954,7 @@ access(all) contract HybridCustody {
/// configured for the provided parent address. Once done, the parent will not have any valid capabilities with
/// which to access the child account.
///
access(Owner | Remove) fun removeParent(parent: Address): Bool {
access(Owner) fun removeParent(parent: Address): Bool {
if self.parents[parent] == nil {
return false
}
Expand Down Expand Up @@ -983,8 +983,8 @@ access(all) contract HybridCustody {
emit AccountUpdated(id: self.uuid, child: self.acct.address, parent: parent, active: false)

let parentManager = getAccount(parent).capabilities.get<&{ManagerPublic}>(HybridCustody.ManagerPublicPath)
if parentManager?.check() == true {
parentManager!.borrow()?.removeParentCallback(child: acct.address)
if parentManager.check() {
parentManager.borrow()?.removeParentCallback(child: acct.address)
}

return true
Expand Down
17 changes: 7 additions & 10 deletions contracts/factories/NFTCollectionFactory.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ access(all) contract NFTProviderAndCollectionFactory {
access(all) struct WithdrawFactory: CapabilityFactory.Factory {
access(all) view fun getCapability(acct: auth(Capabilities) &Account, controllerID: UInt64): Capability? {
if let con = acct.capabilities.storage.getController(byCapabilityID: controllerID) {
if !con.capability.check<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Collection}>() {
if !con.capability.check<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Collection}>() {
return nil
}

return con.capability as! Capability<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Collection}>
return con.capability as! Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Collection}>
}

return nil
Expand All @@ -34,15 +34,12 @@ access(all) contract NFTProviderAndCollectionFactory {
}

access(all) view fun getPublicCapability(acct: &Account, path: PublicPath): Capability? {
if let cap = acct.capabilities.get<&{NonFungibleToken.Collection}>(path) {
if !cap.check() {
return nil
}

return cap
let cap = acct.capabilities.get<&{NonFungibleToken.Collection}>(path)
if !cap.check() {
return nil
}

return nil
return cap
}
}
}
4 changes: 2 additions & 2 deletions contracts/factories/NFTProviderAndCollectionFactory.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ access(all) contract NFTProviderAndCollectionFactory {
access(all) struct Factory: CapabilityFactory.Factory {
access(all) view fun getCapability(acct: auth(Capabilities) &Account, controllerID: UInt64): Capability? {
if let con = acct.capabilities.storage.getController(byCapabilityID: controllerID) {
if !con.capability.check<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>() {
if !con.capability.check<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>() {
return nil
}

return con.capability as! Capability<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>
return con.capability as! Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions contracts/factories/NFTProviderFactory.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ access(all) contract NFTProviderFactory {
access(all) struct Factory: CapabilityFactory.Factory {
access(all) view fun getCapability(acct: auth(Capabilities) &Account, controllerID: UInt64): Capability? {
if let con = acct.capabilities.storage.getController(byCapabilityID: controllerID) {
if !con.capability.check<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>() {
if !con.capability.check<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>() {
return nil
}

return con.capability as! Capability<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>
return con.capability as! Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion contracts/standard/ExampleNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ access(all) contract ExampleNFT: ViewResolver {
}

// withdraw removes an NFT from the collection and moves it to the caller
access(NonFungibleToken.Withdraw | NonFungibleToken.Owner) fun withdraw(withdrawID: UInt64): @{NonFungibleToken.NFT} {
access(NonFungibleToken.Withdraw) fun withdraw(withdrawID: UInt64): @{NonFungibleToken.NFT} {
let token <- self.ownedNFTs.remove(key: withdrawID) ?? panic("missing NFT")

emit Withdraw(id: token.id, from: self.owner?.address)
Expand Down
2 changes: 1 addition & 1 deletion contracts/standard/ExampleNFT2.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ access(all) contract ExampleNFT2: ViewResolver {
}

// withdraw removes an NFT from the collection and moves it to the caller
access(NonFungibleToken.Withdraw | NonFungibleToken.Owner) fun withdraw(withdrawID: UInt64): @{NonFungibleToken.NFT} {
access(NonFungibleToken.Withdraw ) fun withdraw(withdrawID: UInt64): @{NonFungibleToken.NFT} {
let token <- self.ownedNFTs.remove(key: withdrawID) ?? panic("missing NFT")

emit Withdraw(id: token.id, from: self.owner?.address)
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"author": "",
"license": "ISC",
"dependencies": {
"@flowtyio/flow-contracts": "^0.1.0-beta.11"
"@flowtyio/flow-contracts": "^0.1.0-beta.19"
}
}
2 changes: 1 addition & 1 deletion scripts/delegator/find_nft_provider_cap.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ access(all) fun main(addr: Address): Bool {
acct.capabilities.storage.issue<auth(CapabilityDelegator.Get) &{CapabilityDelegator.GetterPrivate}>(CapabilityDelegator.StoragePath).borrow()
?? panic("could not borrow delegator")

let desiredType = Type<Capability<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>>()
let desiredType = Type<Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>>()
let foundType = delegator.findFirstPrivateType(desiredType) ?? panic("no type found")

let nakedCap = delegator.getPrivateCapability(foundType) ?? panic("requested capability type was not found")
Expand Down
2 changes: 1 addition & 1 deletion scripts/delegator/get_all_private_caps.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ access(all) fun main(address: Address): Bool {
?.getAllPrivate()
?? panic("could not borrow delegator")

let desiredType: Type = Type<Capability<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>>()
let desiredType: Type = Type<Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>>()

return privateCaps.length == 1 && privateCaps[0].getType() == desiredType
}
4 changes: 2 additions & 2 deletions scripts/delegator/get_nft_provider.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ access(all) fun main(addr: Address): Bool {
acct.capabilities.storage.issue<auth(CapabilityDelegator.Get) &{CapabilityDelegator.GetterPrivate}>(CapabilityDelegator.StoragePath).borrow()
?? panic("could not borrow delegator")

let capType = Type<Capability<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>>()
let capType = Type<Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>>()
let nakedCap = delegator.getPrivateCapability(capType) ?? panic("requested capability type was not found")

// we don't need to do anything with this cap, being able to cast here is enough to know
// that this works
let cap = nakedCap as! Capability<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>
let cap = nakedCap as! Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>

return true
}
4 changes: 2 additions & 2 deletions scripts/factory/get_nft_provider_from_factory.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ access(all) fun main(addr: Address) {
let controllers = acct.capabilities.storage.getControllers(forPath: d.storagePath)

for c in controllers {
if c.borrowType.isSubtype(of: Type<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>()) {
factory.getCapability(acct: acct, controllerID: c.capabilityID)! as! Capability<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>
if c.borrowType.isSubtype(of: Type<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>()) {
factory.getCapability(acct: acct, controllerID: c.capabilityID)! as! Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>
return
}
}
Expand Down
6 changes: 3 additions & 3 deletions scripts/factory/get_nft_provider_from_factory_allowed.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ access(all) fun main(filterFactoryAddr: Address, providerAddr: Address): Bool {

let factoryManager = ruleAcct.storage.borrow<&CapabilityFactory.Manager>(from: CapabilityFactory.StoragePath)
?? panic("Problem borrowing CapabilityFactory Manager")
let factory = factoryManager.getFactory(Type<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>())
let factory = factoryManager.getFactory(Type<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>())
?? panic("No factory for NFT Provider found")

let d = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>())! as! MetadataViews.NFTCollectionData

var controllerID: UInt64? = nil
for c in providerAcct.capabilities.storage.getControllers(forPath: d.storagePath) {
if c.borrowType.isSubtype(of: Type<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>()) {
if c.borrowType.isSubtype(of: Type<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>()) {
controllerID = c.capabilityID
break
}
}

assert(controllerID != nil, message: "could not find existing provider capcon")

let provider = factory.getCapability(acct: providerAcct, controllerID: controllerID!)! as! Capability<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>
let provider = factory.getCapability(acct: providerAcct, controllerID: controllerID!)! as! Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>

let filter = ruleAcct.storage.borrow<&CapabilityFilter.AllowlistFilter>(from: CapabilityFilter.StoragePath)
?? panic("Problem borrowing CapabilityFilter AllowlistFilter")
Expand Down
1 change: 0 additions & 1 deletion scripts/hybrid-custody/get_account_public_address.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import "HybridCustody"

access(all) fun main(parent: Address, child: Address): Address {
let cap = getAccount(parent).capabilities.get<&{HybridCustody.ManagerPublic}>(HybridCustody.ManagerPublicPath)
?? panic("manager not found")
let manager = cap.borrow()
?? panic("unable to borrow manager")

Expand Down
4 changes: 2 additions & 2 deletions scripts/hybrid-custody/get_child_account_nft_capabilities.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ access(all) fun main(addr: Address): AnyStruct {

var typeIdsWithProvider: {Address: [String]} = {}

let providerType = Type<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>()
let providerType = Type<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>()

// Iterate through child accounts
for address in manager.getChildAddresses() {
Expand All @@ -26,7 +26,7 @@ access(all) fun main(addr: Address): AnyStruct {
}

if let cap = childAcct.getCapability(controllerID: c.capabilityID, type: providerType) {
let providerCap = cap as! Capability<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>
let providerCap = cap as! Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>

if !providerCap.check(){
continue
Expand Down
2 changes: 1 addition & 1 deletion scripts/hybrid-custody/get_nft_provider_capability.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ access(all) fun main(parent: Address, child: Address) {

let d = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>())! as! MetadataViews.NFTCollectionData

let desiredType = Type<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>()
let desiredType = Type<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>()
let controllerID = childAcct.getControllerIDForType(type: desiredType, forPath: d.storagePath)
?? panic("no capability found for desired type")

Expand Down
2 changes: 1 addition & 1 deletion scripts/test/get_nft_provider_capability_optional.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ access(all) fun main(parent: Address, child: Address, returnsNil: Bool): Bool {

let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>())! as! MetadataViews.NFTCollectionData

let type = Type<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>()
let type = Type<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>()
let controllerID = childAcct.getControllerIDForType(type: type, forPath: collectionData.storagePath)
?? panic("could not find controller for desired type")

Expand Down
2 changes: 1 addition & 1 deletion scripts/test/remove_nft_provider_factory.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ access(all) fun main(address: Address): Bool {

let expectedType = Type<NFTProviderFactory.Factory>()

if let removed = managerRef.removeFactory(Type<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>()) {
if let removed = managerRef.removeFactory(Type<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>()) {
return removed.getType() == expectedType
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/test/test_get_accessible_child_nfts.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ access(all) fun main(addr: Address, expectedAddressToIDs: {Address: [UInt64]}){
var typeIdsWithProvider: {Address: [String]} = {}
var nftViews: {Address: {UInt64: MetadataViews.Display}} = {}

let providerType = Type<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>()
let providerType = Type<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>()
let collectionType: Type = Type<@{NonFungibleToken.CollectionPublic}>()

for address in manager.getChildAddresses() {
Expand Down
4 changes: 2 additions & 2 deletions test/CapabilityDelegator_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fun testShareExampleNFTCollectionPrivate() {
Test.assert(e.isPublic == false)
Test.assert(e.active)

let capabilityType = Type<Capability<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>>()
let capabilityType = Type<Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>>()
Test.assertEqual(capabilityType, e.capabilityType)
}

Expand Down Expand Up @@ -101,7 +101,7 @@ fun testRemoveExampleNFTCollectionPrivate() {
Test.assert(e.isPublic == false)
Test.assert(e.active == false)

let capabilityType = Type<Capability<auth(NonFungibleToken.Withdraw, NonFungibleToken.Owner) &{NonFungibleToken.Provider}>>()
let capabilityType = Type<Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>>()
Test.assertEqual(capabilityType, e.capabilityType)

let scriptCode = loadCode("delegator/find_nft_provider_cap.cdc", "scripts")
Expand Down
Loading

0 comments on commit 1346ae1

Please sign in to comment.