Smart Contract
MikoSeaUtility
A.0b80e42aaab305f0.MikoSeaUtility
1import MIKOSEANFTV2 from 0x0b80e42aaab305f0
2import MIKOSEANFT from 0x0b80e42aaab305f0
3import MikoSeaMarket from 0x0b80e42aaab305f0
4import MikoSeaNFTMetadata from 0x0b80e42aaab305f0
5
6access(all) contract MikoSeaUtility {
7 // rate transform from yen to usd, ex: {"USD_TO_JPY": 171.2}
8 access(all) var ratePrice: {String:UFix64}
9 access(self) var metadata: {String:String}
10
11 access(all) let AdminStoragePath: StoragePath
12 access(all) let CollectionStoragePath: StoragePath
13 access(all) let CollectionPublicPath: PublicPath
14
15 // is not in used
16 access(all) struct NFTDataCommon { }
17 access(all) struct NFTDataWithListing { }
18
19 access(all) struct NFTDataCommonWithListing {
20 access(all) let id: UInt64
21 access(all) let serialNumber: UInt64
22 access(all) let image: String
23 access(all) let name: String
24 access(all) let nftMetadata: {String:String}
25 access(all) let nftType: String
26
27 access(all) let projectId: UInt64
28 access(all) let projectTitle: String
29 access(all) let projectDescription: String
30 access(all) let flowProjectId: UInt64
31 access(all) let projectMaxSupply: UInt64
32 access(all) let isNFTReveal: Bool
33
34 access(all) let blockHeight: UInt64
35 access(all) let holder: Address
36 access(all) let isInMarket: Bool
37 access(all) let listingId: UInt64?
38
39 init(id: UInt64,
40 serialNumber: UInt64,
41 name: String,
42 image: String,
43 nftMetadata: {String:String},
44 projectId: UInt64,
45 isNFTReveal: Bool,
46 projectTitle: String,
47 projectDescription: String,
48 maxSupply: UInt64,
49 blockHeight: UInt64,
50 holder: Address,
51 listingId: UInt64?,
52 nftType: String
53 ){
54 self.id = id
55 self.serialNumber = serialNumber
56 self.projectId = projectId
57 self.flowProjectId = projectId
58 self.image = image
59 self.isNFTReveal = isNFTReveal
60 self.projectTitle = projectTitle
61 self.projectDescription = projectDescription
62 self.name = name
63 self.nftMetadata = nftMetadata
64 self.blockHeight = blockHeight
65 self.projectMaxSupply = maxSupply
66 self.holder = holder
67 self.isInMarket = listingId != nil
68 self.listingId = listingId
69 self.nftType = nftType
70 }
71 }
72
73 access(all) fun yenToDollar(yen: UFix64): UFix64 {
74 if MikoSeaUtility.ratePrice["USD_TO_JPY"] == nil {
75 return 0.0
76 }
77 if MikoSeaUtility.ratePrice["USD_TO_JPY"]! <= 0.0 {
78 return 0.0
79 }
80 return yen / MikoSeaUtility.ratePrice["USD_TO_JPY"]!
81 }
82
83 access(all) resource Admin {
84 access(all) fun updateRate(key: String, value: UFix64) {
85 MikoSeaUtility.ratePrice[key] = value
86 }
87 }
88
89 access(all) fun floor(_ num: Fix64): Int {
90 var strRes = ""
91 var numStr = num.toString()
92 var i = 0;
93 while i < numStr.length {
94 if numStr[i] == "." {
95 break;
96 }
97 strRes = strRes.concat(numStr.slice(from: i, upTo: i + 1))
98 i = i + 1
99 }
100 let numInt = Int.fromString(strRes) ?? 0
101 if Fix64(numInt) == num {
102 return numInt
103 }
104 if num >= 0.0 {
105 return numInt
106 }
107 return numInt - 1
108 }
109
110 access(all) fun getListingId(addr: Address, nftType: Type, nftID: UInt64): UInt64? {
111 if let ref = getAccount(addr).capabilities.get<&{MikoSeaMarket.StorefrontPublic}>(MikoSeaMarket.MarketPublicPath).borrow() {
112 for order in ref.getOrders() {
113 if nftID == order.nftID && order.nftType == nftType && order.status != "done" {
114 return order.getId()
115 }
116 }
117 }
118 return nil
119 }
120
121 access(all) fun getNftV2Detail(_ nftID: UInt64): NFTDataCommonWithListing? {
122 if let addr = MIKOSEANFTV2.getHolder(nftID: nftID) {
123 let account = getAccount(addr)
124 let collectioncap = account.capabilities.get<&{MIKOSEANFTV2.CollectionPublic}>(MIKOSEANFTV2.CollectionPublicPath)
125 if let collectionRef = collectioncap.borrow() {
126 if let nft = collectionRef.borrowMIKOSEANFTV2(id: nftID) {
127 let project = MIKOSEANFTV2.getProjectById(nft.nftData.projectId)!
128 return NFTDataCommonWithListing(
129 id: nft.id,
130 serialNumber: nft.nftData.serialNumber,
131 name: nft.getMetadata()["name"] ?? "",
132 image: nft.getImage(),
133 nftMetadata: nft.getMetadata(),
134 projectId: project.projectId,
135 isNFTReveal: project.isReveal,
136 projectTitle: nft.getTitle(),
137 projectDescription: nft.getDescription(),
138 maxSupply: project.maxSupply,
139 blockHeight: nft.nftData.blockHeight,
140 holder: addr,
141 listingId: MikoSeaUtility.getListingId(addr: addr, nftType: Type<@MIKOSEANFTV2.NFT>(), nftID: nft.id),
142 nftType: "mikoseav2"
143 )
144 }
145 }
146 }
147 return nil
148 }
149
150 access(all) fun getNftV2DetailByWallet(nftID: UInt64, addr: Address): NFTDataCommonWithListing? {
151 let account = getAccount(addr)
152 let collectioncap = account.capabilities.get<&{MIKOSEANFTV2.CollectionPublic}>(MIKOSEANFTV2.CollectionPublicPath)
153 if let collectionRef = collectioncap.borrow() {
154 if let nft = collectionRef.borrowMIKOSEANFTV2(id: nftID) {
155 let project = MIKOSEANFTV2.getProjectById(nft.nftData.projectId)!
156 return NFTDataCommonWithListing(
157 id: nft.id,
158 serialNumber: nft.nftData.serialNumber,
159 name: nft.getMetadata()["name"] ?? "",
160 image: nft.getImage(),
161 nftMetadata: nft.getMetadata(),
162 projectId: project.projectId,
163 isNFTReveal: project.isReveal,
164 projectTitle: nft.getTitle(),
165 projectDescription: nft.getDescription(),
166 maxSupply: project.maxSupply,
167 blockHeight: nft.nftData.blockHeight,
168 holder: addr,
169 listingId: MikoSeaUtility.getListingId(addr: addr, nftType: Type<@MIKOSEANFTV2.NFT>(), nftID: nft.id),
170 nftType: "mikoseav2"
171 )
172 }
173 }
174 return nil
175 }
176
177 access(all) fun getNftV1Detail(addr: Address, nftID: UInt64): NFTDataCommonWithListing? {
178 let account = getAccount(addr)
179 let collectionCapability = account.capabilities.get<&{MIKOSEANFT.MikoSeaCollectionPublic}>(MIKOSEANFT.CollectionPublicPath)
180 let collectionRef = collectionCapability.borrow()
181 if let collectionRef = collectionCapability.borrow() {
182 if let nft = collectionRef.borrowMiKoSeaNFT(id: nftID) {
183 let listingId= MikoSeaUtility.getListingId(addr: addr, nftType: Type<@MIKOSEANFTV2.NFT>(), nftID: nft.id)
184 return NFTDataCommonWithListing(
185 id: nft.id,
186 serialNumber: nft.data.mintNumber,
187 name: nft.getTitle(),
188 image: nft.getImage(),
189 nftMetadata: MikoSeaNFTMetadata.getNFTMetadata(nftType: "mikosea", nftID: nft.id) ?? {},
190 projectId: nft.data.projectId,
191 isNFTReveal: true,
192 projectTitle: nft.getTitle(),
193 projectDescription: nft.getDescription(),
194 maxSupply: MIKOSEANFT.getProjectTotalSupply(nft.data.projectId),
195 blockHeight: 0,
196 holder: addr,
197 listingId: MikoSeaUtility.getListingId(addr: addr, nftType: Type<@MIKOSEANFT.NFT>(), nftID: nft.id),
198 nftType: "mikosea"
199 )
200 }
201 }
202 return nil
203 }
204
205 access(all) fun parseNftV2List(_ nfts: [&MIKOSEANFTV2.NFT]): [NFTDataCommonWithListing] {
206 let projects: {UInt64: &MIKOSEANFTV2.ProjectData} = {}
207 let response: [NFTDataCommonWithListing] = []
208 for nft in nfts {
209 if projects[nft.nftData.projectId] == nil {
210 projects[nft.nftData.projectId] = MIKOSEANFTV2.getProjectById(nft.nftData.projectId)!
211 }
212 let project = projects[nft.nftData.projectId]!
213 if let addr = MIKOSEANFTV2.getHolder(nftID: nft.id) {
214 let listingId= MikoSeaUtility.getListingId(addr: addr, nftType: Type<@MIKOSEANFTV2.NFT>(), nftID: nft.id)
215 response.append(
216 NFTDataCommonWithListing(
217 id: nft.id,
218 serialNumber: nft.nftData.serialNumber,
219 name: nft.getMetadata()["name"] ?? "",
220 image: nft.getImage(),
221 nftMetadata: nft.getMetadata(),
222 projectId: project.projectId,
223 isNFTReveal: project.isReveal,
224 projectTitle: nft.getTitle(),
225 projectDescription: nft.getDescription(),
226 maxSupply: project.maxSupply,
227 blockHeight: nft.nftData.blockHeight,
228 holder: addr,
229 listingId: MikoSeaUtility.getListingId(addr: addr, nftType: Type<@MIKOSEANFTV2.NFT>(), nftID: nft.id),
230 nftType: "mikoseav2"
231 )
232 )
233 }
234 }
235 return response
236 }
237
238 init() {
239 let isMainnetAccount = self.account.address == Address(0x0b80e42aaab305f0)
240 if isMainnetAccount {
241 self.AdminStoragePath = /storage/MikoSeaUtilityAdminStoragePath
242 self.CollectionStoragePath = /storage/MikoSeaUtilityCollectionStoragePath
243 self.CollectionPublicPath = /public/MikoSeaUtilityCollectionPublicPath
244 } else {
245 self.AdminStoragePath = /storage/TestMikoSeaUtilityAdminStoragePath
246 self.CollectionStoragePath = /storage/TestMikoSeaUtilityCollectionStoragePath
247 self.CollectionPublicPath = /public/TestMikoSeaUtilityCollectionPublicPath
248 }
249
250 self.ratePrice = {
251 "USD_TO_JPY": 130.75
252 }
253 self.metadata = { }
254
255 // Put the Admin in storage
256 self.account.storage.save(<- create Admin(), to: self.AdminStoragePath)
257 }
258}
259