Get Nonce Account By Owner

package main

import (
	"context"
	"encoding/base64"
	"fmt"
	"log"

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

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

	res, err := c.RpcClient.GetProgramAccountsWithConfig(
		context.Background(),
		common.SystemProgramID.ToBase58(),
		rpc.GetProgramAccountsConfig{
			Encoding: rpc.AccountEncodingBase64,
			Filters: []rpc.GetProgramAccountsConfigFilter{
				{
					DataSize: system.NonceAccountSize,
				},
				{
					MemCmp: &rpc.GetProgramAccountsConfigFilterMemCmp{
						Offset: 8,
						Bytes:  "9aE476sH92Vz7DMPyq5WLPkrKWivxeuTKEFKd2sZZcde", // owner address
					},
				},
			},
		},
	)
	if err != nil {
		log.Fatalf("failed to get program accounts, err: %v", err)
	}

	for _, a := range res.Result {
		fmt.Println("pubkey", a.Pubkey)
		data, err := base64.StdEncoding.DecodeString((a.Account.Data.([]any))[0].(string))
		if err != nil {
			log.Fatalf("failed to decode data, err: %v", err)
		}
		nonceAccount, err := system.NonceAccountDeserialize(data)
		if err != nil {
			log.Fatalf("failed to parse nonce account, err: %v", err)
		}
		fmt.Printf("%+v\n", nonceAccount)
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Last Updated:
Contributors: yihau