|
| 1 | +from solana.publickey import PublicKey |
| 2 | +from solana.keypair import Keypair |
| 3 | +from solana.rpc.async_api import AsyncClient |
| 4 | +from solana.rpc.commitment import Confirmed |
| 5 | +from solana.rpc.types import TxOpts |
| 6 | +from solana.transaction import Transaction |
| 7 | +import solana.system_program as sys |
| 8 | + |
| 9 | +from spl.token.constants import TOKEN_PROGRAM_ID |
| 10 | +from spl.token.async_client import AsyncToken |
| 11 | +from spl.token._layouts import MINT_LAYOUT |
| 12 | +import spl.token.instructions as spl_token |
| 13 | + |
| 14 | + |
| 15 | +async def create_associated_token_account( |
| 16 | + client: AsyncClient, |
| 17 | + payer: Keypair, |
| 18 | + owner: PublicKey, |
| 19 | + mint: PublicKey |
| 20 | +) -> PublicKey: |
| 21 | + txn = Transaction() |
| 22 | + create_txn = spl_token.create_associated_token_account( |
| 23 | + payer=payer.public_key, owner=owner, mint=mint |
| 24 | + ) |
| 25 | + txn.add(create_txn) |
| 26 | + await client.send_transaction(txn, payer, opts=TxOpts(skip_confirmation=False, preflight_commitment=Confirmed)) |
| 27 | + return create_txn.keys[1].pubkey |
| 28 | + |
| 29 | + |
| 30 | +async def create_mint(client: AsyncClient, payer: Keypair, mint: Keypair, mint_authority: PublicKey): |
| 31 | + mint_balance = await AsyncToken.get_min_balance_rent_for_exempt_for_mint(client) |
| 32 | + print(f"Creating pool token mint {mint.public_key}") |
| 33 | + txn = Transaction() |
| 34 | + txn.add( |
| 35 | + sys.create_account( |
| 36 | + sys.CreateAccountParams( |
| 37 | + from_pubkey=payer.public_key, |
| 38 | + new_account_pubkey=mint.public_key, |
| 39 | + lamports=mint_balance, |
| 40 | + space=MINT_LAYOUT.sizeof(), |
| 41 | + program_id=TOKEN_PROGRAM_ID, |
| 42 | + ) |
| 43 | + ) |
| 44 | + ) |
| 45 | + txn.add( |
| 46 | + spl_token.initialize_mint( |
| 47 | + spl_token.InitializeMintParams( |
| 48 | + program_id=TOKEN_PROGRAM_ID, |
| 49 | + mint=mint.public_key, |
| 50 | + decimals=9, |
| 51 | + mint_authority=mint_authority, |
| 52 | + freeze_authority=None, |
| 53 | + ) |
| 54 | + ) |
| 55 | + ) |
| 56 | + await client.send_transaction( |
| 57 | + txn, payer, mint, opts=TxOpts(skip_confirmation=False, preflight_commitment=Confirmed)) |
0 commit comments