-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add tx_command_results example #274
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
Open
Thoralf-M
wants to merge
5
commits into
sdk-bindings
Choose a base branch
from
feat/tx-command-results-example
base: sdk-bindings
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+264
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2afafe7
feat: add tx_command_results example
Thoralf-M ce6e4f5
Apply suggestions from code review
Thoralf-M f57af06
Merge branch 'sdk-bindings' into feat/tx-command-results-example
Thoralf-M ad71360
update to latest changes
Thoralf-M bcca522
fmt with 4 spaces
Thoralf-M 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,76 @@ | ||
// Copyright (c) 2025 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
sdk "bindings/iota_sdk_ffi" | ||
) | ||
|
||
func main() { | ||
client := sdk.GraphQlClientNewDevnet() | ||
|
||
sender, _ := sdk.AddressFromHex("0x611830d3641a68f94a690dcc25d1f4b0dac948325ac18f6dd32564371735f32c") | ||
|
||
builder := sdk.TransactionBuilderInit(sender, client) | ||
|
||
packageAddr := sdk.AddressStdLib() | ||
moduleName, _ := sdk.NewIdentifier("u64") | ||
functionName, _ := sdk.NewIdentifier("max") | ||
builder.MoveCall( | ||
packageAddr, | ||
moduleName, | ||
functionName, | ||
[]*sdk.PtbArgument{sdk.PtbArgumentU64(0), sdk.PtbArgumentU64(1000)}, | ||
nil, | ||
// Assign a name to the result of this command | ||
[]string{"res0"}, | ||
) | ||
|
||
builder.MoveCall( | ||
packageAddr, | ||
moduleName, | ||
functionName, | ||
[]*sdk.PtbArgument{sdk.PtbArgumentU64(1000), sdk.PtbArgumentU64(2000)}, | ||
nil, | ||
// Assign a name to the result of this command | ||
[]string{"res1"}, | ||
) | ||
|
||
builder.SplitCoins( | ||
sdk.PtbArgumentGas(), | ||
// Use the named results of previous commands to use as arguments | ||
[]*sdk.PtbArgument{sdk.PtbArgumentRes("res0"), sdk.PtbArgumentRes("res1")}, | ||
// For nested results, a tuple or vec can be used to name them | ||
[]string{"coin0", "coin1"}, | ||
) | ||
|
||
// Use named results as arguments | ||
builder.TransferObjects(sender, []*sdk.PtbArgument{sdk.PtbArgumentRes("coin0"), sdk.PtbArgumentRes("coin1")}) | ||
|
||
txn, err := builder.Finish() | ||
if err.(*sdk.SdkFfiError) != nil { | ||
log.Fatalf("Failed to create transaction: %v", err) | ||
} | ||
|
||
txnBytes, err := txn.BcsSerialize() | ||
if err != nil { | ||
log.Fatalf("Failed to serialize transaction: %v", err) | ||
} | ||
log.Printf("Signing Digest: %v", sdk.HexEncode(txn.SigningDigest())) | ||
log.Printf("Txn Bytes: %v", sdk.Base64Encode(txnBytes)) | ||
|
||
skipChecks := bool(false) | ||
res, err := client.DryRunTx(txn, &skipChecks) | ||
if err.(*sdk.SdkFfiError) != nil { | ||
log.Fatalf("Failed to send tx: %v", err) | ||
} | ||
|
||
if res.Error != nil { | ||
log.Fatalf("Failed to send tx: %v", *res.Error) | ||
} | ||
|
||
log.Print("Tx 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,68 @@ | ||
// Copyright (c) 2025 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import iota_sdk.* | ||
import kotlinx.coroutines.runBlocking | ||
|
||
fun main() = runBlocking { | ||
try { | ||
val client = GraphQlClient.newDevnet() | ||
|
||
val sender = | ||
Address.fromHex( | ||
"0x611830d3641a68f94a690dcc25d1f4b0dac948325ac18f6dd32564371735f32c" | ||
) | ||
|
||
val builder = TransactionBuilder.init(sender, client) | ||
|
||
val packageAddr = Address.stdLib() | ||
val moduleName = Identifier("u64") | ||
val functionName = Identifier("max") | ||
|
||
builder.moveCall( | ||
packageAddr, | ||
moduleName, | ||
functionName, | ||
listOf(PtbArgument.u64(0u), PtbArgument.u64(1000u)), | ||
// Assign a name to the result of this command | ||
names = listOf("res0"), | ||
) | ||
|
||
builder.moveCall( | ||
packageAddr, | ||
moduleName, | ||
functionName, | ||
listOf(PtbArgument.u64(1000u), PtbArgument.u64(2000u)), | ||
// Assign a name to the result of this command | ||
names = listOf("res1"), | ||
) | ||
|
||
builder.splitCoins( | ||
PtbArgument.gas(), | ||
// Use the named results of previous commands to use as arguments | ||
listOf(PtbArgument.res("res0"), PtbArgument.res("res1")), | ||
// For nested results, a tuple or vec can be used to name them | ||
listOf("coin0", "coin1"), | ||
) | ||
|
||
// Use named results as arguments | ||
builder.transferObjects( | ||
sender, | ||
listOf(PtbArgument.res("coin0"), PtbArgument.res("coin1")), | ||
) | ||
|
||
val txn = builder.finish() | ||
|
||
println("Signing Digest: ${hexEncode(txn.signingDigest())}") | ||
println("Txn Bytes: ${base64Encode(txn.bcsSerialize())}") | ||
|
||
val res = client.dryRunTx(txn, false) | ||
if (res.error != null) { | ||
throw Exception("Failed to send tx: ${res.error}") | ||
} | ||
|
||
println("Tx dry run was successful!") | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
} |
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,64 @@ | ||
# Copyright (c) 2025 IOTA Stiftung | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from lib.iota_sdk_ffi import * | ||
|
||
import asyncio | ||
|
||
async def main(): | ||
try: | ||
client = GraphQlClient.new_devnet() | ||
|
||
sender = Address.from_hex("0x611830d3641a68f94a690dcc25d1f4b0dac948325ac18f6dd32564371735f32c") | ||
|
||
builder = await TransactionBuilder.init(sender, client) | ||
|
||
package_addr = Address.std_lib() | ||
module_name = Identifier("u64") | ||
function_name = Identifier("max") | ||
|
||
builder.move_call( | ||
package_addr, | ||
module_name, | ||
function_name, | ||
[PtbArgument.u64(0), PtbArgument.u64(1000)], | ||
# Assign a name to the result of this command | ||
names=["res0"], | ||
) | ||
|
||
builder.move_call( | ||
package_addr, | ||
module_name, | ||
function_name, | ||
[PtbArgument.u64(1000), PtbArgument.u64(2000)], | ||
# Assign a name to the result of this command | ||
names=["res1"], | ||
) | ||
|
||
builder.split_coins( | ||
PtbArgument.gas(), | ||
# Use the named results of previous commands as arguments | ||
[PtbArgument.res("res0"), PtbArgument.res("res1")], | ||
# For nested results, a tuple or vec can be used to name them | ||
["coin0", "coin1"], | ||
) | ||
|
||
# Use named results as arguments | ||
builder.transfer_objects(sender, [PtbArgument.res("coin0"), PtbArgument.res("coin1")]) | ||
|
||
txn = await builder.finish() | ||
|
||
print("Signing Digest:", hex_encode(txn.signing_digest())) | ||
print("Txn Bytes:", base64_encode(txn.bcs_serialize())) | ||
|
||
res = await client.dry_run_tx(txn, False) | ||
if res.error is not None: | ||
raise Exception("Failed to send tx:", res.error) | ||
|
||
print("Tx dry run was successful!") | ||
|
||
except Exception as e: | ||
print(f"Error: {e}") | ||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
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,56 @@ | ||
// Copyright (c) 2025 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::str::FromStr; | ||
|
||
use base64ct::Encoding; | ||
use eyre::Result; | ||
use iota_graphql_client::Client; | ||
use iota_transaction_builder::{TransactionBuilder, res, unresolved::Argument}; | ||
use iota_types::Address; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let client = Client::new_devnet(); | ||
|
||
let sender_address = | ||
Address::from_str("0x611830d3641a68f94a690dcc25d1f4b0dac948325ac18f6dd32564371735f32c")?; | ||
|
||
let mut builder = TransactionBuilder::new(sender_address).with_client(client.clone()); | ||
builder | ||
.move_call(Address::STD_LIB, "u64", "max") | ||
.arguments((0u64, 1000u64)) | ||
// Assign a name to the result of this command | ||
.name("res0"); | ||
builder | ||
.move_call(Address::STD_LIB, "u64", "max") | ||
.arguments((1000u64, 2000u64)) | ||
.name("res1"); | ||
|
||
builder | ||
// Use the named results of previous commands to use as arguments | ||
.split_coins(Argument::Gas, [res("res0"), res("res1")]) | ||
// For nested results, a tuple or vec can be used to name them | ||
.name(vec!["coin0", "coin1"]); | ||
|
||
// Use named results as arguments | ||
builder.transfer_objects(sender_address, [res("coin0"), res("coin1")]); | ||
|
||
let tx = builder.to_owned().finish().await?; | ||
|
||
println!("Signing Digest: {}", hex::encode(tx.signing_digest())); | ||
println!( | ||
"Tx Bytes: {}", | ||
base64ct::Base64::encode_string(&bcs::to_bytes(&tx)?) | ||
); | ||
|
||
let res = client.dry_run_tx(&tx, false).await?; | ||
|
||
if let Some(err) = res.error { | ||
eyre::bail!("Failed to send tx: {err}"); | ||
} | ||
|
||
println!("Tx dry run was successful!"); | ||
|
||
Ok(()) | ||
} |
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.