-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbuyNFTForSale.cdc
81 lines (66 loc) · 4.39 KB
/
buyNFTForSale.cdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import FindMarket from "../contracts/FindMarket.cdc"
import FindMarketSale from "../contracts/FindMarketSale.cdc"
import NonFungibleToken from "../contracts/standard/NonFungibleToken.cdc"
import MetadataViews from "../contracts/standard/MetadataViews.cdc"
import FINDNFTCatalog from "../contracts/FINDNFTCatalog.cdc"
import FTRegistry from "../contracts/FTRegistry.cdc"
import FungibleToken from "../contracts/standard/FungibleToken.cdc"
import FIND from "../contracts/FIND.cdc"
transaction(user: String, id: UInt64, amount: UFix64) {
var targetCapability : Capability<&{NonFungibleToken.Receiver}>
let walletReference : &FungibleToken.Vault
let saleItemsCap: Capability<&FindMarketSale.SaleItemCollection{FindMarketSale.SaleItemCollectionPublic}>
prepare(account: AuthAccount) {
let marketplace = FindMarket.getFindTenantAddress()
let tenantCapability= FindMarket.getTenantCapability(marketplace)!
let saleItemType= Type<@FindMarketSale.SaleItemCollection>()
let tenant = tenantCapability.borrow()!
let publicPath=FindMarket.getPublicPath(saleItemType, name: tenant.name)
let storagePath= FindMarket.getStoragePath(saleItemType, name:tenant.name)
let saleItemCap= account.getCapability<&FindMarketSale.SaleItemCollection{FindMarketSale.SaleItemCollectionPublic, FindMarket.SaleItemCollectionPublic}>(publicPath)
if !saleItemCap.check() {
//The link here has to be a capability not a tenant, because it can change.
account.save<@FindMarketSale.SaleItemCollection>(<- FindMarketSale.createEmptySaleItemCollection(tenantCapability), to: storagePath)
account.link<&FindMarketSale.SaleItemCollection{FindMarketSale.SaleItemCollectionPublic, FindMarket.SaleItemCollectionPublic}>(publicPath, target: storagePath)
}
let resolveAddress = FIND.resolve(user)
if resolveAddress == nil {
panic("The address input is not a valid name nor address. Input : ".concat(user))
}
let address = resolveAddress!
self.saleItemsCap= FindMarketSale.getSaleItemCapability(marketplace: marketplace, user:address) ?? panic("cannot find sale item cap")
let marketOption = FindMarket.getMarketOptionFromType(Type<@FindMarketSale.SaleItem>())
let item= FindMarket.assertOperationValid(tenant: marketplace, address: address, marketOption: marketOption, id: id)
let collectionIdentifier = FINDNFTCatalog.getCollectionsForType(nftTypeIdentifier: item.getItemType().identifier)?.keys ?? panic("This NFT is not supported by the NFT Catalog yet. Type : ".concat(item.getItemType().identifier))
let collection = FINDNFTCatalog.getCatalogEntry(collectionIdentifier : collectionIdentifier[0])!
let nft = collection.collectionData
let ft = FTRegistry.getFTInfoByTypeIdentifier(item.getFtType().identifier) ?? panic("This FT is not supported by the Find Market yet. Type : ".concat(item.getFtType().identifier))
self.targetCapability= account.getCapability<&{NonFungibleToken.Receiver}>(nft.publicPath)
/* Check for nftCapability */
if !self.targetCapability.check() {
let cd = item.getNFTCollectionData()
// should use account.type here instead
if account.type(at: cd.storagePath) != nil {
let pathIdentifier = nft.publicPath.toString()
let findPath = PublicPath(identifier: pathIdentifier.slice(from: "/public/".length , upTo: pathIdentifier.length).concat("_FIND"))!
account.link<&{NonFungibleToken.CollectionPublic, NonFungibleToken.Receiver, MetadataViews.ResolverCollection}>(
findPath,
target: nft.storagePath
)
self.targetCapability = account.getCapability<&{NonFungibleToken.CollectionPublic, NonFungibleToken.Receiver, MetadataViews.ResolverCollection}>(findPath)
} else {
account.save(<- cd.createEmptyCollection(), to: cd.storagePath)
account.link<&{NonFungibleToken.CollectionPublic, NonFungibleToken.Receiver, MetadataViews.ResolverCollection}>(cd.publicPath, target: cd.storagePath)
account.link<&{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic, NonFungibleToken.Receiver, MetadataViews.ResolverCollection}>(cd.providerPath, target: cd.storagePath)
}
}
self.walletReference = account.borrow<&FungibleToken.Vault>(from: ft.vaultPath) ?? panic("No suitable wallet linked for this account")
}
pre {
self.walletReference.balance > amount : "Your wallet does not have enough funds to pay for this item"
}
execute {
let vault <- self.walletReference.withdraw(amount: amount)
self.saleItemsCap.borrow()!.buy(id:id, vault: <- vault, nftCap: self.targetCapability)
}
}