blocto / solana-go-sdk

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

Getting an error during the creation of Associated Token Account #23

Closed kvakhil95 closed 3 years ago

kvakhil95 commented 3 years ago

I have been trying to create an Associated Token Account using the code given in the documents. The feePayer and Alice are the Private Keys from a previously created account. My mint public key was created in a previous step and it has a few lamports as balance.

I am getting the below error:

send raw tx error, err: rpc response error: {"code":-32002,"message":"Transaction simulation failed: Attempt to debit an account but found no record of a prior credit.","data":{"accounts":null,"err":"AccountNotFound","logs":[]}}

Is there anything I am doing wrong?

yihau commented 3 years ago

I guess your fee payer(funder) don't have enough balance to create token account. It will cost you 0.00203928 SOL for a token account. also remember check your connection is the same

kvakhil95 commented 3 years ago

Just cross-checked. My fee payer has 7.998528400 SOL balance. And the connection is the same too.

yihau commented 3 years ago

would you share your code to me? not sure how you pass these accounts into instruction.

kvakhil95 commented 3 years ago

This is my code. It is taken from the documents you've created.

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/portto/solana-go-sdk/client"
    "github.com/portto/solana-go-sdk/client/rpc"
    "github.com/portto/solana-go-sdk/common"
    "github.com/portto/solana-go-sdk/program/assotokenprog"
    "github.com/portto/solana-go-sdk/types"
)

var feePayer = types.AccountFromPrivateKeyBytes([]byte{171, 198, 211, 210, 224, 159, 123, 194, 109, 67, 84, 90, 248, 134, 20, 25, 32, 185, 1, 216, 114, 253, 61, 71, 211, 88, 58, 242, 27, 146, 27, 137, 19, 110, 184, 252, 165, 125, 39, 128, 238, 132, 66, 123, 192, 136, 48, 33, 92, 107, 3, 44, 179, 4, 191, 172, 226, 212, 98, 118, 113, 163, 233, 105})

var alice = types.AccountFromPrivateKeyBytes([]byte{171, 198, 211, 210, 224, 159, 123, 194, 109, 67, 84, 90, 248, 134, 20, 25, 32, 185, 1, 216, 114, 253, 61, 71, 211, 88, 58, 242, 27, 146, 27, 137, 19, 110, 184, 252, 165, 125, 39, 128, 238, 132, 66, 123, 192, 136, 48, 33, 92, 107, 3, 44, 179, 4, 191, 172, 226, 212, 98, 118, 113, 163, 233, 105})

var mintPubkey = common.PublicKeyFromString("2JrfrWiKQTQKiRh6pUv8YogGwxpZcHNJTVW6E9JdFx5v")

func main() {
    c := client.NewClient(rpc.DevnetRPCEndpoint)

    ata, _, err := common.FindAssociatedTokenAddress(alice.PublicKey, mintPubkey)
    if err != nil {
        log.Fatalf("find ata error, err: %v", err)
    }
    fmt.Println("ata:", ata.ToBase58())

    res, err := c.GetRecentBlockhash(context.Background())
    if err != nil {
        log.Fatalf("get recent block hash error, err: %v\n", err)
    }
    rawTx, err := types.CreateRawTransaction(types.CreateRawTransactionParam{
        Instructions: []types.Instruction{
            assotokenprog.CreateAssociatedTokenAccount(
                feePayer.PublicKey,
                alice.PublicKey,
                mintPubkey,
            ),
        },
        Signers:         []types.Account{feePayer},
        FeePayer:        feePayer.PublicKey,
        RecentBlockHash: res.Blockhash,
    })
    if err != nil {
        log.Fatalf("generate tx error, err: %v\n", err)
    }

    txhash, err := c.SendRawTransaction(context.Background(), rawTx)
    if err != nil {
        log.Fatalf("send raw tx error, err: %v\n", err)
    }

    log.Println("txhash:", txhash)
}

I am able to generate the ATA following which I get the error.

ata: HXjpqhEnEMUC2AWTEs4uampaMPVPgKoFqDNR78GLcBQa
2021/10/04 15:36:03 send raw tx error, err: rpc response error: {"code":-32002,"message":"Transaction simulation failed: Attempt to debit an account but found no record of a prior credit.","data":{"accounts":null,"err":"AccountNotFound","logs":[]}}
exit status 1

This was my exact error.

yihau commented 3 years ago

I've check your fee payer's balance, it shows 0.

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/portto/solana-go-sdk/client"
    "github.com/portto/solana-go-sdk/client/rpc"
    "github.com/portto/solana-go-sdk/common"
    "github.com/portto/solana-go-sdk/types"
)

var feePayer = types.AccountFromPrivateKeyBytes([]byte{171, 198, 211, 210, 224, 159, 123, 194, 109, 67, 84, 90, 248, 134, 20, 25, 32, 185, 1, 216, 114, 253, 61, 71, 211, 88, 58, 242, 27, 146, 27, 137, 19, 110, 184, 252, 165, 125, 39, 128, 238, 132, 66, 123, 192, 136, 48, 33, 92, 107, 3, 44, 179, 4, 191, 172, 226, 212, 98, 118, 113, 163, 233, 105})

var alice = types.AccountFromPrivateKeyBytes([]byte{171, 198, 211, 210, 224, 159, 123, 194, 109, 67, 84, 90, 248, 134, 20, 25, 32, 185, 1, 216, 114, 253, 61, 71, 211, 88, 58, 242, 27, 146, 27, 137, 19, 110, 184, 252, 165, 125, 39, 128, 238, 132, 66, 123, 192, 136, 48, 33, 92, 107, 3, 44, 179, 4, 191, 172, 226, 212, 98, 118, 113, 163, 233, 105})

var mintPubkey = common.PublicKeyFromString("2JrfrWiKQTQKiRh6pUv8YogGwxpZcHNJTVW6E9JdFx5v")

func main() {
    c := client.NewClient(rpc.DevnetRPCEndpoint)
    feePayerBalance, err := c.GetBalance(context.Background(), feePayer.PublicKey.ToBase58())
    if err != nil {
        log.Fatalf("failed to get balance, err: %v", err)
    }
    fmt.Printf("%v, balance: %v\n", feePayer.PublicKey.ToBase58(), feePayerBalance)
}

also I've check your mint. your mint hasn't initialized either.

https://explorer.solana.com/address/2JrfrWiKQTQKiRh6pUv8YogGwxpZcHNJTVW6E9JdFx5v?cluster=devnet

I guess in previous example I used http://localhost:8899 as endpoint in this example I forgot to change it. It used devnet endpoint. so I think you can do

- c := client.NewClient(rpc.DevnetRPCEndpoint)
+ c := client.NewClient("http://localhost:8899")

and it will work

kvakhil95 commented 3 years ago

Thanks. Understood the issue.

yihau commented 3 years ago

It is my fault, I forgot to change all example to use the same endpoint. I've modified it. Thank for your issue!