-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasset.go
More file actions
128 lines (103 loc) · 2.88 KB
/
Copy pathasset.go
File metadata and controls
128 lines (103 loc) · 2.88 KB
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
package assets
import (
"github.com/GoFarsi/assets/entity"
"golang.org/x/exp/maps"
"io"
"net/http"
)
type AssetRepo struct {
Chains map[string]*entity.Chain
}
type Pagination struct {
PageNumber int
PageSize int
}
type Option struct {
*Pagination
}
func New(repoAddress string) *AssetRepo {
client := http.Client{}
resp, err := client.Get(YamlAddress)
if err != nil {
return nil
}
defer resp.Body.Close()
assetsByte, err := io.ReadAll(resp.Body)
if err != nil {
return nil
}
chains := parseAssetsByteToArray(assetsByte)
asset := &AssetRepo{Chains: chains}
switch repoAddress {
case GithubRepoUrl:
asset.adaptLogoPath(formatFilePathByGithubRepo)
}
return asset
}
// GetTotalChainsSize return len of all chains in assets.yaml
func (a *AssetRepo) GetTotalChainsSize() int {
return len(a.Chains)
}
// GetChains return all chains (networks) in assets.yaml
func (a *AssetRepo) GetChains(option *Option) ([]*entity.Chain, error) {
return applyOptionsOnChains(a.Chains, option)
}
// GetTestChains return test chains (networks) in assets.yaml
func (a *AssetRepo) GetTestChains(option *Option) ([]*entity.Chain, error) {
chains := getChainsByType(a.Chains, entity.TestChainType)
return applyOptionsOnChains(chains, option)
}
// GetMainChains return main chains (networks) in assets.yaml
func (a *AssetRepo) GetMainChains(option *Option) ([]*entity.Chain, error) {
chains := getChainsByType(a.Chains, entity.MainChainType)
return applyOptionsOnChains(chains, option)
}
// GetChain return chain by its ID
func (a *AssetRepo) GetChain(Id string) *entity.Chain {
return a.Chains[Id]
}
// GetChainBySymbol return chain by its symbol
func (a *AssetRepo) GetChainBySymbol(symbol string) *entity.Chain {
for _, v := range a.Chains {
if v.Symbol == symbol {
return v
}
}
return nil
}
// GetChainByName return chain by its name
func (a *AssetRepo) GetChainByName(name string) *entity.Chain {
for _, v := range a.Chains {
if v.Name == name {
return v
}
}
return nil
}
// GetAsset return asset by its ID
func (a *AssetRepo) GetAsset(Id string) *entity.Asset {
for _, c := range a.Chains {
if c.Assets[Id] != nil {
return c.Assets[Id]
}
}
return nil
}
// applyOptionsOnChains will check Options passed to requests and apply theme to result chains
func applyOptionsOnChains(chains map[string]*entity.Chain, option *Option) ([]*entity.Chain, error) {
if option != nil && option.Pagination != nil {
return getPaginatedChainList(chains, option.Pagination.PageNumber, option.Pagination.PageSize)
}
return maps.Values(chains), nil
}
// getChainsByType return list of chains by selecting type of chain (test, main,...)
func getChainsByType(chains map[string]*entity.Chain, chainType entity.ChainType) map[string]*entity.Chain {
result := make(map[string]*entity.Chain)
for k, v := range chains {
if v.Type != chainType {
continue
}
result[k] = v
}
return result
}