Receive SPL details using Python Api
In this article, we will study the Python API provided by SOLANA LABS to obtain SPL tokens details, including current price, common delivery and mint address.
Prerequisites
- Install the required libraries:
Python-Solana
and web3
- Set solana knot or use a local development environment with theSolana-SDK
package
Using Python Api
Python API for SPL Token Details is provided from the packSolana_sdk, which includes module
spl_taken.py. Here is an exemplary fragment of code to get started:
Python
By Solana_sdk Import Account_Info, Mint_Info, SPL_TOKEN_DETAILS
Import Web3
Set the Solana knot or use a local development environment
W3 = Web3.Web3 (WebSocket: // Localhost: 8899)
Get details about SPL’s tokens for a given address and address of Solana’s tokena
SPL_TOKEN_DETAILS_ADDRESS = ‘0X … your-Solana-Ten-Address …’
Replace with your address
Def get_spl_token_details ():
SPL_TOKEN_DETAILS = SPL_TOKEN_DETAILS SPL_TOKEN_ADDRESS SPL_TOKEN_DETAILS_ADDRESS
Mint_Address = Account_Info.spl_Mint_Info (SPL_TOKEN_DETAILS.ADDRESS) .account_Info [‘address’]
Current_price = Account_Info.Spl_current_Price (Spl_token_Details.Address)
Total_supply = Account_Info.spl_total_supply (SPL_TOKEN_DETAILS.ADDRESS)
Return {
‘Mintaddress’: Mint_Address,
‘CurrentPrice’: Current_Price,
‘Outalsupply’: Total_supply
}
Get details about SPL tokens
SPL_TOKEN_DETAILS = Get_SPL_TOKEN_DETAILS ()
Print (SPL_TOKEN_DETAILS)
`
Explanation
The code fragment above uses the Account_info
module to retrieve the mint address associated with the given address of SPL. It then uses the spl_current_price
andspl_total_supply
to extract the current price of the marker and the total delivery, respectively.
Using a client of Web3 API
As an alternative, you can use a web3 API client as Web3.py-Solana-Cient
orPysolana
to get details about SPL tokens. Here is an example fragment of code:
`Python
By Solana_sdk Import Account_Info, Mint_Info, SPL_TOKEN_DETAILS
Import Solana_Client
Set the Solana knot or use a local development environment
Customer = Solana_Client.Solanaclient ()
Get details about SPL’s tokens for a given address and address of Solana’s tokena
SPL_TOKEN_DETAILS_ADDRESS = ‘0X … your-Solana-Ten-Address …’
Replace with your address
Def get_spl_token_details ():
SPL_TOKEN_DETAILS = SPL_TOKEN_DETAILS SPL_TOKEN_ADDRESS SPL_TOKEN_DETAILS_ADDRESS
Mint_Address = Account_Info.spl_Mint_Info (SPL_TOKEN_DETAILS.ADDRESS) .account_Info [‘address’]
Current_price = Account_Info.Spl_current_Price (Spl_token_Details.Address)
Total_supply = Account_Info.spl_total_supply (SPL_TOKEN_DETAILS.ADDRESS)
Return {
‘Mintaddress’: Mint_Address,
‘CurrentPrice’: Current_Price,
‘Outalsupply’: Total_supply
}
Get details about SPL tokens
SPL_TOKEN_DETAILS = Get_SPL_TOKEN_DETAILS ()
Print (SPL_TOKEN_DETAILS)
`
Note
The above examples use a local development environment with the SOLANA-SDK package. For the production environments, you need to replace the address localhost: 8899
with the end point of your Solana knot.
I hope this helps! Tell me if you have any questions or need additional help.