blocto / solana-go-sdk

Solana Golang SDK
https://blocto.github.io/solana-go-sdk/
MIT License
349 stars 86 forks source link

Error 32002 when creating associated token account #170

Open Moses-Alero opened 1 month ago

Moses-Alero commented 1 month ago

I keep encountering this error whenever I try to send transactions for creating associated token account

Error sending transaction: {
"code":-32002,
"message":"Transaction simulation failed: Error processing Instruction 0: incorrect program id for instruction",
"data":{
   "accounts":null,
   "err":{"InstructionError":[0,"IncorrectProgramId"]},
   "innerInstructions":null,
   "logs":["Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL invoke [1]",
   "Program log: Create",
   "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]",
   "Program log: Instruction: GetAccountDataSize",
   "Program log: Error: IncorrectProgramId",
   "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 884 of 194378 compute units",
   "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA failed: incorrect program id for instruction",
   "Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL consumed 6506 of 200000 compute units",
   "Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL failed: incorrect program id for instruction"],
   "returnData":null,
   "unitsConsumed":6506
  }
}

Here is the code for the transaction


func CreateTokenAccount(mintPubKey, userPubKey common.PublicKey) string {
    // create associated token account
    c := client.NewClient(rpc.DevnetRPCEndpoint)

    ata, _, err := common.FindAssociatedTokenAddress(userPubKey, mintPubKey)
    if err != nil {
        log.Fatal("Error finding associated token address: ", err)
    }

    fmt.Println("ata: ", ata.ToBase58())
    //send tx
    resp, err := c.GetLatestBlockhash(context.Background())
    if err != nil {
        log.Fatalf("Error getting latest blockhash: %v", err)
    }

    //create associated token account transaction instruction

    tx, err := types.NewTransaction(types.NewTransactionParam{
        Message: types.NewMessage(types.NewMessageParam{
            FeePayer:        feePayer.PublicKey,
            RecentBlockhash: resp.Blockhash,
            Instructions: []types.Instruction{
                associated_token_account.Create(associated_token_account.CreateParam{
                    Funder:                 feePayer.PublicKey,
                    Owner:                  userPubKey,
                    Mint:                   mintPubKey,
                    AssociatedTokenAccount: ata,
                }),
            },
        }),
        Signers: []types.Account{feePayer},
    })

    if err != nil {
        log.Fatalf("Error creating transaction: %v", err)
    }

    //send tx
    txhash, err := c.SendTransaction(context.Background(), tx)
    if err != nil {
        fmt.Printf("Error sending transaction: %v", err.Error())
    }

    fmt.Println("Transaction Hash: ", txhash)
    return ata.String()
}
yihau commented 1 month ago

could you share the userPubkey and mintPubKey you're using?

Moses-Alero commented 1 month ago

@yihau sure

mintPubKey: 2oPGwbo9zQMhDP9dKmVDtyUKDaAPVepJnw1fD85J1p1Q userPubKey: CxT4fH6rTMSzN7dDUnoSMEreA3fG9UwRGtUx4FyJNeRV

yihau commented 1 month ago

sorry, I couldn't reproduce your error. I tested your code and looks fine.

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/blocto/solana-go-sdk/client"
    "github.com/blocto/solana-go-sdk/common"
    "github.com/blocto/solana-go-sdk/program/associated_token_account"
    "github.com/blocto/solana-go-sdk/rpc"
    "github.com/blocto/solana-go-sdk/types"
)

// FUarP2p5EnxD66vVDL4PWRoWMzA56ZVHG24hpEDFShEz
var feePayer, _ = types.AccountFromBase58("4TMFNY9ntAn3CHzguSAvDNLPRoQTaK3sWbQQXdDXaE6KWRBLufGL6PJdsD2koiEe3gGmMdRK3aAw7sikGNksHJrN")

func main() {
    mintPubKey := common.PublicKeyFromString("2oPGwbo9zQMhDP9dKmVDtyUKDaAPVepJnw1fD85J1p1Q")
    userPubKey := common.PublicKeyFromString("CxT4fH6rTMSzN7dDUnoSMEreA3fG9UwRGtUx4FyJNeRV")
    CreateTokenAccount(mintPubKey, userPubKey)
}

func CreateTokenAccount(mintPubKey, userPubKey common.PublicKey) string {
    // create associated token account
    c := client.NewClient(rpc.DevnetRPCEndpoint)

    ata, _, err := common.FindAssociatedTokenAddress(userPubKey, mintPubKey)
    if err != nil {
        log.Fatal("Error finding associated token address: ", err)
    }

    fmt.Println("ata: ", ata.ToBase58())
    //send tx
    resp, err := c.GetLatestBlockhash(context.Background())
    if err != nil {
        log.Fatalf("Error getting latest blockhash: %v", err)
    }

    //create associated token account transaction instruction
    tx, err := types.NewTransaction(types.NewTransactionParam{
        Message: types.NewMessage(types.NewMessageParam{
            FeePayer:        feePayer.PublicKey,
            RecentBlockhash: resp.Blockhash,
            Instructions: []types.Instruction{
                associated_token_account.Create(associated_token_account.CreateParam{
                    Funder:                 feePayer.PublicKey,
                    Owner:                  userPubKey,
                    Mint:                   mintPubKey,
                    AssociatedTokenAccount: ata,
                }),
            },
        }),
        Signers: []types.Account{feePayer},
    })

    if err != nil {
        log.Fatalf("Error creating transaction: %v", err)
    }

    //send tx
    txhash, err := c.SendTransaction(context.Background(), tx)
    if err != nil {
        fmt.Printf("Error sending transaction: %v", err.Error())
    }

    fmt.Println("Transaction Hash: ", txhash)
    return ata.String()
}

could you create another mint, try this one, and see how it goes?

(btw, the tx: https://explorer.solana.com/tx/5Bb1qh7BVT898usL6JhgGoEKrb4oFVV31HQhT1J8SVS2LydDeVL5rXv9QfLvmP9oAETzXpLUReJC4qEj9QVCqvAT?cluster=devnet)

Moses-Alero commented 1 month ago

Alright , Thanks I'll try again maybe I'm doing something wrong.