Skip to content

Commit 1ef3521

Browse files
authored
Split asset services methods (trustwallet#856)
* Split asset services methods * Change method signature
1 parent 1045876 commit 1ef3521

4 files changed

Lines changed: 64 additions & 50 deletions

File tree

‎services/assets/info.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package assets
2+
3+
import (
4+
"github.com/trustwallet/blockatlas/coin"
5+
"github.com/trustwallet/blockatlas/pkg/blockatlas"
6+
"github.com/trustwallet/blockatlas/pkg/errors"
7+
"time"
8+
)
9+
10+
func GetCoinInfo(coinId int, token string) (info *blockatlas.CoinInfo, err error) {
11+
c, ok := coin.Coins[uint(coinId)]
12+
if !ok {
13+
return info, errors.E("coin not found")
14+
}
15+
url := getCoinInfoUrl(c, token)
16+
request := blockatlas.InitClient(url)
17+
err = request.GetWithCache(&info, "info/info.json", nil, time.Hour*1)
18+
return
19+
}
20+
21+
func getCoinInfoUrl(c coin.Coin, token string) string {
22+
if len(token) == 0 {
23+
return AssetsURL + c.Handle
24+
}
25+
return AssetsURL + c.Handle + "/assets/" + token
26+
}

‎services/assets/info_test.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package assets
2+
3+
import (
4+
"github.com/trustwallet/blockatlas/coin"
5+
"testing"
6+
)
7+
8+
func Test_getCoinInfoUrl(t *testing.T) {
9+
type args struct {
10+
c coin.Coin
11+
token string
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
want string
17+
}{
18+
{"test Ethereum coin", args{coin.Ethereum(), ""}, AssetsURL + coin.Ethereum().Handle},
19+
{"test Ethereum token", args{coin.Ethereum(), "0x0000000000b3F879cb30FE243b4Dfee438691c04"}, AssetsURL + coin.Ethereum().Handle + "/assets/" + "0x0000000000b3F879cb30FE243b4Dfee438691c04"},
20+
{"test Binance coin", args{coin.Binance(), ""}, AssetsURL + coin.Binance().Handle},
21+
{"test Binance token", args{coin.Binance(), "0x0000000000b3F879cb30FE243b4Dfee438691c04"}, AssetsURL + coin.Binance().Handle + "/assets/" + "0x0000000000b3F879cb30FE243b4Dfee438691c04"},
22+
}
23+
for _, tt := range tests {
24+
t.Run(tt.name, func(t *testing.T) {
25+
if got := getCoinInfoUrl(tt.args.c, tt.args.token); got != tt.want {
26+
t.Errorf("getCoinInfoUrl() = %v, want %v", got, tt.want)
27+
}
28+
})
29+
}
30+
}
Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ const (
1212
AssetsURL = "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/"
1313
)
1414

15-
func GetCoinInfo(coinId int, token string) (info *blockatlas.CoinInfo, err error) {
16-
c, ok := coin.Coins[uint(coinId)]
17-
if !ok {
18-
return info, errors.E("coin not found")
15+
func requestValidatorsInfo(coin coin.Coin) ([]AssetValidator, error) {
16+
var results []AssetValidator
17+
request := blockatlas.InitClient(AssetsURL + coin.Handle)
18+
err := request.GetWithCache(&results, "validators/list.json", nil, time.Hour*1)
19+
if err != nil {
20+
return nil, errors.E(err, errors.Params{"coin": coin.Handle}).PushToSentry()
1921
}
20-
url := getCoinInfoUrl(c, token)
21-
request := blockatlas.InitClient(url)
22-
err = request.GetWithCache(&info, "info/info.json", nil, time.Hour*1)
23-
return
22+
return results, nil
2423
}
2524

2625
func GetValidatorsMap(api blockatlas.StakeAPI) (blockatlas.ValidatorMap, error) {
@@ -36,7 +35,7 @@ func GetValidatorsMap(api blockatlas.StakeAPI) (blockatlas.ValidatorMap, error)
3635
}
3736

3837
func GetValidators(api blockatlas.StakeAPI) ([]blockatlas.StakeValidator, error) {
39-
assetsValidators, err := getValidatorsInfo(api.Coin())
38+
assetsValidators, err := requestValidatorsInfo(api.Coin())
4039
if err != nil {
4140
return nil, errors.E(err, "unable to fetch validators list from the registry").PushToSentry()
4241
}
@@ -52,16 +51,6 @@ func GetValidators(api blockatlas.StakeAPI) ([]blockatlas.StakeValidator, error)
5251
return results, nil
5352
}
5453

55-
func getValidatorsInfo(coin coin.Coin) ([]AssetValidator, error) {
56-
var results []AssetValidator
57-
request := blockatlas.InitClient(AssetsURL + coin.Handle)
58-
err := request.GetWithCache(&results, "validators/list.json", nil, time.Hour*1)
59-
if err != nil {
60-
return nil, errors.E(err, errors.Params{"coin": coin.Handle}).PushToSentry()
61-
}
62-
return results, nil
63-
}
64-
6554
func normalizeValidators(validators []blockatlas.Validator, assets []AssetValidator, coin coin.Coin) []blockatlas.StakeValidator {
6655
results := make([]blockatlas.StakeValidator, 0)
6756
for _, v := range validators {
@@ -97,10 +86,3 @@ func calculateAnnual(annual float64, commission float64) float64 {
9786
func getImage(c coin.Coin, ID string) string {
9887
return AssetsURL + c.Handle + "/validators/assets/" + ID + "/logo.png"
9988
}
100-
101-
func getCoinInfoUrl(c coin.Coin, token string) string {
102-
if len(token) == 0 {
103-
return AssetsURL + c.Handle
104-
}
105-
return AssetsURL + c.Handle + "/assets/" + token
106-
}
Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,27 +113,3 @@ func TestCalcAnnual(t *testing.T) {
113113
})
114114
}
115115
}
116-
117-
func Test_getCoinInfoUrl(t *testing.T) {
118-
type args struct {
119-
c coin.Coin
120-
token string
121-
}
122-
tests := []struct {
123-
name string
124-
args args
125-
want string
126-
}{
127-
{"test Ethereum coin", args{coin.Ethereum(), ""}, AssetsURL + coin.Ethereum().Handle},
128-
{"test Ethereum token", args{coin.Ethereum(), "0x0000000000b3F879cb30FE243b4Dfee438691c04"}, AssetsURL + coin.Ethereum().Handle + "/assets/" + "0x0000000000b3F879cb30FE243b4Dfee438691c04"},
129-
{"test Binance coin", args{coin.Binance(), ""}, AssetsURL + coin.Binance().Handle},
130-
{"test Binance token", args{coin.Binance(), "0x0000000000b3F879cb30FE243b4Dfee438691c04"}, AssetsURL + coin.Binance().Handle + "/assets/" + "0x0000000000b3F879cb30FE243b4Dfee438691c04"},
131-
}
132-
for _, tt := range tests {
133-
t.Run(tt.name, func(t *testing.T) {
134-
if got := getCoinInfoUrl(tt.args.c, tt.args.token); got != tt.want {
135-
t.Errorf("getCoinInfoUrl() = %v, want %v", got, tt.want)
136-
}
137-
})
138-
}
139-
}

0 commit comments

Comments
 (0)