diff --git a/v2/examples/getbakerearliestwintime/main.go b/v2/examples/getbakerearliestwintime/main.go new file mode 100644 index 0000000..8c27065 --- /dev/null +++ b/v2/examples/getbakerearliestwintime/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "context" + "fmt" + "log" + + v2 "github.com/Concordium/concordium-go-sdk/v2" + "github.com/Concordium/concordium-go-sdk/v2/pb" +) + +// This example retrieves and prints projected earliest wintime of a baker. +func main() { + client, err := v2.NewClient(v2.Config{NodeAddress: "node.testnet.concordium.com:20000"}) + if err != nil { + log.Fatalf("Failed to instantiate client, err: %v", err) + } + + // sending empty context, can also use any other context instead. + resp, err := client.GetBakerEarliestWinTime(context.TODO(), &pb.BakerId{ + Value: 1, + }) + if err != nil { + log.Fatalf("failed to get wintime, err: %v", err) + } + + fmt.Println("wintime: ", resp) +} diff --git a/v2/examples/getbakersrewardperiod/main.go b/v2/examples/getbakersrewardperiod/main.go new file mode 100644 index 0000000..83f8815 --- /dev/null +++ b/v2/examples/getbakersrewardperiod/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "context" + "fmt" + "io" + "log" + + v2 "github.com/Concordium/concordium-go-sdk/v2" +) + +// This example retrieves and prints the info of the bakers in the reward period of a block. +func main() { + client, err := v2.NewClient(v2.Config{NodeAddress: "node.testnet.concordium.com:20000"}) + if err != nil { + log.Fatalf("Failed to instantiate client, err: %v", err) + } + + // sending empty context, can also use any other context instead. + stream, err := client.GetBakersRewardPeriod(context.TODO(), v2.BlockHashInputBest{}) + if err != nil { + log.Fatalf("failed to get BakerRewardPeriodInfos, err: %v", err) + } + + for err == nil { + bakerRewardPeriodInfo, err := stream.Recv() + if err != nil { + if err == io.EOF { + // All BakerRewardPeriodInfo recieved. + break + } + log.Fatalf("Could not receive BakerRewardPeriodInfo, err: %v", err) + } + fmt.Println("BakerRewardPeriodInfo: ", bakerRewardPeriodInfo) + } +} diff --git a/v2/examples/getblockcertificates/main.go b/v2/examples/getblockcertificates/main.go new file mode 100644 index 0000000..4cfabdd --- /dev/null +++ b/v2/examples/getblockcertificates/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "context" + "fmt" + "log" + + v2 "github.com/Concordium/concordium-go-sdk/v2" +) + +// This example retrieves and prints the blockcertificates of a non-genesis block. +func main() { + client, err := v2.NewClient(v2.Config{NodeAddress: "node.testnet.concordium.com:20000"}) + if err != nil { + log.Fatalf("Failed to instantiate client, err: %v", err) + } + + // sending empty context, can also use any other context instead. + resp, err := client.GetBlockCertificates(context.TODO(), v2.BlockHashInputBest{}) + if err != nil { + log.Fatalf("failed to get certificates, err: %v", err) + } + + fmt.Println("certificates: ", resp) +} diff --git a/v2/examples/getconsensusinfo/main.go b/v2/examples/getconsensusinfo/main.go index 9f5d6d0..45016fa 100644 --- a/v2/examples/getconsensusinfo/main.go +++ b/v2/examples/getconsensusinfo/main.go @@ -5,7 +5,7 @@ import ( "fmt" "log" - "github.com/Concordium/concordium-go-sdk/v2" + v2 "github.com/Concordium/concordium-go-sdk/v2" ) // in this example we receive and print consensus info. diff --git a/v2/examples/getfirstblockepoch/main.go b/v2/examples/getfirstblockepoch/main.go new file mode 100644 index 0000000..de5dadc --- /dev/null +++ b/v2/examples/getfirstblockepoch/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "context" + "fmt" + "log" + + v2 "github.com/Concordium/concordium-go-sdk/v2" +) + +// This example retrieves and prints the block hash of the first finalized block in a specific epoch. +func main() { + client, err := v2.NewClient(v2.Config{NodeAddress: "node.testnet.concordium.com:20000"}) + if err != nil { + log.Fatalf("Failed to instantiate client, err: %v", err) + } + + // sending empty context, can also use any other context instead. + resp, err := client.GetFirstBlockEpoch(context.TODO(), v2.EpochRequestBlockHash{ + BlockHash: v2.BlockHashInputLastFinal{}, + }) + if err != nil { + log.Fatalf("failed to get first block, err: %v", err) + } + + fmt.Println("hash: ", resp) +} diff --git a/v2/examples/getwinningbakersepoch/main.go b/v2/examples/getwinningbakersepoch/main.go new file mode 100644 index 0000000..0bd54f8 --- /dev/null +++ b/v2/examples/getwinningbakersepoch/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "context" + "fmt" + "io" + "log" + + v2 "github.com/Concordium/concordium-go-sdk/v2" +) + +// This example retrieves and prints the bakers that won the lottery in a particular historical epoch. +func main() { + client, err := v2.NewClient(v2.Config{NodeAddress: "node.testnet.concordium.com:20000"}) + if err != nil { + log.Fatalf("Failed to instantiate client, err: %v", err) + } + + // sending empty context, can also use any other context instead. + stream, err := client.GetWinningBakersEpoch(context.TODO(), v2.EpochRequestRelativeEpoch{ + GenesisIndex: v2.GenesisIndex{Value: 3}, + Epoch: v2.Epoch{Value: 5}, + }) + if err != nil { + log.Fatalf("failed to get winning bakers, err: %v", err) + } + + for err == nil { + winningBaker, err := stream.Recv() + if err != nil { + if err == io.EOF { + // All WinningBakers recieved. + break + } + log.Fatalf("Could not receive winning baker, err: %v", err) + } + fmt.Println("Winning baker: ", winningBaker) + } +} diff --git a/v2/tests/api_test.go b/v2/tests/api_test.go index 6fa29c5..e517c85 100644 --- a/v2/tests/api_test.go +++ b/v2/tests/api_test.go @@ -466,4 +466,48 @@ func TestExamples(t *testing.T) { require.NoError(t, err) require.Nil(t, blockItems) }) + + t.Run("GetFirstBlockEpoch", func(t *testing.T) { + blochHash, err := client.GetFirstBlockEpoch(context.Background(), v2.EpochRequestRelativeEpoch{ + GenesisIndex: v2.GenesisIndex{Value: 3}, + Epoch: v2.Epoch{Value: 5}, + }) + require.NoError(t, err) + require.NotNil(t, blochHash) + }) + + t.Run("GetWinningBakersEpoch", func(t *testing.T) { + winningBakerStream, err := client.GetWinningBakersEpoch(context.Background(), v2.EpochRequestRelativeEpoch{ + GenesisIndex: v2.GenesisIndex{Value: 3}, + Epoch: v2.Epoch{Value: 5}, + }) + require.NoError(t, err) + require.NotNil(t, winningBakerStream) + + winningBaker, err := winningBakerStream.Recv() + require.NoError(t, err) + require.NotNil(t, winningBaker) + }) + + t.Run("GetBlockCertificates", func(t *testing.T) { + certificate, err := client.GetBlockCertificates(context.Background(), v2.BlockHashInputBest{}) + require.NoError(t, err) + require.NotNil(t, certificate) + }) + + t.Run("GetBakersRewardPeriod", func(t *testing.T) { + bakerRewardPeriodInfoStream, err := client.GetBakersRewardPeriod(context.Background(), v2.BlockHashInputBest{}) + require.NoError(t, err) + require.NotNil(t, bakerRewardPeriodInfoStream) + + bakerRewardPeriodInfo, err := bakerRewardPeriodInfoStream.Recv() + require.NoError(t, err) + require.NotNil(t, bakerRewardPeriodInfo) + }) + + t.Run("GetBakerEarliestWintime", func(t *testing.T) { + timestamp, err := client.GetBakerEarliestWinTime(context.Background(), &pb.BakerId{Value: 1}) + require.NoError(t, err) + require.NotNil(t, timestamp) + }) }