blocto / solana-go-sdk

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

GetNonceAccount error #71

Closed wmvm0 closed 2 years ago

wmvm0 commented 2 years ago

rpc: call error, err: failed to do request, err: Post "": unsupported protocol scheme "", body:

GetNonceAccount has an error

func (sol *solanaClient) GetNonceAccount() (string, error) {
    nonceAccountAddr := "DJyNpXgggw1WGgjTVzFsNjb3fuQZVMqhoakvSBfX9LYx"
    nonceAccount, err := sol.Client.GetNonceAccount(context.Background(), nonceAccountAddr)
    if err != nil {
        log.Fatalf("failed to get nonce account, err: %v", err)
    }
    fmt.Printf("%+v\n", nonceAccount)
    return "", nil
}
yihau commented 2 years ago

what's the endpoint you're using? the error seems like a url error. the code below works fine for me

Code ```go package main import ( "context" "fmt" "log" "github.com/portto/solana-go-sdk/client" "github.com/portto/solana-go-sdk/rpc" ) func main() { c := client.NewClient(rpc.DevnetRPCEndpoint) nonceAccountAddr := "DJyNpXgggw1WGgjTVzFsNjb3fuQZVMqhoakvSBfX9LYx" nonceAccount, err := c.GetNonceAccount(context.Background(), nonceAccountAddr) if err != nil { log.Fatalf("failed to get nonce account, err: %v", err) } fmt.Printf("%+v\n", nonceAccount) } ```
wmvm0 commented 2 years ago

devnet SendTransaction error

I use nonce account and transfer

2022/07/24 11:47:45 failed to send tx, err: rpc response error: {"code":-32002,"message":"Transaction simulation failed: Blockhash not found","data":{"accounts":null,"err":"BlockhashNotFound","logs":[],"unitsConsumed":0}}

fromPriKey := "2iyr7kVaGscVxWZrUcjR67km71QZySyvjc6VxFM95GzMo2VaMSyc49Wd62mtoEdDoHCrhAfWcZG1bp9aGfn8Jqzb"
to := "46gDsWeTK3qf8H1Q8rJYccRnyQjAc8SHviUNh9C6bPZD"

NonceAccountAddr: "DJyNpXgggw1WGgjTVzFsNjb3fuQZVMqhoakvSBfX9LYx",

FeeAccountPriKey: "56KvDmcqnwiXJXbFnwWduuieFckQbw1sQFPyPVohGkzDwgm88fdFPR6aA3wp6jMh1gYMHjgYYPx5FvjMHAQuDkxb",
func (sol *solanaClient) SendTx(fromPriKey string, to string) (string, error) {
    var amount uint64 = 1
    nonceAccountPubkey := common.PublicKeyFromString(sol.solanaConfig.NonceAccountAddr)
    toPublicKey := common.PublicKeyFromString(to)
    nonceAccount, err := sol.Client.GetNonceAccount(context.Background(), nonceAccountPubkey.ToBase58())
    if err != nil {
        log.Fatalf("failed to get nonce account, err: %v", err)
        return "", err
    }
    var feePayer, _ = types.AccountFromBase58(sol.solanaConfig.FeeAccountPriKey)
    var fromUserAccount, _ = types.AccountFromBase58(fromPriKey)

    tx, err := types.NewTransaction(types.NewTransactionParam{
        Signers: []types.Account{feePayer, fromUserAccount},
        Message: types.NewMessage(types.NewMessageParam{
            FeePayer:        feePayer.PublicKey,
            RecentBlockhash: nonceAccount.Nonce.ToBase58(),
            Instructions: []types.Instruction{
                sysprog.AdvanceNonceAccount(sysprog.AdvanceNonceAccountParam{
                    Nonce: nonceAccountPubkey,
                    Auth:  fromUserAccount.PublicKey,
                }),
                sysprog.Transfer(sysprog.TransferParam{
                    From:   fromUserAccount.PublicKey,
                    To:     toPublicKey,
                    Amount: amount,
                }),
                memoprog.BuildMemo(memoprog.BuildMemoParam{
                    Memo: []byte("use nonce"),
                }),
            },
        }),
    })
    if err != nil {
        log.Fatalf("failed to new a transaction, err: %v", err)
        return "", err
    }

    sig, err := sol.Client.SendTransaction(context.Background(), tx)
    if err != nil {
        log.Fatalf("failed to send tx, err: %v", err)
        return "", err
    }

    return sig, nil
}
wmvm0 commented 2 years ago

what's the endpoint you're using? the error seems like a url error. the code below works fine for me

Code

sorry, I filled in the wrong network, now it's ok.

yihau commented 2 years ago

did you use the nonce account with multiple tx and send them at the same time? the error look like that.

yihau commented 2 years ago

feel free to reopen this issue if you still have the problem