Skip to content

Commit

Permalink
added some more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
bjartek committed Feb 6, 2024
1 parent e6b7413 commit 657706a
Show file tree
Hide file tree
Showing 13 changed files with 386 additions and 9 deletions.
37 changes: 34 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"fmt"

"github.com/bjartek/overflow"
)

func main() {
o := overflow.Overflow(overflow.WithReturnErrors())
o := overflow.Overflow(overflow.WithReturnErrors(), overflow.WithLogFull())

o.Tx("test_init", overflow.WithSigner("user"))

/*Gk
fmt.Println("Try to borrow as a concrete NFT collection type when collection holds string")
fmt.Println()
Expand All @@ -33,4 +35,33 @@ func main() {
fmt.Println("check type when empty")
fmt.Println()
o.Script("test6", overflow.WithArg("user", "user"))
fmt.Println("check type when has universal collection")
fmt.Println()
o.Script("test7", overflow.WithArg("user", "user"))
fmt.Println("try to load and destory if string")
fmt.Println()
o.Script("test8", overflow.WithArg("user", "user"))
fmt.Println("try to borrow using anyResource with string panics")
fmt.Println()
o.Script("test9", overflow.WithArg("user", "user"))
fmt.Println("try to borrow using anyResource with another resource")
fmt.Println()
o.Script("test10", overflow.WithArg("user", "user"))
fmt.Println("try to borrow using anyResource with nothing stored")
fmt.Println()
o.Script("test11", overflow.WithArg("user", "user"))
fmt.Println("provider path test with interface impl")
fmt.Println()
o.Script("test12", overflow.WithArg("user", "user"))
fmt.Println("provider path test with concrete impl")
fmt.Println()
o.Script("test13", overflow.WithArg("user", "user"))
*/
}
28 changes: 28 additions & 0 deletions scripts/test10.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This transaction is what an account would run
/// to set itself up to receive NFTs
import "NonFungibleToken"
import "ExampleNFT"
import "MetadataViews"
import "UniversalCollection"

access(all) fun main(user:Address): [String]{


let messages : [String]=[]

let signer = getAuthAccount<auth(BorrowValue, IssueStorageCapabilityController, PublishCapability, SaveValue, UnpublishCapability, LoadValue) &Account>(user)

let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData? ?? panic("ViewResolver does not resolve NFTCollectionData view")

let collection <- UniversalCollection.createEmptyCollection(identifier: "foo", type: ExampleNFT.getType())
signer.storage.save(<- collection, to:collectionData.storagePath)


let col= signer.storage.borrow<&AnyResource>(from: collectionData.storagePath) as? &{NonFungibleToken.Collection}?
if col != nil {
messages.append("borrow using anyResource and then cast as restricted type")
}
return messages

}
29 changes: 29 additions & 0 deletions scripts/test11.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// This transaction is what an account would run
/// to set itself up to receive NFTs
import "NonFungibleToken"
import "ExampleNFT"
import "MetadataViews"
import "UniversalCollection"

access(all) fun main(user:Address): [String]{


let messages : [String]=[]

let signer = getAuthAccount<auth(BorrowValue, IssueStorageCapabilityController, PublishCapability, SaveValue, UnpublishCapability, LoadValue) &Account>(user)

let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData? ?? panic("ViewResolver does not resolve NFTCollectionData view")


let col= signer.storage.borrow<&AnyResource>(from: collectionData.storagePath) as? &{NonFungibleToken.Collection}?
if col != nil {
messages.append("borrow using anyResource and then cast as restricted type")
}else {

messages.append("borrow using anyResource and then cast as restricted type, is empty")

}
return messages

}
47 changes: 47 additions & 0 deletions scripts/test12.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import "NonFungibleToken"
import "ExampleNFT"
import "MetadataViews"
import "UniversalCollection"

access(all) fun main(user:Address): [String]{


let messages : [String]=[]

let signer = getAuthAccount<auth(BorrowValue, IssueStorageCapabilityController, PublishCapability, SaveValue, UnpublishCapability, LoadValue) &Account>(user)

let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData? ?? panic("ViewResolver does not resolve NFTCollectionData view")


let collection <- collectionData.createEmptyCollection()
signer.storage.save(<-collection, to: collectionData.storagePath)

//if we previously had a storage path there will be created a capcon for it but it will not be saved anywhere
// signer.capabilities.storage.issue<auth(NonFungibleToken.Withdraw) &ExampleNFT.Collection>(collectionData.storagePath)

let storagePathIdentifer = collectionData.storagePath.toString().split(separator:"/")[1]
let providerIdentifier = storagePathIdentifer.concat("Provider")
let providerStoragePath = StoragePath(identifier: providerIdentifier)!

let existingProvider= signer.storage.borrow<&Capability>(from: providerStoragePath)
if existingProvider==nil{
messages.append("could not find provider")
let providerCap=signer.capabilities.storage.issue<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Collection}>(collectionData.storagePath)
signer.storage.save(providerCap, to: providerStoragePath)
}

if let existingProvider2= signer.storage.borrow<&Capability>(from: providerStoragePath) {
messages.append("find provider second time")
messages.append(existingProvider2.getType().identifier)

if existingProvider2.check<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Collection}>(){
messages.append("is provider cap")
} else {
messages.append("is notprovider cap")
}
}

return messages

}
67 changes: 67 additions & 0 deletions scripts/test13.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import "NonFungibleToken"
import "ExampleNFT"
import "MetadataViews"
import "UniversalCollection"

access(all) fun main(user:Address): [String]{


let messages : [String]=[]

let signer = getAuthAccount<auth(Storage, Capabilities) &Account>(user)

let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData? ?? panic("ViewResolver does not resolve NFTCollectionData view")


let collection <- collectionData.createEmptyCollection()
signer.storage.save(<-collection, to: collectionData.storagePath)

//this is what will be in storage after we have migrated a public path
let preCap =signer.capabilities.storage.issue<auth(NonFungibleToken.Withdraw) &ExampleNFT.Collection>(collectionData.storagePath)
messages.append("cap ".concat(preCap.id.toString()))

let pre2Cap =signer.capabilities.storage.issue<auth(NonFungibleToken.Withdraw) &ExampleNFT.Collection>(collectionData.storagePath)
messages.append("cap ".concat(pre2Cap.id.toString()))


signer.capabilities.storage.forEachController(forPath: collectionData.storagePath,fun(scc: &StorageCapabilityController): Bool {
messages.append("pre ".concat(scc.capabilityID.toString()).concat(" ").concat(scc.target().toString()))
return false
})


let storagePathIdentifer = collectionData.storagePath.toString().split(separator:"/")[1]
let providerIdentifier = storagePathIdentifer.concat("Provider")
let providerStoragePath = StoragePath(identifier: providerIdentifier)!

let existingProvider= signer.storage.borrow<&Capability>(from: providerStoragePath)
if existingProvider==nil{
let providerCap=signer.capabilities.storage.issue<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Collection}>(collectionData.storagePath)
messages.append("issued providerCap ".concat(providerCap.id.toString()))
//we save it to storage to memoize it
signer.storage.save(providerCap, to: providerStoragePath)
} else {
if !existingProvider!.check<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Collection}>(){
messages.append("we need to revoke")
}
}

if let existingProvider2= signer.storage.borrow<&Capability>(from: providerStoragePath) {
messages.append("find provider second time")
messages.append(existingProvider2.getType().identifier)

if existingProvider2.check<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Collection}>(){
messages.append("is provider cap")
} else {
messages.append("is notprovider cap")
}
}

signer.capabilities.storage.forEachController(forPath: collectionData.storagePath,fun(scc: &StorageCapabilityController): Bool {
messages.append("post ".concat(scc.capabilityID.toString()).concat(" ").concat(scc.target().toString()))
return false
})

return messages

}
1 change: 0 additions & 1 deletion scripts/test3.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ access(all) fun main(user:Address): String{
let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData? ?? panic("ViewResolver does not resolve NFTCollectionData view")

let collection <- UniversalCollection.createEmptyCollection(identifier: "foo", type: ExampleNFT.getType())
//if we save something here it will not work
signer.storage.save(<- collection, to:collectionData.storagePath)


Expand Down
1 change: 0 additions & 1 deletion scripts/test4.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ access(all) fun main(user:Address): [String]{
let collection <- UniversalCollection.createEmptyCollection(identifier: "foo", type: ExampleNFT.getType())

messages.append(collection.getType().identifier)
//if we save something here it will not work
signer.storage.save(<- collection, to:collectionData.storagePath)

// Return early if the account already has a collection
Expand Down
2 changes: 0 additions & 2 deletions scripts/test5.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ access(all) fun main(user:Address): [String]{
let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData? ?? panic("ViewResolver does not resolve NFTCollectionData view")

let collection <- UniversalCollection.createEmptyCollection(identifier: "foo", type: ExampleNFT.getType())

//if we save something here it will not work
signer.storage.save(<- collection, to:collectionData.storagePath)

// Return early if the account already has a collection
Expand Down
44 changes: 44 additions & 0 deletions scripts/test7.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/// This transaction is what an account would run
/// to set itself up to receive NFTs
import "NonFungibleToken"
import "ExampleNFT"
import "MetadataViews"
import "UniversalCollection"

access(all) fun main(user:Address): [String]{


let messages : [String]=[]

let signer = getAuthAccount<auth(BorrowValue, IssueStorageCapabilityController, PublishCapability, SaveValue, UnpublishCapability, LoadValue) &Account>(user)

let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData? ?? panic("ViewResolver does not resolve NFTCollectionData view")


let collection <- UniversalCollection.createEmptyCollection(identifier: "foo", type: ExampleNFT.getType())
signer.storage.save(<- collection, to:collectionData.storagePath)



// Return early if the account already has a collection
if let storedType =signer.storage.type(at: collectionData.storagePath) {
if storedType.isSubtype(of: Type<@{NonFungibleToken.Collection}>()) {
messages.append("store something that is NFT collection")
}

if storedType != Type<&ExampleNFT.Collection>() {
messages.append("we do not store exampleNFT collection")
destroy signer.storage.load<@AnyResource>(from: collectionData.storagePath)

let collection <- ExampleNFT.createEmptyCollection(nftType: Type<@ExampleNFT.NFT>())

//this will not work if the storage has something else there already
// save it to the account
signer.storage.save(<-collection, to: collectionData.storagePath)
messages.append("we could load and destory and add new collection")
}
}
return messages

}
47 changes: 47 additions & 0 deletions scripts/test8.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

/// This transaction is what an account would run
/// to set itself up to receive NFTs
import "NonFungibleToken"
import "ExampleNFT"
import "MetadataViews"
import "UniversalCollection"

access(all) fun main(user:Address): [String]{


let messages : [String]=[]

let signer = getAuthAccount<auth(BorrowValue, IssueStorageCapabilityController, PublishCapability, SaveValue, UnpublishCapability, LoadValue) &Account>(user)

let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData? ?? panic("ViewResolver does not resolve NFTCollectionData view")


signer.storage.save("foo", to:collectionData.storagePath)



// Return early if the account already has a collection
if let storedType =signer.storage.type(at: collectionData.storagePath) {
if storedType.isSubtype(of: Type<@{NonFungibleToken.Collection}>()) {
messages.append("store something that is NFT collection")
}

if storedType != Type<&ExampleNFT.Collection>() {

if storedType.isSubtype(of: Type<@AnyResource>()) {
destroy signer.storage.load<@AnyResource>(from: collectionData.storagePath)
} else {
signer.storage.load<AnyStruct>(from: collectionData.storagePath)
}
let collection <- ExampleNFT.createEmptyCollection(nftType: Type<@ExampleNFT.NFT>())

//this will not work if the storage has something else there already
// save it to the account
signer.storage.save(<-collection, to: collectionData.storagePath)
messages.append("we could load and destory and add new collection")
}
}
return messages

}
29 changes: 29 additions & 0 deletions scripts/test9.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// This transaction is what an account would run
/// to set itself up to receive NFTs
import "NonFungibleToken"
import "ExampleNFT"
import "MetadataViews"
import "UniversalCollection"

access(all) fun main(user:Address): [String]{


let messages : [String]=[]

let signer = getAuthAccount<auth(BorrowValue, IssueStorageCapabilityController, PublishCapability, SaveValue, UnpublishCapability, LoadValue) &Account>(user)

let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData? ?? panic("ViewResolver does not resolve NFTCollectionData view")


signer.storage.save("foo", to:collectionData.storagePath)

// Return early if the account already has a collection
let col= signer.storage.borrow<&AnyResource>(from: collectionData.storagePath) as? &{NonFungibleToken.Collection}?
if col == nil {
messages.append("could not borrow string as AnyResource")
}
return messages

}
2 changes: 0 additions & 2 deletions transactions/setup_account.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ transaction {
let collectionData = ExampleNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>()) as! MetadataViews.NFTCollectionData?
?? panic("ViewResolver does not resolve NFTCollectionData view")

signer.storage.save("foo", to:collectionData.storagePath)

// Return early if the account already has a collection
if signer.storage.borrow<&ExampleNFT.Collection>(from: collectionData.storagePath) != nil {
return
Expand Down
Loading

0 comments on commit 657706a

Please sign in to comment.