diff --git a/rpc/getTokenAccountsByDelegate.go b/rpc/getTokenAccountsByDelegate.go index bd4722c..5b18b7b 100644 --- a/rpc/getTokenAccountsByDelegate.go +++ b/rpc/getTokenAccountsByDelegate.go @@ -7,22 +7,22 @@ import ( "github.com/dfuse-io/solana-go" ) -type GetTokenAccountsByDelegateResult struct { +type GetTokenAccountsResult struct { RPCContext - Value []*TokenAccountsByDelegateResult `json:"value"` + Value []*TokenAccountsResult `json:"value"` } -type TokenAccountsByDelegateResult struct { +type TokenAccountsResult struct { Pubkey solana.PublicKey `json:"pubkey"` // the account Pubkey Account solana.PublicKey `json:"account"` // the account } -type GetTokenAccountsByDelegateConfig struct { +type GetTokenAccountsConfig struct { Mint solana.PublicKey `json:"mint"` // Pubkey of the specific token Mint to limit accounts to // OR: ProgramId solana.PublicKey `json:"programId"` // Pubkey of the Token program ID that owns the accounts } -type GetTokenAccountsByDelegateOpts struct { +type GetTokenAccountsOpts struct { Commitment CommitmentType `json:"commitment,omitempty"` Encoding EncodingType `json:"encoding,omitempty"` @@ -34,9 +34,9 @@ type GetTokenAccountsByDelegateOpts struct { func (cl *Client) GetTokenAccountsByDelegate( ctx context.Context, account solana.PublicKey, // Pubkey of account delegate to query - conf *GetTokenAccountsByDelegateConfig, - opts *GetTokenAccountsByDelegateOpts, -) (out *GetTokenAccountsByDelegateResult, err error) { + conf *GetTokenAccountsConfig, + opts *GetTokenAccountsOpts, +) (out *GetTokenAccountsResult, err error) { params := []interface{}{account} if conf == nil { return nil, errors.New("conf is nil") diff --git a/rpc/getTokenAccountsByOwner.go b/rpc/getTokenAccountsByOwner.go new file mode 100644 index 0000000..f9d285d --- /dev/null +++ b/rpc/getTokenAccountsByOwner.go @@ -0,0 +1,61 @@ +package rpc + +import ( + "context" + "errors" + + "github.com/dfuse-io/solana-go" +) + +// GetTokenAccountsByOwner returns all SPL Token accounts by token owner. +func (cl *Client) GetTokenAccountsByOwner( + ctx context.Context, + owner solana.PublicKey, + conf *GetTokenAccountsConfig, + opts *GetTokenAccountsOpts, +) (out *GetTokenAccountsResult, err error) { + params := []interface{}{owner} + if conf == nil { + return nil, errors.New("conf is nil") + } + if !conf.Mint.IsZero() && !conf.ProgramId.IsZero() { + return nil, errors.New("conf.Mint and conf.ProgramId are both set; must be just one of them") + } + + { + obj := M{} + if !conf.Mint.IsZero() { + obj["mint"] = conf.Mint + } + if !conf.ProgramId.IsZero() { + obj["programId"] = conf.ProgramId + } + if len(obj) > 0 { + params = append(params, obj) + } + } + { + obj := M{} + if opts != nil { + if opts.Commitment != "" { + obj["commitment"] = opts.Commitment + } + if opts.Encoding != "" { + // TODO: remove option? + obj["encoding"] = opts.Encoding + } + if opts.DataSlice != nil { + obj["dataSlice"] = M{ + "offset": opts.DataSlice.Offset, + "length": opts.DataSlice.Length, + } + } + if len(obj) > 0 { + params = append(params, obj) + } + } + } + + err = cl.rpcClient.CallFor(&out, "getTokenAccountsByOwner", params) + return +}