-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
245 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/blocto/solana-go-sdk/rpc" | ||
) | ||
|
||
type GetSlotLeaderConfig struct { | ||
Commitment *rpc.Commitment | ||
MinContextSlot *uint64 | ||
} | ||
|
||
func (c GetSlotLeaderConfig) toRpc() rpc.GetSlotLeaderConfig { | ||
return rpc.GetSlotLeaderConfig{ | ||
Commitment: c.Commitment, | ||
MinContextSlot: c.MinContextSlot, | ||
} | ||
} | ||
|
||
// GetSlotLeader returns the current slot leader | ||
func (c *Client) GetSlotLeader(ctx context.Context) (string, error) { | ||
return process( | ||
func() (rpc.JsonRpcResponse[string], error) { | ||
return c.RpcClient.GetSlotLeader(ctx) | ||
}, | ||
forward[string], | ||
) | ||
} | ||
|
||
// GetSlotWithConfig returns the current slot leader | ||
func (c *Client) GetSlotLeaderWithConfig(ctx context.Context, cfg GetSlotLeaderConfig) (string, error) { | ||
return process( | ||
func() (rpc.JsonRpcResponse[string], error) { | ||
return c.RpcClient.GetSlotLeaderWithConfig(ctx, cfg.toRpc()) | ||
}, | ||
forward[string], | ||
) | ||
} |
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,82 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/blocto/solana-go-sdk/internal/client_test" | ||
"github.com/blocto/solana-go-sdk/pkg/pointer" | ||
"github.com/blocto/solana-go-sdk/rpc" | ||
) | ||
|
||
func TestClient_GetSlotLeader(t *testing.T) { | ||
client_test.TestAll( | ||
t, | ||
[]client_test.Param{ | ||
{ | ||
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getSlotLeader"}`, | ||
ResponseBody: `{"jsonrpc":"2.0","result":"q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot","id":1}`, | ||
F: func(url string) (any, error) { | ||
c := NewClient(url) | ||
return c.GetSlotLeader(context.TODO()) | ||
}, | ||
ExpectedValue: "q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot", | ||
ExpectedError: nil, | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
func TestClient_GetSlotLeaderWithConfig(t *testing.T) { | ||
client_test.TestAll( | ||
t, | ||
[]client_test.Param{ | ||
{ | ||
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getSlotLeader","params":[{"commitment": "processed"}]}`, | ||
ResponseBody: `{"jsonrpc":"2.0","result":"q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot","id":1}`, | ||
F: func(url string) (any, error) { | ||
c := NewClient(url) | ||
return c.GetSlotLeaderWithConfig( | ||
context.TODO(), | ||
GetSlotLeaderConfig{ | ||
Commitment: pointer.Get(rpc.CommitmentProcessed), | ||
}, | ||
) | ||
}, | ||
ExpectedValue: "q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot", | ||
ExpectedError: nil, | ||
}, | ||
{ | ||
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getSlotLeader","params":[{"minContextSlot":0}]}`, | ||
ResponseBody: `{"jsonrpc":"2.0","result":"q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot","id":1}`, | ||
F: func(url string) (any, error) { | ||
c := NewClient(url) | ||
return c.GetSlotLeaderWithConfig( | ||
context.TODO(), | ||
GetSlotLeaderConfig{ | ||
MinContextSlot: pointer.Get[uint64](0), | ||
}, | ||
) | ||
}, | ||
ExpectedValue: "q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot", | ||
ExpectedError: nil, | ||
}, | ||
{ | ||
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getSlotLeader","params":[{"commitment": "confirmed","minContextSlot":10}]}`, | ||
ResponseBody: `{"jsonrpc":"2.0","result":"q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot","id":1}`, | ||
F: func(url string) (any, error) { | ||
c := NewClient(url) | ||
return c.GetSlotLeaderWithConfig( | ||
context.TODO(), | ||
GetSlotLeaderConfig{ | ||
Commitment: pointer.Get(rpc.CommitmentConfirmed), | ||
MinContextSlot: pointer.Get[uint64](10), | ||
}, | ||
) | ||
}, | ||
ExpectedValue: "q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot", | ||
ExpectedError: 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,23 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type GetSlotLeaderResponse JsonRpcResponse[string] | ||
|
||
// GetSlotLeaderConfig is a option config for `getSlotLeader` | ||
type GetSlotLeaderConfig struct { | ||
Commitment *Commitment `json:"commitment,omitempty"` | ||
MinContextSlot *uint64 `json:"minContextSlot,omitempty"` | ||
} | ||
|
||
// GetSlotLeader returns the current slot leader | ||
func (c *RpcClient) GetSlotLeader(ctx context.Context) (JsonRpcResponse[string], error) { | ||
return call[JsonRpcResponse[string]](c, ctx, "getSlotLeader") | ||
} | ||
|
||
// GetSlotWithConfig returns the current slot leader | ||
func (c *RpcClient) GetSlotLeaderWithConfig(ctx context.Context, cfg GetSlotLeaderConfig) (JsonRpcResponse[string], error) { | ||
return call[JsonRpcResponse[string]](c, ctx, "getSlotLeader", cfg) | ||
} |
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,101 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/blocto/solana-go-sdk/internal/client_test" | ||
"github.com/blocto/solana-go-sdk/pkg/pointer" | ||
) | ||
|
||
func TestGetSlotLeader(t *testing.T) { | ||
client_test.TestAll( | ||
t, | ||
[]client_test.Param{ | ||
{ | ||
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getSlotLeader"}`, | ||
ResponseBody: `{"jsonrpc":"2.0","result":"q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot","id":1}`, | ||
F: func(url string) (any, error) { | ||
c := NewRpcClient(url) | ||
return c.GetSlotLeader(context.TODO()) | ||
}, | ||
ExpectedValue: JsonRpcResponse[string]{ | ||
JsonRpc: "2.0", | ||
Id: 1, | ||
Error: nil, | ||
Result: "q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot", | ||
}, | ||
ExpectedError: nil, | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
func TestGetSlotLeaderWithConfig(t *testing.T) { | ||
client_test.TestAll( | ||
t, | ||
[]client_test.Param{ | ||
{ | ||
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getSlotLeader","params":[{"commitment": "processed"}]}`, | ||
ResponseBody: `{"jsonrpc":"2.0","result":"q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot","id":1}`, | ||
F: func(url string) (any, error) { | ||
c := NewRpcClient(url) | ||
return c.GetSlotLeaderWithConfig( | ||
context.TODO(), | ||
GetSlotLeaderConfig{ | ||
Commitment: pointer.Get(CommitmentProcessed), | ||
}, | ||
) | ||
}, | ||
ExpectedValue: JsonRpcResponse[string]{ | ||
JsonRpc: "2.0", | ||
Id: 1, | ||
Error: nil, | ||
Result: "q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot", | ||
}, | ||
ExpectedError: nil, | ||
}, | ||
{ | ||
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getSlotLeader","params":[{"minContextSlot":0}]}`, | ||
ResponseBody: `{"jsonrpc":"2.0","result":"q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot","id":1}`, | ||
F: func(url string) (any, error) { | ||
c := NewRpcClient(url) | ||
return c.GetSlotLeaderWithConfig( | ||
context.TODO(), | ||
GetSlotLeaderConfig{ | ||
MinContextSlot: pointer.Get[uint64](0), | ||
}, | ||
) | ||
}, | ||
ExpectedValue: JsonRpcResponse[string]{ | ||
JsonRpc: "2.0", | ||
Id: 1, | ||
Error: nil, | ||
Result: "q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot", | ||
}, | ||
ExpectedError: nil, | ||
}, | ||
{ | ||
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getSlotLeader","params":[{"commitment": "confirmed","minContextSlot":10}]}`, | ||
ResponseBody: `{"jsonrpc":"2.0","result":"q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot","id":1}`, | ||
F: func(url string) (any, error) { | ||
c := NewRpcClient(url) | ||
return c.GetSlotLeaderWithConfig( | ||
context.TODO(), | ||
GetSlotLeaderConfig{ | ||
Commitment: pointer.Get(CommitmentConfirmed), | ||
MinContextSlot: pointer.Get[uint64](10), | ||
}, | ||
) | ||
}, | ||
ExpectedValue: JsonRpcResponse[string]{ | ||
JsonRpc: "2.0", | ||
Id: 1, | ||
Error: nil, | ||
Result: "q9XWcZ7T1wP4bW9SB4XgNNwjnFEJ982nE8aVbbNuwot", | ||
}, | ||
ExpectedError: nil, | ||
}, | ||
}, | ||
) | ||
} |