pub struct AptosContainer { /* private fields */ }
Expand description

AptosContainer is a struct that encapsulates the configuration and runtime details for managing an Aptos node and its associated resources within a Docker container.

§Fields

  • node_url - URL for accessing the Aptos node from external systems.

  • inner_url - Internal URL for accessing the Aptos node from within the container or local environment.

  • chain_id - Chain ID for the network.

  • deploy_contract - If set to true, contracts will be deployed upon initialization.

  • override_accounts - Flag indicating whether to deploy contracts to the Aptos node. Optional list of account addresses to override default accounts.

  • container - The Docker container instance running the Aptos node or shell.

  • contract_path - Path to the directory where contract files are stored.

  • contracts - A mutex-protected set of contracts.

  • accounts - A read-write lock protecting a list of account addresses.

  • accounts_channel_rx - A mutex-protected optional receiver for account-related communication channels.

  • accounts_channel_tx - A read-write lock protecting an optional sender for account-related communication channels.

Implementations§

source§

impl AptosContainer

source

pub async fn init() -> Result<Self>

Initializes a AptosContainer.

§Returns

A new AptosContainer instance.

§Example
use aptos_testcontainer::aptos_container::AptosContainer;

#[tokio::main]
async fn main() {
    let aptos_container = AptosContainer::init().await.unwrap();
}
source§

impl AptosContainer

source

pub fn get_node_url(&self) -> String

Get node_url from AptosContainer

source

pub fn get_chain_id(&self) -> u8

Get chain_id from AptosContainer

source

pub async fn get_initiated_accounts(&self) -> Result<Vec<String>>

Retrieves the list of accounts, either from an overridden source or from an initialized state.

§Returns
  • Result<Vec<String>> - A vector containing the accounts as String if successful.
§Example
use aptos_testcontainer::aptos_container::AptosContainer;

#[tokio::main]
async fn main() {
    let aptos_container = AptosContainer::init().await.unwrap();
    let accounts = aptos_container.get_initiated_accounts().await.unwrap();
}
source

pub async fn run_command(&self, command: &str) -> Result<(String, String)>

Executes a shell command inside the Docker container.

§Arguments
  • command - A string representing the shell command to execute inside the container.
§Returns
  • Result<(String, String)> - A tuple containing the stdout and stderr outputs from the command execution.
§Example
use aptos_testcontainer::aptos_container::AptosContainer;

#[tokio::main]
async fn main() {
    let aptos_container = AptosContainer::init().await.unwrap();
    let command = "bin/sh -c mkdir my_file".to_string();
    let (stdout, stderr) = aptos_container.run_command(&command).await.unwrap();
    println!("stdout: {:?}", stdout);
    println!("stderr: {:?}", stderr)
}
source

pub async fn run( &self, number_of_accounts: usize, callback: impl FnOnce(Vec<String>) -> Pin<Box<dyn Future<Output = Result<()>>>>, ) -> Result<()>

This async function handles account initialization and execution of a callback function with the provided or received accounts.

§Parameters:
  • number_of_accounts: The number of accounts required for the operation.
  • callback: A closure that takes the accounts and returns a Future wrapped in a Pin and boxed as a dynamic trait Future<Output = Result<()>>.
§Example
use aptos_testcontainer::aptos_container::AptosContainer;
use aptos_testcontainer::utils::get_account_address;
use std::collections::HashMap;

#[tokio::main]
async fn main() {
    let aptos_containe = AptosContainer::init().await.unwrap();
    let _ = aptos_containe.run(2, |accounts| {
            Box::pin(async move {
                let aptos_container = AptosContainer::init().await.unwrap();
                let accounts = aptos_container.get_initiated_accounts().await.unwrap();
                let module_account_private_key = accounts.first().unwrap();
                let module_account_address = get_account_address(module_account_private_key);
                let mut named_addresses = HashMap::new();
                named_addresses.insert("verifier_addr".to_string(), module_account_address);
                aptos_container
                    .upload_contract(
                        "./contract-samples/sample1",
                        module_account_private_key,
                        &named_addresses,
                        None,
                        false,
                    )
                    .await
                    .unwrap();
                Ok(())
            })
    });
}
source

pub async fn run_script( &self, local_dir: impl AsRef<Path>, private_key: &str, named_addresses: &HashMap<String, String>, script_paths: &Vec<&str>, ) -> Result<()>

Executes a script located within the specified directory.

§Parameters
  • local_dir: The directory path containing contract code.
  • private_key: The private key of the account that will sign and execute the scripts.
  • named_addresses: A mapping of named addresses used for the script compilation.
  • script_paths: A vector of sub-directory paths within the local_dir where the scripts are located.
§Example
use std::collections::HashMap;
use aptos_testcontainer::aptos_container::AptosContainer;
use aptos_testcontainer::utils::get_account_address;

#[tokio::main]
async fn main() {
    let aptos_container = AptosContainer::init().await.unwrap();
    let accounts = aptos_container.get_initiated_accounts().await.unwrap();
    let module_account_private_key = accounts.first().unwrap();
    let module_account_address = get_account_address(module_account_private_key);

    let local_dir = "./contract-samples/sample2";

    let mut named_addresses = HashMap::new();
    named_addresses.insert("verifier_addr".to_string(), module_account_address.clone());
    named_addresses.insert("lib_addr".to_string(), module_account_address);
    aptos_container
        .run_script(
        local_dir,
        module_account_private_key,
        &named_addresses,
        &vec!["verifier"],
        )
    .await
    .unwrap();
}
source

pub async fn upload_contract( &self, local_dir: &str, private_key: &str, named_addresses: &HashMap<String, String>, sub_packages: Option<Vec<&str>>, override_contract: bool, ) -> Result<()>

Uploads smart contracts to the Aptos node, optionally overriding existing contracts and handling sub-packages.

§Arguments
  • local_dir - The local directory containing the contract files.
  • private_key - The private key used for publishing the contract.
  • named_addresses - A hash map of named addresses for the contracts.
  • sub_packages - Optional list of sub-packages to handle separately. If None, the entire contract directory is handled as a whole.
  • override_contract - A boolean flag indicating whether to override existing contracts.
§Example
use std::collections::HashMap;
use aptos_testcontainer::aptos_container::AptosContainer;
use aptos_testcontainer::utils::get_account_address;

#[tokio::main]
async fn main() {
    let aptos_container = AptosContainer::init().await.unwrap();
    let accounts = aptos_container.get_initiated_accounts().await.unwrap();
    let module_account_private_key = accounts.first().unwrap();
    let module_account_address = get_account_address(module_account_private_key);
    let mut named_addresses = HashMap::new();
    named_addresses.insert("verifier_addr".to_string(), module_account_address);
    aptos_container
        .upload_contract(
            "./contract-samples/sample1",
            module_account_private_key,
            &named_addresses,
            None,
            false,
        )
    .await
    .unwrap();
}

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
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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
source§

impl<T> IntoResult<T> for T

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.
source§

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

source§

fn vzip(self) -> V

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