Struct ScriptBuilder

Source
pub struct ScriptBuilder {
    pub script: Encoder,
}
Expand description

A builder for constructing Neo smart contract scripts.

The ScriptBuilder provides methods to create and manipulate scripts by adding opcodes, pushing data, and performing various operations required for Neo smart contract execution.

§Examples

use neo3::neo_builder::ScriptBuilder;
use neo3::neo_types::OpCode;
use num_bigint::BigInt;

let mut builder = ScriptBuilder::new();
builder.push_integer(BigInt::from(42))
       .push_data("Hello, Neo!".as_bytes().to_vec())
       .op_code(&[OpCode::Add]);

let script = builder.to_bytes();

Fields§

§script: Encoder

Implementations§

Source§

impl ScriptBuilder

Source

pub fn script(&self) -> &Encoder

Source§

impl ScriptBuilder

Source

pub fn new() -> Self

Creates a new ScriptBuilder instance.

§Examples
use neo3::neo_builder::ScriptBuilder;

let builder = ScriptBuilder::new();
Source

pub fn op_code(&mut self, op_codes: &[OpCode]) -> &mut Self

Appends one or more opcodes to the script.

§Arguments
  • op_codes - A slice of OpCode values to append to the script.
§Returns

A mutable reference to the ScriptBuilder for method chaining.

§Examples
use neo3::neo_builder::ScriptBuilder;
use neo3::neo_types::OpCode;

let mut builder = ScriptBuilder::new();
builder.op_code(&[OpCode::Push1, OpCode::Push2, OpCode::Add]);
Source

pub fn op_code_with_arg(&mut self, opcode: OpCode, argument: Bytes) -> &mut Self

Appends an opcode with an argument to the script.

§Arguments
  • opcode - The OpCode to append.
  • argument - The data argument for the opcode.
§Returns

A mutable reference to the ScriptBuilder for method chaining.

§Examples
use neo3::neo_builder::ScriptBuilder;
use neo3::neo_types::OpCode;

let mut builder = ScriptBuilder::new();
builder.op_code_with_arg(OpCode::PushData1, vec![0x01, 0x02, 0x03]);
Source

pub fn contract_call( &mut self, hash160: &H160, method: &str, params: &[ContractParameter], call_flags: Option<CallFlags>, ) -> Result<&mut Self, BuilderError>

Appends a contract call operation to the script.

§Arguments
  • hash160 - The 160-bit hash of the contract to call.
  • method - The name of the method to call.
  • params - A slice of ContractParameter values to pass as arguments to the method.
  • call_flags - An optional CallFlags value specifying the call flags.
§Returns

A Result containing a mutable reference to the ScriptBuilder for method chaining, or a BuilderError if an error occurs.

§Examples
use neo3::neo_builder::ScriptBuilder;
use primitive_types::H160;
use neo3::neo_types::ContractParameter;
use neo3::builder::CallFlags;

let mut builder = ScriptBuilder::new();
let contract_hash = H160::from_slice(&[0; 20]);
let result = builder.contract_call(
    &contract_hash,
    "transfer",
    &[ContractParameter::from("NeoToken"), ContractParameter::from(1000)],
    Some(CallFlags::All)
);
Source

pub fn sys_call(&mut self, operation: InteropService) -> &mut Self

Appends a system call operation to the script.

§Arguments
  • operation - The InteropService to call.
§Returns

A mutable reference to the ScriptBuilder for method chaining.

§Examples
use neo3::neo_builder::ScriptBuilder;
use neo3::builder::InteropService;

let mut builder = ScriptBuilder::new();
builder.sys_call(InteropService::SystemRuntimeCheckWitness);
Source

pub fn push_params( &mut self, params: &[ContractParameter], ) -> Result<&mut Self, BuilderError>

Pushes an array of contract parameters to the script.

§Arguments
  • params - A slice of ContractParameter values to push to the script.
§Returns

A mutable reference to the ScriptBuilder for method chaining.

§Examples
use neo3::neo_builder::ScriptBuilder;
use neo3::neo_types::ContractParameter;

let mut builder = ScriptBuilder::new();
builder.push_params(&[
    ContractParameter::from("param1"),
    ContractParameter::from(42),
    ContractParameter::from(true)
]);
Source

pub fn push_param( &mut self, param: &ContractParameter, ) -> Result<&mut Self, BuilderError>

Pushes a single contract parameter to the script.

§Arguments
  • param - The ContractParameter value to push to the script.
§Returns

A Result containing a mutable reference to the ScriptBuilder for method chaining, or a BuilderError if an error occurs.

§Examples
use neo3::neo_builder::ScriptBuilder;
use neo3::neo_types::ContractParameter;

let mut builder = ScriptBuilder::new();
builder.push_param(&ContractParameter::from("Hello, Neo!")).unwrap();
Source

pub fn push_integer(&mut self, i: BigInt) -> &mut Self

Adds a push operation with the given integer to the script.

The integer is encoded in its two’s complement representation and little-endian byte order.

The integer can be up to 32 bytes in length. Values larger than 32 bytes will return an error.

§Arguments
  • i - The integer to push to the script
§Returns

A mutable reference to the ScriptBuilder for method chaining.

§Examples
use neo3::neo_builder::ScriptBuilder;
use num_bigint::BigInt;

let mut builder = ScriptBuilder::new();
builder.push_integer(BigInt::from(42));
Source

pub fn push_opcode_bytes( &mut self, opcode: OpCode, argument: Vec<u8>, ) -> &mut ScriptBuilder

Append opcodes to the script in the provided order.

§Arguments
  • opcode - The opcode to append
  • argument - The data argument for the opcode
§Returns

A mutable reference to the ScriptBuilder for method chaining.

§Examples
use neo3::neo_builder::ScriptBuilder;
use neo3::neo_types::OpCode;

let mut builder = ScriptBuilder::new();
builder.push_opcode_bytes(OpCode::PushData1, vec![0x01, 0x02, 0x03]);
Source

pub fn push_data(&mut self, data: Vec<u8>) -> &mut Self

Pushes data to the script.

§Arguments
  • data - The data to push to the script.
§Returns

A mutable reference to the ScriptBuilder for method chaining.

§Examples
use neo3::neo_builder::ScriptBuilder;

let mut builder = ScriptBuilder::new();
builder.push_data("Hello, Neo!".as_bytes().to_vec());
Source

pub fn push_bool(&mut self, b: bool) -> &mut Self

Pushes a boolean value to the script.

§Arguments
  • b - The boolean value to push to the script.
§Returns

A mutable reference to the ScriptBuilder for method chaining.

§Examples
use neo3::neo_builder::ScriptBuilder;

let mut builder = ScriptBuilder::new();
builder.push_bool(true);
Source

pub fn push_array( &mut self, arr: &[ContractParameter], ) -> Result<&mut Self, BuilderError>

Pushes an array of contract parameters to the script.

§Arguments
  • arr - A slice of ContractParameter values to push to the script.
§Returns

A Result containing a mutable reference to the ScriptBuilder for method chaining, or a BuilderError if an error occurs.

Source

pub fn push_map( &mut self, map: &HashMap<ContractParameter, ContractParameter>, ) -> Result<&mut Self, BuilderError>

Pushes a map of contract parameters to the script.

§Arguments
  • map - A reference to a HashMap mapping ContractParameter keys to ContractParameter values.
§Returns

A Result containing a mutable reference to the ScriptBuilder for method chaining, or a BuilderError if an error occurs.

Source

pub fn pack(&mut self) -> &mut Self

Appends the Pack opcode to the script.

§Returns

A mutable reference to the ScriptBuilder for method chaining.

Source

pub fn to_bytes(&self) -> Bytes

Returns the script as a Bytes object.

Source

pub fn build_verification_script(pub_key: &Secp256r1PublicKey) -> Bytes

Builds a verification script for the given public key.

§Arguments
  • pub_key - The public key to use for verification.
§Returns

A Bytes object containing the verification script.

Source

pub fn build_multi_sig_script( pubkeys: &mut [Secp256r1PublicKey], threshold: u8, ) -> Result<Bytes, BuilderError>

Builds a multi-signature script for the given public keys and threshold.

§Arguments
  • pubkeys - A mutable slice of Secp256r1PublicKey values representing the public keys.
  • threshold - The minimum number of signatures required to validate the script.
§Returns

A Result containing a Bytes object containing the multi-signature script, or a BuilderError if an error occurs.

Source

pub fn build_contract_script( sender: &H160, nef_checksum: u32, name: &str, ) -> Result<Bytes, BuilderError>

Builds a contract script for the given sender, NEF checksum, and contract name.

This method creates a script for deploying a smart contract on the Neo N3 blockchain.

§Arguments
  • sender - The 160-bit hash of the contract sender.
  • nef_checksum - The checksum of the NEF (Neo Executable Format) file.
  • name - The name of the contract.
§Returns

A Result containing a Bytes object with the contract deployment script, or a BuilderError if an error occurs.

§Examples
use neo3::neo_builder::ScriptBuilder;
use primitive_types::H160;
use std::str::FromStr;

let sender = H160::from_str("0xd2a4cff31913016155e38e474a2c06d08be276cf").unwrap();
let nef_checksum = 1234567890;
let name = "MyContract";

let script = ScriptBuilder::build_contract_script(&sender, nef_checksum, name).unwrap();
  • nef_checksum - The checksum of the NEF file.
  • name - The name of the contract.
§Returns

A Result containing a Bytes object containing the contract script, or a BuilderError if an error occurs.

Source

pub fn build_contract_call_and_unwrap_iterator( contract_hash: &H160, method: &str, params: &[ContractParameter], max_items: u32, call_flags: Option<CallFlags>, ) -> Result<Bytes, BuilderError>

Builds a script that calls a contract method and unwraps the iterator result.

This method is particularly useful when calling contract methods that return iterators. It automatically handles the iteration process and collects the results into an array.

§Arguments
  • contract_hash - The 160-bit hash of the contract to call.
  • method - The name of the method to call.
  • params - A slice of ContractParameter values to pass as arguments to the method.
  • max_items - The maximum number of items to retrieve from the iterator.
  • call_flags - An optional CallFlags value specifying the call flags.
§Returns

A Result containing a Bytes object with the script that calls the contract method and unwraps the iterator result into an array, or a BuilderError if an error occurs.

§Examples
use neo3::neo_builder::ScriptBuilder;
use neo3::neo_types::ContractParameter;
use neo3::builder::CallFlags;
use primitive_types::H160;
use std::str::FromStr;

// Call a contract method that returns an iterator and collect up to 100 items
let contract_hash = H160::from_str("0xd2a4cff31913016155e38e474a2c06d08be276cf").unwrap();
let method = "getTokens";
let params = vec![ContractParameter::from("owner_address")];
let max_items = 100;

// Build the script
let script = ScriptBuilder::build_contract_call_and_unwrap_iterator(
    &contract_hash,
    method,
    &params,
    max_items,
    Some(CallFlags::All)
).unwrap();

// The resulting script will:
// 1. Call the contract method
// 2. Iterate through the returned iterator
// 3. Collect up to max_items into an array
// 4. Leave the array on the stack
Source

pub fn len(&self) -> usize

Returns the length of the script in bytes.

This method is useful for determining the current position in the script, which is needed for calculating jump offsets in control flow operations.

§Returns

The length of the script in bytes.

§Examples
use neo3::neo_builder::ScriptBuilder;

let mut builder = ScriptBuilder::new();
builder.push_data("Hello, Neo!".as_bytes().to_vec());
let script_length = builder.len();
println!("Script length: {} bytes", script_length);

Trait Implementations§

Source§

impl Debug for ScriptBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for ScriptBuilder

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for ScriptBuilder

Source§

fn eq(&self, other: &ScriptBuilder) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for ScriptBuilder

Source§

impl StructuralPartialEq for ScriptBuilder

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,