michaelhly / solana-py

Solana Python SDK
https://michaelhly.github.io/solana-py
MIT License
979 stars 252 forks source link

`initialize_account` gives `Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA failed: incorrect program id for instruction` #448

Open abderraouf-belalia opened 1 month ago

abderraouf-belalia commented 1 month ago

Hello,

I am running the following script to create and mint an SPL Token.

from solders.keypair import Keypair
from solana.rpc.api import Client
from solana.transaction import Transaction
from solders.system_program import create_account, CreateAccountParams
from spl.token.instructions import (
    initialize_mint,
    InitializeMintParams,
    mint_to,
    MintToParams,
    initialize_account,
    InitializeAccountParams,
)
from solana.transaction import Transaction
from spl.token.constants import (
    TOKEN_PROGRAM_ID,
)

def token_to_decimals(amount: float | int) -> int:
    DECIMALS = 1000000000
    return int(amount * DECIMALS)

def create_spl_token():

    client = Client("https://api.devnet.solana.com")

    with open(
        "solana/keywbrUGnsKa9USpCsFwUZFENQ6jmrZHQfEz3Fw8wXJ.json"
    ) as file:
        keypair_json = file.read()

    payer: Keypair = Keypair.from_json(keypair_json)
    # Airdrop some SOL to the payer
    # client.request_airdrop(payer.pubkey(), 1000000000)  # 1 SOL

    # Generate a new keypair for the mint
    mint = Keypair()

    # Get the minimum lamports required for rent exemption
    mint_rent = client.get_minimum_balance_for_rent_exemption(82)

    # Create the mint account
    create_mint_params = CreateAccountParams(
        from_pubkey=payer.pubkey(),
        to_pubkey=mint.pubkey(),
        lamports=mint_rent.value,
        space=82,
        owner=TOKEN_PROGRAM_ID,
    )
    create_mint_account_ix = create_account(params=create_mint_params)

    # Initialize the mint
    init_mint_params = InitializeMintParams(
        program_id=TOKEN_PROGRAM_ID,
        mint=mint.pubkey(),
        decimals=9,
        mint_authority=payer.pubkey(),
        freeze_authority=payer.pubkey(),
    )
    init_mint_ix = initialize_mint(params=init_mint_params)

    # Create and send the transaction
    tx = Transaction().add(create_mint_account_ix, init_mint_ix)
    response = client.send_transaction(tx, payer, mint)

    print(f"Initialized mint account: {mint.pubkey()}")
    print(f"Transaction Signature: {response.value}")
    print("\n")

    # Create a token account for the payer
    token_account = Keypair()
    token_account_rent = client.get_minimum_balance_for_rent_exemption(165)

    create_account_params = CreateAccountParams(
        from_pubkey=payer.pubkey(),
        to_pubkey=token_account.pubkey(),
        lamports=token_account_rent.value,
        space=165,
        owner=TOKEN_PROGRAM_ID,
    )
    create_token_account_ix = create_account(params=create_account_params)

    init_account_params = InitializeAccountParams(
        program_id=TOKEN_PROGRAM_ID,
        account=token_account.pubkey(),
        mint=mint.pubkey(),
        owner=payer.pubkey(),
    )

    init_account_ix = initialize_account(params=init_account_params)

    tx = Transaction().add(create_token_account_ix, init_account_ix)
    response = client.send_transaction(tx, payer, token_account)

    print(f"Initialized token account: {token_account.pubkey()}")
    print(f"Transaction Signature: {response.value}")
    print("\n")

    mint_amount = 1000
    # Mint tokens to the account
    mint_to_ix = mint_to(
        MintToParams(
            program_id=TOKEN_PROGRAM_ID,
            mint=mint.pubkey(),
            dest=token_account.pubkey(),
            mint_authority=payer.pubkey(),
            amount=token_to_decimals(mint_amount),  # 1 token with 9 decimals
        )
    )

    # Create and send the transaction
    tx = Transaction().add(mint_to_ix)
    response = client.send_transaction(tx, payer)

    print(f"Token mint address: {mint.pubkey()}")
    print(f"Token account address: {token_account.pubkey()}")
    print(f"Minted {mint_amount} to {token_account.pubkey()}")
    print(f"Transaction Signature: {response.value}")
    print("\n")

if __name__ == "__main__":
    create_spl_token()

And I am getting the following output,

Initialized mint account: 9RUSwHt1oaJfwKDD5k3tM9s8JwKZViv9sQEkFfwVhqmC
Transaction Signature: 3n7SuR9JR4ACoCsE62DqgStXuppCeM6mP45NCPevyKvjtrbxmnxiF6h9QnFcK9PBpDTywBa8zqwKXDBjJBF2KNG9

Traceback (most recent call last):
  File "/home/ergod/git/app/run.py", line 125, in <module>
    create_spl_token()
  File "/home/ergod/git/app/run.py", line 95, in create_spl_token
    response = client.send_transaction(tx, payer, token_account)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ergod/.cache/pypoetry/virtualenvs/app-SIc2h3L_-py3.12/lib/python3.12/site-packages/solana/rpc/api.py", line 1059, in send_transaction
    txn_resp = self.send_raw_transaction(txn.serialize(), opts=opts_to_use)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ergod/.cache/pypoetry/virtualenvs/app-SIc2h3L_-py3.12/lib/python3.12/site-packages/solana/rpc/api.py", line 993, in send_raw_transaction
    resp = self._provider.make_request(body, SendTransactionResp)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ergod/.cache/pypoetry/virtualenvs/app-SIc2h3L_-py3.12/lib/python3.12/site-packages/solana/exceptions.py", line 43, in argument_decorator
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/ergod/.cache/pypoetry/virtualenvs/app-SIc2h3L_-py3.12/lib/python3.12/site-packages/solana/rpc/providers/http.py", line 49, in make_request
    return _parse_raw(raw, parser=parser)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ergod/.cache/pypoetry/virtualenvs/app-SIc2h3L_-py3.12/lib/python3.12/site-packages/solana/rpc/providers/core.py", line 97, in _parse_raw
    raise RPCException(parsed)
solana.rpc.core.RPCException: SendTransactionPreflightFailureMessage { message: "Transaction simulation failed: Error processing Instruction 1: incorrect program id for instruction", data: RpcSimulateTransactionResult(RpcSimulateTransactionResult { err: Some(InstructionError(1, IncorrectProgramId)), logs: Some(["Program 11111111111111111111111111111111 invoke [1]", "Program 11111111111111111111111111111111 success", "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [1]", "Program log: Instruction: InitializeAccount", "Program log: Error: IncorrectProgramId", "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2926 of 399850 compute units", "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA failed: incorrect program id for instruction"]), accounts: None, units_consumed: Some(3076), return_data: None, inner_instructions: None }) }

Can I get some help on this given that I am checked the tests and how to use the initialize_account and I still get an error.