forked from gagliardetto/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from fractalwagmi/ricebin_getAccountInfoWithRpc…
…Context fork getAccountInfo to return rpcContext if not found
- Loading branch information
Showing
3 changed files
with
143 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |