Skip to content

Commit cf25077

Browse files
[Feature]Runtime packages (#15)
* feat: boostraped move * feat: dependancy graph * feat: grpc only support * fix: align Sui dependency sources * feat: small fix Signed-off-by: David <davidrotari9@gmail.com> * chore: small fix --------- Signed-off-by: David <davidrotari9@gmail.com> Signed-off-by: David <70471875+davidrotari19@users.noreply.github.com>
1 parent aef3cdd commit cf25077

23 files changed

Lines changed: 3095 additions & 232 deletions

File tree

‎sui-move-call/src/lib.rs‎

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,55 @@ pub enum CallSpecError {
365365
/// The provided function string is not a valid Move identifier.
366366
#[error("invalid Move identifier for function: `{0}`")]
367367
Function(String),
368+
/// A call argument could not be encoded or converted.
369+
#[error(transparent)]
370+
Arg(#[from] CallArgError),
371+
}
372+
373+
/// Move call target without encoded arguments.
374+
///
375+
/// This is the lower-level boundary for PTB composition: generated code owns package, module,
376+
/// function, and type arguments, while `sui-move-ptb` can wire arbitrary PTB `Argument`s including
377+
/// previous command results.
378+
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
379+
pub struct CallTarget {
380+
/// Package ID that contains the Move module.
381+
pub package: Address,
382+
/// Move module identifier.
383+
pub module: Identifier,
384+
/// Move function identifier.
385+
pub function: Identifier,
386+
/// Type arguments for the call (Move `TypeTag`s).
387+
pub type_arguments: Vec<TypeTag>,
388+
}
389+
390+
impl CallTarget {
391+
/// Create an empty call target for a `(package, module, function)` triple.
392+
pub fn new(
393+
package: Address,
394+
module: impl AsRef<str>,
395+
function: impl AsRef<str>,
396+
) -> Result<Self, CallSpecError> {
397+
let module_str = module.as_ref();
398+
let function_str = function.as_ref();
399+
400+
let module = Identifier::from_str(module_str)
401+
.map_err(|_| CallSpecError::Module(module_str.to_string()))?;
402+
let function = Identifier::from_str(function_str)
403+
.map_err(|_| CallSpecError::Function(function_str.to_string()))?;
404+
405+
Ok(Self {
406+
package,
407+
module,
408+
function,
409+
type_arguments: Vec::new(),
410+
})
411+
}
412+
413+
/// Append a type argument derived from a `MoveType`.
414+
pub fn push_type_arg<T: MoveType>(&mut self) {
415+
self.type_arguments.push(T::type_tag_static());
416+
}
368417
}
369418

370419
/// A description of a Move function call.
@@ -419,21 +468,30 @@ impl CallSpec {
419468
module: impl AsRef<str>,
420469
function: impl AsRef<str>,
421470
) -> Result<Self, CallSpecError> {
422-
let module_str = module.as_ref();
423-
let function_str = function.as_ref();
424-
425-
let module = Identifier::from_str(module_str)
426-
.map_err(|_| CallSpecError::Module(module_str.to_string()))?;
427-
let function = Identifier::from_str(function_str)
428-
.map_err(|_| CallSpecError::Function(function_str.to_string()))?;
471+
Ok(Self::from_target(CallTarget::new(
472+
package, module, function,
473+
)?))
474+
}
429475

430-
Ok(Self {
431-
package,
432-
module,
433-
function,
434-
type_arguments: Vec::new(),
476+
/// Create an empty call spec from a generated call target.
477+
pub fn from_target(target: CallTarget) -> Self {
478+
Self {
479+
package: target.package,
480+
module: target.module,
481+
function: target.function,
482+
type_arguments: target.type_arguments,
435483
arguments: Vec::new(),
436-
})
484+
}
485+
}
486+
487+
/// Return this call spec's target without its encoded arguments.
488+
pub fn target(&self) -> CallTarget {
489+
CallTarget {
490+
package: self.package,
491+
module: self.module.clone(),
492+
function: self.function.clone(),
493+
type_arguments: self.type_arguments.clone(),
494+
}
437495
}
438496

439497
/// Append a type argument derived from a `MoveType`.
@@ -471,8 +529,8 @@ impl CallSpec {
471529
/// Convenience re-exports for downstream code.
472530
pub mod prelude {
473531
pub use crate::{
474-
CallArg, CallArgError, CallSpec, CallSpecError, MoveObject, ObjectArg, ReceivingMoveObject,
475-
SharedMoveObject, ToCallArg, ToCallArgMut,
532+
CallArg, CallArgError, CallSpec, CallSpecError, CallTarget, MoveObject, ObjectArg,
533+
ReceivingMoveObject, SharedMoveObject, ToCallArg, ToCallArgMut,
476534
};
477535
pub use sui_move::prelude::*;
478536
pub use sui_sdk_types::Mutability;

‎sui-move-codegen/README.md‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,48 @@ The pipeline is intentionally split in two:
3333
(`NormalizedPackage`).
3434
2. **Render (offline)**: render Rust source from that IR.
3535

36-
Because the IR is JSON-friendly, you can commit it and re-render deterministically in CI without
36+
Because the IR is JSON-serializable, you can commit it and re-render deterministically in CI without
3737
needing network access.
3838

3939
## What gets generated
4040

4141
Given a `NormalizedPackage` (either fetched from gRPC or loaded from JSON), this crate can render:
4242

43-
- A `pub const PACKAGE: Address` (the on-chain package id)
43+
- `CALL_PACKAGE` / `TYPE_PACKAGE` constants for generated calls and type identity
44+
- `call_package()` / `type_package()` / `with_packages(...)` helpers for scoped package-id overrides
4445
- One Rust module per Move module (or a flat layout via `RenderOptions::flatten`)
4546
- Move datatypes as Rust types (structs use `#[sui_move::move_struct]` via `sui-move`’s `derive`
4647
feature)
47-
- Move functions as Rust functions that return `sui_move_call::CallSpec`
48+
- Move functions as generated `*_target` functions
49+
- Optional typed Rust functions that return `sui_move_call::CallSpec`
4850
- (optional) A `TxExt` trait implemented for `sui_move_runtime::Tx` (enable with
4951
`RenderOptions::emit_tx_ext`)
5052

5153
Those generated call builders are designed to be used directly in higher layers:
5254
- `sui-move-ptb` can consume `CallSpec` to build a `ProgrammableTransaction`
5355
- `sui-move-runtime` can consume `CallSpec` via its tx builder (or `sui_move_runtime::tx!`)
5456

57+
Use `with_packages` when generated calls target one deployment package while type tags retain the
58+
package that defines the Move types:
59+
60+
```rust
61+
# use sui_sdk_types::Address;
62+
# mod bindings {
63+
# pub fn with_packages<R>(
64+
# _call_package: sui_sdk_types::Address,
65+
# _type_package: sui_sdk_types::Address,
66+
# f: impl FnOnce() -> R,
67+
# ) -> R { f() }
68+
# pub mod m {
69+
# pub fn f(_arg0: u64) {}
70+
# }
71+
# }
72+
# let localnet_package = Address::ZERO;
73+
bindings::with_packages(localnet_package, localnet_package, || {
74+
bindings::m::f(10);
75+
});
76+
```
77+
5578
## Example: render from an in-memory IR
5679

5780
```rust
@@ -101,7 +124,7 @@ let pkg = NormalizedPackage {
101124
};
102125

103126
let code = render_package(&pkg, &RenderOptions::default());
104-
assert!(code.contains("pub const PACKAGE"));
127+
assert!(code.contains("pub const CALL_PACKAGE"));
105128
assert!(code.contains("pub struct S"));
106129
assert!(code.contains("pub fn f"));
107130
```
@@ -185,7 +208,7 @@ let pkg = NormalizedPackage {
185208
};
186209

187210
let code = render_package(&pkg, &RenderOptions::default());
188-
let start = code.find("pub fn mutate").unwrap();
211+
let start = code.find("pub fn mutate(").unwrap();
189212
let sig_end = start + code[start..].find('{').unwrap();
190213
let sig = &code[start..sig_end];
191214

@@ -312,14 +335,14 @@ let pkg = NormalizedPackage {
312335
};
313336

314337
let code = render_package(&pkg, &RenderOptions::default());
315-
let start = code.find("pub fn id").unwrap();
338+
let start = code.find("pub fn id<T0>(").unwrap();
316339
let sig_end = start + code[start..].find('{').unwrap();
317340
let sig = &code[start..sig_end];
318341

319342
assert!(sig.contains("pub fn id<T0>(arg0: Vec<T0>)"));
320343
assert!(sig.contains("where"));
321344
assert!(sig.contains("T0: sm::MoveType + sm::HasStore"));
322-
assert!(code.contains("spec.push_type_arg::<T0>();"));
345+
assert!(code.contains("target.push_type_arg::<T0>();"));
323346
```
324347

325348
## Example: fetch over gRPC
@@ -334,14 +357,14 @@ let package_id: Address = "0x2".parse()?;
334357
335358
let pkg = fetch_package(&mut client, package_id).await?;
336359
let json = pkg.to_json_string()?;
337-
println!("{json}");
360+
println!("{} bytes", json.len());
338361
# Ok(())
339362
# }
340363
```
341364

342365
## Recommended workflow
343366

344-
To keep builds deterministic, fetch metadata once and commit it (JSON), then render from JSON:
367+
To keep builds deterministic, fetch metadata once and commit it as JSON, then render from JSON:
345368

346369
1. Fetch and save `NormalizedPackage` JSON (out-of-band; not in `build.rs`)
347370
2. Render Rust bindings from that JSON during builds or as a pre-generation step

0 commit comments

Comments
 (0)