diff --git a/asset.go b/asset.go index d0ad3728..175b8b22 100644 --- a/asset.go +++ b/asset.go @@ -2,10 +2,11 @@ package assets import ( "github.com/GoFarsi/assets/entity" + "golang.org/x/exp/maps" ) type AssetRepo struct { - Chains []*entity.Chain + Chains map[string]*entity.Chain } type Pagination struct { @@ -34,6 +35,11 @@ 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) @@ -46,22 +52,38 @@ func (a *AssetRepo) GetMainChains(option *Option) ([]*entity.Chain, error) { return applyOptionsOnChains(chains, option) } -// GetChain return chain by its id +// GetChain return chain by its ID func (a *AssetRepo) GetChain(Id string) *entity.Chain { - for _, c := range a.Chains { - if c.Id == Id { - return c + 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 } -// GetChainBySymbol return chain by its symbol -func (a *AssetRepo) GetChainBySymbol(symbol string) *entity.Chain { +// 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.Symbol == symbol { - return c + if c.Assets[Id] != nil { + return c.Assets[Id] } } @@ -69,22 +91,23 @@ func (a *AssetRepo) GetChainBySymbol(symbol string) *entity.Chain { } // applyOptionsOnChains will check Options passed to requests and apply theme to result chains -func applyOptionsOnChains(chains []*entity.Chain, option *Option) ([]*entity.Chain, error) { +func applyOptionsOnChains(chains map[string]*entity.Chain, option *Option) ([]*entity.Chain, error) { if option.Pagination != nil { return getPaginatedChainList(chains, option.Pagination.PageNumber, option.Pagination.PageSize) } - return chains, nil + return maps.Values(chains), nil } // getChainsByType return list of chains by selecting type of chain (test, main,...) -func getChainsByType(chains []*entity.Chain, chainType entity.ChainType) (result []*entity.Chain) { - for _, c := range chains { - if c.Type != chainType { +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 = append(result, c) + result[k] = v } return result diff --git a/entity/asset.go b/entity/asset.go index db4252d4..a3d3bfa4 100644 --- a/entity/asset.go +++ b/entity/asset.go @@ -30,6 +30,8 @@ type Asset struct { LogoPNG string `yaml:"logo_png"` Types []string `yaml:"types"` Standards []string `yaml:"standards"` + + ChainUUID string } func (a *Asset) GetName() string { diff --git a/entity/chain.go b/entity/chain.go index 8b56dcdb..dc5e1571 100644 --- a/entity/chain.go +++ b/entity/chain.go @@ -8,23 +8,21 @@ const ( ) type Chain struct { - Id string `yaml:"id"` - ParentId string `yaml:"parent_id"` - Name string `yaml:"name"` - Symbol string `yaml:"symbol"` - LogoPNG string `yaml:"logo_png"` - ChainId string `yaml:"chain_id"` - Assets []*Asset `yaml:"assets"` - Type ChainType `yaml:"type"` + ParentId string `yaml:"parent_id"` + Name string `yaml:"name"` + Symbol string `yaml:"symbol"` + LogoPNG string `yaml:"logo_png"` + ChainId string `yaml:"chain_id"` + Assets map[string]*Asset `yaml:"assets"` + Type ChainType `yaml:"type"` } -func NewChain(name, symbol string, chainType ChainType, chainId string, assets ...*Asset) *Chain { +func NewChain(name, symbol string, chainType ChainType, chainId string) *Chain { return &Chain{ Name: name, Symbol: symbol, ChainId: chainId, Type: chainType, - Assets: assets, } } @@ -60,6 +58,6 @@ func (c *Chain) IsTestNet() bool { return false } -func (c *Chain) GetChainAssets() []*Asset { +func (c *Chain) GetChainAssets() map[string]*Asset { return c.Assets } diff --git a/go.mod b/go.mod index 8ad3bfde..585166be 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,5 @@ module github.com/GoFarsi/assets go 1.20 require gopkg.in/yaml.v3 v3.0.1 + +require golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 diff --git a/go.sum b/go.sum index a62c313c..31d65f3e 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o= +golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/list.go b/list.go index d42d7233..2b120070 100644 --- a/list.go +++ b/list.go @@ -3,10 +3,11 @@ package assets import ( "errors" "github.com/GoFarsi/assets/entity" + "golang.org/x/exp/maps" ) // getPaginatedChainList returns paginated list of chains with pageNumber and pageSize params -func getPaginatedChainList(chains []*entity.Chain, pageNumber, pageSize int) (result []*entity.Chain, err error) { +func getPaginatedChainList(chains map[string]*entity.Chain, pageNumber, pageSize int) (result []*entity.Chain, err error) { totalChains := len(chains) pageNumber, err = validatePageNumber(pageNumber) if err != nil { @@ -23,7 +24,7 @@ func getPaginatedChainList(chains []*entity.Chain, pageNumber, pageSize int) (re end = totalChains } - return chains[start:end], nil + return maps.Values(chains)[start:end], nil } // validatePageNumber will check the value of pageNumber to be greater than 0 diff --git a/parser.go b/parser.go index 440f61e7..0f9bd256 100644 --- a/parser.go +++ b/parser.go @@ -10,7 +10,7 @@ import ( var assetsByte []byte // parseAssetsByteToArray parse the byte contents of yaml file to array of entity.Chain -func parseAssetsByteToArray() (chains []*entity.Chain) { +func parseAssetsByteToArray() (chains map[string]*entity.Chain) { _ = yaml.Unmarshal(assetsByte, &chains) return chains } diff --git a/resources/assets.yaml b/resources/assets.yaml index 7f791591..92b0c6eb 100644 --- a/resources/assets.yaml +++ b/resources/assets.yaml @@ -1,4 +1,4 @@ -- id: bc08bb60-ebfb-11ed-bd6d-bce92fb9ddda +bc08bb60-ebfb-11ed-bd6d-bce92fb9ddda: parent_id: "" name: Ethereum symbol: ETH @@ -6,7 +6,7 @@ chain_id: 1 type: main assets: - - id: bc2cd3b5-ebfb-11ed-bd6d-bce92fb9ddda + bc2cd3b5-ebfb-11ed-bd6d-bce92fb9ddda: name: Tether symbol: USDT contracts: @@ -20,7 +20,7 @@ types: [ ] standards: - ERC20 - - id: ae43c0bd-2587-4384-936d-24cc2e5a1b0c + ae43c0bd-2587-4384-936d-24cc2e5a1b0c: name: Dai symbol: DAI contracts: @@ -34,7 +34,7 @@ types: [ ] standards: - ERC20 - - id: db4461d5-4747-436c-9d7c-51c88e597813 + db4461d5-4747-436c-9d7c-51c88e597813: name: Binance Coin symbol: BNB contracts: @@ -47,7 +47,7 @@ logo_png: resources/img/db4461d5-4747-436c-9d7c-51c88e597813.png types: [ ] standards: - - id: fa7bfc08-25dd-477d-bb83-bf0a1039b446 + fa7bfc08-25dd-477d-bb83-bf0a1039b446: name: USD Coin symbol: USDC contracts: @@ -60,14 +60,14 @@ logo_png: resources/img/fa7bfc08-25dd-477d-bb83-bf0a1039b446.png types: [ ] standards: -- id: cfc035ff-52c3-4357-bebd-02ad6813d123 +cfc035ff-52c3-4357-bebd-02ad6813d123: parent_id: "" name: BNB Smart Chain (BEP20) symbol: BNB logo_png: resources/img/db4461d5-4747-436c-9d7c-51c88e597813.png type: main assets: - - id: c4ed735b-9c87-4735-a029-fb11fef57ecb + c4ed735b-9c87-4735-a029-fb11fef57ecb: name: XRP Coin symbol: XRP contracts: @@ -80,7 +80,7 @@ logo_png: resources/img/c4ed735b-9c87-4735-a029-fb11fef57ecb.png types: [] standards: -- id: f438b0c8-3b98-4da7-88f3-10ff760b9ff1 +f438b0c8-3b98-4da7-88f3-10ff760b9ff1: parent_id: "" name: Goerli symbol: ETH @@ -88,7 +88,7 @@ chain_id: 1 type: test assets: - - id: f0e13421-4798-45ed-9ce5-a559cdb5133e + f0e13421-4798-45ed-9ce5-a559cdb5133e: name: Tether symbol: USDT contracts: