Smart Contract

TitTokenRepository

A.66b60643244a7738.TitTokenRepository

Deployed

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

Dependents

0 imports
1// Testnet
2// import FungibleToken from 0x9a0766d93b6608b7
3// import TitToken from 0x6e9ac121d7106a09
4
5
6// Mainnet
7import FungibleToken from 0xf233dcee88fe0abe
8import TitToken from 0x66b60643244a7738 // Replace with actual address
9
10pub contract TitTokenRepository {
11
12    pub event TresorCreated(tresorId: UInt64, seller: Address, price: UFix64, amount: UFix64)
13    pub event TokensPurchased(tresorId: UInt64, buyer: Address, price: UFix64, amount: UFix64)
14    pub event TresorRemoved(tresorId: UInt64, seller: Address)
15
16    pub let RepositoryStoragePath: StoragePath
17    pub let RepositoryPublicPath: PublicPath
18
19    pub var nextTresorId: UInt64
20
21    pub struct TresorDetails {
22        pub let tresorId: UInt64
23        pub let seller: Address
24        pub let price: UFix64
25        pub let amount: UFix64
26
27        init (tresorId: UInt64, seller: Address, price: UFix64, amount: UFix64) {
28            self.tresorId = tresorId
29            self.seller = seller
30            self.price = price
31            self.amount = amount
32        }
33    }
34
35    pub resource interface TresorPublic {
36
37        pub fun transferAndRemoveTresor(buyerVaultRef: &{FungibleToken.Receiver}, repositoryRef: &TitTokenRepository.Repository{TitTokenRepository.RepositoryPublic})
38
39//        pub fun purchaseTokens(tresorId: UInt64, buyer: AuthAccount, paymentVault: @FungibleToken.Vault)
40
41        pub fun transferTokens(buyerVaultRef: &{FungibleToken.Receiver}) 
42
43        pub fun getDetails(): TresorDetails
44    }
45
46    pub resource Tresor: TresorPublic {
47        pub let details: TresorDetails
48        pub let seller: Address
49        pub let price: UFix64
50        pub let amount: UFix64
51        pub var tokenVault: @FungibleToken.Vault
52
53
54
55
56        pub fun getDetails(): TresorDetails {
57            return self.details
58        }
59
60        // This method allows the transfer of tokens to a buyer's vault
61        pub fun transferTokens(buyerVaultRef: &{FungibleToken.Receiver}) {
62            let amount = self.amount
63            let tokens <- self.tokenVault.withdraw(amount: amount)
64            buyerVaultRef.deposit(from: <- tokens)
65        }
66
67            // New function to handle the transfer and removal
68        pub fun transferAndRemoveTresor(buyerVaultRef: &{FungibleToken.Receiver}, repositoryRef: &TitTokenRepository.Repository{TitTokenRepository.RepositoryPublic}) {
69            // Transfer tokens
70            let amount = self.amount
71            let tokens <- self.tokenVault.withdraw(amount: amount)
72            buyerVaultRef.deposit(from: <- tokens)
73
74            // Remove the Tresor from the repository, triggering destruction
75            repositoryRef.removeTresor(signer: self.getDetails().seller, tresorId: self.getDetails().tresorId)
76        //    emit TokensPurchased(tresorId: self.getDetails().tresorId, buyer: buyer.address, price: tresor.price, amount: tresor.amount)
77        
78        }
79
80
81        init(_seller: Address, _price: UFix64, _amount: UFix64, _vault: @FungibleToken.Vault, _tresorId: UInt64) {
82            self.seller = _seller
83            self.price = _price
84            self.amount = _amount
85            self.tokenVault <- _vault
86
87            self.details = TresorDetails(
88                tresorId: _tresorId,
89                seller: _seller,
90                price: _price,
91                amount: _amount
92            )
93        }
94
95
96
97        destroy() {
98            destroy self.tokenVault
99        }
100    }
101
102    pub resource interface RepositoryManager {
103        pub fun purchaseTokens(tresorId: UInt64, buyer: AuthAccount, paymentVault: @FungibleToken.Vault)
104
105        pub fun createTresor(signer: AuthAccount, price: UFix64, amount: UFix64): UInt64
106
107        pub fun removeTresor(signer: Address, tresorId: UInt64) 
108    }
109
110    pub resource interface RepositoryPublic {
111        pub fun removeTresor(signer: Address, tresorId: UInt64) 
112        pub fun getTresorIDs(): [UInt64]
113//        pub fun getTresorDetails(tresorId: UInt64): TresorDetails
114        pub fun borrowTresor(tresorResourceID: UInt64): &Tresor{TresorPublic}?
115    }
116
117    pub resource Repository: RepositoryManager, RepositoryPublic {
118
119        pub var tresors: @{UInt64: Tresor}
120
121        //A resource with the form of Tresor is created in the createTResor function
122        // and moved to the tresors dictionary with the current index
123        // When the purchase tokens function is called the resource is moved from that index
124        // and the tokens are deposited into the buyers TitTokenStorage
125
126                // Function to purchase TitTokens
127
128        pub fun purchaseTokens(tresorId: UInt64, buyer: AuthAccount, paymentVault: @FungibleToken.Vault) {
129            let tresor <- self.tresors.remove(key: tresorId)
130                ?? panic("Tresor does not exist.")
131
132            let seller = getAccount(tresor.seller)
133
134            // Ensure the paymentVault is a Flow token vault and deposit Flow tokens into the seller's vault
135            let receiver = seller.getCapability<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)
136                .borrow() ?? panic("Could not borrow receiver reference to the seller's Flow token vault")
137            receiver.deposit(from: <- paymentVault)
138
139            // Ensure the buyer has a TitToken receiver and transfer TitTokens from the tresor to the buyer
140            let buyerReceiver = buyer.getCapability<&{FungibleToken.Receiver}>(TitToken.VaultReceiverPath)
141                .borrow() ?? panic("Could not borrow receiver reference to the buyer's TitToken vault")
142            tresor.transferTokens(buyerVaultRef: buyerReceiver)
143
144
145            emit TokensPurchased(tresorId: tresorId, buyer: buyer.address, price: tresor.price, amount: tresor.amount)
146            destroy tresor
147        }
148
149        // Function to list TitTokens for sale
150        pub fun createTresor(signer: AuthAccount, price: UFix64, amount: UFix64): UInt64 {
151            let vaultRef = signer.borrow<&TitToken.Vault{FungibleToken.Provider, FungibleToken.Balance}>(from: TitToken.VaultStoragePath)
152                ?? panic("Could not borrow reference to the TitToken vault")
153
154            let tokens <- vaultRef.withdraw(amount: amount)
155
156            // Use the contract-level nextTresorId for uniqueness
157            let tresorId = TitTokenRepository.nextTresorId
158            TitTokenRepository.nextTresorId = TitTokenRepository.nextTresorId + 1
159
160            let tresor <- create Tresor(_seller: signer.address, _price: price, _amount: amount, _vault: <- tokens, _tresorId: tresorId)
161            self.tresors[tresorId] <-! tresor
162            emit TresorCreated(tresorId: tresorId, seller: signer.address, price: price, amount: amount)
163
164            return tresorId
165        }
166
167
168
169        // Function to remove a tresor
170        pub fun removeTresor(signer: Address, tresorId: UInt64) {
171            let tresor <- self.tresors.remove(key: tresorId)
172                ?? panic("Tresor does not exist.")
173
174            // assert(tresor.seller == signer.address, message: "Only the seller can remove the tresor")
175
176            emit TresorRemoved(tresorId: tresorId, seller: signer)
177            destroy tresor
178        }
179
180        // This function works for this contract, the problem with this contract
181        // is that we can't use the purchase function, because even though the 
182        // IDs exist, the Tresor resource for some reason can't find them???
183        pub fun getTresorIDs(): [UInt64] {
184            return self.tresors.keys
185        }
186
187        pub fun borrowTresor(tresorResourceID: UInt64): &Tresor{TresorPublic}? {
188
189            if self.tresors[tresorResourceID] != nil {
190                return(&self.tresors[tresorResourceID] as &Tresor{TresorPublic}?)
191            } else {
192                return nil
193            }
194
195        }
196
197
198        // Destructor to clean up the tresors dictionary
199        destroy() {
200            destroy self.tresors
201        }
202
203        init() {
204            self.tresors <- {}
205        }
206
207    }
208
209
210
211
212//        // Function to get details of all tresors
213//    pub fun getAllTresorDetails(): [TresorDetails] {
214//        let detailsArray: [TresorDetails] = []
215//        for tresor in self.tresors.values {
216//            detailsArray.append(tresor.getDetails())
217//        }
218//        return detailsArray
219//    }
220
221
222    pub fun createRepository(): @Repository {
223        return <- create Repository()
224    }
225
226
227    init() {
228        self.RepositoryStoragePath = /storage/TitTokenRepository
229        self.RepositoryPublicPath = /public/TitTokenRepository
230
231        self.nextTresorId = 1
232    }
233}
234