Smart Contract

TitTokenMarket

A.66b60643244a7738.TitTokenMarket

Deployed

16h ago
Feb 28, 2026, 02:29:07 AM UTC

Dependents

0 imports
1import FungibleToken from 0xf233dcee88fe0abe
2import TitToken from 0x66b60643244a7738 // Replace with actual address
3
4pub contract TitTokenMarket {
5    pub resource Listing {
6        pub let seller: Address
7        pub let price: UFix64
8        pub let amount: UFix64
9        pub var tokenVault: @FungibleToken.Vault
10
11        init(_seller: Address, _price: UFix64, _amount: UFix64, _vault: @FungibleToken.Vault) {
12            self.seller = _seller
13            self.price = _price
14            self.amount = _amount
15            self.tokenVault <- _vault
16        }
17
18        // This method allows the transfer of tokens to a buyer's vault
19        pub fun transferTokens(buyerVaultRef: &{FungibleToken.Receiver}) {
20            let amount = self.amount
21            let tokens <- self.tokenVault.withdraw(amount: amount)
22            buyerVaultRef.deposit(from: <- tokens)
23        }
24
25        destroy() {
26            destroy self.tokenVault
27        }
28    }
29
30    pub var listings: @{UInt64: Listing}
31    pub var nextListingId: UInt64
32
33
34    pub event ListingCreated(listingId: UInt64, seller: Address, price: UFix64, amount: UFix64)
35    pub event TokensPurchased(listingId: UInt64, buyer: Address, price: UFix64, amount: UFix64)
36    pub event ListingRemoved(listingId: UInt64, seller: Address)
37
38    // Function to list TitTokens for sale
39    pub fun createListing(signer: AuthAccount, price: UFix64, amount: UFix64): UInt64 {
40        let vaultRef = signer.borrow<&TitToken.Vault{FungibleToken.Provider, FungibleToken.Balance}>(from: TitToken.VaultStoragePath)
41            ?? panic("Could not borrow reference to the TitToken vault")
42
43        let tokens <- vaultRef.withdraw(amount: amount)
44
45        let listingId = self.nextListingId
46        self.nextListingId = self.nextListingId + 1
47
48        let listing <- create Listing(_seller: signer.address, _price: price, _amount: amount, _vault: <- tokens)
49        self.listings[listingId] <-! listing
50        emit ListingCreated(listingId: listingId, seller: signer.address, price: price, amount: amount)
51
52        return listingId
53    }
54
55    // Function to purchase TitTokens
56    pub fun purchaseTokens(listingId: UInt64, buyer: AuthAccount, paymentVault: @FungibleToken.Vault) {
57        let listing <- self.listings.remove(key: listingId)
58            ?? panic("Listing does not exist.")
59
60        let seller = getAccount(listing.seller)
61
62        // Ensure the paymentVault is a Flow token vault and deposit Flow tokens into the seller's vault
63        let receiver = seller.getCapability<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)
64            .borrow() ?? panic("Could not borrow receiver reference to the seller's Flow token vault")
65        receiver.deposit(from: <- paymentVault)
66
67        // Ensure the buyer has a TitToken receiver and transfer TitTokens from the listing to the buyer
68        let buyerReceiver = buyer.getCapability<&{FungibleToken.Receiver}>(TitToken.VaultReceiverPath)
69            .borrow() ?? panic("Could not borrow receiver reference to the buyer's TitToken vault")
70        listing.transferTokens(buyerVaultRef: buyerReceiver)
71
72        emit TokensPurchased(listingId: listingId, buyer: buyer.address, price: listing.price, amount: listing.amount)
73        destroy listing
74    }
75
76
77    // Function to remove a listing
78    pub fun removeListing(signer: AuthAccount, listingId: UInt64) {
79        let listing <- self.listings.remove(key: listingId)
80            ?? panic("Listing does not exist.")
81
82        assert(listing.seller == signer.address, message: "Only the seller can remove the listing")
83
84        emit ListingRemoved(listingId: listingId, seller: signer.address)
85        destroy listing
86    }
87
88    init() {
89        self.listings <- {}
90        self.nextListingId = 1
91    }
92}
93