Smart Contract

StarlyIDParser

A.5b82f21c0edf76e3.StarlyIDParser

Deployed

19h ago
Feb 28, 2026, 02:30:51 AM UTC

Dependents

0 imports
1access(all) contract StarlyIDParser {
2    access(self) let powersOf10: [UInt32; 8];
3
4    init() {
5        self.powersOf10 = [
6            1,
7            10,
8            100,
9            1000,
10            10000,
11            100000,
12            1000000,
13            10000000
14        ]
15    }
16
17    access(all) struct StarlyID {
18        access(all) let collectionID: String
19        access(all) let cardID: UInt32
20        access(all) let edition: UInt32
21
22        init(
23            collectionID: String,
24            cardID: UInt32,
25            edition: UInt32
26        ) {
27            self.collectionID = collectionID
28            self.cardID = cardID
29            self.edition = edition
30        }
31    }
32
33    access(all) fun parse(starlyID: String): StarlyID {
34        let length = starlyID.length
35        var x = length - 1
36        var collectionEndIndex = 0
37        var editionEndIndex = 0
38        while x > 0 {
39            if starlyID[x] == "/" {
40                if editionEndIndex == 0 {
41                    editionEndIndex = x
42                } else {
43                    collectionEndIndex = x
44                    break
45                }
46            }
47            x = x - 1
48        }
49        let collectionID = starlyID.slice(from: 0, upTo: collectionEndIndex)
50        let cardID = starlyID.slice(from: collectionEndIndex + 1, upTo: editionEndIndex)
51        let edition = starlyID.slice(from: editionEndIndex + 1, upTo: length)
52        return StarlyID(collectionID: collectionID, cardID: self.parseInt(cardID), edition: self.parseInt(edition))
53    }
54
55    access(all) fun parseInt(_ str: String): UInt32 {
56        var number: UInt32 = 0
57        let chars = str.utf8
58        assert(chars.length > 0, message: "Cannot parse ".concat(str))
59        assert(chars.length < 9, message: "Number too big ".concat(str))
60
61        var i = 0
62        for c in chars {
63            let multiplier = self.powersOf10[chars.length - 1 - i]
64            let digit = UInt32(c) - 48
65            if digit < 0 || digit > 9 {
66                panic("Cannot parse ".concat(str))
67            }
68            number = number + digit * multiplier
69            i = i + 1
70        }
71        return number
72    }
73}
74