Skip to content

Commit

Permalink
update deprecated scripts & txns
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Oct 16, 2024
1 parent 4c512ff commit 235c24d
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 191 deletions.
1 change: 0 additions & 1 deletion scripts/example-nft/mint_to_account.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ transaction(receiver: Address, name: String, description: String, thumbnail: Str
let d = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>())! as! MetadataViews.NFTCollectionData

let c = getAccount(receiver).capabilities.get<&{NonFungibleToken.CollectionPublic}>(d.publicPath)
?? panic("no receiver capability found")
let r = c.borrow() ?? panic("could not borrow collection")
self.minter.mintNFT(recipient: r, name: name, description: description, thumbnail: thumbnail, royaltyReceipient: self.minter.owner!.address)
}
Expand Down
20 changes: 9 additions & 11 deletions scripts/example-nft/setup_full.cdc
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import "NonFungibleToken"
import "MetadataViews"

import ExampleNFT from "ExampleNFT"
import "ExampleNFT"

transaction {
prepare(acct: AuthAccount) {
let d = ExampleNFT.resolveView(Type<MetadataViews.NFTCollectionData>())! as! MetadataViews.NFTCollectionData
prepare(acct: auth(BorrowValue, SaveValue, PublishCapability, UnpublishCapability) &Account) {
let d = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>())! as! MetadataViews.NFTCollectionData

if acct.borrow<&ExampleNFT.Collection>(from: d.storagePath) == nil {
acct.save(<- ExampleNFT.createEmptyCollection(), to: ExampleNFT.CollectionStoragePath)
if acct.storage.borrow<&ExampleNFT.Collection>(from: d.storagePath) == nil {
acct.storage.save(<- ExampleNFT.createEmptyCollection(), to: d.storagePath)
}

acct.unlink(d.publicPath)
acct.link<&ExampleNFT.Collection{ExampleNFT.ExampleNFTCollectionPublic, NonFungibleToken.CollectionPublic}>(d.publicPath, target: d.storagePath)

acct.unlink(d.providerPath)
acct.link<&ExampleNFT.Collection{ExampleNFT.ExampleNFTCollectionPublic, NonFungibleToken.CollectionPublic, NonFungibleToken.Provider}>(d.providerPath, target: d.storagePath)
acct.capabilities.unpublish(d.publicPath)
let cap = acct.capabilities.storage.issue<&ExampleNFT.Collection>(d.storagePath)
acct.capabilities.publish(cap, at: d.publicPath)
}
}
}
6 changes: 3 additions & 3 deletions scripts/example-nft/setup_only_save.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import "MetadataViews"
import "ExampleNFT"

transaction {
prepare(acct: AuthAccount) {
if acct.borrow<&ExampleNFT.Collection>(from: ExampleNFT.CollectionStoragePath) == nil {
acct.save(<- ExampleNFT.createEmptyCollection(), to: ExampleNFT.CollectionStoragePath)
prepare(acct: auth(BorrowValue, SaveValue) &Account) {
if acct.storage.borrow<&ExampleNFT.Collection>(from: ExampleNFT.CollectionStoragePath) == nil {
acct.storage.save(<- ExampleNFT.createEmptyCollection(), to: ExampleNFT.CollectionStoragePath)
}
}
}
17 changes: 0 additions & 17 deletions scripts/example-nft/setup_public.cdc

This file was deleted.

56 changes: 28 additions & 28 deletions scripts/hybrid-custody/get_accessible_child_account_nfts.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,45 @@ import "MetadataViews"

// This script iterates through a parent's child accounts,
// identifies private paths with an accessible NonFungibleToken.Provider, and returns the corresponding typeIds
pub fun main(addr: Address): AnyStruct {
let manager = getAuthAccount(addr).borrow<&HybridCustody.Manager>(from: HybridCustody.ManagerStoragePath) ?? panic ("manager does not exist")
var typeIdsWithProvider = {} as {Address: [String]}
access(all) fun main(addr: Address, expectedAddressToIDs: {Address: [UInt64]}): AnyStruct {
let manager = getAuthAccount<auth(Storage) &Account>(addr).storage.borrow<auth(HybridCustody.Manage) &HybridCustody.Manager>(from: HybridCustody.ManagerStoragePath)
?? panic ("manager does not exist")

// Address -> nft UUID -> Display
var nftViews = {} as {Address: {UInt64: MetadataViews.Display}}
var typeIdsWithProvider: {Address: [String]} = {}
var nftViews: {Address: {UInt64: MetadataViews.Display}} = {}


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

// Iterate through child accounts
for address in manager.getChildAddresses() {
let acct = getAuthAccount(address)
let acct = getAuthAccount<auth(Storage, Capabilities) &Account>(address)
let foundTypes: [String] = []
let views: {UInt64: MetadataViews.Display} = {}
let childAcct = manager.borrowAccount(addr: address) ?? panic("child account not found")
// get all private paths
acct.forEachPrivate(fun (path: PrivatePath, type: Type): Bool {
// Check which private paths have NFT Provider AND can be borrowed
if !type.isSubtype(of: providerType){
return true
}
if let cap = childAcct.getCapability(path: path, type: Type<&{NonFungibleToken.Provider}>()) {
let providerCap = cap as! Capability<&{NonFungibleToken.Provider}>

if !providerCap.check(){
// if this isn't a provider capability, exit the account iteration function for this path
return true
for s in acct.storage.storagePaths {
for c in acct.capabilities.storage.getControllers(forPath: s) {
if !c.borrowType.isSubtype(of: providerType){
continue
}

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

if !providerCap.check(){
continue
}

foundTypes.append(cap.borrow<&AnyResource>()!.getType().identifier)
typeIdsWithProvider[address] = foundTypes
break
}
foundTypes.append(cap.borrow<&AnyResource>()!.getType().identifier)
}
return true
})
typeIdsWithProvider[address] = foundTypes
}

// iterate storage, check if typeIdsWithProvider contains the typeId, if so, add to views
acct.forEachStored(fun (path: StoragePath, type: Type): Bool {
acct.storage.forEachStored(fun (path: StoragePath, type: Type): Bool {

if typeIdsWithProvider[address] == nil {
return true
Expand All @@ -59,12 +59,12 @@ pub fun main(addr: Address): AnyStruct {
if type.isInstance(collectionType) {
continue
}
if let collection = acct.borrow<&{NonFungibleToken.CollectionPublic}>(from: path) {
if let collection = acct.storage.borrow<&{NonFungibleToken.CollectionPublic}>(from: path) {
// Iterate over IDs & resolve the view
for id in collection.getIDs() {
let nft = collection.borrowNFT(id: id)
let nft = collection.borrowNFT(id)!
if let display = nft.resolveView(Type<MetadataViews.Display>())! as? MetadataViews.Display {
views.insert(key: nft.uuid, display)
views.insert(key: id, display)
}
}
}
Expand Down
27 changes: 13 additions & 14 deletions scripts/hybrid-custody/get_all_collection_data_from_storage.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,31 @@ import "HybridCustody"

/// Helper function that retrieves data about all publicly accessible NFTs in an account
///
pub fun getAllViewsFromAddress(_ address: Address): [MetadataViews.NFTCollectionData] {
access(all) fun getAllViewsFromAddress(_ address: Address): [MetadataViews.NFTCollectionData] {

let account: AuthAccount = getAuthAccount(address)
let account = getAuthAccount<auth(BorrowValue) &Account>(address)
let data: [MetadataViews.NFTCollectionData] = []

let collectionType: Type = Type<@{NonFungibleToken.CollectionPublic, MetadataViews.ResolverCollection}>()
let collectionType: Type = Type<@{NonFungibleToken.Collection}>()
let viewType: Type = Type<MetadataViews.NFTCollectionData>()

// Iterate over each public path
account.forEachStored(fun (path: StoragePath, type: Type): Bool {
// Return if not the type we're looking for
if !type.isInstance(collectionType) && !type.isSubtype(of: collectionType) {
account.storage.forEachStored(fun (path: StoragePath, type: Type): Bool {
// Return early if the collection is broken or is not the type we're looking for
if type.isRecovered || (!type.isInstance(collectionType) && !type.isSubtype(of: collectionType)) {
return true
}
if let collectionRef = account
.borrow<&{NonFungibleToken.CollectionPublic, MetadataViews.ResolverCollection}>(from: path) {
if let collectionRef = account.storage.borrow<&{NonFungibleToken.Collection}>(from: path) {
// Return early if no Resolver found in the Collection
let ids: [UInt64]= collectionRef.getIDs()
if ids.length == 0 {
return true
}
// Otherwise, attempt to get the NFTCollectionData & append if exists
if let dataView = collectionRef.borrowViewResolver(id: ids[0]).resolveView(viewType) as! MetadataViews.NFTCollectionData? {
data.append(dataView)
if let resolver = collectionRef.borrowViewResolver(id: ids[0]) {
if let dataView = resolver.resolveView(viewType) as! MetadataViews.NFTCollectionData? {
data.append(dataView)
}
}
}
return true
Expand All @@ -37,16 +38,14 @@ pub fun getAllViewsFromAddress(_ address: Address): [MetadataViews.NFTCollection

/// Script that retrieve data about all NFT Collections in the storage of an account and any of its child accounts
///
pub fun main(address: Address): {Address: [MetadataViews.NFTCollectionData]} {
access(all) fun main(address: Address): {Address: [MetadataViews.NFTCollectionData]} {

let allNFTData: {Address: [MetadataViews.NFTCollectionData]} = {address: getAllViewsFromAddress(address)}
let seen: [Address] = [address]

/* Iterate over any child accounts */
//
if let managerRef = getAccount(address).getCapability<&HybridCustody.Manager{HybridCustody.ManagerPublic}>(
HybridCustody.ManagerPublicPath
).borrow() {
if let managerRef = getAccount(address).capabilities.borrow<&HybridCustody.Manager>(HybridCustody.ManagerPublicPath) {

for childAddress in managerRef.getChildAddresses() {
allNFTData.insert(key: childAddress, getAllViewsFromAddress(childAddress))
Expand Down
14 changes: 7 additions & 7 deletions scripts/hybrid-custody/get_all_vault_bal_from_storage.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import "HybridCustody"

/// Returns a mapping of balances indexed on the Type of resource containing the balance
///
pub fun getAllBalancesInStorage(_ address: Address): {Type: UFix64} {
access(all) fun getAllBalancesInStorage(_ address: Address): {Type: UFix64} {
// Get the account
let account: AuthAccount = getAuthAccount(address)
let account = getAuthAccount<auth(BorrowValue) &Account>(address)
// Init for return value
let balances: {Type: UFix64} = {}
// Track seen Types in array
let seen: [Type] = []
// Assign the type we'll need
let balanceType: Type = Type<@{FungibleToken.Balance}>()
// Iterate over all stored items & get the path if the type is what we're looking for
account.forEachStored(fun (path: StoragePath, type: Type): Bool {
if type.isInstance(balanceType) || type.isSubtype(of: balanceType) {
account.storage.forEachStored(fun (path: StoragePath, type: Type): Bool {
if !type.isRecovered && (type.isInstance(balanceType) || type.isSubtype(of: balanceType)) {
// Get a reference to the resource & its balance
let vaultRef = account.borrow<&{FungibleToken.Balance}>(from: path)!
let vaultRef = account.storage.borrow<&{FungibleToken.Balance}>(from: path)!
// Insert a new values if it's the first time we've seen the type
if !seen.contains(type) {
balances.insert(key: type, vaultRef.balance)
Expand All @@ -34,7 +34,7 @@ pub fun getAllBalancesInStorage(_ address: Address): {Type: UFix64} {

/// Queries for FT.Vault balance of all FT.Vaults in the specified account and all of its associated accounts
///
pub fun main(address: Address): {Address: {Type: UFix64}} {
access(all) fun main(address: Address): {Address: {Type: UFix64}} {

// Get the balance for the given address
let balances: {Address: {Type: UFix64}} = { address: getAllBalancesInStorage(address) }
Expand All @@ -43,7 +43,7 @@ pub fun main(address: Address): {Address: {Type: UFix64}} {

/* Iterate over any associated accounts */
//
if let managerRef = getAuthAccount(address)
if let managerRef = getAuthAccount<auth(BorrowValue) &Account>(address).storage
.borrow<&HybridCustody.Manager>(from: HybridCustody.ManagerStoragePath) {

for childAccount in managerRef.getChildAddresses() {
Expand Down
53 changes: 0 additions & 53 deletions scripts/hybrid-custody/get_nft_display_view_from_public.cdc

This file was deleted.

56 changes: 56 additions & 0 deletions scripts/hybrid-custody/get_nft_display_view_from_storage.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import "NonFungibleToken"
import "ViewResolver"
import "MetadataViews"
import "HybridCustody"

/// Returns resolved Display from given address at specified path for each ID or nil if ResolverCollection is not found
///
access(all) fun getViews(_ address: Address, _ collectionPath: StoragePath): {UInt64: MetadataViews.Display} {

let account = getAuthAccount<auth(BorrowValue) &Account>(address)
let views: {UInt64: MetadataViews.Display} = {}

// Borrow the Collection
if let collection = account.storage
.borrow<&{NonFungibleToken.Collection}>(from: collectionPath) {
// Iterate over IDs & resolve the view
for id in collection.getIDs() {
if let resolver = collection.borrowViewResolver(id: id) {
if let display = resolver.resolveView(Type<MetadataViews.Display>()) as! MetadataViews.Display? {
views.insert(key: id, display)
}
}
}
}

return views
}

/// Queries for the MetadataViews.Display of each NFT across all associated accounts from Collections at the provided
/// PublicPath
///
access(all) fun main(address: Address, collectionPath: StoragePath): {Address: {UInt64: MetadataViews.Display}} {

let allViews: {Address: {UInt64: MetadataViews.Display}} = {address: getViews(address, collectionPath)}
let seen: [Address] = [address]

/* Iterate over any associated accounts */
//
if let managerRef = getAuthAccount<auth(BorrowValue) &Account>(address).storage
.borrow<&HybridCustody.Manager>(from: HybridCustody.ManagerStoragePath) {

for childAccount in managerRef.getChildAddresses() {
allViews.insert(key: childAccount, getViews(address, collectionPath))
seen.append(childAccount)
}

for ownedAccount in managerRef.getOwnedAddresses() {
if seen.contains(ownedAccount) == false {
allViews.insert(key: ownedAccount, getViews(address, collectionPath))
seen.append(ownedAccount)
}
}
}

return allViews
}
11 changes: 6 additions & 5 deletions scripts/hybrid-custody/get_owned_addresses.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import "HybridCustody"

/// Returns a list of all ownedAccount addresses in the `parent` account's `HybridCustody.Manager`
///
pub fun main(parent: Address): [Address] {
let acct = getAuthAccount(parent)
let manager = acct.borrow<&HybridCustody.Manager>(from: HybridCustody.ManagerStoragePath)
?? panic("manager not found")
access(all) fun main(parent: Address): [Address] {
let acct = getAuthAccount<auth(BorrowValue) &Account>(parent)
let manager = acct.storage.borrow<&HybridCustody.Manager>(
from: HybridCustody.ManagerStoragePath
) ?? panic("A HybridCustody Manager has not been initialized in account with address ".concat(parent.toString()))
return manager.getOwnedAddresses()
}
}
Loading

0 comments on commit 235c24d

Please sign in to comment.