2022-01-14 14:02:39 +00:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
2022-01-27 23:59:22 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
libFile "github.com/trustwallet/assets-go-libs/file"
|
|
|
|
"github.com/trustwallet/assets-go-libs/path"
|
|
|
|
"github.com/trustwallet/assets-go-libs/validation/info"
|
|
|
|
"github.com/trustwallet/assets-go-libs/validation/tokenlist"
|
|
|
|
"github.com/trustwallet/go-primitives/asset"
|
|
|
|
"github.com/trustwallet/go-primitives/coin"
|
|
|
|
"github.com/trustwallet/go-primitives/types"
|
2022-01-14 14:02:39 +00:00
|
|
|
|
|
|
|
"github.com/trustwallet/assets/internal/config"
|
|
|
|
)
|
|
|
|
|
2022-01-27 23:59:22 +00:00
|
|
|
func CreateAssetInfoJSONTemplate(token string) error {
|
|
|
|
c, tokenID, err := asset.ParseID(token)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse token id: %v", err)
|
2022-01-14 14:02:39 +00:00
|
|
|
}
|
2022-01-27 23:59:22 +00:00
|
|
|
|
|
|
|
chain, ok := coin.Coins[c]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid token")
|
2022-01-14 14:02:39 +00:00
|
|
|
}
|
2022-01-27 23:59:22 +00:00
|
|
|
|
|
|
|
assetInfoPath := path.GetAssetInfoPath(chain.Handle, tokenID)
|
|
|
|
|
|
|
|
var emptyStr string
|
|
|
|
var emptyInt int
|
|
|
|
assetInfoModel := info.AssetModel{
|
|
|
|
Name: &emptyStr,
|
|
|
|
Type: &emptyStr,
|
|
|
|
Symbol: &emptyStr,
|
|
|
|
Decimals: &emptyInt,
|
|
|
|
Website: &emptyStr,
|
|
|
|
Explorer: &emptyStr,
|
|
|
|
Status: &emptyStr,
|
|
|
|
ID: &tokenID,
|
|
|
|
Links: []info.Link{
|
|
|
|
{
|
|
|
|
Name: &emptyStr,
|
|
|
|
URL: &emptyStr,
|
|
|
|
},
|
2022-01-14 14:02:39 +00:00
|
|
|
},
|
2022-01-27 23:59:22 +00:00
|
|
|
Tags: []string{""},
|
2022-01-14 14:02:39 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 23:59:22 +00:00
|
|
|
bytes, err := json.Marshal(&assetInfoModel)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to marshal json: %v", err)
|
2022-01-14 14:02:39 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 23:59:22 +00:00
|
|
|
f, err := libFile.CreateFileWithPath(assetInfoPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create file: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2022-01-14 14:02:39 +00:00
|
|
|
|
2022-01-27 23:59:22 +00:00
|
|
|
_, err = f.Write(bytes)
|
2022-01-14 14:02:39 +00:00
|
|
|
if err != nil {
|
2022-01-27 23:59:22 +00:00
|
|
|
return fmt.Errorf("failed to write bytes to file")
|
2022-01-14 14:02:39 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 23:59:22 +00:00
|
|
|
err = libFile.FormatJSONFile(assetInfoPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to format json file")
|
|
|
|
}
|
2022-01-14 14:02:39 +00:00
|
|
|
|
2022-01-27 23:59:22 +00:00
|
|
|
return nil
|
2022-01-14 14:02:39 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 10:00:13 +00:00
|
|
|
func AddTokenToTokenListJSON(chain coin.Coin, assetID, tokenID string, tokenListType path.TokenListType) error {
|
2022-01-27 23:59:22 +00:00
|
|
|
setup()
|
|
|
|
|
2022-01-28 10:00:13 +00:00
|
|
|
// Check for duplicates.
|
2022-01-27 23:59:22 +00:00
|
|
|
tokenListTypes := []path.TokenListType{path.TokenlistDefault, path.TokenlistExtended}
|
|
|
|
for _, t := range tokenListTypes {
|
|
|
|
tokenListPath := path.GetTokenListPath(chain.Handle, t)
|
|
|
|
var list tokenlist.Model
|
2022-01-28 10:00:13 +00:00
|
|
|
|
2022-01-27 23:59:22 +00:00
|
|
|
err := libFile.ReadJSONFile(tokenListPath, &list)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read data from %s: %w", tokenListPath, err)
|
|
|
|
}
|
2022-01-28 10:00:13 +00:00
|
|
|
|
2022-01-27 23:59:22 +00:00
|
|
|
for _, item := range list.Tokens {
|
2022-01-28 10:00:13 +00:00
|
|
|
if item.Asset == assetID {
|
2022-01-27 23:59:22 +00:00
|
|
|
return fmt.Errorf("duplicate asset, already exist in %s", tokenListPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var list tokenlist.Model
|
|
|
|
tokenListPath := path.GetTokenListPath(chain.Handle, tokenListType)
|
2022-01-28 10:00:13 +00:00
|
|
|
|
2022-01-27 23:59:22 +00:00
|
|
|
err := libFile.ReadJSONFile(tokenListPath, &list)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read data from %s: %w", tokenListPath, err)
|
2022-01-14 14:02:39 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 23:59:22 +00:00
|
|
|
assetInfo, err := getAssetInfo(chain, tokenID)
|
2022-01-14 14:02:39 +00:00
|
|
|
if err != nil {
|
2022-01-27 23:59:22 +00:00
|
|
|
return fmt.Errorf("failed to get token info: %w", err)
|
2022-01-14 14:02:39 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 23:59:22 +00:00
|
|
|
newToken := tokenlist.Token{
|
2022-01-28 10:00:13 +00:00
|
|
|
Asset: assetID,
|
2022-01-27 23:59:22 +00:00
|
|
|
Type: types.TokenType(*assetInfo.Type),
|
|
|
|
Address: *assetInfo.ID,
|
|
|
|
Name: *assetInfo.Name,
|
|
|
|
Symbol: *assetInfo.Symbol,
|
|
|
|
Decimals: uint(*assetInfo.Decimals),
|
|
|
|
LogoURI: path.GetAssetLogoURL(config.Default.URLs.AssetsApp, chain.Handle, tokenID),
|
|
|
|
}
|
|
|
|
list.Tokens = append(list.Tokens, newToken)
|
|
|
|
|
2022-06-14 12:57:34 +00:00
|
|
|
data, err := libFile.PrepareJSONData(&tokenlist.Model{
|
2022-01-27 23:59:22 +00:00
|
|
|
Name: fmt.Sprintf("Trust Wallet: %s", coin.Coins[chain.ID].Name),
|
|
|
|
LogoURI: config.Default.URLs.Logo,
|
|
|
|
Timestamp: time.Now().Format(config.Default.TimeFormat),
|
|
|
|
Tokens: list.Tokens,
|
|
|
|
Version: tokenlist.Version{Major: list.Version.Major + 1},
|
|
|
|
})
|
2022-06-14 12:57:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return libFile.CreateJSONFile(tokenListPath, data)
|
2022-01-14 14:02:39 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 23:59:22 +00:00
|
|
|
func getAssetInfo(chain coin.Coin, tokenID string) (*info.AssetModel, error) {
|
|
|
|
path := path.GetAssetInfoPath(chain.Handle, tokenID)
|
|
|
|
var assetModel info.AssetModel
|
2022-01-28 10:00:13 +00:00
|
|
|
|
2022-01-27 23:59:22 +00:00
|
|
|
err := libFile.ReadJSONFile(path, &assetModel)
|
2022-01-14 14:02:39 +00:00
|
|
|
if err != nil {
|
2022-01-27 23:59:22 +00:00
|
|
|
return nil, fmt.Errorf("failed to read data from info.json: %w", err)
|
2022-01-14 14:02:39 +00:00
|
|
|
}
|
2022-01-27 23:59:22 +00:00
|
|
|
|
|
|
|
return &assetModel, nil
|
2022-01-14 14:02:39 +00:00
|
|
|
}
|