-
Notifications
You must be signed in to change notification settings - Fork 3
chore(examples): Add stake and unstake example #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1822b87
chore(examples): Add stake and unstake example
DaughterOfMars 52c3551
split and fix examples
DaughterOfMars 4dc3032
add constructors and bindings example
DaughterOfMars d3f49ae
Merge branch 'sdk-bindings' into chore/stake-example
DaughterOfMars cca7b8a
clippy
DaughterOfMars dfe7032
Add some logs
DaughterOfMars e5d7dcc
formatting
DaughterOfMars 47600cb
Merge branch 'sdk-bindings' into chore/stake-example
DaughterOfMars 82678b6
fixes after merge
DaughterOfMars File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,121 @@ | ||
| // Copyright (c) 2025 IOTA Stiftung | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "log" | ||
|
|
||
| sdk "bindings/iota_sdk_ffi" | ||
| ) | ||
|
|
||
| func isNilError(err error) bool { | ||
| if sdkErr, ok := err.(*sdk.SdkFfiError); ok { | ||
| return sdkErr == nil | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func main() { | ||
| client := sdk.GraphQlClientNewDevnet() | ||
|
|
||
| myAddress, err := sdk.AddressFromHex("0x611830d3641a68f94a690dcc25d1f4b0dac948325ac18f6dd32564371735f32c") | ||
| if err != nil { | ||
| log.Fatalf("Failed to parse address: %v", err) | ||
| } | ||
|
|
||
| validators, err := client.ActiveValidators(nil, &sdk.PaginationFilter{Direction: sdk.DirectionForward}) | ||
| if !isNilError(err) { | ||
| log.Fatalf("Failed to get active validators: %v", err) | ||
| } | ||
|
|
||
| if len(validators.Data) == 0 { | ||
| log.Fatal("No validators found") | ||
| } | ||
| validator := validators.Data[0] | ||
|
|
||
| var validatorName string | ||
| if validator.Name == nil { | ||
| validatorName = "with no name" | ||
| } else { | ||
| validatorName = *validator.Name | ||
| } | ||
| log.Printf("Staking to validator %v", validatorName) | ||
|
|
||
| coinObjId, err := sdk.ObjectIdFromHex("0xd04077fe3b6fad13b3d4ed0d535b7ca92afcac8f0f2a0e0925fb9f4f0b30c699") | ||
| if err != nil { | ||
| log.Fatalf("Failed to parse object ID: %v", err) | ||
| } | ||
| coin, err := client.Object(coinObjId, nil) | ||
| if !isNilError(err) { | ||
| log.Fatalf("Failed to get coin: %v", err) | ||
| } | ||
|
|
||
| gasCoinObjId, err := sdk.ObjectIdFromHex("0x0b0270ee9d27da0db09651e5f7338dfa32c7ee6441ccefa1f6e305735bcfc7ab") | ||
| if err != nil { | ||
| log.Fatalf("Failed to parse object ID: %v", err) | ||
| } | ||
| gasCoin, err := client.Object(gasCoinObjId, nil) | ||
| if !isNilError(err) { | ||
| log.Fatalf("Failed to get gas coin: %v", err) | ||
| } | ||
|
|
||
| iotaSystemAddress, err := sdk.AddressFromHex("0x3") | ||
| if err != nil { | ||
| log.Fatalf("Failed to parse address: %v", err) | ||
| } | ||
|
|
||
| iotaSystemId, err := sdk.ObjectIdFromHex("0x5") | ||
| if err != nil { | ||
| log.Fatalf("Failed to parse object ID: %v", err) | ||
| } | ||
|
|
||
| iotaSystemModule, err := sdk.NewIdentifier("iota_system") | ||
| if err != nil { | ||
| log.Fatalf("Failed to parse identifier: %v", err) | ||
| } | ||
|
|
||
| requestAddStakeFn, err := sdk.NewIdentifier("request_add_stake") | ||
| if err != nil { | ||
| log.Fatalf("Failed to parse identifier: %v", err) | ||
| } | ||
|
|
||
| builder := sdk.NewTransactionBuilder() | ||
| inputs := []*sdk.Argument{ | ||
| builder.Input(sdk.UnresolvedInputNewShared(iotaSystemId, 1, true)), | ||
| builder.Input(sdk.UnresolvedInputFromObject(*coin).WithOwnedKind()), | ||
| builder.Input(sdk.UnresolvedInputNewPure(validator.Address.ToBytes())), | ||
| } | ||
| builder.MoveCall( | ||
| sdk.Function{ | ||
| Package: iotaSystemAddress, | ||
| Module: iotaSystemModule, | ||
| Function: requestAddStakeFn, | ||
| }, | ||
| inputs, | ||
| ) | ||
| builder.SetSender(myAddress) | ||
| builder.SetGasBudget(50000000) | ||
| gasPrice, err := client.ReferenceGasPrice(nil) | ||
| if !isNilError(err) { | ||
| log.Fatalf("Failed to get gas price: %v", err) | ||
| } | ||
| builder.SetGasPrice(*gasPrice) | ||
| builder.AddGasObjects([]*sdk.UnresolvedInput{sdk.UnresolvedInputFromObject(*gasCoin).WithOwnedKind()}) | ||
|
|
||
| txn, err := builder.Finish() | ||
| if err != nil { | ||
| log.Fatalf("Failed to create transaction: %v", err) | ||
| } | ||
|
|
||
| res, err := client.DryRunTx(txn, nil) | ||
| if !isNilError(err) { | ||
| log.Fatalf("Failed to get gas price: %v", err) | ||
| } | ||
|
|
||
| if res.Error != nil { | ||
| log.Fatalf("Failed to stake: %v", *res.Error) | ||
| } | ||
|
|
||
| log.Print("Stake dry run was successful!") | ||
| } | ||
This file contains hidden or 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,100 @@ | ||
| // Copyright (c) 2025 IOTA Stiftung | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "log" | ||
|
|
||
| sdk "bindings/iota_sdk_ffi" | ||
| ) | ||
|
|
||
| func isNilError(err error) bool { | ||
| if sdkErr, ok := err.(*sdk.SdkFfiError); ok { | ||
| return sdkErr == nil | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func main() { | ||
| client := sdk.GraphQlClientNewDevnet() | ||
|
|
||
| stakedIotaType := "0x3::staking_pool::StakedIota" | ||
| stakedIotas, err := client.Objects(&sdk.ObjectFilter{TypeTag: &stakedIotaType}, nil) | ||
| if !isNilError(err) { | ||
| log.Fatalf("Failed to get staked iota: %v", err) | ||
| } | ||
| if len(stakedIotas.Data) == 0 { | ||
| log.Fatal("No staked iota objects found") | ||
| } | ||
| stakedIota := stakedIotas.Data[0] | ||
|
|
||
| gasCoinType := sdk.StructTagGasCoin().String() | ||
| gasCoins, err := client.Objects(&sdk.ObjectFilter{TypeTag: &gasCoinType, Owner: stakedIota.Owner().AsAddressOpt()}, nil) | ||
| if !isNilError(err) { | ||
| log.Fatalf("Failed to get gas coin: %v", err) | ||
| } | ||
| if len(gasCoins.Data) == 0 { | ||
| log.Fatal("No gas coins found") | ||
| } | ||
| gasCoin := gasCoins.Data[0] | ||
|
|
||
| iotaSystemAddress, err := sdk.AddressFromHex("0x3") | ||
| if err != nil { | ||
| log.Fatalf("Failed to parse address: %v", err) | ||
| } | ||
|
|
||
| iotaSystemId, err := sdk.ObjectIdFromHex("0x5") | ||
| if err != nil { | ||
| log.Fatalf("Failed to parse object ID: %v", err) | ||
| } | ||
|
|
||
| iotaSystemModule, err := sdk.NewIdentifier("iota_system") | ||
| if err != nil { | ||
| log.Fatalf("Failed to parse identifier: %v", err) | ||
| } | ||
|
|
||
| requestAddStakeFn, err := sdk.NewIdentifier("request_withdraw_stake") | ||
| if err != nil { | ||
| log.Fatalf("Failed to parse identifier: %v", err) | ||
| } | ||
|
|
||
| builder := sdk.NewTransactionBuilder() | ||
| inputs := []*sdk.Argument{ | ||
| builder.Input(sdk.UnresolvedInputNewShared(iotaSystemId, 1, true)), | ||
| builder.Input(sdk.UnresolvedInputFromObject(stakedIota).WithOwnedKind()), | ||
| } | ||
| builder.MoveCall( | ||
| sdk.Function{ | ||
| Package: iotaSystemAddress, | ||
| Module: iotaSystemModule, | ||
| Function: requestAddStakeFn, | ||
| TypeArgs: []*sdk.TypeTag{}, | ||
| }, | ||
| inputs, | ||
| ) | ||
| builder.SetSender(gasCoin.Owner().AsAddress()) | ||
| builder.SetGasBudget(50000000) | ||
| gasPrice, err := client.ReferenceGasPrice(nil) | ||
| if !isNilError(err) { | ||
| log.Fatalf("Failed to get gas price: %v", err) | ||
| } | ||
| builder.SetGasPrice(*gasPrice) | ||
| builder.AddGasObjects([]*sdk.UnresolvedInput{sdk.UnresolvedInputFromObject(gasCoin).WithOwnedKind()}) | ||
|
|
||
| txn, err := builder.Finish() | ||
| if err != nil { | ||
| log.Fatalf("Failed to create transaction: %v", err) | ||
| } | ||
|
|
||
| res, err := client.DryRunTx(txn, nil) | ||
| if !isNilError(err) { | ||
| log.Fatalf("Failed to get gas price: %v", err) | ||
| } | ||
|
|
||
| if res.Error != nil { | ||
| log.Fatalf("Failed to unstake: %v", *res.Error) | ||
| } | ||
|
|
||
| log.Print("Unstake dry run was successful!") | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,87 @@ | ||
| // Copyright (c) 2025 IOTA Stiftung | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import iota_sdk.Address | ||
| import iota_sdk.Direction | ||
| import iota_sdk.Function | ||
| import iota_sdk.GraphQlClient | ||
| import iota_sdk.Identifier | ||
| import iota_sdk.ObjectId | ||
| import iota_sdk.PaginationFilter | ||
| import iota_sdk.TransactionBuilder | ||
| import iota_sdk.UnresolvedInput | ||
| import kotlinx.coroutines.runBlocking | ||
|
|
||
| fun main() = runBlocking { | ||
| try { | ||
| val client = GraphQlClient.newDevnet() | ||
|
|
||
| val myAddress = | ||
| Address.fromHex( | ||
| "0x611830d3641a68f94a690dcc25d1f4b0dac948325ac18f6dd32564371735f32c" | ||
| ) | ||
|
|
||
| val validators = client.activeValidators(null, PaginationFilter(Direction.FORWARD)) | ||
| if (validators.data.isEmpty()) { | ||
| throw Exception("no validators found") | ||
| } | ||
| val validator = validators.data[0] | ||
|
|
||
| println("Staking to validator ${validator.name ?: "with no name"}") | ||
|
|
||
| val coin = | ||
| client.`object`( | ||
| ObjectId.fromHex( | ||
| "0xd04077fe3b6fad13b3d4ed0d535b7ca92afcac8f0f2a0e0925fb9f4f0b30c699" | ||
| ) | ||
| ) | ||
| if (coin == null) { | ||
| throw Exception("missing coin") | ||
| } | ||
|
|
||
| val gasCoin = | ||
| client.`object`( | ||
| ObjectId.fromHex( | ||
| "0x0b0270ee9d27da0db09651e5f7338dfa32c7ee6441ccefa1f6e305735bcfc7ab" | ||
| ) | ||
| ) | ||
| if (gasCoin == null) { | ||
| throw Exception("missing gas coin") | ||
| } | ||
|
|
||
| val builder = TransactionBuilder() | ||
| val inputs = | ||
| listOf( | ||
| builder.input(UnresolvedInput.newShared(ObjectId.fromHex("0x5"), 1u, true)), | ||
| builder.input(UnresolvedInput.fromObject(coin).withOwnedKind()), | ||
| builder.input(UnresolvedInput.newPure(validator.address.toBytes())), | ||
| ) | ||
| builder.moveCall( | ||
| Function( | ||
| Address.fromHex("0x3"), | ||
| Identifier("iota_system"), | ||
| Identifier("request_add_stake"), | ||
| ), | ||
| inputs | ||
| ) | ||
| builder.setSender(myAddress) | ||
| builder.setGasBudget(50000000u) | ||
| val refGasPrice = client.referenceGasPrice(null) | ||
| if (refGasPrice == null) { | ||
| throw Exception("missing ref gas price") | ||
| } | ||
| builder.setGasPrice(refGasPrice) | ||
| builder.addGasObjects(listOf(UnresolvedInput.fromObject(gasCoin).withOwnedKind())) | ||
|
|
||
| val txn = builder.finish() | ||
| val res = client.dryRunTx(txn, false) | ||
|
|
||
| if (res.error != null) { | ||
| throw Exception(res.error) | ||
| } | ||
|
|
||
| println("Stake dry run was successful!") | ||
| } catch (e: Exception) { | ||
| e.printStackTrace() | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.