Skip to content

Commit 3384e33

Browse files
DaughterOfMarsthibault-martinezAlex6323
authored
chore(iota-transaction-builder): Add crate documentation (#309)
* Add library docs * Add method docs and more info * missed * use main fn * review * Update crates/iota-transaction-builder/src/lib.rs Co-authored-by: Thibault Martinez <[email protected]> * fix fmt * review and add docs to FFI * fix link * fix docs * move lib doc to README * Remove feature flag section * Update crates/iota-sdk-ffi/src/transaction_builder/mod.rs Co-authored-by: /alex/ <[email protected]> * allow some more lints and re gen bindings * dprint and bindings again * Update crates/iota-sdk-ffi/README.md Co-authored-by: Thibault Martinez <[email protected]> * Remove useless doc links in ffi crate * bindings and fix links --------- Co-authored-by: Thibault Martinez <[email protected]> Co-authored-by: /alex/ <[email protected]>
1 parent f64425c commit 3384e33

File tree

18 files changed

+858
-492
lines changed

18 files changed

+858
-492
lines changed

bindings/go/iota_sdk_ffi/iota_sdk_ffi.go

Lines changed: 95 additions & 110 deletions
Large diffs are not rendered by default.

bindings/go/iota_sdk_ffi/iota_sdk_ffi.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4314,6 +4314,11 @@ void* uniffi_iota_sdk_ffi_fn_method_transactionbuilder_gas(void* ptr, void* obje
43144314
void* uniffi_iota_sdk_ffi_fn_method_transactionbuilder_gas_budget(void* ptr, uint64_t budget, RustCallStatus *out_status
43154315
);
43164316
#endif
4317+
#ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_METHOD_TRANSACTIONBUILDER_GAS_COINS
4318+
#define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_METHOD_TRANSACTIONBUILDER_GAS_COINS
4319+
void* uniffi_iota_sdk_ffi_fn_method_transactionbuilder_gas_coins(void* ptr, RustBuffer object_ids, RustCallStatus *out_status
4320+
);
4321+
#endif
43174322
#ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_METHOD_TRANSACTIONBUILDER_GAS_PRICE
43184323
#define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_FN_METHOD_TRANSACTIONBUILDER_GAS_PRICE
43194324
void* uniffi_iota_sdk_ffi_fn_method_transactionbuilder_gas_price(void* ptr, uint64_t price, RustCallStatus *out_status
@@ -7842,6 +7847,12 @@ uint16_t uniffi_iota_sdk_ffi_checksum_method_transactionbuilder_gas(void
78427847
#define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_TRANSACTIONBUILDER_GAS_BUDGET
78437848
uint16_t uniffi_iota_sdk_ffi_checksum_method_transactionbuilder_gas_budget(void
78447849

7850+
);
7851+
#endif
7852+
#ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_TRANSACTIONBUILDER_GAS_COINS
7853+
#define UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_TRANSACTIONBUILDER_GAS_COINS
7854+
uint16_t uniffi_iota_sdk_ffi_checksum_method_transactionbuilder_gas_coins(void
7855+
78457856
);
78467857
#endif
78477858
#ifndef UNIFFI_FFIDEF_UNIFFI_IOTA_SDK_FFI_CHECKSUM_METHOD_TRANSACTIONBUILDER_GAS_PRICE

bindings/kotlin/lib/iota_sdk/iota_sdk_ffi.kt

Lines changed: 103 additions & 110 deletions
Large diffs are not rendered by default.

bindings/python/lib/iota_sdk_ffi.py

Lines changed: 107 additions & 110 deletions
Large diffs are not rendered by default.

crates/iota-sdk-ffi/README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,116 @@
11
# IOTA SDK FFI
22

3+
Core type definitions for the IOTA blockchain.
4+
35
This crate can generate bindings for various languages using [UniFFI](https://github.com/mozilla/uniffi-rs).
46

7+
[IOTA](https://iota.org) is a next-generation smart contract platform with high throughput, low latency, and an asset-oriented programming model powered by the Move programming language. This crate provides type definitions for working with the data that makes up the IOTA blockchain.
8+
9+
## BCS
10+
11+
[BCS](https://docs.rs/bcs) is the serialization format used to represent the state of the blockchain and is used extensively throughout the IOTA ecosystem. In particular the BCS format is leveraged because it _"guarantees canonical serialization, meaning that for any given data type, there is a one-to-one correspondence between in-memory values and valid byte representations."_
12+
13+
One benefit of this property of having a canonical serialized representation is to allow different entities in the ecosystem to all agree on how a particular type should be interpreted and more importantly define a deterministic representation for hashing and signing.
14+
15+
This library strives to guarantee that the types defined are fully BCS-compatible with the data that the network produces. The one caveat to this would be that as the IOTA protocol evolves, new type variants are added and older versions of this library may not support those newly added variants. The expectation is that the most recent release of this library will support new variants and types as they are released to IOTA's `testnet` network.
16+
17+
See the documentation for the various types defined by this crate for a specification of their BCS serialized representation which will be defined using ABNF notation as described by [RFC-5234](https://datatracker.ietf.org/doc/html/rfc5234). In addition to the format itself, some types have an extra layer of verification and may impose additional restrictions on valid byte representations above and beyond those already provided by BCS. In these instances the documentation for those types will clearly specify these additional restrictions.
18+
19+
Here are some common rules:
20+
21+
```text
22+
; --- BCS Value ---
23+
bcs-value = bcs-struct / bcs-enum / bcs-length-prefixed / bcs-fixed-length
24+
bcs-length-prefixed = bytes / string / vector / option
25+
bcs-fixed-length = u8 / u16 / u32 / u64 / u128 /
26+
i8 / i16 / i32 / i64 / i128 /
27+
boolean
28+
bcs-struct = *bcs-value ; Sequence of serialized fields
29+
bcs-enum = uleb128-index bcs-value ; Enum index and associated value
30+
; --- Length-prefixed types ---
31+
bytes = uleb128 *OCTET ; Raw bytes of the specified length
32+
string = uleb128 *OCTET ; valid utf8 string of the specified length
33+
vector = uleb128 *bcs-value ; Length-prefixed list of values
34+
option = %x00 / (%x01 bcs-value) ; optional value
35+
; --- Fixed-length types ---
36+
u8 = OCTET ; 1-byte unsigned integer
37+
u16 = 2OCTET ; 2-byte unsigned integer, little-endian
38+
u32 = 4OCTET ; 4-byte unsigned integer, little-endian
39+
u64 = 8OCTET ; 8-byte unsigned integer, little-endian
40+
u128 = 16OCTET ; 16-byte unsigned integer, little-endian
41+
i8 = OCTET ; 1-byte signed integer
42+
i16 = 2OCTET ; 2-byte signed integer, little-endian
43+
i32 = 4OCTET ; 4-byte signed integer, little-endian
44+
i64 = 8OCTET ; 8-byte signed integer, little-endian
45+
i128 = 16OCTET ; 16-byte signed integer, little-endian
46+
boolean = %x00 / %x01 ; Boolean: 0 = false, 1 = true
47+
array = *(bcs-value) ; Fixed-length array
48+
; --- ULEB128 definition ---
49+
uleb128 = 1*5uleb128-byte ; Variable-length ULEB128 encoding
50+
uleb128-byte = %x00-7F / %x80-FF ; ULEB128 continuation rules
51+
uleb128-index = uleb128 ; ULEB128-encoded variant index
52+
```
53+
54+
## Transaction Builder
55+
56+
This crate contains the
57+
[TransactionBuilder](./src/transaction_builder/mod.rs), which allows
58+
for construction of Programmable Transactions which can be executed on the
59+
IOTA network.
60+
61+
### Methods
62+
63+
The following methods are available:
64+
65+
#### Commands
66+
67+
Each command method adds one or more commands to the final transaction. Some commands have optional follow-up methods. Most command results can be named, which allows them to be used later in the transaction via the `PTBArgument::Res` variant. When a single name is provided, the result will be named, and when a list of names is provided, the names will be used for the individual nested results.
68+
69+
- `move_call`: Call a move function.
70+
- `send_iota`: Send IOTA coins to a recipient address.
71+
- `send_coins`: Send coins of any type to a recipient address.
72+
- `merge_coins`: Merge a list of coins into a single primary coin.
73+
- `split_coins`: Split a coin into coins of various amounts.
74+
- `transfer_objects`: Send objects to a recipient address.
75+
- `publish`: Publish a move package.
76+
- `upgrade`: Upgrade a move package.
77+
- `make_move_vec`: Create a move `vector`.
78+
79+
#### Metadata
80+
81+
These methods set various metadata which may be needed for the execution.
82+
83+
- `gas`: Add a gas coin to pay for the execution.
84+
- `gas_coins`: Add gas coins to pay for the execution.
85+
- `gas_budget`: Set the maximum gas budget to spend.
86+
- `gas_price`: Set the gas price.
87+
- `sponsor`: Set the gas sponsor address.
88+
- `gas_station_sponsor`: Set the gas station URL. See [Gas Station](#gas-station) for more info.
89+
- `expiration`: Set the transaction expiration epoch.
90+
91+
### Finalization and Execution
92+
93+
There are several ways to finish the builder. First, the [finish](transaction_builder::TransactionBuilder::finish) method can be used to return the resulting [Transaction](iota_types::Transaction), which can be manually serialized, executed, etc.
94+
95+
Additionally, the builder can directly [dry_run](transaction_builder::TransactionBuilder::dry_run) or [execute](transaction_builder::TransactionBuilder::execute) the transaction.
96+
97+
When the transaction is resolved, the builder will try to ensure a valid state by de-duplicating and converting appropriate inputs into references to the gas coin. This means that the same input can be passed multiple times and the final transaction will only contain one instance. However, in some cases an invalid state can still be reached. For instance, if a coin is used both for gas and as part of a group of coins, i.e. when transferring objects, the transaction can not possibly be valid.
98+
99+
#### Defaults
100+
101+
The builder can set some values by default. The
102+
following are the default behaviors for each metadata value.
103+
104+
- Gas: One page of coins owned by the sender.
105+
- Gas Budget: A dry run will be used to estimate.
106+
- Gas Price: The current reference gas price.
107+
108+
### Gas Station
109+
110+
The Transaction Builder supports executing via a [Gas Station](https://github.com/iotaledger/gas-station). To do so, the URL, duration, and headers must be provided via [gas_station_sponsor](transaction_builder::TransactionBuilder::gas_station_sponsor).
111+
112+
By default the request will contain the header `Content-Type: application/json` When this data has been set, calling [execute](transaction_builder::TransactionBuilder::execute) will request gas from and send the resulting transaction to this endpoint instead of using the GraphQL client.
113+
5114
## Supported languages
6115

7116
- [Go](../../bindings/go)

crates/iota-sdk-ffi/src/faucet.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct FaucetClient(iota_graphql_client::faucet::FaucetClient);
1414
#[uniffi::export(async_runtime = "tokio")]
1515
impl FaucetClient {
1616
/// Construct a new `FaucetClient` with the given faucet service URL. This
17-
/// [`FaucetClient`] expects that the service provides two endpoints:
17+
/// `FaucetClient` expects that the service provides two endpoints:
1818
/// /v1/gas and /v1/status. As such, do not provide the request
1919
/// endpoint, just the top level service endpoint.
2020
///
@@ -68,7 +68,7 @@ impl FaucetClient {
6868

6969
/// Check the faucet request status.
7070
///
71-
/// Possible statuses are defined in: [`BatchSendStatusType`]
71+
/// Possible statuses are defined in: `BatchSendStatusType`
7272
pub async fn request_status(&self, id: String) -> Result<Option<BatchSendStatus>> {
7373
Ok(self
7474
.0

crates/iota-sdk-ffi/src/graphql.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl GraphQLClient {
230230
// Checkpoints API
231231
// ===========================================================================
232232

233-
/// Get the [`CheckpointSummary`] for a given checkpoint digest or
233+
/// Get the `CheckpointSummary` for a given checkpoint digest or
234234
/// checkpoint id. If none is provided, it will use the last known
235235
/// checkpoint id.
236236
#[uniffi::method(default(digest = None, seq_num = None))]
@@ -249,7 +249,7 @@ impl GraphQLClient {
249249
.map(Arc::new))
250250
}
251251

252-
/// Get a page of [`CheckpointSummary`] for the provided parameters.
252+
/// Get a page of `CheckpointSummary` for the provided parameters.
253253
#[uniffi::method(default(pagination_filter = None))]
254254
pub async fn checkpoints(
255255
&self,
@@ -339,7 +339,7 @@ impl GraphQLClient {
339339
// Objects API
340340
// ===========================================================================
341341

342-
/// Return an object based on the provided [`Address`].
342+
/// Return an object based on the provided `Address`.
343343
///
344344
/// If the object does not exist (e.g., due to pruning), this will return
345345
/// `Ok(None)`. Similarly, if this is not an object but an address, it
@@ -362,7 +362,7 @@ impl GraphQLClient {
362362

363363
/// Return a page of objects based on the provided parameters.
364364
///
365-
/// Use this function together with the [`ObjectFilter::owner`] to get the
365+
/// Use this function together with the `ObjectFilter::owner` to get the
366366
/// objects owned by an address.
367367
///
368368
/// # Example
@@ -395,8 +395,8 @@ impl GraphQLClient {
395395
.into())
396396
}
397397

398-
/// Return the object's bcs content [`Vec<u8>`] based on the provided
399-
/// [`Address`].
398+
/// Return the object's bcs content `Vec<u8>` based on the provided
399+
/// `Address`.
400400
pub async fn object_bcs(&self, object_id: &ObjectId) -> Result<Option<Vec<u8>>> {
401401
Ok(self.0.read().await.object_bcs(**object_id).await?)
402402
}
@@ -736,7 +736,7 @@ impl GraphQLClient {
736736
///
737737
/// The `name` argument is a json serialized type.
738738
///
739-
/// This returns [`DynamicFieldOutput`] which contains the name, the value
739+
/// This returns `DynamicFieldOutput` which contains the name, the value
740740
/// as json, and object.
741741
///
742742
/// # Example
@@ -771,7 +771,7 @@ impl GraphQLClient {
771771
///
772772
/// The `name` argument is a json serialized type.
773773
///
774-
/// This returns [`DynamicFieldOutput`] which contains the name, the value
774+
/// This returns `DynamicFieldOutput` which contains the name, the value
775775
/// as json, and object.
776776
pub async fn dynamic_object_field(
777777
&self,
@@ -791,7 +791,7 @@ impl GraphQLClient {
791791
/// Get a page of dynamic fields for the provided address. Note that this
792792
/// will also fetch dynamic fields on wrapped objects.
793793
///
794-
/// This returns [`Page`] of [`DynamicFieldOutput`]s.
794+
/// This returns a page of `DynamicFieldOutput`s.
795795
#[uniffi::method(default(pagination_filter = None))]
796796
pub async fn dynamic_fields(
797797
&self,
@@ -824,7 +824,7 @@ impl GraphQLClient {
824824
// Dry Run API
825825
// ===========================================================================
826826

827-
/// Dry run a [`Transaction`] and return the transaction effects and dry run
827+
/// Dry run a `Transaction` and return the transaction effects and dry run
828828
/// error (if any).
829829
///
830830
/// `skipChecks` optional flag disables the usual verification checks that
@@ -842,7 +842,7 @@ impl GraphQLClient {
842842
.into())
843843
}
844844

845-
/// Dry run a [`TransactionKind`] and return the transaction effects and dry
845+
/// Dry run a `TransactionKind` and return the transaction effects and dry
846846
/// run error (if any).
847847
///
848848
/// `skipChecks` optional flag disables the usual verification checks that

crates/iota-sdk-ffi/src/lib.rs

Lines changed: 12 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,22 @@
11
// Copyright (c) 2025 IOTA Stiftung
22
// SPDX-License-Identifier: Apache-2.0
33

4-
//! Core type definitions for the IOTA blockchain.
5-
//!
6-
//! [IOTA] is a next-generation smart contract platform with high throughput,
7-
//! low latency, and an asset-oriented programming model powered by the Move
8-
//! programming language. This crate provides type definitions for working with
9-
//! the data that makes up the IOTA blockchain.
10-
//!
11-
//! [IOTA]: https://iota.org
12-
//!
13-
//! # Feature flags
14-
//!
15-
//! This library uses a set of [feature flags] to reduce the number of
16-
//! dependencies and amount of compiled code. By default, no features are
17-
//! enabled which allows one to enable a subset specifically for their use case.
18-
//! Below is a list of the available feature flags.
19-
//!
20-
//! - `schemars`: Enables JSON schema generation using the [schemars] library.
21-
//! - `serde`: Enables support for serializing and deserializing types to/from
22-
//! BCS utilizing [serde] library.
23-
//! - `rand`: Enables support for generating random instances of a number of
24-
//! types via the [rand] library.
25-
//! - `hash`: Enables support for hashing, which is required for deriving
26-
//! addresses and calculating digests for various types.
27-
//! - `proptest`: Enables support for the [proptest] library by providing
28-
//! implementations of [proptest::arbitrary::Arbitrary] for many types.
29-
//!
30-
//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
31-
//! [serde]: https://docs.rs/serde
32-
//! [rand]: https://docs.rs/rand
33-
//! [proptest]: https://docs.rs/proptest
34-
//! [schemars]: https://docs.rs/schemars
35-
//! [proptest::arbitrary::Arbitrary]: https://docs.rs/proptest/latest/proptest/arbitrary/trait.Arbitrary.html
36-
//!
37-
//! # BCS
38-
//!
39-
//! [BCS] is the serialization format used to represent the state of the
40-
//! blockchain and is used extensively throughout the IOTA ecosystem. In
41-
//! particular the BCS format is leveraged because it _"guarantees canonical
42-
//! serialization, meaning that for any given data type, there is a one-to-one
43-
//! correspondence between in-memory values and valid byte representations."_
44-
//! One benefit of this property of having a canonical serialized representation
45-
//! is to allow different entities in the ecosystem to all agree on how a
46-
//! particular type should be interpreted and more importantly define a
47-
//! deterministic representation for hashing and signing.
48-
//!
49-
//! This library strives to guarantee that the types defined are fully
50-
//! BCS-compatible with the data that the network produces. The one caveat to
51-
//! this would be that as the IOTA protocol evolves, new type variants are added
52-
//! and older versions of this library may not support those newly
53-
//! added variants. The expectation is that the most recent release of this
54-
//! library will support new variants and types as they are released to IOTA's
55-
//! `testnet` network.
56-
//!
57-
//! See the documentation for the various types defined by this crate for a
58-
//! specification of their BCS serialized representation which will be defined
59-
//! using ABNF notation as described by [RFC-5234]. In addition to the format
60-
//! itself, some types have an extra layer of verification and may impose
61-
//! additional restrictions on valid byte representations above and beyond those
62-
//! already provided by BCS. In these instances the documentation for those
63-
//! types will clearly specify these additional restrictions.
64-
//!
65-
//! Here are some common rules:
66-
//!
67-
//! ```text
68-
//! ; --- BCS Value ---
69-
//! bcs-value = bcs-struct / bcs-enum / bcs-length-prefixed / bcs-fixed-length
70-
//! bcs-length-prefixed = bytes / string / vector / option
71-
//! bcs-fixed-length = u8 / u16 / u32 / u64 / u128 /
72-
//! i8 / i16 / i32 / i64 / i128 /
73-
//! boolean
74-
//! bcs-struct = *bcs-value ; Sequence of serialized fields
75-
//! bcs-enum = uleb128-index bcs-value ; Enum index and associated value
76-
//!
77-
//! ; --- Length-prefixed types ---
78-
//! bytes = uleb128 *OCTET ; Raw bytes of the specified length
79-
//! string = uleb128 *OCTET ; valid utf8 string of the specified length
80-
//! vector = uleb128 *bcs-value ; Length-prefixed list of values
81-
//! option = %x00 / (%x01 bcs-value) ; optional value
82-
//!
83-
//! ; --- Fixed-length types ---
84-
//! u8 = OCTET ; 1-byte unsigned integer
85-
//! u16 = 2OCTET ; 2-byte unsigned integer, little-endian
86-
//! u32 = 4OCTET ; 4-byte unsigned integer, little-endian
87-
//! u64 = 8OCTET ; 8-byte unsigned integer, little-endian
88-
//! u128 = 16OCTET ; 16-byte unsigned integer, little-endian
89-
//! i8 = OCTET ; 1-byte signed integer
90-
//! i16 = 2OCTET ; 2-byte signed integer, little-endian
91-
//! i32 = 4OCTET ; 4-byte signed integer, little-endian
92-
//! i64 = 8OCTET ; 8-byte signed integer, little-endian
93-
//! i128 = 16OCTET ; 16-byte signed integer, little-endian
94-
//! boolean = %x00 / %x01 ; Boolean: 0 = false, 1 = true
95-
//! array = *(bcs-value) ; Fixed-length array
96-
//!
97-
//! ; --- ULEB128 definition ---
98-
//! uleb128 = 1*5uleb128-byte ; Variable-length ULEB128 encoding
99-
//! uleb128-byte = %x00-7F / %x80-FF ; ULEB128 continuation rules
100-
//! uleb128-index = uleb128 ; ULEB128-encoded variant index
101-
//! ```
102-
//!
103-
//! [BCS]: https://docs.rs/bcs
104-
//! [RFC-5234]: https://datatracker.ietf.org/doc/html/rfc5234
105-
1064
#![expect(unused)]
107-
#![allow(clippy::wrong_self_convention)]
5+
#![allow(
6+
clippy::wrong_self_convention,
7+
clippy::should_implement_trait,
8+
clippy::new_without_default
9+
)]
10810

10911
use base64ct::Encoding;
11012

111-
mod crypto;
112-
mod error;
113-
mod faucet;
114-
mod graphql;
115-
mod transaction_builder;
116-
mod types;
117-
mod uniffi_helpers;
13+
pub mod crypto;
14+
pub mod error;
15+
pub mod faucet;
16+
pub mod graphql;
17+
pub mod transaction_builder;
18+
pub mod types;
19+
pub mod uniffi_helpers;
11820

11921
uniffi::setup_scaffolding!();
12022

0 commit comments

Comments
 (0)