Skip to content

Commit 2724f47

Browse files
authored
1.2.8 : BAYC ML (#61)
1 parent edba10a commit 2724f47

File tree

14 files changed

+443
-914
lines changed

14 files changed

+443
-914
lines changed

DownloadCryptoPunks/main.swift

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//
77

88
import Foundation
9+
import PromiseKit
10+
import BigInt
911

1012
print("Hello, World!")
1113

@@ -22,31 +24,65 @@ private func makeImageUrl(_ tokenId:UInt) -> URL? {
2224
return URL(string:"https://api.asciipunks.com/punks/\(tokenId)/rendered.png")
2325
}
2426

25-
var collectionName = "AsciiPunks"
27+
func downloadIpfsImage(_ tokenId:UInt) -> Promise<Media.IpfsImage?> {
28+
print("Downloading \(tokenId)")
29+
return baycContract.ethContract.image(BigUInt(tokenId))
30+
}
31+
32+
33+
var collectionName = baycContract.name
2634
// let contractAddressHex = "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb"
27-
let totalSize = 1704 //CryptoPunksCollection.info.totalSupply
35+
let totalSize = 4080 //10000
36+
37+
let from = UserDefaults.standard.integer(forKey:"\(collectionName).startTokenId")
38+
39+
func saveToken(_ tokenId : Int) -> Promise<Void> {
40+
downloadIpfsImage(UInt(tokenId))
41+
.map { image -> Void in
42+
print("Downloaded \(tokenId)")
43+
let filename = getDocumentsDirectory()
44+
.appendingPathComponent("../")
45+
.appendingPathComponent("Github")
46+
.appendingPathComponent("NFTY")
47+
.appendingPathComponent("DownloadCryptoPunks")
48+
.appendingPathComponent("Images")
49+
.appendingPathComponent(collectionName)
50+
.appendingPathComponent("png")
51+
.appendingPathComponent("\(tokenId).png")
52+
image.flatMap { try! $0.data.write(to: filename) }
53+
}
54+
}
55+
56+
var tokenId = 4050 //UserDefaults.standard.integer(forKey: "\(collectionName).startTokenId")
57+
// if (tokenId == 0 || tokenId == totalSize ) { tokenId = 2000 }
58+
var prev : Promise<Int> = Promise.value(tokenId)
59+
print(tokenId)
2860

29-
for tokenId in 1703...2048 {
61+
let parallelCount = 10
62+
while tokenId < totalSize {
63+
64+
// print(tokenId)
3065

31-
let imageUrl = makeImageUrl(UInt(tokenId))
32-
print(imageUrl);
33-
switch(imageUrl) {
34-
case .some(let url):
35-
let data = downloadImageUrl(url:url)
36-
let filename = getDocumentsDirectory()
37-
.appendingPathComponent("../")
38-
.appendingPathComponent("Github")
39-
.appendingPathComponent("NFTY")
40-
.appendingPathComponent("DownloadCryptoPunks")
41-
.appendingPathComponent("Images")
42-
.appendingPathComponent(collectionName)
43-
.appendingPathComponent("png")
44-
.appendingPathComponent("\(tokenId).png")
45-
data.flatMap { try! $0.write(to: filename) }
46-
case .none:
47-
print("Bad URL:\(imageUrl)")
66+
let next = prev.then { tokenId -> Promise<Int> in
67+
print(tokenId)
68+
UserDefaults.standard.set(tokenId, forKey: "\(collectionName).startTokenId")
69+
var promises : [Promise<Void>] = []
70+
var count = 0
71+
while (tokenId + count) < totalSize && count < parallelCount {
72+
print(tokenId,count)
73+
promises.append(saveToken(tokenId + count))
74+
count+=1
75+
}
76+
return when(fulfilled:promises).map { () -> Int in return tokenId + count }
4877
}
78+
79+
tokenId+=parallelCount
80+
prev = next
81+
4982
}
5083

84+
try? hang(prev)
85+
// .done(on: DispatchQueue.main) { print("Done") }
86+
// .catch(on: DispatchQueue.main) { print($0)}
5187

5288

NFTY.xcodeproj/project.pbxproj

Lines changed: 92 additions & 22 deletions
Large diffs are not rendered by default.

NFTY.xcodeproj/xcuserdata/vkohli.xcuserdatad/xcschemes/xcschememanagement.plist

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<key>DownloadCryptoPunks.xcscheme_^#shared#^_</key>
2929
<dict>
3030
<key>orderHint</key>
31-
<integer>2</integer>
31+
<integer>1</integer>
3232
</dict>
3333
<key>MyPlayground (Playground) 1.xcscheme</key>
3434
<dict>
@@ -54,12 +54,12 @@
5454
<key>NFTY.xcscheme_^#shared#^_</key>
5555
<dict>
5656
<key>orderHint</key>
57-
<integer>5</integer>
57+
<integer>3</integer>
5858
</dict>
5959
<key>ParseTokenDistances.xcscheme_^#shared#^_</key>
6060
<dict>
6161
<key>orderHint</key>
62-
<integer>3</integer>
62+
<integer>4</integer>
6363
</dict>
6464
<key>PromiseKit (Playground) 1.xcscheme</key>
6565
<dict>
@@ -106,12 +106,12 @@
106106
<key>RankImages.xcscheme_^#shared#^_</key>
107107
<dict>
108108
<key>orderHint</key>
109-
<integer>4</integer>
109+
<integer>0</integer>
110110
</dict>
111111
<key>RarityRanking.xcscheme_^#shared#^_</key>
112112
<dict>
113113
<key>orderHint</key>
114-
<integer>1</integer>
114+
<integer>2</integer>
115115
</dict>
116116
<key>SimpleStorage (Playground) 1.xcscheme</key>
117117
<dict>
@@ -158,7 +158,7 @@
158158
<key>TestUI.xcscheme_^#shared#^_</key>
159159
<dict>
160160
<key>orderHint</key>
161-
<integer>0</integer>
161+
<integer>5</integer>
162162
</dict>
163163
</dict>
164164
</dict>

NFTY/Model/CollectionContracts/BAYC.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class BAYC_Contract : ContractInterface {
2121

2222
private var pricesCache : [UInt : ObservablePromise<NFTPriceStatus>] = [:]
2323

24-
private var name = "BoredApeYachtClub"
24+
let name = "BoredApeYachtClub"
2525

2626
let contractAddressHex = "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D"
2727

@@ -45,9 +45,21 @@ class BAYC_Contract : ContractInterface {
4545
URLSession.shared.dataTask(with: request,completionHandler:{ data, response, error -> Void in
4646
// print(data,response,error)
4747
do {
48-
seal.fulfill(try JSONDecoder().decode(TokenUriData.self, from: data!))
48+
switch(data) {
49+
case .some(let data):
50+
if (data.isEmpty) {
51+
print(data,response,error)
52+
seal.reject(NSError(domain:"", code:404, userInfo:nil))
53+
} else {
54+
seal.fulfill(try JSONDecoder().decode(TokenUriData.self, from: data))
55+
}
56+
case .none:
57+
print(data,response,error)
58+
seal.reject(error ?? NSError(domain:"", code:404, userInfo:nil))
59+
}
4960
} catch {
50-
seal.reject("JSON Serialization error:\(error), json=\(data.map { String(decoding: $0, as: UTF8.self) } ?? "")" as! Error)
61+
print(data,response,error)
62+
seal.reject(error)
5163
}
5264
}).resume()
5365
}
@@ -69,7 +81,7 @@ class BAYC_Contract : ContractInterface {
6981
}
7082
}
7183

72-
private var ethContract = IpfsImageEthContract(address:"0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D")
84+
var ethContract = IpfsImageEthContract(address:"0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D")
7385

7486
private func download(_ tokenId:BigUInt) -> ObservablePromise<Media.IpfsImage?> {
7587
switch(try? imageCache.object(forKey:tokenId)) {

NFTY/Model/NFT.swift

Lines changed: 4 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ struct CollectionInfo {
198198
let similarTokens : SimilarTokensGetter?
199199
let rarityRank : RarityRankGetter
200200
}
201-
202201
struct CollectionData : HasContractInterface {
203202
let recentTrades: NftRecentTradesObject
204203
let contract: ContractInterface
@@ -264,195 +263,12 @@ let CryptoPunks_rarityRanks : [UInt] = load("CryptoPunks_rarityRanks.json")
264263
let AsciiPunks_nearestTokens : [[UInt]] = load("AsciiPunks_nearestTokens.json")
265264
let AsciiPunks_rarityRanks : [UInt] = load("AsciiPunks_rarityRanks.json")
266265

266+
let BAYC_nearestTokens : [[UInt]] = load("BoredApeYachtClub_nearestTokens.json")
267+
let BAYC_rarityRanks : [UInt] = load("BoredApeYachtClub_rarityRanks.json")
268+
269+
267270
let cryptoPunksContract = CryptoPunksContract();
268271
let cryptoKittiesContract = CryptoKittiesAuction();
269272
let asciiPunksContract = AsciiPunksContract();
270273
let autoGlyphsContract = AutoglyphsContract()
271274
let baycContract = BAYC_Contract()
272-
273-
let CompositeCollection = CompositeRecentTradesObject([
274-
CompositeRecentTradesObject.CollectionInitializer(
275-
info:CollectionInfo(
276-
address:cryptoPunksContract.contractAddressHex,
277-
url1:SAMPLE_PUNKS[0],
278-
url2:SAMPLE_PUNKS[1],
279-
url3:SAMPLE_PUNKS[2],
280-
url4:SAMPLE_PUNKS[3],
281-
name:"CryptoPunks",
282-
webLink: URL(string:"https://www.larvalabs.com/cryptopunks")!,
283-
themeColor:Color.yellow,
284-
themeLabelColor:Color.systemBackground,
285-
subThemeColor: /* FFB61E */ Color(red: 255/255, green: 182/255, blue: 30/255),
286-
collectionColor:Color.yellow,
287-
disableRecentTrades:false,
288-
blur:0,
289-
samplePadding:10,
290-
similarTokens : SimilarTokensGetter(label:"Punks") { tokenId in CryptoPunks_nearestTokens[safe:Int(tokenId)] },
291-
rarityRank : { tokenId in CryptoPunks_rarityRanks[safe:Int(tokenId)] }),
292-
contract:cryptoPunksContract),
293-
CompositeRecentTradesObject.CollectionInitializer(
294-
info:CollectionInfo(
295-
address:autoGlyphsContract.contractAddressHex,
296-
url1:SAMPLE_AUTOGLYPHS[0],
297-
url2:SAMPLE_AUTOGLYPHS[1],
298-
url3:SAMPLE_AUTOGLYPHS[2],
299-
url4:SAMPLE_AUTOGLYPHS[3],
300-
name:"Autoglyphs",
301-
webLink: URL(string:"https://www.larvalabs.com/autoglyphs")!,
302-
themeColor:Color.label,
303-
themeLabelColor:Color.gray,
304-
subThemeColor:Color.label,
305-
collectionColor:Color.white,
306-
disableRecentTrades:false,
307-
blur:0,
308-
samplePadding:10,
309-
similarTokens: nil,
310-
rarityRank : { tokenId in nil }),
311-
contract:autoGlyphsContract),
312-
CompositeRecentTradesObject.CollectionInitializer(
313-
info:CollectionInfo(
314-
address:asciiPunksContract.contractAddressHex,
315-
url1:SAMPLE_ASCII_PUNKS[0],
316-
url2:SAMPLE_ASCII_PUNKS[1],
317-
url3:SAMPLE_ASCII_PUNKS[2],
318-
url4:SAMPLE_ASCII_PUNKS[3],
319-
name:"AsciiPunks",
320-
webLink: URL(string:"https://asciipunks.com")!,
321-
themeColor:Color.label,
322-
themeLabelColor:Color.systemBackground,
323-
subThemeColor:Color.label,
324-
collectionColor:Color.black,
325-
disableRecentTrades:false,
326-
blur:0,
327-
samplePadding:10,
328-
similarTokens : SimilarTokensGetter(label:"Punks") { tokenId in AsciiPunks_nearestTokens[safe:Int(tokenId)] },
329-
rarityRank : { tokenId in AsciiPunks_rarityRanks[safe:Int(tokenId)] }),
330-
contract:asciiPunksContract),
331-
CompositeRecentTradesObject.CollectionInitializer(
332-
info:CollectionInfo(
333-
address:baycContract.contractAddressHex,
334-
url1:SAMPLE_BAYC[0],
335-
url2:SAMPLE_BAYC[1],
336-
url3:SAMPLE_BAYC[2],
337-
url4:SAMPLE_BAYC[3],
338-
name:"BoredApeYachtClub",
339-
webLink: URL(string:"https://boredapeyachtclub.com/#/")!,
340-
themeColor:Color.black,
341-
themeLabelColor:Color.white,
342-
subThemeColor:Color.white,
343-
collectionColor:Color.black,
344-
disableRecentTrades:false,
345-
blur:0,
346-
samplePadding:15,
347-
similarTokens: nil,
348-
rarityRank : { tokenId in nil }),
349-
contract:baycContract),
350-
CompositeRecentTradesObject.CollectionInitializer(
351-
info:CollectionInfo(
352-
address:cryptoKittiesContract.contractAddressHex,
353-
url1:SAMPLE_KITTIES[0],
354-
url2:SAMPLE_KITTIES[1],
355-
url3:SAMPLE_KITTIES[2],
356-
url4:SAMPLE_KITTIES[3],
357-
name:"CryptoKitties",
358-
webLink: URL(string:"https://www.cryptokitties.co")!,
359-
themeColor: /* 78e08f */ Color(red: 120/255, green: 224/255, blue: 143/255),
360-
themeLabelColor:Color.systemBackground,
361-
subThemeColor: /* 78e08f */ Color(red: 120/255, green: 224/255, blue: 143/255),
362-
collectionColor:/* 78e08f */ Color(red: 120/255, green: 224/255, blue: 143/255),
363-
disableRecentTrades:true,
364-
blur:0,samplePadding:0,
365-
similarTokens: nil,
366-
rarityRank : { tokenId in nil }),
367-
contract:cryptoKittiesContract),
368-
]
369-
)
370-
371-
let SampleToken = NFT(
372-
address: "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb",
373-
tokenId: 340, name: "CryptoPunks",
374-
media: .image(MediaImageEager(URL(string:"https://www.larvalabs.com/public/images/cryptopunks/punk0385.png")!)))
375-
376-
let SampleCollection = CompositeCollection.collections[0]
377-
378-
379-
public extension Color {
380-
static let lightText = Color(UIColor.lightText)
381-
static let darkText = Color(UIColor.darkText)
382-
383-
static let label = Color(UIColor.label)
384-
static let secondaryLabel = Color(UIColor.secondaryLabel)
385-
static let tertiaryLabel = Color(UIColor.tertiaryLabel)
386-
static let quaternaryLabel = Color(UIColor.quaternaryLabel)
387-
388-
static let systemBackground = Color(UIColor.systemBackground)
389-
static let secondarySystemBackground = Color(UIColor.secondarySystemBackground)
390-
static let tertiarySystemBackground = Color(UIColor.tertiarySystemBackground)
391-
392-
// There are more..
393-
}
394-
395-
let COLLECTIONS : [Collection] = CompositeCollection.collections
396-
397-
struct CollectionsFactory {
398-
399-
let collections : [String : Collection] = Dictionary(uniqueKeysWithValues: COLLECTIONS.map{ ($0.info.address,$0) })
400-
401-
func getByAddress(_ address:String) -> Collection? {
402-
return collections[address]
403-
}
404-
405-
}
406-
407-
let collectionsFactory = CollectionsFactory()
408-
409-
extension Array {
410-
subscript (safe index: Int) -> Element? {
411-
return indices ~= index ? self[index] : nil
412-
}
413-
}
414-
415-
extension String {
416-
/*
417-
Truncates the string to the specified length number of characters and appends an optional trailing string if longer.
418-
- Parameter length: Desired maximum lengths of a string
419-
- Parameter trailing: A 'String' that will be appended after the truncation.
420-
421-
- Returns: 'String' object.
422-
*/
423-
func trunc(length: Int, trailing: String = "") -> String {
424-
return (self.count > length) ? self.prefix(length) + trailing : self
425-
}
426-
427-
func deletingPrefix(_ prefix: String) -> String {
428-
guard self.hasPrefix(prefix) else { return self }
429-
return String(self.dropFirst(prefix.count))
430-
}
431-
}
432-
433-
extension URL {
434-
func params() -> [String:Any] {
435-
var dict = [String:Any]()
436-
437-
if let components = URLComponents(url: self, resolvingAgainstBaseURL: false) {
438-
if let queryItems = components.queryItems {
439-
for item in queryItems {
440-
dict[item.name] = item.value!
441-
}
442-
}
443-
return dict
444-
} else {
445-
return [:]
446-
}
447-
}
448-
}
449-
450-
let SAMPLE_WALLET_ADDRESS = try! EthereumAddress(
451-
hex: "0x208b82b04449cd51803fae4b1561450ba13d9510",
452-
eip55:false)
453-
454-
enum CloudDefaultStorageKeys : String {
455-
case walletAddress = "walletAddress"
456-
case favoritesDict = "favoritesDict"
457-
case friendsDict = "friendsDict"
458-
}

0 commit comments

Comments
 (0)