Smart Contract

MotoGPCardSerialPoolV2

A.a49cc0ee46c54bfb.MotoGPCardSerialPoolV2

Deployed

1d ago
Feb 26, 2026, 09:43:52 PM UTC

Dependents

0 imports
1import ContractVersion from 0xa49cc0ee46c54bfb
2import MotoGPAdmin from 0xa49cc0ee46c54bfb
3
4pub contract MotoGPCardSerialPoolV2: ContractVersion {
5
6    pub fun getVersion(): String {
7        return "1.0.2"
8    }
9
10    // Should be used only to set a serial base not equal to 0
11    //
12    pub fun setSerialBase(adminRef: &MotoGPAdmin.Admin, cardID: UInt64, base: UInt64) {
13        if self.serialBaseByCardID[cardID] != nil {
14            assert(base > self.serialBaseByCardID[cardID]!, message: "new base is less than current base")
15        }
16        self.serialBaseByCardID[cardID] = base
17    }
18
19    // Method to add sequential serials for a card id
20    // Can be called multiple times
21    // Will generate serial starting from the base for that cardID
22    //
23    pub fun addSerials(adminRef: &MotoGPAdmin.Admin, cardID: UInt64, count: UInt64) {
24        if self.serialBaseByCardID[cardID] == nil {
25            self.serialBaseByCardID[cardID] = 0
26        }
27
28        var index: UInt64 = 0
29        if self.serialsByCardID[cardID] == nil {
30            self.serialsByCardID[cardID] = [];
31        }
32
33        while (index < count) {
34            index = index + UInt64(1)
35            self.serialsByCardID[cardID]!.append(index + self.serialBaseByCardID[cardID]!)
36        }
37        
38        self.serialBaseByCardID[cardID] =  index + self.serialBaseByCardID[cardID]!
39    }
40
41    // Method to pick a serial for a cardID
42    // Randomness for n should be generated before calling this method
43    //
44    access(account) fun pickSerial(n: UInt64, cardID: UInt64): UInt64 {
45       
46        pre {
47            self.serialsByCardID[cardID]!.length != 0 : "No serials for cardID ".concat(cardID.toString())
48        }
49        
50        let r = n % UInt64(self.serialsByCardID[cardID]!.length)
51        return self.serialsByCardID[cardID]!.remove(at: r)
52    }
53
54    pub fun getSerialBaseByCardID(cardID: UInt64): UInt64 {
55        return self.serialBaseByCardID[cardID] ?? 0    
56    }
57
58    pub fun getAllSerialsByCardID(cardID: UInt64): [UInt64] {
59        return self.serialsByCardID[cardID] ?? []
60    }
61
62    access(contract) let serialsByCardID : {UInt64 : [UInt64]}
63    access(contract) let serialBaseByCardID : {UInt64 : UInt64}
64
65    init() {
66        self.serialsByCardID = {}
67        self.serialBaseByCardID = {}
68    }
69}