Skip to content
This repository was archived by the owner on Jul 8, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions web/cadence/scripts/get_account_item.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import NonFungibleToken from 0xNonFungibleToken
import MetadataViews from 0xMetadataViews
import KittyItems from 0xKittyItems

pub struct KittyItem {
pub let name: String
pub let description: String
pub let image: String

pub let itemID: UInt64
pub let resourceID: UInt64
pub let kind: KittyItems.Kind
pub let rarity: KittyItems.Rarity
pub let owner: Address

init(
name: String,
description: String,
image: String,
itemID: UInt64,
resourceID: UInt64,
kind: KittyItems.Kind,
rarity: KittyItems.Rarity,
owner: Address,
) {
self.name = name
self.description = description
self.image = image

self.itemID = itemID
self.resourceID = resourceID
self.kind = kind
self.rarity = rarity
self.owner = owner
}
}

pub fun fetch(address: Address, itemID: UInt64): KittyItem? {
if let collection = getAccount(address).getCapability<&KittyItems.Collection{NonFungibleToken.CollectionPublic, KittyItems.KittyItemsCollectionPublic}>(KittyItems.CollectionPublicPath).borrow() {

if let item = collection.borrowKittyItem(id: itemID) {

if let view = item.resolveView(Type<MetadataViews.Display>()) {

let display = view as! MetadataViews.Display

let owner: Address = item.owner!.address!

let ipfsThumbnail = display.thumbnail as! MetadataViews.IPFSFile

return KittyItem(
name: display.name,
description: display.description,
image: item.imageCID(),
itemID: itemID,
resourceID: item.uuid,
kind: item.kind,
rarity: item.rarity,
owner: address,
)
}
}
}

return nil
}

pub fun main(keys: [String], addresses: [Address], ids: [UInt64]): {String: KittyItem?} {
let r: {String: KittyItem?} = {}
var i = 0
while i < keys.length {
let key = keys[i]
let address = addresses[i]
let id = ids[i]
r[key] = fetch(address: address, itemID: id)
i = i + 1
}
return r
}
10 changes: 10 additions & 0 deletions web/cadence/scripts/get_account_items.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import NonFungibleToken from 0xNonFungibleToken
import KittyItems from 0xKittyItems

pub fun main(address: Address): [UInt64] {
if let collection = getAccount(address).getCapability<&KittyItems.Collection{NonFungibleToken.CollectionPublic, KittyItems.KittyItemsCollectionPublic}>(KittyItems.CollectionPublicPath).borrow() {
return collection.getIDs()
}

return []
}
82 changes: 82 additions & 0 deletions web/cadence/scripts/get_listing.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import NonFungibleToken from 0xNonFungibleToken
import MetadataViews from 0xMetadataViews
import NFTStorefront from 0xNFTStorefront
import KittyItems from 0xKittyItems

pub struct ListingItem {
pub let name: String
pub let description: String
pub let image: String

pub let itemID: UInt64
pub let resourceID: UInt64
pub let kind: KittyItems.Kind
pub let rarity: KittyItems.Rarity
pub let owner: Address
pub let price: UFix64

init(
name: String,
description: String,
image: String,
itemID: UInt64,
resourceID: UInt64,
kind: KittyItems.Kind,
rarity: KittyItems.Rarity,
owner: Address,
price: UFix64
) {
self.name = name
self.description = description
self.image = image

self.itemID = itemID
self.resourceID = resourceID
self.kind = kind
self.rarity = rarity
self.owner = owner
self.price = price
}
}

pub fun main(address: Address, listingResourceID: UInt64): ListingItem? {
if let storefrontRef = getAccount(address).getCapability<&NFTStorefront.Storefront{NFTStorefront.StorefrontPublic}>(NFTStorefront.StorefrontPublicPath).borrow() {

if let listing = storefrontRef.borrowListing(listingResourceID: listingResourceID) {

let details = listing.getDetails()

let itemID = details.nftID
let itemPrice = details.salePrice

if let collection = getAccount(address).getCapability<&KittyItems.Collection{NonFungibleToken.CollectionPublic, KittyItems.KittyItemsCollectionPublic}>(KittyItems.CollectionPublicPath).borrow() {

if let item = collection.borrowKittyItem(id: itemID) {

if let view = item.resolveView(Type<MetadataViews.Display>()) {

let display = view as! MetadataViews.Display

let owner: Address = item.owner!.address!

let ipfsThumbnail = display.thumbnail as! MetadataViews.IPFSFile

return ListingItem(
name: display.name,
description: display.description,
image: item.imageCID(),
itemID: itemID,
resourceID: item.uuid,
kind: item.kind,
rarity: item.rarity,
owner: address,
price: itemPrice
)
}
}
}
}
}

return nil
}
12 changes: 12 additions & 0 deletions web/cadence/scripts/get_listings.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import NFTStorefront from 0xNFTStorefront

pub fun main(address: Address): [UInt64] {
let storefrontRef = getAccount(address)
.getCapability<&NFTStorefront.Storefront{NFTStorefront.StorefrontPublic}>(
NFTStorefront.StorefrontPublicPath
)
.borrow()
?? panic("Could not borrow public storefront from address")

return storefrontRef.getListingIDs()
}
23 changes: 23 additions & 0 deletions web/cadence/scripts/is_account_initialized.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import FungibleToken from 0xFungibleToken
import NonFungibleToken from 0xNonFungibleToken
import KittyItems from 0xKittyItems
import NFTStorefront from 0xNFTStorefront

pub fun hasItems(_ address: Address): Bool {
return getAccount(address)
.getCapability<&KittyItems.Collection{NonFungibleToken.CollectionPublic, KittyItems.KittyItemsCollectionPublic}>(KittyItems.CollectionPublicPath)
.check()
}

pub fun hasStorefront(_ address: Address): Bool {
return getAccount(address)
.getCapability<&NFTStorefront.Storefront{NFTStorefront.StorefrontPublic}>(NFTStorefront.StorefrontPublicPath)
.check()
}

pub fun main(address: Address): {String: Bool} {
let ret: {String: Bool} = {}
ret["KittyItems"] = hasItems(address)
ret["KittyItemsMarket"] = hasStorefront(address)
return ret
}
62 changes: 62 additions & 0 deletions web/cadence/transactions/create_listing.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import FungibleToken from 0xFungibleToken
import NonFungibleToken from 0xNonFungibleToken
import FlowToken from 0xFlowToken
import KittyItems from 0xKittyItems
import NFTStorefront from 0xNFTStorefront

pub fun getOrCreateStorefront(account: AuthAccount): &NFTStorefront.Storefront {
if let storefrontRef = account.borrow<&NFTStorefront.Storefront>(from: NFTStorefront.StorefrontStoragePath) {
return storefrontRef
}

let storefront <- NFTStorefront.createStorefront()

let storefrontRef = &storefront as &NFTStorefront.Storefront

account.save(<-storefront, to: NFTStorefront.StorefrontStoragePath)

account.link<&NFTStorefront.Storefront{NFTStorefront.StorefrontPublic}>(NFTStorefront.StorefrontPublicPath, target: NFTStorefront.StorefrontStoragePath)

return storefrontRef
}

transaction(saleItemID: UInt64, saleItemPrice: UFix64) {

let flowReceiver: Capability<&FlowToken.Vault{FungibleToken.Receiver}>
let kittyItemsProvider: Capability<&KittyItems.Collection{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>
let storefront: &NFTStorefront.Storefront

prepare(account: AuthAccount) {
// We need a provider capability, but one is not provided by default so we create one if needed.
let kittyItemsCollectionProviderPrivatePath = /private/kittyItemsCollectionProvider

self.flowReceiver = account.getCapability<&FlowToken.Vault{FungibleToken.Receiver}>(/public/flowTokenReceiver)!

assert(self.flowReceiver.borrow() != nil, message: "Missing or mis-typed FLOW receiver")

if !account.getCapability<&KittyItems.Collection{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>(kittyItemsCollectionProviderPrivatePath)!.check() {
account.link<&KittyItems.Collection{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>(kittyItemsCollectionProviderPrivatePath, target: KittyItems.CollectionStoragePath)
}

self.kittyItemsProvider = account.getCapability<&KittyItems.Collection{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>(kittyItemsCollectionProviderPrivatePath)!

assert(self.kittyItemsProvider.borrow() != nil, message: "Missing or mis-typed KittyItems.Collection provider")

self.storefront = getOrCreateStorefront(account: account)
}

execute {
let saleCut = NFTStorefront.SaleCut(
receiver: self.flowReceiver,
amount: saleItemPrice
)

self.storefront.createListing(
nftProviderCapability: self.kittyItemsProvider,
nftType: Type<@KittyItems.NFT>(),
nftID: saleItemID,
salePaymentVaultType: Type<@FlowToken.Vault>(),
saleCuts: [saleCut]
)
}
}
36 changes: 36 additions & 0 deletions web/cadence/transactions/initialize_account.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import FungibleToken from 0xFungibleToken
import NonFungibleToken from 0xNonFungibleToken
import KittyItems from 0xKittyItems
import NFTStorefront from 0xNFTStorefront

pub fun hasItems(_ address: Address): Bool {
return getAccount(address)
.getCapability<&KittyItems.Collection{NonFungibleToken.CollectionPublic, KittyItems.KittyItemsCollectionPublic}>(KittyItems.CollectionPublicPath)
.check()
}

pub fun hasStorefront(_ address: Address): Bool {
return getAccount(address)
.getCapability<&NFTStorefront.Storefront{NFTStorefront.StorefrontPublic}>(NFTStorefront.StorefrontPublicPath)
.check()
}

transaction {
prepare(acct: AuthAccount) {
if !hasItems(acct.address) {
if acct.borrow<&KittyItems.Collection>(from: KittyItems.CollectionStoragePath) == nil {
acct.save(<-KittyItems.createEmptyCollection(), to: KittyItems.CollectionStoragePath)
}
acct.unlink(KittyItems.CollectionPublicPath)
acct.link<&KittyItems.Collection{NonFungibleToken.CollectionPublic, KittyItems.KittyItemsCollectionPublic}>(KittyItems.CollectionPublicPath, target: KittyItems.CollectionStoragePath)
}

if !hasStorefront(acct.address) {
if acct.borrow<&NFTStorefront.Storefront>(from: NFTStorefront.StorefrontStoragePath) == nil {
acct.save(<-NFTStorefront.createStorefront(), to: NFTStorefront.StorefrontStoragePath)
}
acct.unlink(NFTStorefront.StorefrontPublicPath)
acct.link<&NFTStorefront.Storefront{NFTStorefront.StorefrontPublic}>(NFTStorefront.StorefrontPublicPath, target: NFTStorefront.StorefrontStoragePath)
}
}
}
62 changes: 62 additions & 0 deletions web/cadence/transactions/purchase_listing.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import FungibleToken from 0xFungibleToken
import NonFungibleToken from 0xNonFungibleToken
import FlowToken from 0xFlowToken
import KittyItems from 0xKittyItems
import NFTStorefront from 0xNFTStorefront

pub fun getOrCreateCollection(account: AuthAccount): &KittyItems.Collection{NonFungibleToken.Receiver} {
if let collectionRef = account.borrow<&KittyItems.Collection>(from: KittyItems.CollectionStoragePath) {
return collectionRef
}

// create a new empty collection
let collection <- KittyItems.createEmptyCollection() as! @KittyItems.Collection

let collectionRef = &collection as &KittyItems.Collection

// save it to the account
account.save(<-collection, to: KittyItems.CollectionStoragePath)

// create a public capability for the collection
account.link<&KittyItems.Collection{NonFungibleToken.CollectionPublic, KittyItems.KittyItemsCollectionPublic}>(KittyItems.CollectionPublicPath, target: KittyItems.CollectionStoragePath)

return collectionRef
}

transaction(listingResourceID: UInt64, storefrontAddress: Address) {
let paymentVault: @FungibleToken.Vault
let kittyItemsCollection: &KittyItems.Collection{NonFungibleToken.Receiver}
let storefront: &NFTStorefront.Storefront{NFTStorefront.StorefrontPublic}
let listing: &NFTStorefront.Listing{NFTStorefront.ListingPublic}

prepare(account: AuthAccount) {
self.storefront = getAccount(storefrontAddress)
.getCapability<&NFTStorefront.Storefront{NFTStorefront.StorefrontPublic}>(
NFTStorefront.StorefrontPublicPath
)!
.borrow()
?? panic("Could not borrow Storefront from provided address")

self.listing = self.storefront.borrowListing(listingResourceID: listingResourceID)
?? panic("No Listing with that ID in Storefront")

let price = self.listing.getDetails().salePrice

let mainFLOWVault = account.borrow<&FlowToken.Vault>(from: /storage/flowTokenVault)
?? panic("Cannot borrow FLOW vault from account storage")

self.paymentVault <- mainFLOWVault.withdraw(amount: price)

self.kittyItemsCollection = getOrCreateCollection(account: account)
}

execute {
let item <- self.listing.purchase(
payment: <-self.paymentVault
)

self.kittyItemsCollection.deposit(token: <-item)

self.storefront.cleanup(listingResourceID: listingResourceID)
}
}
14 changes: 14 additions & 0 deletions web/cadence/transactions/remove_listing.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import NFTStorefront from 0xNFTStorefront

transaction(listingResourceID: UInt64) {
let storefront: &NFTStorefront.Storefront{NFTStorefront.StorefrontManager}

prepare(acct: AuthAccount) {
self.storefront = acct.borrow<&NFTStorefront.Storefront{NFTStorefront.StorefrontManager}>(from: NFTStorefront.StorefrontStoragePath)
?? panic("Missing or mis-typed NFTStorefront.Storefront")
}

execute {
self.storefront.removeListing(listingResourceID: listingResourceID)
}
}
Loading