-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
234 lines (209 loc) · 7.62 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/iotexproject/assets/chain"
"github.com/iotexproject/assets/own"
)
func main() {
chains := make(map[string]string)
chains["1"] = "ethereum"
chains["4689"] = "iotex"
chains["4690"] = "iotex_test"
app := fiber.New()
app.Use(cors.New())
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("iotex assets")
})
// GET /tokenlist/:chain
app.Get("/tokenlist/:chain", func(c *fiber.Ctx) error {
chain := c.Params("chain")
if strings.HasPrefix(chain, "/") || strings.HasPrefix(chain, "./") || strings.HasPrefix(chain, "..") {
return c.Status(http.StatusBadRequest).SendString("forbid prefix")
}
if t, ok := chains[chain]; ok {
chain = t
}
data, err := os.ReadFile("./blockchains/" + chain + "/tokenlist.json")
if err != nil {
return c.Status(http.StatusBadRequest).SendString("unsupported chain")
}
c.Context().Response.Header.SetContentType(fiber.MIMEApplicationJSON)
return c.SendString(string(data))
})
// GET /token/:chain/:address
app.Get("/token/:chain/:address", func(c *fiber.Ctx) error {
chainName := c.Params("chain")
if strings.HasPrefix(chainName, "/") || strings.HasPrefix(chainName, "./") || strings.HasPrefix(chainName, "..") {
return c.Status(http.StatusBadRequest).SendString("forbid prefix")
}
if t, ok := chains[chainName]; ok {
chainName = t
}
address := strings.ToLower(c.Params("address"))
var data []byte
if _, err := os.Stat("./blockchains/" + chainName + "/assets/" + address + "/info.json"); os.IsNotExist(err) {
data, err = os.ReadFile("./blockchains/" + chainName + "/tokenlist.json")
if err != nil {
return c.Status(http.StatusBadRequest).SendString("unsupported chain or token")
}
var tokenList chain.TokenList
if err = json.Unmarshal(data, &tokenList); err != nil {
return c.Status(http.StatusInternalServerError).SendString("token info json error")
}
info, err := tokenList.ConvertDetialToInfo(address)
if err != nil {
return c.Status(http.StatusBadRequest).SendString("unsupported token")
}
data, _ = json.Marshal(info)
} else {
data, err = os.ReadFile("./blockchains/" + chainName + "/assets/" + address + "/info.json")
if err != nil {
return c.Status(http.StatusBadRequest).SendString("unsupported chain or token")
}
}
c.Context().Response.Header.SetContentType(fiber.MIMEApplicationJSON)
return c.SendString(string(data))
})
// GET /token/:chain/:address/image/:tokenId
app.Get("token/:chain/:address/image/:tokenId", func(c *fiber.Ctx) error {
chainName := c.Params("chain")
if strings.HasPrefix(chainName, "/") || strings.HasPrefix(chainName, "./") || strings.HasPrefix(chainName, "..") {
return c.Status(http.StatusBadRequest).SendString("forbid prefix")
}
if t, ok := chains[chainName]; ok {
chainName = t
}
address := strings.ToLower(c.Params("address"))
tokenId := c.Params("tokenId")
if chainName == "iotex" {
return c.SendString(fmt.Sprintf("https://image.mimo.exchange/%s/%s.png?width=300", address, tokenId))
}
var tokenInfo chain.TokenInfo
var tokenList chain.TokenList
data, err := os.ReadFile("./blockchains/" + chainName + "/tokenlist.json")
if err != nil {
return c.Status(http.StatusBadRequest).SendString("unsupported chain or token")
}
if err = json.Unmarshal(data, &tokenList); err != nil {
return c.Status(http.StatusInternalServerError).SendString("token info json error")
}
if _, err := os.Stat("./blockchains/" + chainName + "/assets/" + address + "/info.json"); os.IsNotExist(err) {
info, err := tokenList.ConvertDetialToInfo(address)
if err != nil {
info = &chain.TokenInfo{
Id: address,
TokenURI: "dynamic",
}
// process dynamic
// return c.Status(http.StatusBadRequest).SendString("unsupported token")
}
tokenInfo = *info
} else {
data, err := os.ReadFile("./blockchains/" + chainName + "/assets/" + address + "/info.json")
if err != nil {
return c.Status(http.StatusBadRequest).SendString("unsupported chain or token")
}
if err := json.Unmarshal(data, &tokenInfo); err != nil {
return c.Status(http.StatusInternalServerError).SendString("token info json error")
}
}
rpc, err := tokenList.GetRPC()
if err != nil {
log.Printf("parse token image error: %v\n", err)
return c.Status(http.StatusInternalServerError).SendString(err.Error())
}
image, err := chain.ParseNFTImage(chainName, rpc, &tokenInfo, tokenId)
if err != nil {
log.Printf("parse token image error: %v\n", err)
return c.Status(http.StatusInternalServerError).SendString("parse token image error")
}
return c.SendString(image)
})
app.Get("image/:id", func(c *fiber.Ctx) error {
id := c.Params("id")
data, ok := chain.IMAGE_CACHE.Get(id)
if !ok {
log.Printf("fetch data from cache fail")
return c.Status(http.StatusInternalServerError).SendString("fetch data from cache fail")
}
c.Response().Header.Add("Content-Type", "image/svg+xml")
return c.Send(data.([]byte))
})
app.Get("image/static/:file", func(c *fiber.Ctx) error {
file := c.Params("file")
return c.SendFile("static/" + file)
})
app.Get("/account/:chain/own/:account", func(c *fiber.Ctx) error {
chainName := c.Params("chain")
if strings.HasPrefix(chainName, "/") || strings.HasPrefix(chainName, "./") || strings.HasPrefix(chainName, "..") {
return c.Status(http.StatusBadRequest).SendString("forbid prefix")
}
if t, ok := chains[chainName]; ok {
chainName = t
}
var tokenList chain.TokenList
tokenData, err := os.ReadFile("./blockchains/" + chainName + "/tokenlist.json")
if err != nil {
return c.Status(http.StatusBadRequest).SendString("unsupported chain or token")
}
if err = json.Unmarshal(tokenData, &tokenList); err != nil {
return c.Status(http.StatusInternalServerError).SendString("token info json error")
}
account := c.Params("account")
tokenType := c.Query("type", "721")
skip, _ := strconv.Atoi(c.Query("skip", "0"))
first, _ := strconv.Atoi(c.Query("first", "10"))
var fetcher own.Fetcher
rpc, err := tokenList.GetRPC()
if err != nil {
log.Printf("fetch rpc error: %v\n", err)
return c.Status(http.StatusInternalServerError).SendString(err.Error())
}
if chainName == "ethereum" {
fetcher, err = own.NewEthereumFetcher(rpc)
if err != nil {
log.Printf("instance ethereum fetcher error: %v\n", err)
return c.Status(http.StatusInternalServerError).SendString(err.Error())
}
} else if chainName == "iotex" {
fetcher, err = own.NewIoTeXFetcher(rpc)
if err != nil {
log.Printf("instance iotex fetcher error: %v\n", err)
return c.Status(http.StatusInternalServerError).SendString(err.Error())
}
} else if chainName == "iotex_test" {
fetcher, err = own.NewIoTeXTestnetFetcher(rpc)
if err != nil {
log.Printf("instance iotex testnet fetcher error: %v\n", err)
return c.Status(http.StatusInternalServerError).SendString(err.Error())
}
} else {
return c.Status(http.StatusInternalServerError).SendString("chain does not supported")
}
data, err := fetcher.FetchOwnTokens(account, tokenType, skip, first)
if err != nil {
log.Printf("fetch own tokens error: %v\n", err)
return c.Status(http.StatusInternalServerError).SendString("fetch own tokens error")
}
// process name and symbol with tokenlist
for i, token := range data {
for _, t := range tokenList.Tokens {
if strings.EqualFold(token.Contract, t.Address) {
data[i].Name = t.Name
data[i].Symbol = t.Symbol
break
}
}
}
return c.JSON(data)
})
log.Fatal(app.Listen(":3000"))
}