Macro ptbuilder

Source
macro_rules! ptbuilder {
    ($builder:ident {}) => { ... };
    ($builder:ident {
        package $name:ident $value:literal;
        $($tt:tt)*
    }) => { ... };
    ($builder:ident {
        package $name:ident;
        $($tt:tt)*
    }) => { ... };
    ($builder:ident {
        package $name:ident: $value:expr_2021;
        $($tt:tt)*
    }) => { ... };
    ($builder:ident {
        input pure $name:ident;
        $($tt:tt)*
    }) => { ... };
    ($builder:ident {
        input pure $name:ident: $value:expr_2021;
        $($tt:tt)*
    }) => { ... };
    ($builder:ident {
        input obj $name:ident;
        $($tt:tt)*
    }) => { ... };
    ($builder:ident {
        input obj $name:ident: $value:expr_2021;
        $($tt:tt)*
    }) => { ... };
    ($builder:ident {
        type $T:ident;
        $($tt:tt)*
    }) => { ... };
    ($builder:ident {
        type $T:ident = $value:expr_2021;
        $($tt:tt)*
    }) => { ... };
    ($builder:ident {
        $package:ident::$module:ident::$fun:ident$(<$($T:ident),+>)?($($arg:ident),* $(,)?);
        $($tt:tt)*
    }) => { ... };
    ($builder:ident {
        let $ret:ident = $package:ident::$module:ident::$fun:ident$(<$($T:ident),+>)?($($arg:ident),* $(,)?);
        $($tt:tt)*
    }) => { ... };
    ($builder:ident {
        let ($($ret:ident),+) = $package:ident::$module:ident::$fun:ident$(<$($T:ident),+>)?($($arg:ident),* $(,)?);
        $($tt:tt)*
    }) => { ... };
    ($builder:ident {
        $(let $ret:ident =)? command! $variant:ident($($args:tt)*);
        $($tt:tt)*
    }) => { ... };
    ($builder:ident {
        let ($($ret:ident),+) = command! $variant:ident($($args:tt)*);
        $($tt:tt)*
    }) => { ... };
}
Expand description

Build a programmable transaction using Move-like syntax and an existing builder.

This will make the package, type, input and argument variables declared inside the macro available in the outer scope.

§Overview

This allows users to incrementally build a programmable transaction using an existing ProgrammableTransactionBuilder with the syntax

let mut builder = ProgrammableTransactionBuilder::new();
af_ptbuilder::ptbuilder!(builder {
    // ...
});

where everything inside the braces uses the same syntax as ptb!. The user is responsible for initializing the builder and calling ProgrammableTransactionBuilder::finish at the end.

This can be useful if the number of calls is only known at runtime or if it’s desirable to only include some calls based on some runtime logic. It still allows users to use a convenient syntax and separate what happens at ‘programmable transaction time’.