Buy Tokens

Example code that would purchase .1 sol worth of "MOGULMONEY"

from solders.pubkey import Pubkey
from solana.rpc.api import Client
from solders.keypair import Keypair

import solcoin.buy_tokens as buy

PUBLIC_KEY = "your_account_pubkey_string" # ex:G3tmXiWmgnhhjb4N12YK7QgmaqtRaCRaL6i4nx2ueKwr
TOKEN_MINT = "token_mint_string" # ex:6oDn2PDvjtKYoWVp9cNNe1WCepjS8VQzhBRS8qmXpump
mint_pubkey = Pubkey.from_string(TOKEN_MINT)
client = Client("your_RPC_url") # ex:https://api.mainnet-beta.solana.com

tokensOrSolAmount = .1 # how many tokens or sol you want to purchase
tokensOrSol = 'sol' # either 'token' or 'sol', whichever unit you want to buy in
SLIPPAGE_PERCENT = 20
PRIORITY_FEE = .000001 

private_key_base58 = "private_key_base58_string" # your base58 private key string
payer_keypair = Keypair.from_base58_string(private_key_base58)


sig, status = buy.purchase_token(mint_pubkey, client, tokensOrSolAmount, tokensOrSol, SLIPPAGE_PERCENT, PUBLIC_KEY, payer_keypair, PRIORITY_FEE)

print(sig) # prints the signature of the transaction
print(status) # prints the current status of the transaction

  • PUBLIC_KEY = The public key of the account you are using to purchase the tokens, make sure this account has enough sol in it to cover fees and pay for rent

  • TOKEN_MINT = the mint pubkey of the token you are trying to purchase

  • client = the rpc client you are using, replace the rpc_url with your rpc provider's url

  • TokensOrSolAmount = the amount of tokens you want to purchase or sol you want to purchase; depends on the value of tokensOrSol

  • TokensOrSol = what unit you are buying either 'token' or 'sol'

  • SLIPPAGE_Percent = % price slippage for transaction to still proceed

  • PRIORITY_FEE = fee paid divided among 150k compute units

  • private_key_base58 = the private key string of the account you are using to purchase the tokens.

Last updated