Skip to content

Commit

Permalink
feat(client): add get epoch info
Browse files Browse the repository at this point in the history
  • Loading branch information
yihau committed Nov 14, 2023
1 parent 5702662 commit 9b2c2b9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
37 changes: 37 additions & 0 deletions client/rpc_get_epoch_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package client

import (
"context"

"github.com/blocto/solana-go-sdk/rpc"
)

type GetEpochInfo struct {
AbsoluteSlot uint64
BlockHeight uint64
Epoch uint64
SlotIndex uint64
SlotsInEpoch uint64
TransactionCount *uint64
}

// GetEpochInfo returns information about the current epoch
func (c *Client) GetEpochInfo(ctx context.Context) (GetEpochInfo, error) {
return process(
func() (rpc.JsonRpcResponse[rpc.GetEpochInfo], error) {
return c.RpcClient.GetEpochInfo(ctx)
},
convertGetEpochInfo,
)
}

func convertGetEpochInfo(v rpc.GetEpochInfo) (GetEpochInfo, error) {
return GetEpochInfo{
AbsoluteSlot: v.AbsoluteSlot,
BlockHeight: v.BlockHeight,
Epoch: v.Epoch,
SlotIndex: v.SlotIndex,
SlotsInEpoch: v.SlotsInEpoch,
TransactionCount: v.TransactionCount,
}, nil
}
34 changes: 34 additions & 0 deletions client/rpc_get_epoch_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package client

import (
"context"
"testing"

"github.com/blocto/solana-go-sdk/internal/client_test"
"github.com/blocto/solana-go-sdk/pkg/pointer"
)

func TestClient_GetEpochInfo(t *testing.T) {
client_test.TestAll(
t,
[]client_test.Param{
{
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getEpochInfo"}`,
ResponseBody: `{"jsonrpc":"2.0","result":{"absoluteSlot":86715160,"blockHeight":84901536,"epoch":200,"slotIndex":315160,"slotsInEpoch":432000,"transactionCount":2265984079},"id":1}`,
F: func(url string) (any, error) {
c := NewClient(url)
return c.GetEpochInfo(context.TODO())
},
ExpectedValue: GetEpochInfo{
AbsoluteSlot: 86715160,
BlockHeight: 84901536,
Epoch: 200,
SlotIndex: 315160,
SlotsInEpoch: 432000,
TransactionCount: pointer.Get[uint64](2265984079),
},
ExpectedError: nil,
},
},
)
}

0 comments on commit 9b2c2b9

Please sign in to comment.