// Thirdparty importsimport"MetadataViews"import"FungibleToken"import"FlowToken"// Fixes importsimport"Fixes"import"FixesAvatar"import"FixesHeartbeat"import"FRC20FTShared"import"FRC20Indexer"import"FRC20TradingRecord"import"FRC20Storefront"import"FRC20Marketplace"import"FRC20MarketManager"transaction( listingId: UInt64) {letstorefront: auth(FRC20Storefront.Owner) &FRC20Storefront.Storefrontletlisting: &{FRC20Storefront.ListingPublic}prepare(acct: auth(Storage, Capabilities) &Account) {/** ------------- Start -- FRC20 Storefront Initialization ------------- */// Create Storefront if it doesn't existif acct.storage.borrow<&AnyResource>(from: FRC20Storefront.StorefrontStoragePath) ==nil { acct.storage.save(<- FRC20Storefront.createStorefront(), to: FRC20Storefront.StorefrontStoragePath) acct.capabilities.unpublish(FRC20Storefront.StorefrontPublicPath) acct.capabilities.publish( acct.capabilities.storage.issue<&FRC20Storefront.Storefront>(FRC20Storefront.StorefrontStoragePath), at: FRC20Storefront.StorefrontPublicPath ) } self.storefront = acct.storage .borrow<auth(FRC20Storefront.Owner) &FRC20Storefront.Storefront>(from: FRC20Storefront.StorefrontStoragePath)
??panic("Missing or mis-typed NFTStorefront Storefront")/** ------------- End -------------------------------------------------- */ self.listing = self.storefront.borrowListing(listingId)??panic("Failed to borrow listing:".concat(listingId.toString()))lettick = self.listing.getTickName()/** ------------- Start -- TradingRecords General Initialization ------------- */// Ensure hooks are initializedif acct.storage.borrow<&AnyResource>(from: FRC20FTShared.TransactionHookStoragePath) ==nil {lethooks <- FRC20FTShared.createHooks() acct.storage.save(<- hooks, to: FRC20FTShared.TransactionHookStoragePath) }// link the hooks to the public pathif acct .capabilities.get<&FRC20FTShared.Hooks>(FRC20FTShared.TransactionHookPublicPath) .borrow()==nil {// link the hooks to the public path acct.capabilities.unpublish(FRC20FTShared.TransactionHookPublicPath) acct.capabilities.publish( acct.capabilities.storage.issue<&FRC20FTShared.Hooks>(FRC20FTShared.TransactionHookStoragePath), at: FRC20FTShared.TransactionHookPublicPath ) }// borrow the hooks referencelethooksRef = acct.storage .borrow<auth(FRC20FTShared.Manage) &FRC20FTShared.Hooks>(from: FRC20FTShared.TransactionHookStoragePath)??panic("The hooks were not created")// Ensure Trading Records is initializedif acct.storage.borrow<&AnyResource>(from: FRC20TradingRecord.TradingRecordsStoragePath) ==nil {lettradingRecords <- FRC20TradingRecord.createTradingRecords(nil) acct.storage.save(<- tradingRecords, to: FRC20TradingRecord.TradingRecordsStoragePath)// link the trading records to the public path acct.capabilities.unpublish(FRC20TradingRecord.TradingRecordsPublicPath) acct.capabilities.publish( acct.capabilities.storage.issue<&FRC20TradingRecord.TradingRecords>(FRC20TradingRecord.TradingRecordsStoragePath),
at: FRC20TradingRecord.TradingRecordsPublicPath) }// Ensure trading record hook is added to the hooks// get the public capability of the trading record hooklettradingRecordsCap = acct .capabilities.get<&FRC20TradingRecord.TradingRecords>( FRC20TradingRecord.TradingRecordsPublicPath )assert(tradingRecordsCap.check(), message:"The trading record hook is not valid")// get the reference of the trading record hookletrecordsRef = tradingRecordsCap.borrow()??panic("The trading record hook is not valid")if!hooksRef.hasHook(recordsRef.getType()) { hooksRef.addHook(tradingRecordsCap) }// Ensure Fixes Avatar is initializedif acct.storage.borrow<&AnyResource>(from: FixesAvatar.AvatarStoragePath) ==nil { acct.storage.save(<- FixesAvatar.createProfile(), to: FixesAvatar.AvatarStoragePath)// link the avatar to the public path acct.capabilities.unpublish(FixesAvatar.AvatarPublicPath) acct.capabilities.publish( acct.capabilities.storage.issue<&FixesAvatar.Profile>(FixesAvatar.AvatarStoragePath), at: FixesAvatar.AvatarPublicPath ) }letprofileCap = FixesAvatar.getProfileCap(acct.address)assert(profileCap.check(), message:"The profile is not valid")letprofileRef = profileCap.borrow()??panic("The profile is not valid")if!hooksRef.hasHook(profileRef.getType()) { hooksRef.addHook(profileCap) }/** ------------- End ----------------------------------------------------------------- */ }execute {letuserAddress = self.storefront.owner!.addressletdetails = self.listing.getDetails()letmarket = FRC20MarketManager.borrowMarket(details.tick)varrankedIdInMarket: String? = nil// get from marketif market !=nil {letaccessible = market!.canAccess(addr: userAddress)if accessible {ifletitem = market!.getListedItem(type: details.type, rank: details.priceRank(), id: listingId) { rankedIdInMarket = item.rankedId } } }// remove listingletins <- self.storefront.removeListing(listingResourceID: listingId)destroy ins// remove from marketif rankedIdInMarket !=nil { market!.tryRemoveCompletedListing(rankedId: rankedIdInMarket!) }log("Done: User cancel list") }}