-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement getting external token info for BEP20 (#23)
* Tweak validation text * Implement getting external token info for BEP20
- Loading branch information
Showing
7 changed files
with
129 additions
and
61 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 was deleted.
Oops, something went wrong.
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,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 | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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