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 2 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
39 changes: 39 additions & 0 deletions web/cadence/scripts/get_account_item.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import NonFungibleToken from 0xNonFungibleToken
import KittyItems from 0xKittyItems

pub struct AccountItem {
pub let itemID: UInt64
pub let typeID: UInt64
pub let rarityID: UInt64
pub let owner: Address

init(itemID: UInt64, typeID: UInt64, rarityID: UInt64, owner: Address) {
self.itemID = itemID
self.typeID = typeID
self.rarityID = rarityID
self.owner = owner
}
}

pub fun fetch(address: Address, id: UInt64): AccountItem? {
if let col = getAccount(address).getCapability<&KittyItems.Collection{NonFungibleToken.CollectionPublic, KittyItems.KittyItemsCollectionPublic}>(KittyItems.CollectionPublicPath).borrow() {
if let item = col.borrowKittyItem(id: id) {
return AccountItem(itemID: id, typeID: item.typeID, rarityID: item.rarityID, owner: address)
}
}

return nil
}

pub fun main(keys: [String], addresses: [Address], ids: [UInt64]): {String: AccountItem?} {
let r: {String: AccountItem?} = {}
var i = 0
while i < keys.length {
let key = keys[i]
let address = addresses[i]
let id = ids[i]
r[key] = fetch(address: address, id: 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 []
}
37 changes: 37 additions & 0 deletions web/cadence/scripts/get_listing.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import NonFungibleToken from 0xNonFungibleToken
import NFTStorefront from 0xNFTStorefront
import KittyItems from 0xKittyItems

pub struct SaleItem {
pub let itemID: UInt64
pub let typeID: UInt64
pub let owner: Address
pub let price: UFix64

init(itemID: UInt64, typeID: UInt64, owner: Address, price: UFix64) {
self.itemID = itemID
self.typeID = typeID
self.owner = owner
self.price = price
}
}

pub fun main(address: Address, listingResourceID: UInt64): SaleItem? {
let account = getAccount(address)
if let storefrontRef = account.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 = account.getCapability<&KittyItems.Collection{NonFungibleToken.CollectionPublic, KittyItems.KittyItemsCollectionPublic}>(KittyItems.CollectionPublicPath).borrow() {
if let item = collection.borrowKittyItem(id: itemID) {
return SaleItem(itemID: itemID, typeID: item.typeID, owner: address, price: itemPrice)
}
}
}
}

return nil
}
11 changes: 11 additions & 0 deletions web/cadence/scripts/get_listings.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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)
}
}
7 changes: 7 additions & 0 deletions web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ module.exports = {
reactStrictMode: true,
trailingSlash: true,
productionBrowserSourceMaps: true,
webpack: (config, _options) => {
config.module.rules.push({
test: /\.cdc/,
type: "asset/source",
})
return config
},
}
43 changes: 2 additions & 41 deletions web/src/flow/script.get-account-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,9 @@ import * as fcl from "@onflow/fcl"
import * as t from "@onflow/types"
import {batch} from "src/flow/util/batch"
import {expandAccountItemKey} from "src/hooks/useAccountItem"
import getAccountItemScript from "cadence/scripts/get_account_item.cdc"

const CODE = fcl.cdc`
import NonFungibleToken from 0xNonFungibleToken
import KittyItems from 0xKittyItems

pub struct AccountItem {
pub let itemID: UInt64
pub let typeID: UInt64
pub let rarityID: UInt64
pub let owner: Address

init(itemID: UInt64, typeID: UInt64, rarityID: UInt64, owner: Address) {
self.itemID = itemID
self.typeID = typeID
self.rarityID = rarityID
self.owner = owner
}
}

pub fun fetch(address: Address, id: UInt64): AccountItem? {
if let col = getAccount(address).getCapability<&KittyItems.Collection{NonFungibleToken.CollectionPublic, KittyItems.KittyItemsCollectionPublic}>(KittyItems.CollectionPublicPath).borrow() {
if let item = col.borrowKittyItem(id: id) {
return AccountItem(itemID: id, typeID: item.typeID, rarityID: item.rarityID, owner: address)
}
}

return nil
}

pub fun main(keys: [String], addresses: [Address], ids: [UInt64]): {String: AccountItem?} {
let r: {String: AccountItem?} = {}
var i = 0
while i < keys.length {
let key = keys[i]
let address = addresses[i]
let id = ids[i]
r[key] = fetch(address: address, id: id)
i = i + 1
}
return r
}
`
const CODE = fcl.cdc`${getAccountItemScript}`

const collate = px => {
return Object.keys(px).reduce(
Expand Down
14 changes: 1 addition & 13 deletions web/src/flow/script.get-account-items.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import * as fcl from "@onflow/fcl"
import {Address} from "@onflow/types"
import {expandAccountItemsKey} from "src/hooks/useAccountItems"

const FETCH_ACCOUNT_ITEMS_SCRIPT = `
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 []
}
`
import FETCH_ACCOUNT_ITEMS_SCRIPT from "cadence/scripts/get_account_items.cdc"

export function fetchAccountItems(key) {
const {address} = expandAccountItemsKey(key)
Expand Down
Loading