Skip to content

Commit

Permalink
Merge pull request #1 from fractalwagmi/ricebin_getAccountInfoWithRpc…
Browse files Browse the repository at this point in the history
…Context

fork getAccountInfo to return rpcContext if not found
  • Loading branch information
ricebin authored Aug 3, 2022
2 parents 02d7072 + ebdd48a commit 44c7c4c
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 4 deletions.
19 changes: 15 additions & 4 deletions rpc/getAccountInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ func (cl *Client) GetAccountInfoWithOpts(
ctx context.Context,
account solana.PublicKey,
opts *GetAccountInfoOpts,
) (*GetAccountInfoResult, error) {
out, err := cl.getAccountInfoWithOpts(ctx, account, opts)
if err != nil {
return nil, err
}
if out.Value == nil {
return nil, ErrNotFound
}
return out, nil
}

func (cl *Client) getAccountInfoWithOpts(
ctx context.Context,
account solana.PublicKey,
opts *GetAccountInfoOpts,
) (out *GetAccountInfoResult, err error) {

obj := M{
Expand Down Expand Up @@ -127,9 +142,5 @@ func (cl *Client) GetAccountInfoWithOpts(
if out == nil {
return nil, errors.New("expected a value, got null result")
}
if out.Value == nil {
return nil, ErrNotFound
}

return out, nil
}
35 changes: 35 additions & 0 deletions rpc/getAccountInfoWithRpcContext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021 github.com/gagliardetto
// This file has been modified by github.com/gagliardetto
//
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rpc

import (
"context"
"github.com/gagliardetto/solana-go"
)

// GetAccountInfoWithRpcContext is similar to GetAccountInfoWithOpts but will return rpcContext and nil account if account is not found
func (cl *Client) GetAccountInfoWithRpcContext(
ctx context.Context,
account solana.PublicKey,
opts *GetAccountInfoOpts,
) (*Account, *RPCContext, error) {
out, err := cl.getAccountInfoWithOpts(ctx, account, opts)
if err != nil {
return nil, nil, err
}
return out.Value, &out.RPCContext, nil
}
93 changes: 93 additions & 0 deletions rpc/getAccountInfoWithRpcContext_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package rpc

import (
"context"
stdjson "encoding/json"
"github.com/gagliardetto/solana-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func TestClient_GetAccountInfoWithRpcContext(t *testing.T) {
type wants struct {
account *Account
rpcContext *RPCContext
}
tests := []struct {
name string
responseBody string
key solana.PublicKey
opts GetAccountInfoOpts
want wants
}{
{
name: "No Data",
responseBody: `{"context":{"slot":83986106}}`,
key: solana.MustPublicKeyFromBase58("7xLk17EQQ5KLDLDe44wCmupJKJjTGd8hs3eSVVhCx932"),
opts: GetAccountInfoOpts{
Encoding: solana.EncodingJSON,
Commitment: CommitmentMax,
DataSlice: &DataSlice{
Offset: uint64Ptr(22),
Length: uint64Ptr(33),
},
},
want: wants{
rpcContext: &RPCContext{
Context: Context{
Slot: 83986106,
},
},
},
},
{
name: "Happy",
responseBody: `{"context":{"slot":83986105},"value":{"data":["dGVzdA==","base64"],"executable":true,"lamports":999999,"owner":"11111111111111111111111111111111","rentEpoch":207}}`,
key: solana.MustPublicKeyFromBase58("7xLk17EQQ5KLDLDe44wCmupJKJjTGd8hs3eSVVhCx932"),
opts: GetAccountInfoOpts{
Encoding: solana.EncodingJSON,
Commitment: CommitmentMax,
DataSlice: &DataSlice{
Offset: uint64Ptr(22),
Length: uint64Ptr(33),
},
},
want: wants{
account: &Account{
Lamports: 999999,
Owner: solana.MustPublicKeyFromBase58("11111111111111111111111111111111"),
Data: DataBytesOrJSONFromBytes([]byte("test")),
Executable: true,
RentEpoch: 207,
},
rpcContext: &RPCContext{
Context: Context{
Slot: 83986105,
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server, closer := mockJSONRPC(t, stdjson.RawMessage(wrapIntoRPC(tt.responseBody)))
defer closer()
client := New(server.URL)

acct, rpcContext, err := client.GetAccountInfoWithRpcContext(
context.Background(),
tt.key,
&tt.opts,
)
require.NoError(t, err)

assert.Equal(t, tt.want.account, acct)
assert.Equal(t, tt.want.rpcContext, rpcContext)
})
}
}

func uint64Ptr(in uint64) *uint64 {
return &in
}

0 comments on commit 44c7c4c

Please sign in to comment.