Launch SPL Tokens on Solana with Pump.fun
This Python script provides functionality to launch custom SPL tokens on the Solana blockchain through Pump.fun, a platform that enables the creation and management of tokens.
Key Components
Metadata Upload
Transaction Creation
Token Launch Process
Detailed Breakdown
Uploading Token Metadata
The
_upload_metadata
function handles the uploading of token metadata to the IPFS through Pump.fun. The metadata includes the token's name, ticker, description, and image URL. If additional options (such as social media links) are provided, they are included in the metadata upload.Copy
Form Data: This method forms the required data for metadata upload, which includes the token’s basic information, image, and any optional social media URLs.
Image Download: The image for the token is downloaded from the provided URL.
IPFS Upload: The metadata and image are uploaded to Pump.fun's IPFS endpoint.
Response: On successful upload, it returns the metadata response, including the metadata URI.
Creating the Token Transaction
The
_create_token_transaction
function prepares a transaction for minting the token on the Solana blockchain through Pump.fun. The transaction includes the token's metadata and minting information.Copy
Transaction Payload: The payload for the transaction is built using the agent’s wallet address, mint keypair, and metadata URI.
Transaction Request: A request is made to the Pump.fun API to create the transaction.
Serialized Transaction: The transaction is returned as serialized data that can be signed and sent to the network.
Launching the Token
The
launch_pumpfun_token
method orchestrates the process of launching a token. It includes uploading metadata, creating the transaction, and then signing and sending the transaction to the Solana network.Copy
Mint Keypair: A new mint keypair is generated for the token.
Metadata Upload: Calls
_upload_metadata
to upload the token's metadata to IPFS.Transaction Creation: Calls
_create_token_transaction
to create the token transaction.Transaction Signing: The transaction is signed using the mint keypair and sent to the Solana network via
sign_and_send_transaction
.
Result: The method returns a
TokenLaunchResult
containing the transaction signature, mint address, and metadata URI.
Helper Functions
fix_asyncio_for_windows()
This function is used to ensure compatibility of asynchronous tasks when running on Windows. It fixes an issue with the default event loop on Windows machines that might otherwise result in anasyncio
exception.sign_and_send_transaction()
This function handles signing and sending the token transaction to the Solana blockchain. It is critical for the final step in the token launch process.
Example Usage
Below is an example of how to use the PumpfunTokenManager
to launch a new token:
Copy
Error Handling
The code handles various exceptions during the token launch process:
Metadata Upload: If the metadata upload fails (e.g., image download or IPFS upload), it raises an error.
Transaction Creation: If creating the transaction via the Pump.fun API fails, an exception is raised with the relevant error message.
Transaction Signing: If the transaction fails during signing or submission to the Solana network, the exception is caught, and an appropriate error message is logged.
Example of error handling:
Copy
Key Features
IPFS Metadata Upload: Upload token metadata (name, symbol, description, image) to IPFS.
Transaction Creation: Create and serialize the minting transaction for a new token.
Customizable Options: Allow customization of initial liquidity, slippage, and priority fees for the token launch.
Solana Integration: Full integration with Solana blockchain for minting and token management.
Error Logging: Detailed logging and error handling throughout the process.
Last updated