2021-12-19 21:39:53 +00:00
|
|
|
package processor
|
|
|
|
|
|
|
|
import (
|
2022-01-09 20:30:49 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2021-12-19 21:39:53 +00:00
|
|
|
"fmt"
|
2022-01-09 20:30:49 +00:00
|
|
|
"os"
|
2021-12-19 21:39:53 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2021-12-22 16:06:46 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-12-27 09:39:11 +00:00
|
|
|
|
2021-12-20 15:03:08 +00:00
|
|
|
fileLib "github.com/trustwallet/assets-go-libs/file"
|
|
|
|
"github.com/trustwallet/assets-go-libs/image"
|
|
|
|
"github.com/trustwallet/assets-go-libs/path"
|
|
|
|
"github.com/trustwallet/assets-go-libs/validation/info"
|
2022-01-16 14:13:01 +00:00
|
|
|
"github.com/trustwallet/assets-go-libs/validation/tokenlist"
|
2021-12-19 21:39:53 +00:00
|
|
|
"github.com/trustwallet/assets/internal/config"
|
|
|
|
"github.com/trustwallet/go-libs/blockchain/binance"
|
|
|
|
"github.com/trustwallet/go-libs/blockchain/binance/explorer"
|
|
|
|
assetlib "github.com/trustwallet/go-primitives/asset"
|
|
|
|
"github.com/trustwallet/go-primitives/coin"
|
|
|
|
"github.com/trustwallet/go-primitives/numbers"
|
|
|
|
"github.com/trustwallet/go-primitives/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
assetsPage = 1
|
|
|
|
assetsRows = 1000
|
|
|
|
marketPairsLimit = 1000
|
|
|
|
tokensListLimit = 10000
|
|
|
|
|
|
|
|
twLogoURL = "https://trustwallet.com/assets/images/favicon.png"
|
|
|
|
timestampFormat = "2006-01-02T15:04:05.000000"
|
2022-01-09 20:30:49 +00:00
|
|
|
|
|
|
|
activeStatus = "active"
|
2021-12-19 21:39:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Service) UpdateBinanceTokens() error {
|
|
|
|
explorerClient := explorer.InitClient(config.Default.ClientURLs.Binance.Explorer, nil)
|
|
|
|
|
|
|
|
bep2AssetList, err := explorerClient.FetchBep2Assets(assetsPage, assetsRows)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dexClient := binance.InitClient(config.Default.ClientURLs.Binance.Dex, "", nil)
|
|
|
|
|
|
|
|
marketPairs, err := dexClient.FetchMarketPairs(marketPairsLimit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-22 16:06:46 +00:00
|
|
|
tokenList, err := dexClient.FetchTokens(tokensListLimit)
|
2021-12-19 21:39:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
chain, err := types.GetChainFromAssetType(string(types.BEP2))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fetchMissingAssets(chain, bep2AssetList.AssetInfoList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-22 16:06:46 +00:00
|
|
|
tokens, err := generateTokenList(marketPairs, tokenList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-09 20:30:49 +00:00
|
|
|
sortTokens(tokens)
|
|
|
|
|
2021-12-22 16:06:46 +00:00
|
|
|
return createTokenListJSON(chain, tokens)
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func fetchMissingAssets(chain coin.Coin, assets []explorer.Bep2Asset) error {
|
|
|
|
for _, a := range assets {
|
|
|
|
if a.AssetImg == "" || a.Decimals == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-12-20 15:03:08 +00:00
|
|
|
assetLogoPath := path.GetAssetLogoPath(chain.Handle, a.Asset)
|
2022-01-20 13:14:34 +00:00
|
|
|
if fileLib.Exists(assetLogoPath) {
|
2021-12-19 21:39:53 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := createLogo(assetLogoPath, a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := createInfoJSON(chain, a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createLogo(assetLogoPath string, a explorer.Bep2Asset) error {
|
2021-12-20 15:03:08 +00:00
|
|
|
err := fileLib.CreateDirPath(assetLogoPath)
|
2021-12-19 21:39:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-20 15:03:08 +00:00
|
|
|
return image.CreatePNGFromURL(a.AssetImg, assetLogoPath)
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createInfoJSON(chain coin.Coin, a explorer.Bep2Asset) error {
|
2022-01-20 13:14:34 +00:00
|
|
|
explorerURL, err := coin.GetCoinExploreURL(chain, a.Asset, "")
|
2021-12-19 21:39:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
assetType := string(types.BEP2)
|
|
|
|
website := ""
|
|
|
|
description := "-"
|
2022-01-09 20:30:49 +00:00
|
|
|
status := activeStatus
|
2021-12-19 21:39:53 +00:00
|
|
|
|
|
|
|
assetInfo := info.AssetModel{
|
|
|
|
Name: &a.Name,
|
|
|
|
Type: &assetType,
|
|
|
|
Symbol: &a.MappedAsset,
|
|
|
|
Decimals: &a.Decimals,
|
|
|
|
Website: &website,
|
|
|
|
Description: &description,
|
|
|
|
Explorer: &explorerURL,
|
|
|
|
Status: &status,
|
|
|
|
ID: &a.Asset,
|
|
|
|
}
|
|
|
|
|
2021-12-20 15:03:08 +00:00
|
|
|
assetInfoPath := path.GetAssetInfoPath(chain.Handle, a.Asset)
|
2021-12-19 21:39:53 +00:00
|
|
|
|
2021-12-20 15:03:08 +00:00
|
|
|
return fileLib.CreateJSONFile(assetInfoPath, &assetInfo)
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 14:13:01 +00:00
|
|
|
func createTokenListJSON(chain coin.Coin, tokens []tokenlist.Token) error {
|
2021-12-20 15:03:08 +00:00
|
|
|
tokenListPath := path.GetTokenListPath(chain.Handle)
|
2021-12-19 21:39:53 +00:00
|
|
|
|
2022-01-16 14:13:01 +00:00
|
|
|
var oldTokenList tokenlist.Model
|
2021-12-22 16:06:46 +00:00
|
|
|
err := fileLib.ReadJSONFile(tokenListPath, &oldTokenList)
|
2021-12-19 21:39:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.DeepEqual(oldTokenList.Tokens, tokens) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-22 16:06:46 +00:00
|
|
|
if len(tokens) == 0 {
|
|
|
|
return nil
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 16:06:46 +00:00
|
|
|
log.Debugf("Tokenlist: list with %d tokens and %d pairs written to %s.",
|
|
|
|
len(tokens), countTotalPairs(tokens), tokenListPath)
|
|
|
|
|
2022-01-16 14:13:01 +00:00
|
|
|
return fileLib.CreateJSONFile(tokenListPath, &tokenlist.Model{
|
2021-12-22 16:06:46 +00:00
|
|
|
Name: fmt.Sprintf("Trust Wallet: %s", coin.Coins[chain.ID].Name),
|
|
|
|
LogoURI: twLogoURL,
|
|
|
|
Timestamp: time.Now().Format(timestampFormat),
|
|
|
|
Tokens: tokens,
|
2022-01-16 14:13:01 +00:00
|
|
|
Version: tokenlist.Version{Major: oldTokenList.Version.Major + 1},
|
2021-12-22 16:06:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-16 14:13:01 +00:00
|
|
|
func countTotalPairs(tokens []tokenlist.Token) int {
|
2021-12-22 16:06:46 +00:00
|
|
|
var counter int
|
|
|
|
for _, token := range tokens {
|
|
|
|
counter += len(token.Pairs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return counter
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 14:13:01 +00:00
|
|
|
func sortTokens(tokens []tokenlist.Token) {
|
2021-12-19 21:39:53 +00:00
|
|
|
sort.Slice(tokens, func(i, j int) bool {
|
|
|
|
if len(tokens[i].Pairs) != len(tokens[j].Pairs) {
|
|
|
|
return len(tokens[i].Pairs) > len(tokens[j].Pairs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokens[i].Address < tokens[j].Address
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, token := range tokens {
|
|
|
|
sort.Slice(token.Pairs, func(i, j int) bool {
|
|
|
|
return token.Pairs[i].Base < token.Pairs[j].Base
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 14:13:01 +00:00
|
|
|
func generateTokenList(marketPairs []binance.MarketPair, tokenList binance.Tokens) ([]tokenlist.Token, error) {
|
2021-12-19 21:39:53 +00:00
|
|
|
if len(marketPairs) < 5 {
|
|
|
|
return nil, fmt.Errorf("no markets info is returned from Binance DEX: %d", len(marketPairs))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tokenList) < 5 {
|
|
|
|
return nil, fmt.Errorf("no tokens info is returned from Binance DEX: %d", len(tokenList))
|
|
|
|
}
|
|
|
|
|
2022-01-16 14:13:01 +00:00
|
|
|
pairsMap := make(map[string][]tokenlist.Pair)
|
2021-12-19 21:39:53 +00:00
|
|
|
pairsList := make(map[string]struct{})
|
|
|
|
tokensMap := make(map[string]binance.Token)
|
|
|
|
|
|
|
|
for _, token := range tokenList {
|
|
|
|
tokensMap[token.Symbol] = token
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, marketPair := range marketPairs {
|
2022-01-09 20:30:49 +00:00
|
|
|
if !isTokenExistOrActive(marketPair.BaseAssetSymbol) || !isTokenExistOrActive(marketPair.QuoteAssetSymbol) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenSymbol := marketPair.QuoteAssetSymbol
|
2021-12-19 21:39:53 +00:00
|
|
|
|
2022-01-09 20:30:49 +00:00
|
|
|
if val, exists := pairsMap[tokenSymbol]; exists {
|
2021-12-19 21:39:53 +00:00
|
|
|
val = append(val, getPair(marketPair))
|
2022-01-09 20:30:49 +00:00
|
|
|
pairsMap[tokenSymbol] = val
|
2021-12-19 21:39:53 +00:00
|
|
|
} else {
|
2022-01-16 14:13:01 +00:00
|
|
|
pairsMap[tokenSymbol] = []tokenlist.Pair{getPair(marketPair)}
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pairsList[marketPair.BaseAssetSymbol] = struct{}{}
|
|
|
|
pairsList[marketPair.QuoteAssetSymbol] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2022-01-16 14:13:01 +00:00
|
|
|
tokenItems := make([]tokenlist.Token, 0, len(pairsList))
|
2021-12-19 21:39:53 +00:00
|
|
|
|
|
|
|
for pair := range pairsList {
|
|
|
|
token := tokensMap[pair]
|
|
|
|
|
2022-01-16 14:13:01 +00:00
|
|
|
var pairs []tokenlist.Pair
|
2021-12-19 21:39:53 +00:00
|
|
|
pairs, exists := pairsMap[token.Symbol]
|
|
|
|
if !exists {
|
2022-01-16 14:13:01 +00:00
|
|
|
pairs = make([]tokenlist.Pair, 0)
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 14:13:01 +00:00
|
|
|
tokenItems = append(tokenItems, tokenlist.Token{
|
2021-12-19 21:39:53 +00:00
|
|
|
Asset: getAssetIDSymbol(token.Symbol, coin.Coins[coin.BINANCE].Symbol, coin.BINANCE),
|
2022-01-07 11:45:16 +00:00
|
|
|
Type: getTokenType(token.Symbol, coin.Coins[coin.BINANCE].Symbol, types.BEP2),
|
2021-12-19 21:39:53 +00:00
|
|
|
Address: token.Symbol,
|
|
|
|
Name: token.Name,
|
|
|
|
Symbol: token.OriginalSymbol,
|
|
|
|
Decimals: coin.Coins[coin.BINANCE].Decimals,
|
|
|
|
LogoURI: getLogoURI(token.Symbol, coin.Coins[coin.BINANCE].Handle, coin.Coins[coin.BINANCE].Symbol),
|
|
|
|
Pairs: pairs,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokenItems, nil
|
|
|
|
}
|
|
|
|
|
2022-01-09 20:30:49 +00:00
|
|
|
func isTokenExistOrActive(symbol string) bool {
|
|
|
|
if symbol == coin.Coins[coin.BINANCE].Symbol {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
assetPath := path.GetAssetInfoPath(coin.Coins[coin.BINANCE].Handle, symbol)
|
|
|
|
|
|
|
|
infoFile, err := os.Open(assetPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("asset file open error: %s", err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
if _, err = buf.ReadFrom(infoFile); err != nil {
|
|
|
|
log.Debugf("buffer read error: %s", err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
infoFile.Close()
|
|
|
|
|
|
|
|
var infoAsset info.AssetModel
|
|
|
|
err = json.Unmarshal(buf.Bytes(), &infoAsset)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("json unmarshalling error: %s", err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if infoAsset.GetStatus() != activeStatus {
|
|
|
|
log.Debugf("asset status [%s] is not active", symbol)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-01-16 14:13:01 +00:00
|
|
|
func getPair(marketPair binance.MarketPair) tokenlist.Pair {
|
|
|
|
return tokenlist.Pair{
|
2021-12-19 21:39:53 +00:00
|
|
|
Base: getAssetIDSymbol(marketPair.BaseAssetSymbol, coin.Coins[coin.BINANCE].Symbol, coin.BINANCE),
|
|
|
|
LotSize: strconv.FormatInt(numbers.ToSatoshi(marketPair.LotSize), 10),
|
|
|
|
TickSize: strconv.FormatInt(numbers.ToSatoshi(marketPair.TickSize), 10),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAssetIDSymbol(tokenID string, nativeCoinID string, coinType uint) string {
|
|
|
|
if tokenID == nativeCoinID {
|
|
|
|
return assetlib.BuildID(coinType, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
return assetlib.BuildID(coinType, tokenID)
|
|
|
|
}
|
|
|
|
|
2022-01-07 11:45:16 +00:00
|
|
|
func getTokenType(symbol string, nativeCoinSymbol string, tokenType types.TokenType) types.TokenType {
|
2021-12-19 21:39:53 +00:00
|
|
|
if symbol == nativeCoinSymbol {
|
2022-01-07 11:45:16 +00:00
|
|
|
return types.Coin
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tokenType
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLogoURI(id, githubChainFolder, nativeCoinSymbol string) string {
|
|
|
|
if id == nativeCoinSymbol {
|
2021-12-20 15:03:08 +00:00
|
|
|
return path.GetChainLogoURL(config.Default.URLs.TWAssetsApp, githubChainFolder)
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 15:03:08 +00:00
|
|
|
return path.GetAssetLogoURL(config.Default.URLs.TWAssetsApp, githubChainFolder, id)
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|