Skip to content

Commit b860331

Browse files
feat(bindings): add epoch example (#167)
* feat(bindings): add epoch example * plural --------- Co-authored-by: Thibault Martinez <[email protected]>
1 parent 6f6d863 commit b860331

File tree

4 files changed

+125
-3
lines changed

4 files changed

+125
-3
lines changed

.github/workflows/bindings.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
run: make go
2727
- name: Checks for uncommitted changes
2828
run: git diff --exit-code
29-
- name: Run the example
29+
- name: Run the examples
3030
run: make go-examples
3131
kotlin:
3232
runs-on: ubuntu-latest
@@ -42,7 +42,7 @@ jobs:
4242
curl -s "https://get.sdkman.io" | bash
4343
source "$HOME/.sdkman/bin/sdkman-init.sh"
4444
sdk install gradle
45-
- name: Run the example
45+
- name: Run the examples
4646
run: make kotlin-examples
4747
python:
4848
runs-on: ubuntu-latest
@@ -53,5 +53,5 @@ jobs:
5353
run: make python
5454
- name: Checks for uncommitted changes
5555
run: git diff --exit-code
56-
- name: Run the example
56+
- name: Run the examples
5757
run: make python-examples

bindings/go/examples/epoch.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) 2025 IOTA Stiftung
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"log"
9+
10+
sdk "bindings/iota_sdk_ffi"
11+
)
12+
13+
func isNilError(err error) bool {
14+
if sdkErr, ok := err.(*sdk.SdkFfiError); ok {
15+
return sdkErr == nil
16+
}
17+
return false
18+
}
19+
20+
func main() {
21+
client := sdk.GraphQlClientNewDevnet()
22+
23+
// Get current epoch
24+
currentEpoch, err := client.Epoch(nil)
25+
if !isNilError(err) {
26+
log.Fatalf("Failed to get current epoch: %v", err)
27+
}
28+
if currentEpoch == nil {
29+
log.Fatal("Current epoch is nil")
30+
}
31+
32+
fmt.Printf("Current epoch: %d\n", currentEpoch.EpochId)
33+
fmt.Printf("Current epoch start time: %d\n", currentEpoch.StartTimestamp)
34+
35+
// Get previous epoch
36+
previousEpochId := currentEpoch.EpochId - 1
37+
previousEpoch, err := client.Epoch(&previousEpochId)
38+
if !isNilError(err) {
39+
log.Fatalf("Failed to get previous epoch: %v", err)
40+
}
41+
if previousEpoch == nil {
42+
log.Fatal("Previous epoch is nil")
43+
}
44+
45+
fmt.Printf("Previous epoch: %d\n", previousEpoch.EpochId)
46+
if previousEpoch.TotalStakeRewards != nil {
47+
fmt.Printf("Previous epoch stake rewards: %s\n", *previousEpoch.TotalStakeRewards)
48+
}
49+
}

bindings/kotlin/examples/Epoch.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2025 IOTA Stiftung
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import iota_sdk.GraphQlClient
5+
import kotlinx.coroutines.runBlocking
6+
7+
fun main() = runBlocking {
8+
try {
9+
val client = GraphQlClient.newDevnet()
10+
11+
// Get current epoch
12+
val currentEpoch = client.epoch(null)
13+
if (currentEpoch == null) {
14+
println("Current epoch is null")
15+
return@runBlocking
16+
}
17+
18+
println("Current epoch: ${currentEpoch.epochId}")
19+
println("Current epoch start time: ${currentEpoch.startTimestamp}")
20+
21+
// Get previous epoch
22+
val previousEpochId = currentEpoch.epochId - 1u
23+
val previousEpoch = client.epoch(previousEpochId)
24+
if (previousEpoch == null) {
25+
println("Previous epoch is null")
26+
return@runBlocking
27+
}
28+
29+
println("Previous epoch: ${previousEpoch.epochId}")
30+
previousEpoch.totalStakeRewards?.let { rewards ->
31+
println("Previous epoch stake rewards: $rewards")
32+
}
33+
} catch (e: Exception) {
34+
e.printStackTrace()
35+
}
36+
}

bindings/python/examples/epoch.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (c) 2025 IOTA Stiftung
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import asyncio
5+
from lib.iota_sdk_ffi import GraphQlClient
6+
7+
8+
async def main():
9+
try:
10+
client = GraphQlClient.new_devnet()
11+
12+
# Get current epoch
13+
current_epoch = await client.epoch(None)
14+
if current_epoch is None:
15+
print("Current epoch is None")
16+
return
17+
18+
print(f"Current epoch: {current_epoch.epoch_id}")
19+
print(f"Current epoch start time: {current_epoch.start_timestamp}")
20+
21+
# Get previous epoch
22+
previous_epoch_id = current_epoch.epoch_id - 1
23+
previous_epoch = await client.epoch(previous_epoch_id)
24+
if previous_epoch is None:
25+
print("Previous epoch is None")
26+
return
27+
28+
print(f"Previous epoch: {previous_epoch.epoch_id}")
29+
if previous_epoch.total_stake_rewards is not None:
30+
print(f"Previous epoch stake rewards: {previous_epoch.total_stake_rewards}")
31+
32+
except Exception as e:
33+
print(f"Error: {e}")
34+
35+
36+
if __name__ == "__main__":
37+
asyncio.run(main())

0 commit comments

Comments
 (0)