2020-09-14 20:52:19 -07:00
|
|
|
import base64
|
|
|
|
|
|
|
|
from solana.publickey import PublicKey
|
|
|
|
from solana.rpc.api import Client
|
2021-08-10 07:06:07 -07:00
|
|
|
from spl.token.constants import WRAPPED_SOL_MINT
|
2020-09-14 20:52:19 -07:00
|
|
|
|
2020-10-01 08:37:32 -07:00
|
|
|
from pyserum._layouts.market import MINT_LAYOUT
|
2020-09-19 23:01:44 -07:00
|
|
|
|
2020-09-14 20:52:19 -07:00
|
|
|
|
2020-09-16 22:11:39 -07:00
|
|
|
def load_bytes_data(addr: PublicKey, conn: Client):
|
|
|
|
res = conn.get_account_info(addr)
|
2020-09-14 20:52:19 -07:00
|
|
|
if ("result" not in res) or ("value" not in res["result"]) or ("data" not in res["result"]["value"]):
|
|
|
|
raise Exception("Cannot load byte data.")
|
|
|
|
data = res["result"]["value"]["data"][0]
|
|
|
|
return base64.decodebytes(data.encode("ascii"))
|
2020-09-19 23:01:44 -07:00
|
|
|
|
|
|
|
|
|
|
|
def get_mint_decimals(conn: Client, mint_pub_key: PublicKey) -> int:
|
|
|
|
"""Get the mint decimals for a token mint"""
|
2020-09-24 08:11:00 -07:00
|
|
|
if mint_pub_key == WRAPPED_SOL_MINT:
|
|
|
|
return 9
|
|
|
|
|
2020-09-19 23:01:44 -07:00
|
|
|
bytes_data = load_bytes_data(mint_pub_key, conn)
|
|
|
|
return MINT_LAYOUT.parse(bytes_data).decimals
|