blocto / solana-go-sdk

Solana Golang SDK
https://blocto.github.io/solana-go-sdk/
MIT License
410 stars 109 forks source link

how to get the mint name of 7i5KKsX2weiTkry7jA4ZwSuXGhs5eJBEjY8vVxR4pfRx #147

Open yangyile1990 opened 1 year ago

yangyile1990 commented 1 year ago
func Test_getMintName_GMT(t *testing.T) {
    mintName, erc := getMintName(context.TODO(), caseSolana.client2, "7i5KKsX2weiTkry7jA4ZwSuXGhs5eJBEjY8vVxR4pfRx", "unknown")
    require.Nil(t, erc)
    t.Log(mintName)
}

func getMintName(ctx context.Context, client *client.Client, mintAddress string, defaultName string) (string, *cerror.CError) {
    metadata, ok, erc := getMintMeta(ctx, client, mintAddress)
    if erc != nil {
        return "", erc
    }
    if !ok {
        return defaultName, nil
    }
    return metadata.Data.Symbol, nil
}

func getMintMeta(ctx context.Context, client *client.Client, mintAddress string) (*token_metadata.Metadata, bool, *cerror.CError) {
    meta, err := token_metadata.GetTokenMetaPubkey(common.PublicKeyFromString(mintAddress))
    if err != nil {
        return nil, false, cerror.NewError(cerror.CODE_THIRD_REQ_ERROR, "获得 token meta 错误")
    }
    res, err := client.GetAccountInfo(ctx, meta.ToBase58())
    if err != nil {
        return nil, false, cerror.NewError(cerror.CODE_THIRD_REQ_ERROR, "获得 account 信息错误")
    }
    if res.Data == nil {
        return nil, false, nil
    }
    metadata, err := token_metadata.MetadataDeserialize(res.Data)
    if err != nil {
        return nil, false, cerror.NewError(cerror.CODE_THIRD_REQ_ERROR, "解析 metadata 错误", err.Error())
    }
    return &metadata, true, nil
}
yangyile1990 commented 1 year ago
curl 'https://api.solscan.io/token/meta?token=7i5KKsX2weiTkry7jA4ZwSuXGhs5eJBEjY8vVxR4pfRx&cluster=' \
  -H 'authority: api.solscan.io' \
  -H 'accept: application/json, text/plain, */*' \
  -H 'accept-language: zh,zh-CN;q=0.9' \
  -H 'if-none-match: W/"118-Fo0YcKx0NxCVMD0HmXG5Yx3H1mM"' \
  -H 'origin: https://solscan.io' \
  -H 'referer: https://solscan.io/' \
  -H 'sec-ch-ua: "Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  -H 'sec-fetch-dest: empty' \
  -H 'sec-fetch-mode: cors' \
  -H 'sec-fetch-site: same-site' \
  -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36' \
  --compressed
{"succcess":true,"data":{"symbol":"GMT","address":"7i5KKsX2weiTkry7jA4ZwSuXGhs5eJBEjY8vVxR4pfRx","name":"GMT","icon":"https://raw.githubusercontent.com/solana-labs/token-list/main/assets/mainnet/7i5KKsX2weiTkry7jA4ZwSuXGhs5eJBEjY8vVxR4pfRx/logo.png","decimals":9,"holder":392134}}% 
yihau commented 1 year ago

you can check https://github.com/solana-labs/token-list/blob/main/src/tokens/solana.tokenlist.json if you can't get a metadata from a mint.

yangyile1990 commented 1 year ago

thank you! you are the best go developer. very nice.