Skip to content

Instantly share code, notes, and snippets.

@billythedummy
Last active February 17, 2025 16:00
Show Gist options
  • Save billythedummy/958aa92dcff3ea3f1c364dd84144bf1e to your computer and use it in GitHub Desktop.
Save billythedummy/958aa92dcff3ea3f1c364dd84144bf1e to your computer and use it in GitHub Desktop.
create-tokenkeg-mint-with-seed.py
'''
Tested with:
- solana==0.34.3
- solders==0.21.0
Pre-requisites:
- grind out desired pubkey with base set to BASE and owner set to tokenkeg program
using a tool like https://github.com/cavemanloverboy/vanity
- set PAYER const to path to signing keypair file
- set BASE const to path to signing keypair file that the pubkey is derived from
- set MINT to desired pubkey and SEED to found seed
- set other consts - DECIMALS, MINT_AUTHORITY, FREEZE_AUTHORITY
'''
from solana.rpc.api import Client
from solders.compute_budget import set_compute_unit_limit, set_compute_unit_price
from solders.instruction import AccountMeta, Instruction
from solders.keypair import Keypair
from solders.pubkey import Pubkey
from solders.system_program import create_account_with_seed, CreateAccountWithSeedParams
from solders.token import ID as TOKENKEG_ID
from solders.transaction import Transaction
BASE = "TODO-PATH-TO-BASE-KP.JSON"
PAYER = "TODO-PATH-TO-PAYER-KP.JSON"
MINT = Pubkey.from_string("TODO")
SEED = "bytes([...]).decode('utf-8')" # [...] is array output by vanity
DECIMALS = 9 # TODO
MINT_AUTHORITY = Pubkey.from_string("TODO")
FREEZE_AUTHORITY = None # TODO
RPC = "https://api.mainnet-beta.solana.com"
PRIO_FEES_LAMPORTS = 5000
CU_LIMIT = 5000
# Might change with network rent param changes
MINT_RENT_EXEMPT_LAMPORTS = 1_461_600
MINT_ACC_SIZE = 82
def load_keypair(fname):
with open(fname, "r") as f:
return Keypair.from_json(f.read())
def initialize_mint2_ix(mint, decimals, mint_authority, freeze_authority_opt):
return Instruction(
TOKENKEG_ID,
initialize_mint2_ix_data(decimals, mint_authority, freeze_authority_opt),
[AccountMeta(mint, False, True)]
)
def initialize_mint2_ix_data(decimals, mint_authority, freeze_authority_opt):
ba = bytearray([20])
ba.extend(bytes([decimals]))
ba.extend(bytes(mint_authority))
if freeze_authority_opt:
ba.extend(bytes([1]))
ba.extend(bytes(freeze_authority_opt))
else:
ba.extend(bytes([0]))
return bytes(ba)
if __name__ == "__main__":
rpc = Client(RPC)
payer = load_keypair(PAYER)
base = load_keypair(BASE)
rbh = rpc.get_latest_blockhash().value.blockhash
tx = Transaction.new_signed_with_payer(
[
set_compute_unit_price(round(1_000_000 * PRIO_FEES_LAMPORTS / CU_LIMIT)),
set_compute_unit_limit(CU_LIMIT),
create_account_with_seed(CreateAccountWithSeedParams(
from_pubkey=payer.pubkey(),
to_pubkey=MINT,
base=base.pubkey(),
seed=SEED,
lamports=MINT_RENT_EXEMPT_LAMPORTS,
space=MINT_ACC_SIZE,
owner=TOKENKEG_ID,
)),
initialize_mint2_ix(MINT, DECIMALS, MINT_AUTHORITY, FREEZE_AUTHORITY)
],
payer.pubkey(),
[payer, base],
rbh,
)
tx_bytes = bytes(tx)
sig = rpc.send_raw_transaction(tx_bytes).value
print(f"{sig}")
rpc.confirm_transaction(sig, commitment="confirmed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment