Skip to content

Commit

Permalink
Remove pkg/errors dependency, fix getting external asset info req (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
unanoc authored Nov 29, 2021
1 parent f5259e9 commit be98393
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
13 changes: 6 additions & 7 deletions pkg/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@ package pkg

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/pkg/errors"
)

// nolint: noctx
func GetHTTPResponse(url string, v interface{}) error {
res, err := http.Get(url)
if err != nil {
return errors.Wrap(err, "failed to make GET request")
return fmt.Errorf("failed to make GET request: %v", err)
}
defer res.Body.Close()

if res.StatusCode != 200 {
return errors.New("failed to obtain json")
return fmt.Errorf("failed to obtain json: %v", err)
}

body, err := ioutil.ReadAll(res.Body)
if err != nil {
return errors.Wrap(err, "failed to read bytes from body")
return fmt.Errorf("failed to read bytes from body: %v", err)
}

err = json.Unmarshal(body, v)
if err != nil {
return errors.Wrap(err, "failed to unmarshal json")
return fmt.Errorf("failed to unmarshal json: %v", err)
}

return nil
Expand All @@ -37,7 +36,7 @@ func GetHTTPResponse(url string, v interface{}) error {
func GetHTTPResponseCode(url string) (int, error) {
res, err := http.Get(url)
if err != nil {
return 0, errors.Wrap(err, "failed to make GET request")
return 0, fmt.Errorf("failed to make GET request: %v", err)
}
defer res.Body.Close()

Expand Down
26 changes: 21 additions & 5 deletions pkg/validation/info/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package info

import (
"fmt"
"strconv"
"strings"

"github.com/trustwallet/assets-go-libs/pkg"
)

type ExternalTokenInfo struct {
Symbol string `json:"symbol"`
Decimals int `json:"decimals"`
HoldersCount int `json:"holdersCount"`
Symbol string
Decimals int
HoldersCount int
}

func GetExternalTokenInfo(tokenID, tokentType string) (*ExternalTokenInfo, error) {
Expand All @@ -24,16 +25,31 @@ func GetExternalTokenInfo(tokenID, tokentType string) (*ExternalTokenInfo, error
return nil, nil
}

type ExternalTokenInfoERC20 struct {
Symbol string `json:"symbol"`
Decimals string `json:"decimals"`
HoldersCount int `json:"holdersCount"`
}

func GetTokenInfoForERC20(tokenID string) (*ExternalTokenInfo, error) {
url := fmt.Sprintf("https://api.ethplorer.io/getTokenInfo/%s?apiKey=freekey", tokenID)

var result ExternalTokenInfo
var result ExternalTokenInfoERC20
err := pkg.GetHTTPResponse(url, &result)
if err != nil {
return nil, err
}

return &result, nil
decimals, err := strconv.Atoi(result.Decimals)
if err != nil {
return nil, err
}

return &ExternalTokenInfo{
Symbol: result.Symbol,
Decimals: decimals,
HoldersCount: result.HoldersCount,
}, nil
}

// TODO: Implement it.
Expand Down

0 comments on commit be98393

Please sign in to comment.