-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New methods for validation package (#17)
* Add func for GET HTTP request * Add GetHTTPResponseCode to pkg * Add methods for getting external asset info * Make separate jobs for CI
- Loading branch information
Showing
7 changed files
with
117 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package pkg | ||
|
||
import ( | ||
"encoding/json" | ||
"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") | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.StatusCode != 200 { | ||
return errors.New("failed to obtain json") | ||
} | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to read bytes from body") | ||
} | ||
|
||
err = json.Unmarshal(body, v) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to unmarshal json") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// nolint: noctx | ||
func GetHTTPResponseCode(url string) (int, error) { | ||
res, err := http.Get(url) | ||
if err != nil { | ||
return 0, errors.Wrap(err, "failed to make GET request") | ||
} | ||
defer res.Body.Close() | ||
|
||
return res.StatusCode, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package info | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/trustwallet/assets-go-libs/pkg" | ||
) | ||
|
||
type ExternalTokenInfo struct { | ||
Symbol string `json:"symbol"` | ||
Decimals int `json:"decimals"` | ||
HoldersCount int `json:"holdersCount"` | ||
} | ||
|
||
func GetExternalTokenInfo(tokenID, tokentType string) (*ExternalTokenInfo, error) { | ||
switch strings.ToLower(tokentType) { | ||
case "erc20": | ||
return GetTokenInfoForERC20(tokenID) | ||
case "bep20": | ||
return GetTokenInfoForBEP20(tokenID) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func GetTokenInfoForERC20(tokenID string) (*ExternalTokenInfo, error) { | ||
url := fmt.Sprintf("https://api.ethplorer.io/getTokenInfo/%s?apiKey=freekey", tokenID) | ||
|
||
var result ExternalTokenInfo | ||
err := pkg.GetHTTPResponse(url, &result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &result, nil | ||
} | ||
|
||
// TODO: Implement it. | ||
func GetTokenInfoForBEP20(tokenID string) (*ExternalTokenInfo, error) { | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters