Skip to content

Commit

Permalink
include tron token info on validation (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoskarakostas authored Feb 10, 2023
1 parent acae5a2 commit 0ca1ce5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
4 changes: 4 additions & 0 deletions validation/info/external/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions validation/info/external/trc10.go
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 38 additions & 0 deletions validation/info/external/trc20.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 0ca1ce5

Please sign in to comment.