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 totrue, 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
impl AptosContainer
Sourcepub fn get_node_url(&self) -> String
pub fn get_node_url(&self) -> String
Get node_url from AptosContainer
Sourcepub fn get_chain_id(&self) -> u8
pub fn get_chain_id(&self) -> u8
Get chain_id from AptosContainer
Sourcepub async fn get_initiated_accounts(&self) -> Result<Vec<String>>
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 asStringif 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();
}Sourcepub async fn run_command(&self, command: &str) -> Result<(String, String)>
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 thestdoutandstderroutputs 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)
}Sourcepub async fn run(
&self,
number_of_accounts: usize,
callback: impl FnOnce(Vec<String>) -> Pin<Box<dyn Future<Output = Result<()>>>>,
) -> Result<()>
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 aFuturewrapped in aPinand boxed as a dynamic traitFuture<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(())
})
});
}Sourcepub async fn run_script(
&self,
local_dir: impl AsRef<Path>,
private_key: &str,
named_addresses: &HashMap<String, String>,
script_paths: &Vec<&str>,
) -> Result<()>
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 thelocal_dirwhere 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();
}Sourcepub 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<()>
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. IfNone, 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§
impl !Freeze for AptosContainer
impl !RefUnwindSafe for AptosContainer
impl !UnwindSafe for AptosContainer
impl Send for AptosContainer
impl Sync for AptosContainer
impl Unpin for AptosContainer
impl UnsafeUnpin for AptosContainer
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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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