|
| 1 | +package rpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/gagliardetto/solana-go" |
| 8 | + stdjson "github.com/goccy/go-json" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +// TestClient_GetProgramAccountsWithOpts_MinContextSlot pins that the |
| 14 | +// minContextSlot opt is forwarded into the params object. |
| 15 | +func TestClient_GetProgramAccountsWithOpts_MinContextSlot(t *testing.T) { |
| 16 | + responseBody := `[{"account":{"data":["dGVzdA==","base64"],"executable":true,"lamports":2039280,"owner":"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA","rentEpoch":206},"pubkey":"7xLk17EQQ5KLDLDe44wCmupJKJjTGd8hs3eSVVhCx932"}]` |
| 17 | + server, closer := mockJSONRPC(t, stdjson.RawMessage(wrapIntoRPC(responseBody))) |
| 18 | + defer closer() |
| 19 | + client := New(server.URL) |
| 20 | + |
| 21 | + pubkeyString := "7xLk17EQQ5KLDLDe44wCmupJKJjTGd8hs3eSVVhCx932" |
| 22 | + pubKey := solana.MustPublicKeyFromBase58(pubkeyString) |
| 23 | + |
| 24 | + minSlot := uint64(123456789) |
| 25 | + _, err := client.GetProgramAccountsWithOpts( |
| 26 | + context.Background(), |
| 27 | + pubKey, |
| 28 | + &GetProgramAccountsOpts{ |
| 29 | + MinContextSlot: &minSlot, |
| 30 | + }, |
| 31 | + ) |
| 32 | + require.NoError(t, err) |
| 33 | + |
| 34 | + reqBody := server.RequestBody(t) |
| 35 | + reqBody["id"] = any(nil) |
| 36 | + |
| 37 | + assert.Equal(t, |
| 38 | + map[string]any{ |
| 39 | + "id": any(nil), |
| 40 | + "jsonrpc": "2.0", |
| 41 | + "method": "getProgramAccounts", |
| 42 | + "params": []any{ |
| 43 | + pubkeyString, |
| 44 | + map[string]any{ |
| 45 | + "encoding": "base64", |
| 46 | + "minContextSlot": float64(minSlot), |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + reqBody, |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +// TestClient_GetTokenAccountsByOwner_MinContextSlot pins that the |
| 55 | +// minContextSlot opt is forwarded into the params object for the owner variant. |
| 56 | +func TestClient_GetTokenAccountsByOwner_MinContextSlot(t *testing.T) { |
| 57 | + responseBody := `{"context":{"slot":1},"value":[]}` |
| 58 | + server, closer := mockJSONRPC(t, stdjson.RawMessage(wrapIntoRPC(responseBody))) |
| 59 | + defer closer() |
| 60 | + client := New(server.URL) |
| 61 | + |
| 62 | + ownerString := "7xLk17EQQ5KLDLDe44wCmupJKJjTGd8hs3eSVVhCx932" |
| 63 | + owner := solana.MustPublicKeyFromBase58(ownerString) |
| 64 | + programIDString := "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" |
| 65 | + programID := solana.MustPublicKeyFromBase58(programIDString) |
| 66 | + |
| 67 | + minSlot := uint64(987654321) |
| 68 | + _, err := client.GetTokenAccountsByOwner( |
| 69 | + context.Background(), |
| 70 | + owner, |
| 71 | + &GetTokenAccountsConfig{ProgramId: &programID}, |
| 72 | + &GetTokenAccountsOpts{MinContextSlot: &minSlot}, |
| 73 | + ) |
| 74 | + require.NoError(t, err) |
| 75 | + |
| 76 | + reqBody := server.RequestBody(t) |
| 77 | + reqBody["id"] = any(nil) |
| 78 | + |
| 79 | + assert.Equal(t, |
| 80 | + map[string]any{ |
| 81 | + "id": any(nil), |
| 82 | + "jsonrpc": "2.0", |
| 83 | + "method": "getTokenAccountsByOwner", |
| 84 | + "params": []any{ |
| 85 | + ownerString, |
| 86 | + map[string]any{"programId": programIDString}, |
| 87 | + map[string]any{ |
| 88 | + "encoding": "base64", |
| 89 | + "minContextSlot": float64(minSlot), |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + reqBody, |
| 94 | + ) |
| 95 | +} |
| 96 | + |
| 97 | +// TestClient_GetTokenAccountsByDelegate_MinContextSlot pins the same for the |
| 98 | +// delegate variant. |
| 99 | +func TestClient_GetTokenAccountsByDelegate_MinContextSlot(t *testing.T) { |
| 100 | + responseBody := `{"context":{"slot":1},"value":[]}` |
| 101 | + server, closer := mockJSONRPC(t, stdjson.RawMessage(wrapIntoRPC(responseBody))) |
| 102 | + defer closer() |
| 103 | + client := New(server.URL) |
| 104 | + |
| 105 | + delegateString := "7xLk17EQQ5KLDLDe44wCmupJKJjTGd8hs3eSVVhCx932" |
| 106 | + delegate := solana.MustPublicKeyFromBase58(delegateString) |
| 107 | + programIDString := "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" |
| 108 | + programID := solana.MustPublicKeyFromBase58(programIDString) |
| 109 | + |
| 110 | + minSlot := uint64(42) |
| 111 | + _, err := client.GetTokenAccountsByDelegate( |
| 112 | + context.Background(), |
| 113 | + delegate, |
| 114 | + &GetTokenAccountsConfig{ProgramId: &programID}, |
| 115 | + &GetTokenAccountsOpts{MinContextSlot: &minSlot}, |
| 116 | + ) |
| 117 | + require.NoError(t, err) |
| 118 | + |
| 119 | + reqBody := server.RequestBody(t) |
| 120 | + reqBody["id"] = any(nil) |
| 121 | + |
| 122 | + assert.Equal(t, |
| 123 | + map[string]any{ |
| 124 | + "id": any(nil), |
| 125 | + "jsonrpc": "2.0", |
| 126 | + "method": "getTokenAccountsByDelegate", |
| 127 | + "params": []any{ |
| 128 | + delegateString, |
| 129 | + map[string]any{"programId": programIDString}, |
| 130 | + map[string]any{ |
| 131 | + "encoding": "base64", |
| 132 | + "minContextSlot": float64(minSlot), |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + reqBody, |
| 137 | + ) |
| 138 | +} |
0 commit comments