Skip to content

Commit

Permalink
Implement getting external token info for BEP20 (#23)
Browse files Browse the repository at this point in the history
* Tweak validation text

* Implement getting external token info for BEP20
  • Loading branch information
unanoc authored Dec 3, 2021
1 parent 78a042a commit 7035e4e
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 61 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,6 @@ github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/trustwallet/go-libs v0.2.17 h1:l2zr5Ow19GTPnoWbhjExAhTbz7xLcYPXBa10EIgItjI=
github.com/trustwallet/go-libs v0.2.17/go.mod h1:gBDrDkN5wOY+8kwlU6pnv3l/seGJy2qH1OpTKe7NnXA=
github.com/trustwallet/go-primitives v0.0.11 h1:uVjm1IssSrur5MjnSbl+LerljnaU8pvgoBxq7krjNvI=
github.com/trustwallet/go-primitives v0.0.11/go.mod h1:XPY046lWNrCZ67ZsCeQlJYbKTMDpulc9y7hiRjnL9R4=
github.com/trustwallet/go-primitives v0.0.14-0.20211130135322-c76db95f1406 h1:lezUwyiPYCChVu12XPGlc51cG4GE6hE0ONrKNG/UDQ8=
github.com/trustwallet/go-primitives v0.0.14-0.20211130135322-c76db95f1406/go.mod h1:jLqd7rm+4EYG5JdpxhngM9HwbqfEXzKy/wK4vUB7STs=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
Expand Down
58 changes: 0 additions & 58 deletions pkg/validation/info/external.go

This file was deleted.

67 changes: 67 additions & 0 deletions pkg/validation/info/external/bep20.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package external

import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
)

var (
holdersRegexp = regexp.MustCompile(`(\d+)\saddresses`)
decimalsRegexp = regexp.MustCompile(`(\d+)\s<\/div>`)
symbolRegexp = regexp.MustCompile(`<b>(\w+)<\/b>\s<span`)
)

func GetTokenInfoForBEP20(tokenID string) (*TokenInfo, error) {
url := fmt.Sprintf("https://bscscan.com/token/%s", tokenID)

resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()

dataInBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

// Remove all "," from content
pageContent := strings.ReplaceAll(string(dataInBytes), ",", "")

var holders, decimals int
var symbol string

match := symbolRegexp.FindStringSubmatch(pageContent)
if len(match) > 1 {
symbol = match[1]
if err != nil {
return nil, err
}
}

match = decimalsRegexp.FindStringSubmatch(pageContent)
if len(match) > 1 {
decimals, err = strconv.Atoi(match[1])
if err != nil {
return nil, err
}
}

match = holdersRegexp.FindStringSubmatch(pageContent)
if len(match) > 1 {
holders, err = strconv.Atoi(match[1])
if err != nil {
return nil, err
}
}

return &TokenInfo{
Symbol: symbol,
Decimals: decimals,
HoldersCount: holders,
}, nil
}
35 changes: 35 additions & 0 deletions pkg/validation/info/external/erc20.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package external

import (
"fmt"
"strconv"

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

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

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

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

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

return &TokenInfo{
Symbol: result.Symbol,
Decimals: decimals,
HoldersCount: result.HoldersCount,
}, nil
}
22 changes: 22 additions & 0 deletions pkg/validation/info/external/external.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package external

import (
"strings"
)

type TokenInfo struct {
Symbol string
Decimals int
HoldersCount int
}

func GetTokenInfo(tokenID, tokentType string) (*TokenInfo, error) {
switch strings.ToLower(tokentType) {
case "erc20":
return GetTokenInfoForERC20(tokenID)
case "bep20":
return GetTokenInfoForBEP20(tokenID)
}

return nil, nil
}
2 changes: 1 addition & 1 deletion pkg/validation/info/fields_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func ValidateCoinLinks(links []Link) error {
prefix := allowedLinkKeys[*l.Name]
if prefix != "" {
if !strings.HasPrefix(*l.URL, prefix) {
return fmt.Errorf("ivalid value for links.url field, allowed only with prefixes - %s",
return fmt.Errorf("invalid value for links.url field, allowed only with prefixes - %s",
strings.Join(supportedLinkValues(), ", "))
}
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/validation/info/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func supportedLinkNames() []string {
func supportedLinkValues() []string {
values := make([]string, 0, len(allowedLinkKeys))
for _, v := range allowedLinkKeys {
if v == "" {
continue
}

values = append(values, v)
}

Expand Down

0 comments on commit 7035e4e

Please sign in to comment.