Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to get the decimal of a mint address? #154

Open
yangyile1990 opened this issue Aug 18, 2023 · 2 comments
Open

how to get the decimal of a mint address? #154

yangyile1990 opened this issue Aug 18, 2023 · 2 comments

Comments

@yangyile1990
Copy link

{
"info": {
"amount": "404469000000",
"authority": "BEj8jY6a41Mfyqz7KvHFqhakQmWYY2xsSKfvPx2poB9d",
"destination": "5m3joKpY9vKqA2daofrmjnjAc45EdjURyRU3M85Q2VKE",
"source": "6pPARkoGAYDozZqwv36ZPFG3FBGRjErNL7Dg4MGEhuBu"
},
"type": "transfer"
}

then I get the mint address is:
bitsTXvEAEgZNfadxjJTEMqnaDwGpjk255YrRGfTyp8

then I use this:

curl --location 'https://api.mainnet-beta.solana.com' \
--header 'Content-Type: application/json' \
--data '{
    "method": "getAccountInfo",
    "jsonrpc": "2.0",
    "params": [
        "bitsTXvEAEgZNfadxjJTEMqnaDwGpjk255YrRGfTyp8",
        {
            "encoding": "jsonParsed"
        }
    ],
    "id": "e064a6e9-dca7-4258-8308-7f8a8162adf7"
}'

get:

{
    "jsonrpc": "2.0",
    "result": {
        "context": {
            "apiVersion": "1.14.23",
            "slot": 212136054
        },
        "value": {
            "data": {
                "parsed": {
                    "info": {
                        "decimals": 5,
                        "freezeAuthority": "C6PvxYgZ8dmAmiYHmpYR2kQaSUpPy5RdXj94kzEhre7f",
                        "isInitialized": true,
                        "mintAuthority": "C6PvxYgZ8dmAmiYHmpYR2kQaSUpPy5RdXj94kzEhre7f",
                        "supply": "906958523307480289"
                    },
                    "type": "mint"
                },
                "program": "spl-token",
                "space": 82
            },
            "executable": false,
            "lamports": 1461600,
            "owner": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
            "rentEpoch": 0
        }
    },
    "id": "e064a6e9-dca7-4258-8308-7f8a8162adf7"
}

while. I think should give the a function:

func GetMintDecimals(mintAddress string) (uint8, error){}

input: bitsTXvEAEgZNfadxjJTEMqnaDwGpjk255YrRGfTyp8
output: 5, nil

@yangyile1990
Copy link
Author

yangyile1990 commented Aug 18, 2023

my code is:

func GetMintDecimals(endpoint string, mintAddress string) (int, *utils_erc.Erc) {
	var resp = struct {
		Result struct {
			Value struct {
				Data struct {
					Parsed struct {
						Info struct {
							Decimals int `json:"decimals"`
						} `json:"info"`
						Type string `json:"type"`
					} `json:"parsed"`
				} `json:"data"`
			} `json:"value"`
		} `json:"result"`
	}{}
	response, err := uvchain_resty2.NewHttp().SetRetryCount(3).
		SetTimeout(time.Second*3).
		R().
		SetResult(&resp).
		SetHeader("Accept", "application/json").
		SetBody(map[string]any{
			"method":  "getAccountInfo",
			"jsonrpc": "2.0",
			"params": []any{
				mintAddress,
				map[string]any{
					"encoding": "jsonParsed",
				},
			},
			"id": uuid.New().String(),
		}).
		Post(endpoint)
	if err != nil {
		return 0, utils_erc.GetGen().NewErc(utils_erc.CODE_THIRD_REQ_ERROR, err.Error())
	}
	if response.StatusCode() != 200 {
		return 0, utils_erc.GetGen().NewErc(utils_erc.CODE_THIRD_REQ_ERROR, response.Status())
	}
	if typ := resp.Result.Value.Data.Parsed.Type; typ != "mint" {
		return 0, utils_erc.GetGen().NewErc(utils_erc.CODE_NODE_RESP_ERROR, "not mint", typ)
	}
	return resp.Result.Value.Data.Parsed.Info.Decimals, nil
}

while, I think, your sdk can give me the function.

@ryanCool
Copy link

@yangyile1990 below code should work

tokenPubKey := solana.MustPublicKeyFromBase58("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB")
accountInfo, err := solanaClient.GetTokenSupply(
	context.TODO(),
	tokenPubKey,
	rpc.CommitmentFinalized,
)

fmt.Println(accountInfo.Value.Decimals)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants