Skip to content

Commit 14dbf2b

Browse files
author
Jiri Malek
committed
Split ERC20 tokens list from the base config file.
1 parent a146f44 commit 14dbf2b

File tree

5 files changed

+124
-19
lines changed

5 files changed

+124
-19
lines changed

doc/tokens.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"0x0000000000000000000000000000000000000000": "https://repository.fantom.network/logos/erc20.svg",
3+
"0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83": "https://cryptologos.cc/logos/fantom-ftm-logo.svg?v=003",
4+
"0xad84341756bf337f5a0164515b1f6f993d194e1f": "https://cdn.worldvectorlogo.com/logos/usd-1.svg",
5+
"0xc165aaac01573520906282541626a6fe7f65b22e": "https://repository.fantom.network/logos/erc20.svg",
6+
"0x874d9348f6cdfce3acc588db32f824110b0f97a5": "https://repository.fantom.network/logos/erc20.svg",
7+
"0x64a4e3b2cbaab7ecf10ec787b0593df06257174c": "https://repository.fantom.network/logos/erc20.svg",
8+
"0x073588d3bc59d9759f9262c6ebf425f78be8b9bb": "https://repository.fantom.network/logos/erc20.svg",
9+
"0x4597b2eb9e3a0d0ac29f5edcf9e4272772014abc": "https://repository.fantom.network/logos/erc20.svg",
10+
"0x31f9ba2bd8202eb963d3872ce6f82b9d0ec026d2": "https://repository.fantom.network/logos/erc20.svg",
11+
"0x60e51a1f545a2fded778dc8dab2a0df8f7c97527": "https://repository.fantom.network/logos/erc20.svg",
12+
"0x67fe92968cf26ce96028b61bc09f8faa008ac0de": "https://repository.fantom.network/logos/erc20.svg",
13+
"0x15abf369e3401ed7ad4dadde27c95464d757c44a": "https://repository.fantom.network/logos/erc20.svg",
14+
"0xf5fefa0a30bdb416823a201500947a05e60e3aa3": "https://repository.fantom.network/logos/erc20.svg",
15+
"0x67587cb6321906316dc37fd3df476ac45bd98356": "https://repository.fantom.network/logos/erc20.svg",
16+
"0x965e1a7a57fd094ab82ca2813e9ba3bd9a3a69d8": "https://repository.fantom.network/logos/erc20.svg",
17+
"0xcda9a05e95d0bf9bea4bcb7747f499a6035da90c": "https://repository.fantom.network/logos/erc20.svg",
18+
"0x0bb1b2090b2f8c33c8522c34853fd7d5b2d37ea9": "https://repository.fantom.network/logos/erc20.svg",
19+
"0x69c744d3444202d35a2783929a0f930f2fbb05ad": "https://repository.fantom.network/logos/sftm.svg",
20+
"0xcde58e4b6c7b78b04da664edb0a9752cc5fedbd5": "https://repository.fantom.network/logos/gbp.svg",
21+
"0x24d39324cf3697fd9fd78714e8cdeb5df66e3dcd": "https://repository.fantom.network/logos/cny.svg",
22+
"0xe105621721d1293c27be7718e041a4ce0ebb227e": "https://repository.fantom.network/logos/euro.svg",
23+
"0x2b9c073ec670b70f417bbaf3bbb052aa563a0a23": "https://repository.fantom.network/logos/krw.svg",
24+
"0x3b74389bc23057325bab96523dbec8c445f55799": "https://repository.fantom.network/logos/jpy.svg",
25+
"0x81740d647493a61329e1c574a11ee7577659fb14": "https://repository.fantom.network/logos/chf.svg",
26+
"0xe1146b9ac456fcbb60644c36fd3f868a9072fc6e": "https://repository.fantom.network/logos/bitcoin.svg",
27+
"0x658b0c7613e890ee50b8c4bc6a3f41ef411208ad": "https://repository.fantom.network/logos/eth.svg",
28+
"0xa649a19423052dc6b320360b3c760884e095ac57": "https://repository.fantom.network/logos/link.svg",
29+
"0x078eef5a2fb533e1a4d487ef64b27df113d12c32": "https://repository.fantom.network/logos/band.svg",
30+
"0x27f26f00e1605903645bbabc0a73e35027dccd45": "https://repository.fantom.network/logos/bnb.svg",
31+
"0x2202c52c9076a49400aaccf159e1956269eaa673": "https://repository.fantom.network/logos/gold.svg",
32+
"0xf15e88eef35bf4709a4c3e99c00358f9247d4531": "https://repository.fantom.network/logos/silver.svg",
33+
"0xe297e06761a5489380538a0308b6f9b4a53bea45": "https://repository.fantom.network/logos/wti.svg",
34+
"0xe64B9fd040D1F9D4715C645e0D567EF69958D3D9": "https://modefi.io/images/modefi128.png"
35+
}

internal/config/config.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,14 @@ type Config struct {
4949
// Governance configuration
5050
Governance Governance `mapstructure:"governance"`
5151

52-
// KnownTokens is a list of known ERC20 tokens
52+
// TokenLogoFilePath contains the path to JSON file with the map
53+
// of known ERC20 tokens to their logo URLs.
54+
// The file will be loaded on configuration loading.
55+
TokenLogoFilePath string `mapstructure:"erc20_tokens_file"`
56+
57+
// TokenLogo is a list of known ERC20 tokens
5358
// mapped to URL addresses of their logos.
54-
TokenLogo map[common.Address]string `mapstructure:"erc20_logos"`
59+
TokenLogo map[common.Address]string
5560
}
5661

5762
// Server represents the GraphQL server configuration
@@ -118,9 +123,9 @@ type Staking struct {
118123

119124
// DeFi represents the DeFi and financial contracts configuration.
120125
type DeFi struct {
121-
FMint DeFiFMint `mapstructure:"fmint"`
122-
Uniswap DeFiUniswap `mapstructure:"uniswap"`
123-
FLend DeFiFLend `mapstructure:"flend"`
126+
FMint DeFiFMint `mapstructure:"fmint"`
127+
Uniswap DeFiUniswap `mapstructure:"uniswap"`
128+
FLend DeFiFLend `mapstructure:"flend"`
124129
PriceSymbols []string `mapstructure:"symbols"`
125130
}
126131

internal/config/default.go

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ const (
7373

7474
// defDefiFMintAddressProvider represents the address of the fMintAddressProvider
7575
defDefiUniswapRouter = EmptyAddress
76+
77+
// defTokenLogoFilePath represents the default path to the tokens map file
78+
defTokenLogoFilePath = "tokens.json"
7679
)
7780

7881
// default list of API peers
@@ -106,6 +109,7 @@ func applyDefaults(cfg *viper.Viper) {
106109
cfg.SetDefault(keySolCompilerPath, defSolCompilerPath)
107110
cfg.SetDefault(keyApiPeers, defApiPeers)
108111
cfg.SetDefault(keyApiStateOrigin, defApiStateOrigin)
112+
cfg.SetDefault(keyErc20TokenMapFilePath, defTokenLogoFilePath)
109113
cfg.SetDefault(keyErc20Logos, defERC20Logo)
110114

111115
// server timeouts

internal/config/keys.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ const (
4545
keySolCompilerPath = "compiler.sol"
4646

4747
// utility options
48-
keyVotingSources = "voting.sources"
49-
keyErc20Logos = "erc20_logos"
48+
keyVotingSources = "voting.sources"
49+
keyErc20TokenMapFilePath = "erc20_tokens_file"
50+
keyErc20Logos = "erc20_logos"
5051

5152
// PoS staking configuration
5253
keyStakingSfcContract = "staking.sfc"

internal/config/load.go

+72-12
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,50 @@ package config
22

33
import (
44
"crypto/ecdsa"
5+
"encoding/json"
56
"fantom-api-graphql/internal/types"
67
"flag"
78
"fmt"
89
"github.com/ethereum/go-ethereum/common"
910
"github.com/ethereum/go-ethereum/crypto"
1011
"github.com/mitchellh/mapstructure"
1112
"github.com/spf13/viper"
13+
"io/ioutil"
1214
"log"
15+
"os"
1316
"reflect"
1417
)
1518

1619
// Load provides a loaded configuration for Fantom API server.
1720
func Load() (*Config, error) {
21+
// Get the config reader
22+
cfg, err := readConfigFile()
23+
if err != nil {
24+
return nil, err
25+
}
26+
27+
// prep the container and try to unmarshal
28+
// the config file into the config structure
29+
var config Config
30+
if err = cfg.Unmarshal(&config, setupConfigUnmarshaler); err != nil {
31+
log.Println("can not extract API server configuration")
32+
log.Println(err.Error())
33+
return nil, err
34+
}
35+
36+
// try to load the logo map file
37+
loadErc20LogMap(&config)
38+
39+
// return the final config
40+
return &config, nil
41+
}
42+
43+
// readConfigFile reads the config file and provides instance
44+
// of the loaded configuration.
45+
func readConfigFile() (*viper.Viper, error) {
46+
// inform about tokens loading
47+
log.Printf("loading app configuration")
48+
1849
// Get the config reader
1950
cfg := reader()
2051

@@ -23,27 +54,56 @@ func Load() (*Config, error) {
2354

2455
// Try to read the file
2556
if err := cfg.ReadInConfig(); err != nil {
26-
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
27-
// Config file not found; ignore the error, we may not need the config file
28-
log.Print("configuration file not found, using default values")
29-
} else {
57+
// is this an error notifying missing config file?
58+
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
3059
// Config file was found but another error was produced
3160
log.Printf("can not read the server configuration")
3261
return nil, err
3362
}
63+
64+
// Config file not found; ignore the error, we may not need the config file
65+
log.Print("configuration file not found, using default values")
3466
}
3567

36-
// prep the container and try to unmarshal
37-
// the config file into the config structure
38-
var config Config
39-
err := cfg.Unmarshal(&config, setupConfigUnmarshaler)
68+
return cfg, nil
69+
}
70+
71+
// loadErc20LogMap loads the map of ERC20 token logos.
72+
func loadErc20LogMap(cfg *Config) {
73+
// is there any path at all?
74+
if cfg.TokenLogoFilePath == "" {
75+
log.Print("ERC20 tokens map file path not available")
76+
return
77+
}
78+
79+
// try to open the file
80+
f, err := os.Open(cfg.TokenLogoFilePath)
4081
if err != nil {
41-
log.Println("can not extract API server configuration")
42-
log.Println(err.Error())
43-
return nil, err
82+
log.Printf("can not open ERC20 tokens map file; %s", err.Error())
83+
return
4484
}
4585

46-
return &config, nil
86+
// make sure to close the file
87+
defer f.Close()
88+
89+
// inform about tokens loading
90+
log.Printf("loading ERC20 tokens from %s", cfg.TokenLogoFilePath)
91+
92+
// read the whole file
93+
data, err := ioutil.ReadAll(f)
94+
if err != nil {
95+
log.Printf("can not read ERC20 tokens map file; %s", err.Error())
96+
return
97+
}
98+
99+
// try to unmarshal the data
100+
if err := json.Unmarshal(data, &cfg.TokenLogo); err != nil {
101+
log.Printf("can not decode ERC20 tokens map file; %s", err.Error())
102+
return
103+
}
104+
105+
// inform about tokens
106+
log.Printf("found %d ERC20 tokens", len(cfg.TokenLogo))
47107
}
48108

49109
// setupConfigUnmarshaler configures the Config loader to properly unmarshal

0 commit comments

Comments
 (0)