getters.gno
6.05 Kb ยท 216 lines
1package tokenhub
2
3import (
4 "std"
5 "strings"
6
7 "gno.land/p/demo/grc/grc1155"
8 "gno.land/p/demo/grc/grc20"
9 "gno.land/p/demo/grc/grc721"
10 "gno.land/p/demo/ufmt"
11 "gno.land/r/demo/grc20reg"
12 "gno.land/r/sys/users"
13)
14
15// GetUserTokenBalances returns a string of all the grc20 tokens the user owns
16func GetUserTokenBalances(userNameOrAddress string) string {
17 return getTokenBalances(userNameOrAddress, false)
18}
19
20// GetUserTokenBalancesNonZero returns a string of all the grc20 tokens the user owns, but only the ones that have a balance greater than 0
21func GetUserTokenBalancesNonZero(userNameOrAddress string) string {
22 return getTokenBalances(userNameOrAddress, true)
23}
24
25// GetUserNFTBalances returns a string of all the NFTs the user owns
26func GetUserNFTBalances(userNameOrAddress string) string {
27 return getNFTBalances(userNameOrAddress)
28}
29
30// GetUserMultiTokenBalances returns a string of all the multi-tokens the user owns
31func GetUserMultiTokenBalances(userNameOrAddress string) string {
32 return getMultiTokenBalances(userNameOrAddress, false)
33}
34
35// GetUserMultiTokenBalancesNonZero returns a string of all the multi-tokens the user owns, but only the ones that have a balance greater than 0
36func GetUserMultiTokenBalancesNonZero(userNameOrAddress string) string {
37 return getMultiTokenBalances(userNameOrAddress, true)
38}
39
40// GetToken returns a token instance for a given key
41func GetToken(key string) *grc20.Token {
42 getter := grc20reg.Get(key)
43 return getter()
44}
45
46// MustGetToken returns a token instance for a given key, panics if the token is not found
47func MustGetToken(key string) *grc20.Token {
48 getter := grc20reg.MustGet(key)
49 if getter == nil {
50 panic("unknown token: " + key)
51 }
52 return getter()
53}
54
55// GetNFT returns an NFT instance for a given key
56func GetNFT(key string) grc721.IGRC721 {
57 nftGetter, ok := registeredNFTs.Get(key)
58 if !ok {
59 return nil
60 }
61 return (nftGetter.(grc721.NFTGetter))()
62}
63
64// MustGetNFT returns an NFT instance for a given key, panics if the NFT is not found
65func MustGetNFT(key string) grc721.IGRC721 {
66 nftGetter := GetNFT(key)
67 if nftGetter == nil {
68 panic("unknown NFT: " + key)
69 }
70 return nftGetter
71}
72
73// GetMultiToken returns a multi-token instance for a given key
74func GetMultiToken(key string) grc1155.IGRC1155 {
75 info, ok := registeredMTs.Get(key)
76 if !ok {
77 return nil
78 }
79 mt := info.(GRC1155TokenInfo).Collection
80 return mt()
81}
82
83// MustGetMultiToken returns a multi-token instance for a given key, panics if the multi-token is not found
84func MustGetMultiToken(key string) grc1155.IGRC1155 {
85 info := GetMultiToken(key)
86 if info == nil {
87 panic("unknown multi-token: " + key)
88 }
89 return info
90}
91
92// GetAllNFTs returns a string of all the NFTs registered
93func GetAllNFTs() string {
94 var out string
95 registeredNFTs.Iterate("", "", func(key string, value interface{}) bool {
96 out += ufmt.Sprintf("NFT:%s,", key)
97 return false
98 })
99 return out
100}
101
102// GetAllTokens returns a string of all the tokens registered
103func GetAllTokens() string {
104 var out string
105 grc20reg.GetRegistry().Iterate("", "", func(key string, value interface{}) bool {
106 out += "Token:" + key + ","
107 return false
108 })
109 return out
110}
111
112// GetAllTokenWithDetails returns a string of all the tokens registered with their details
113func GetAllTokenWithDetails() string {
114 var out string
115 grc20reg.GetRegistry().Iterate("", "", func(key string, value interface{}) bool {
116 tokenGetter := value.(grc20.TokenGetter)
117 token := tokenGetter()
118 out += ufmt.Sprintf("Token:%s,Name:%s,Symbol:%s,Decimals:%d;", key, token.GetName(), token.GetSymbol(), token.GetDecimals())
119 return false
120 })
121 return out
122}
123
124// GetAllMultiTokens returns a string of all the multi-tokens registered
125func GetAllMultiTokens() string {
126 var out string
127 registeredMTs.Iterate("", "", func(key string, value interface{}) bool {
128 out += "MultiToken:" + key + ","
129 return false
130 })
131 return out
132}
133
134// GetAllRegistered returns a string of all the registered tokens, NFTs and multi-tokens
135func GetAllRegistered() string {
136 return GetAllNFTs() + GetAllTokens() + GetAllMultiTokens()
137}
138
139// getNFTBalances returns a string of all the NFTs the user owns
140func getNFTBalances(input string) string {
141 addr := getAddressForUsername(input)
142 if !addr.IsValid() {
143 panic("invalid address or username: " + input)
144 }
145 var out string
146
147 registeredNFTs.Iterate("", "", func(key string, value interface{}) bool {
148 nftGetter := value.(grc721.NFTGetter)
149 nft := nftGetter()
150 key_parts := strings.Split(key, ".")
151 owner, err := nft.OwnerOf(grc721.TokenID(key_parts[len(key_parts)-1]))
152 if err == nil && addr == owner { // show only the nfts owner owns
153 out += "NFT:" + key + ","
154 }
155 return false
156 })
157
158 return out
159}
160
161// getTokenBalances returns a string of all the tokens the user owns
162func getTokenBalances(input string, nonZero bool) string {
163 addr := getAddressForUsername(input)
164 if !addr.IsValid() {
165 panic("invalid address or username: " + input)
166 }
167 var out string
168 grc20reg.GetRegistry().Iterate("", "", func(key string, value interface{}) bool {
169 tokenGetter := value.(grc20.TokenGetter)
170 token := tokenGetter()
171 balance := token.BalanceOf(addr)
172 if !nonZero || balance > 0 {
173 out += ufmt.Sprintf("Token:%s:%d,", key, balance)
174 }
175 return false
176 })
177
178 return out
179}
180
181// getMultiTokenBalances returns a string of all the multi-tokens the user owns
182func getMultiTokenBalances(input string, nonZero bool) string {
183 addr := getAddressForUsername(input)
184 if !addr.IsValid() {
185 panic("invalid address or username: " + input)
186 }
187 var out string
188
189 registeredMTs.Iterate("", "", func(key string, value interface{}) bool {
190 info := value.(GRC1155TokenInfo)
191 mt := info.Collection()
192 balance, err := mt.BalanceOf(addr, grc1155.TokenID(info.TokenID))
193 if err == nil {
194 if !nonZero || balance > 0 {
195 out += ufmt.Sprintf("MultiToken:%s:%d,", key, balance)
196 }
197 }
198 return false
199 })
200
201 return out
202}
203
204// getAddressForUsername returns an address for a given username or address
205func getAddressForUsername(addrOrName string) std.Address {
206 addr := std.Address(addrOrName)
207 if addr.IsValid() {
208 return addr
209 }
210
211 if userData, _ := users.ResolveName(addrOrName); userData != nil {
212 return userData.Addr()
213 }
214
215 return ""
216}