From 0ca1ce5aed9bdeebb1ea9453bc92f04e08966192 Mon Sep 17 00:00:00 2001 From: Nikos Karakostas Date: Fri, 10 Feb 2023 16:16:55 +0200 Subject: [PATCH] include tron token info on validation (#84) --- validation/info/external/external.go | 4 +++ validation/info/external/trc10.go | 38 ++++++++++++++++++++++++++++ validation/info/external/trc20.go | 38 ++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 validation/info/external/trc10.go create mode 100644 validation/info/external/trc20.go diff --git a/validation/info/external/external.go b/validation/info/external/external.go index 4d33733..7270158 100644 --- a/validation/info/external/external.go +++ b/validation/info/external/external.go @@ -35,6 +35,10 @@ func GetTokenInfo(tokenID, tokentType string) (*TokenInfo, error) { return GetTokenInfoByScraping(fmt.Sprintf("https://snowtrace.io/token/%s", tokenID)) case "spl": return GetTokenInfoForSPL(tokenID) + case "trc20": + return GetTokenInfoForTRC20(tokenID) + case "trc10": + return GetTokenInfoForTRC10(tokenID) } return nil, nil diff --git a/validation/info/external/trc10.go b/validation/info/external/trc10.go new file mode 100644 index 0000000..9a585f8 --- /dev/null +++ b/validation/info/external/trc10.go @@ -0,0 +1,38 @@ +package external + +import ( + "errors" + "fmt" + + "github.com/trustwallet/assets-go-libs/http" +) + +const trc10APIURL = "https://apilist.tronscan.io/api/token?id=%s" + +type TRC10TokensResponse struct { + Data []struct { + Symbol string `json:"abbr"` + Decimals int `json:"precision"` + HoldersCount int `json:"nrOfTokenHolders"` + } `json:"data"` +} + +func GetTokenInfoForTRC10(tokenID string) (*TokenInfo, error) { + url := fmt.Sprintf(trc10APIURL, tokenID) + + var res TRC10TokensResponse + err := http.GetHTTPResponse(url, &res) + if err != nil { + return nil, err + } + + if len(res.Data) == 0 { + return nil, errors.New("not found") + } + + return &TokenInfo{ + Symbol: res.Data[0].Symbol, + Decimals: res.Data[0].Decimals, + HoldersCount: res.Data[0].HoldersCount, + }, nil +} diff --git a/validation/info/external/trc20.go b/validation/info/external/trc20.go new file mode 100644 index 0000000..3f4c228 --- /dev/null +++ b/validation/info/external/trc20.go @@ -0,0 +1,38 @@ +package external + +import ( + "errors" + "fmt" + + "github.com/trustwallet/assets-go-libs/http" +) + +const trc20APIURL = "https://apilist.tronscan.io/api/token_trc20?contract=%s" + +type TRC20TokensResponse struct { + TRC20Tokens []struct { + Symbol string `json:"symbol"` + Decimals int `json:"decimals"` + HoldersCount int `json:"holders_count"` + } `json:"trc20_tokens"` +} + +func GetTokenInfoForTRC20(tokenID string) (*TokenInfo, error) { + url := fmt.Sprintf(trc20APIURL, tokenID) + + var res TRC20TokensResponse + err := http.GetHTTPResponse(url, &res) + if err != nil { + return nil, err + } + + if len(res.TRC20Tokens) == 0 { + return nil, errors.New("not found") + } + + return &TokenInfo{ + Symbol: res.TRC20Tokens[0].Symbol, + Decimals: res.TRC20Tokens[0].Decimals, + HoldersCount: res.TRC20Tokens[0].HoldersCount, + }, nil +}