// 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: &FRC20Storefront.Storefrontletlisting: &FRC20Storefront.Listing{FRC20Storefront.ListingPublic}prepare(acct: AuthAccount) {/** ------------- Start -- FRC20 Storefront Initialization ------------- */// Create Storefront if it doesn't existif acct.borrow<&AnyResource>(from: FRC20Storefront.StorefrontStoragePath) ==nil { acct.save(<- FRC20Storefront.createStorefront(), to: FRC20Storefront.StorefrontStoragePath) acct.unlink(FRC20Storefront.StorefrontPublicPath) acct.link<&FRC20Storefront.Storefront{FRC20Storefront.StorefrontPublic}>(FRC20Storefront.StorefrontPublicPath, target: FRC20Storefront.StorefrontStoragePath)
} self.storefront = acct.borrow<&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.borrow<&AnyResource>(from: FRC20FTShared.TransactionHookStoragePath) ==nil {lethooks <- FRC20FTShared.createHooks() acct.save(<- hooks, to: FRC20FTShared.TransactionHookStoragePath) }// link the hooks to the public pathif acct .getCapability<&FRC20FTShared.Hooks{FRC20FTShared.TransactionHook, FixesHeartbeat.IHeartbeatHook}>(FRC20FTShared.TransactionHookPublicPath)
.borrow()==nil {// link the hooks to the public path acct.unlink(FRC20FTShared.TransactionHookPublicPath) acct.link<&FRC20FTShared.Hooks{FRC20FTShared.TransactionHook, FixesHeartbeat.IHeartbeatHook}>( FRC20FTShared.TransactionHookPublicPath, target: FRC20FTShared.TransactionHookStoragePath ) }// borrow the hooks referencelethooksRef = acct.borrow<&FRC20FTShared.Hooks>(from: FRC20FTShared.TransactionHookStoragePath)??panic("The hooks were not created")// Ensure Trading Records is initializedif acct.borrow<&AnyResource>(from: FRC20TradingRecord.TradingRecordsStoragePath) ==nil {lettradingRecords <- FRC20TradingRecord.createTradingRecords(nil) acct.save(<- tradingRecords, to: FRC20TradingRecord.TradingRecordsStoragePath)// link the trading records to the public path acct.unlink(FRC20TradingRecord.TradingRecordsPublicPath) acct.link<&FRC20TradingRecord.TradingRecords{FRC20TradingRecord.TradingRecordsPublic, FRC20TradingRecord.TradingStatusViewer, FRC20FTShared.TransactionHook}>(FRC20TradingRecord.TradingRecordsPublicPath, target: FRC20TradingRecord.TradingRecordsStoragePath)
}// Ensure trading record hook is added to the hooks// get the public capability of the trading record hooklettradingRecordsCap = acct .getCapability<&FRC20TradingRecord.TradingRecords{FRC20TradingRecord.TradingRecordsPublic, FRC20TradingRecord.TradingStatusViewer, FRC20FTShared.TransactionHook}>(
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.borrow<&AnyResource>(from: FixesAvatar.AvatarStoragePath) ==nil { acct.save(<- FixesAvatar.create(), to: FixesAvatar.AvatarStoragePath)// link the avatar to the public path acct.unlink(FixesAvatar.AvatarPublicPath) acct.link<&FixesAvatar.Profile{FixesAvatar.ProfilePublic, FRC20FTShared.TransactionHook, MetadataViews.Resolver}>(FixesAvatar.AvatarPublicPath, target: FixesAvatar.AvatarStoragePath)
}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") }}