Smart Contract
FTRegistry
A.097bafa4e0b48eef.FTRegistry
1access(all) contract FTRegistry {
2
3 /* Event */
4 access(all) event ContractInitialized()
5 access(all) event FTInfoRegistered(alias: String, typeIdentifier: String)
6 access(all) event FTInfoRemoved(alias: String, typeIdentifier: String)
7
8 /* Variables */
9 // Mapping of {Type Identifier : FT Info Struct}
10 access(contract) var fungibleTokenList : {String : FTInfo}
11
12 access(contract) var aliasMap : {String : String}
13
14 /* Struct */
15 access(all) struct FTInfo {
16 access(all) let alias : String
17 access(all) let type : Type
18 access(all) let typeIdentifier : String
19 // Whether it is stable coin or other type of coins.
20 access(all) let tag : [String ]
21 access(all) let icon : String?
22 access(all) let receiverPath : PublicPath
23 access(all) let receiverPathIdentifier : String
24 access(all) let balancePath : PublicPath
25 access(all) let balancePathIdentifier : String
26 access(all) let vaultPath : StoragePath
27 access(all) let vaultPathIdentifier : String
28
29 init(alias : String, type: Type, typeIdentifier: String, tag:[String], icon: String?, receiverPath: PublicPath, balancePath: PublicPath, vaultPath: StoragePath) {
30 self.alias = alias
31 self.type = type
32 self.typeIdentifier = typeIdentifier
33 self.tag = tag
34 self.icon = icon
35 self.receiverPath = receiverPath
36 self.receiverPathIdentifier = receiverPath.toString().slice(from: "/public/".length, upTo: receiverPath.toString().length)
37 self.balancePath = balancePath
38 self.balancePathIdentifier = balancePath.toString().slice(from: "/public/".length, upTo: balancePath.toString().length)
39 self.vaultPath = vaultPath
40 self.vaultPathIdentifier = vaultPath.toString().slice(from: "/storage/".length, upTo: vaultPath.toString().length)
41 }
42
43 }
44
45 /* getters */
46 access(all) fun getFTInfoByTypeIdentifier(_ typeIdentifier: String) : FTInfo? {
47 return FTRegistry.fungibleTokenList[typeIdentifier]
48 }
49
50 access(all) fun getFTInfoByAlias(_ alias: String) : FTInfo? {
51 if let identifier = FTRegistry.aliasMap[alias] {
52 return FTRegistry.fungibleTokenList[identifier]
53 }
54 return nil
55 }
56
57 access(all) fun getFTInfo(_ input: String) : FTInfo? {
58 if let info = self.getFTInfoByAlias(input) {
59 return info
60 }
61 if let info = self.getFTInfoByTypeIdentifier(input) {
62 return info
63 }
64 return nil
65 }
66
67 access(all) fun getTypeIdentifier(_ alias: String) : String? {
68 return FTRegistry.aliasMap[alias]
69 }
70
71 access(all) fun getFTInfoAll() : {String : FTInfo} {
72 return FTRegistry.fungibleTokenList
73 }
74
75 access(all) fun getSupportedFTAlias() : [String] {
76 return FTRegistry.aliasMap.keys
77 }
78
79 access(all) fun getSupportedFTTypeIdentifier() : [String] {
80 return FTRegistry.aliasMap.values
81 }
82
83 /* setters */
84 access(account) fun setFTInfo(alias: String, type: Type, tag: [String], icon: String?, receiverPath: PublicPath, balancePath: PublicPath, vaultPath: StoragePath) {
85 if FTRegistry.fungibleTokenList.containsKey(type.identifier) {
86 panic("This FungibleToken Register already exist. Type Identifier : ".concat(type.identifier))
87 }
88 let typeIdentifier : String = type.identifier
89 FTRegistry.fungibleTokenList[typeIdentifier] = FTInfo(alias: alias,
90 type: type,
91 typeIdentifier: typeIdentifier,
92 tag : tag,
93 icon: icon,
94 receiverPath: receiverPath,
95 balancePath: balancePath,
96 vaultPath: vaultPath)
97
98 FTRegistry.aliasMap[alias] = typeIdentifier
99 emit FTInfoRegistered(alias: alias, typeIdentifier: typeIdentifier)
100 }
101
102 access(account) fun removeFTInfoByTypeIdentifier(_ typeIdentifier: String) : FTInfo {
103 let info = FTRegistry.fungibleTokenList.remove(key: typeIdentifier) ?? panic("Cannot find this Fungible Token Registry. Type : ".concat(typeIdentifier))
104 FTRegistry.aliasMap.remove(key: info.alias)
105 emit FTInfoRemoved(alias:info.alias, typeIdentifier: info.typeIdentifier)
106 return info
107 }
108
109 access(account) fun removeFTInfoByAlias(_ alias: String) : FTInfo {
110 let typeIdentifier = self.getTypeIdentifier(alias) ?? panic("Cannot find type identifier from this alias. Alias : ".concat(alias))
111 return self.removeFTInfoByTypeIdentifier(typeIdentifier)
112 }
113
114 init() {
115 self.fungibleTokenList = {}
116 self.aliasMap = {}
117 }
118}
119