|
| 1 | +access(all) contract TopShotIPFSResolver { |
| 2 | + |
| 3 | + access(all) event CIDSet(setID: UInt32, playID: UInt32, subeditionID: UInt32, mediaType: String, cid: String) |
| 4 | + access(all) event GatewayUpdated(gateway: String) |
| 5 | + |
| 6 | + access(all) let numBuckets: UInt64 |
| 7 | + |
| 8 | + // Each shard is a resource stored at its own path so writes |
| 9 | + // borrow a reference and mutate in place — no dict copy. |
| 10 | + access(all) resource Shard { |
| 11 | + access(all) let entries: {String: {String: String}} |
| 12 | + |
| 13 | + init() { |
| 14 | + self.entries = {} |
| 15 | + } |
| 16 | + |
| 17 | + access(contract) fun setCID(key: String, mediaType: String, cid: String) { |
| 18 | + var inner = self.entries[key] ?? {} |
| 19 | + inner[mediaType] = cid |
| 20 | + self.entries[key] = inner |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + // IPFS gateway base URL (e.g., "https://ipfs.dapperlabs.com/ipfs/") |
| 25 | + access(all) var gateway: String |
| 26 | + |
| 27 | + access(all) let AdminStoragePath: StoragePath |
| 28 | + |
| 29 | + access(all) resource Admin { |
| 30 | + access(all) fun setCID(setID: UInt32, playID: UInt32, subeditionID: UInt32, mediaType: String, cid: String) { |
| 31 | + let key = TopShotIPFSResolver.buildKey(setID: setID, playID: playID, subeditionID: subeditionID) |
| 32 | + let bucket = TopShotIPFSResolver.getBucket(setID: setID, playID: playID, subeditionID: subeditionID) |
| 33 | + let shard = TopShotIPFSResolver.borrowShard(bucket) |
| 34 | + shard.setCID(key: key, mediaType: mediaType, cid: cid) |
| 35 | + emit CIDSet(setID: setID, playID: playID, subeditionID: subeditionID, mediaType: mediaType, cid: cid) |
| 36 | + } |
| 37 | + |
| 38 | + access(all) fun setGateway(gateway: String) { |
| 39 | + TopShotIPFSResolver.gateway = gateway |
| 40 | + emit GatewayUpdated(gateway: gateway) |
| 41 | + } |
| 42 | + |
| 43 | + access(all) fun clearAllCIDs() { |
| 44 | + var i: UInt64 = 0 |
| 45 | + while i < TopShotIPFSResolver.numBuckets { |
| 46 | + let path = TopShotIPFSResolver.shardPath(i) |
| 47 | + if let existing <- TopShotIPFSResolver.account.storage.load<@Shard>(from: path) { |
| 48 | + destroy existing |
| 49 | + } |
| 50 | + i = i + 1 |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + access(all) view fun getCIDs(setID: UInt32, playID: UInt32, subeditionID: UInt32): {String: String}? { |
| 56 | + let key = self.buildKey(setID: setID, playID: playID, subeditionID: subeditionID) |
| 57 | + let bucket = self.getBucket(setID: setID, playID: playID, subeditionID: subeditionID) |
| 58 | + let path = self.shardPath(bucket) |
| 59 | + if let shard = self.account.storage.borrow<&Shard>(from: path) { |
| 60 | + if let entry = shard.entries[key] { |
| 61 | + // Return a copy, not a reference |
| 62 | + let result: {String: String} = {} |
| 63 | + for mediaType in entry.keys { |
| 64 | + result[mediaType] = entry[mediaType] |
| 65 | + } |
| 66 | + return result |
| 67 | + } |
| 68 | + } |
| 69 | + return nil |
| 70 | + } |
| 71 | + |
| 72 | + access(contract) fun borrowShard(_ bucket: UInt64): &Shard { |
| 73 | + let path = self.shardPath(bucket) |
| 74 | + if let shard = self.account.storage.borrow<&Shard>(from: path) { |
| 75 | + return shard |
| 76 | + } |
| 77 | + self.account.storage.save(<- create Shard(), to: path) |
| 78 | + return self.account.storage.borrow<&Shard>(from: path)! |
| 79 | + } |
| 80 | + |
| 81 | + access(all) view fun shardPath(_ bucket: UInt64): StoragePath { |
| 82 | + return StoragePath(identifier: "TopShotIPFSShard_".concat(bucket.toString()))! |
| 83 | + } |
| 84 | + |
| 85 | + access(all) view fun getBucket(setID: UInt32, playID: UInt32, subeditionID: UInt32): UInt64 { |
| 86 | + return (UInt64(setID) + UInt64(playID) + UInt64(subeditionID)) % self.numBuckets |
| 87 | + } |
| 88 | + |
| 89 | + access(all) view fun buildKey(setID: UInt32, playID: UInt32, subeditionID: UInt32): String { |
| 90 | + return setID.toString() |
| 91 | + .concat("_") |
| 92 | + .concat(playID.toString()) |
| 93 | + .concat("_") |
| 94 | + .concat(subeditionID.toString()) |
| 95 | + } |
| 96 | + |
| 97 | + init() { |
| 98 | + self.numBuckets = 31 |
| 99 | + self.gateway = "https://ipfs.dapperlabs.com/ipfs/" |
| 100 | + self.AdminStoragePath = /storage/TopShotIPFSResolverAdmin |
| 101 | + self.account.storage.save<@Admin>(<- create Admin(), to: self.AdminStoragePath) |
| 102 | + } |
| 103 | +} |
0 commit comments