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: EncoderImplementations§
Source§impl ScriptBuilder
impl ScriptBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new ScriptBuilder instance.
§Examples
use neo3::neo_builder::ScriptBuilder;
let builder = ScriptBuilder::new();Sourcepub fn op_code(&mut self, op_codes: &[OpCode]) -> &mut Self
pub fn op_code(&mut self, op_codes: &[OpCode]) -> &mut Self
Appends one or more opcodes to the script.
§Arguments
op_codes- A slice ofOpCodevalues 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]);Sourcepub fn op_code_with_arg(&mut self, opcode: OpCode, argument: Bytes) -> &mut Self
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- TheOpCodeto 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]);Sourcepub fn contract_call(
&mut self,
hash160: &H160,
method: &str,
params: &[ContractParameter],
call_flags: Option<CallFlags>,
) -> Result<&mut Self, BuilderError>
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 ofContractParametervalues to pass as arguments to the method.call_flags- An optionalCallFlagsvalue 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)
);Sourcepub fn sys_call(&mut self, operation: InteropService) -> &mut Self
pub fn sys_call(&mut self, operation: InteropService) -> &mut Self
Appends a system call operation to the script.
§Arguments
operation- TheInteropServiceto 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);Sourcepub fn push_params(
&mut self,
params: &[ContractParameter],
) -> Result<&mut Self, BuilderError>
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 ofContractParametervalues 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)
]);Sourcepub fn push_param(
&mut self,
param: &ContractParameter,
) -> Result<&mut Self, BuilderError>
pub fn push_param( &mut self, param: &ContractParameter, ) -> Result<&mut Self, BuilderError>
Pushes a single contract parameter to the script.
§Arguments
param- TheContractParametervalue 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();Sourcepub fn push_integer(&mut self, i: BigInt) -> &mut Self
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));Sourcepub fn push_opcode_bytes(
&mut self,
opcode: OpCode,
argument: Vec<u8>,
) -> &mut ScriptBuilder
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 appendargument- 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]);Sourcepub fn push_array(
&mut self,
arr: &[ContractParameter],
) -> Result<&mut Self, BuilderError>
pub fn push_array( &mut self, arr: &[ContractParameter], ) -> Result<&mut Self, BuilderError>
Sourcepub fn push_map(
&mut self,
map: &HashMap<ContractParameter, ContractParameter>,
) -> Result<&mut Self, BuilderError>
pub fn push_map( &mut self, map: &HashMap<ContractParameter, ContractParameter>, ) -> Result<&mut Self, BuilderError>
Sourcepub fn pack(&mut self) -> &mut Self
pub fn pack(&mut self) -> &mut Self
Appends the Pack opcode to the script.
§Returns
A mutable reference to the ScriptBuilder for method chaining.
Sourcepub fn build_verification_script(pub_key: &Secp256r1PublicKey) -> Bytes
pub fn build_verification_script(pub_key: &Secp256r1PublicKey) -> Bytes
Sourcepub fn build_multi_sig_script(
pubkeys: &mut [Secp256r1PublicKey],
threshold: u8,
) -> Result<Bytes, BuilderError>
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 ofSecp256r1PublicKeyvalues 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.
Sourcepub fn build_contract_script(
sender: &H160,
nef_checksum: u32,
name: &str,
) -> Result<Bytes, BuilderError>
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.
Sourcepub fn build_contract_call_and_unwrap_iterator(
contract_hash: &H160,
method: &str,
params: &[ContractParameter],
max_items: u32,
call_flags: Option<CallFlags>,
) -> Result<Bytes, BuilderError>
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 ofContractParametervalues to pass as arguments to the method.max_items- The maximum number of items to retrieve from the iterator.call_flags- An optionalCallFlagsvalue 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,
¶ms,
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 stackSourcepub fn len(&self) -> usize
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
impl Debug for ScriptBuilder
Source§impl Hash for ScriptBuilder
impl Hash for ScriptBuilder
Source§impl PartialEq for ScriptBuilder
impl PartialEq for ScriptBuilder
impl Eq for ScriptBuilder
impl StructuralPartialEq for ScriptBuilder
Auto Trait Implementations§
impl Freeze for ScriptBuilder
impl RefUnwindSafe for ScriptBuilder
impl Send for ScriptBuilder
impl Sync for ScriptBuilder
impl Unpin for ScriptBuilder
impl UnwindSafe for ScriptBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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