Skip to content

Commit

Permalink
Merge pull request #12 from BeakerTools/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
arthurvinci authored Oct 21, 2024
2 parents 9ab3679 + 11c65d0 commit df21e21
Show file tree
Hide file tree
Showing 12 changed files with 590 additions and 191 deletions.
84 changes: 75 additions & 9 deletions test-engine/src/call_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub struct CallBuilder<'a> {
output_manifest: Option<(String, String)>,
admin_badge: Vec<(ResourceAddress, Option<BTreeSet<NonFungibleLocalId>>)>,
with_trace: bool,
with_tx_fee: bool,
log_title: Option<&'a str>,
deposit_destination: ComponentAddress,
manifest_data: Option<TransactionManifestData>,
}
Expand All @@ -42,9 +44,27 @@ impl<'a> CallBuilder<'a> {
admin_badge: vec![],
with_trace: false,
manifest_data: None,
with_tx_fee: false,
log_title: None,
}
}

pub fn with_test_engine<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut TestEngine) -> R,
{
f(self.test_engine)
}

pub fn with_manifest_builder<F>(mut self, f: F) -> Self
where
F: FnOnce(ManifestBuilder) -> ManifestBuilder,
{
self.manifest_builder = f(self.manifest_builder);

self
}

/// Creates a call builder for a method call of the current component and skip the transaction execution.
///
/// # Arguments
Expand All @@ -61,7 +81,7 @@ impl<'a> CallBuilder<'a> {
/// * `entity_name`: reference name or address of the entity to call the method on.
/// * `method_name`: name of the method.
/// * `args`: environment arguments to call the method.
pub fn call_from_component<G: GlobalReference>(
pub fn call_from<G: GlobalReference>(
self,
entity_name: G,
method_name: &str,
Expand Down Expand Up @@ -101,11 +121,27 @@ impl<'a> CallBuilder<'a> {
true,
);

if let Some(title) = self.log_title {
Self::output_log_title(title);
}

Self::output_logs(&receipt);

if self.with_tx_fee {
Self::output_tx_fee(&receipt);
}

receipt
}

pub fn execute_and_expect_success(self) -> CommitResult {
self.execute().expect_commit_success().clone()
}

pub fn execute_and_expect_failure(self) -> CommitResult {
self.execute().expect_commit_failure().clone()
}

/// Deposits the batch to the given account.
///
/// # Arguments
Expand Down Expand Up @@ -142,21 +178,21 @@ impl<'a> CallBuilder<'a> {
pub fn transfer<
E: ReferenceName,
R: ReferenceName + Clone + 'static,
D: TryInto<Decimal> + Clone + 'static,
// D: TryInto<Decimal> + Clone + 'static,
>(
self,
recipient: E,
resource: R,
amount: D,
amount: Decimal,
) -> Self
where
<D as TryInto<Decimal>>::Error: std::fmt::Debug,
// where
// <D as TryInto<Decimal>>::Error: std::fmt::Debug,
{
self.call_from_component(
self.call_from(
recipient,
"try_deposit_or_abort",
vec![
Box::new(Fungible::Bucket(resource.clone(), amount)),
Box::new(Fungible::FromAccount(resource.clone(), amount)),
Box::new(None::<u64>),
],
)
Expand All @@ -174,11 +210,11 @@ impl<'a> CallBuilder<'a> {
resource: R,
ids: Vec<T>,
) -> Self {
self.call_from_component(
self.call_from(
recipient,
"try_deposit_or_abort",
vec![
Box::new(NonFungible::Bucket(
Box::new(NonFungible::FromAccount(
resource,
ids.into_iter().map(|id| id.to_id()).collect(),
)),
Expand Down Expand Up @@ -240,6 +276,20 @@ impl<'a> CallBuilder<'a> {
self
}

/// Displays tx fee or not.
///
/// # Arguments
/// * `trace`:
pub fn with_log_tx_fee(mut self) -> Self {
self.with_tx_fee = true;
self
}

pub fn with_log_title(mut self, title: &'a str) -> Self {
self.log_title = Some(title);
self
}

pub(crate) fn call_method_internal(
mut self,
component: impl ResolvableGlobalAddress,
Expand Down Expand Up @@ -294,8 +344,16 @@ impl<'a> CallBuilder<'a> {
false,
);

if let Some(title) = self.log_title {
Self::output_log_title(title);
}

Self::output_logs(&receipt);

if self.with_tx_fee {
Self::output_tx_fee(&receipt);
}

receipt
}

Expand Down Expand Up @@ -418,6 +476,14 @@ impl<'a> CallBuilder<'a> {
}
}
}

fn output_log_title(title: &str) {
println!("\nTX: {title}");
}

fn output_tx_fee(receipt: &TransactionReceipt) {
println!("Transaction fees:{:?}", receipt.fee_summary.total_cost());
}
}

impl SimpleMethodCaller for CallBuilder<'_> {
Expand Down
110 changes: 106 additions & 4 deletions test-engine/src/engine_interface.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::collections::BTreeMap;
use std::path::Path;

use crate::account::Account;
use crate::internal_prelude::*;
use std::collections::BTreeMap;
use std::path::Path;

pub struct EngineInterface {
simulator: DefaultLedgerSimulator,
Expand All @@ -23,6 +22,23 @@ impl EngineInterface {
}
}

pub fn new_with_custom_genesis(genesis: CustomGenesis) -> Self {
let test_runner_builder = LedgerSimulatorBuilder::new()
.with_custom_genesis(genesis)
.without_kernel_trace()
.build();
Self {
simulator: test_runner_builder,
}
}

pub fn with_simulator<F, R>(&mut self, action: F) -> R
where
F: FnOnce(&mut DefaultLedgerSimulator) -> R,
{
action(&mut self.simulator)
}

pub fn publish_package<P: AsRef<Path>>(&mut self, package_dir: P) -> TransactionReceipt {
self.simulator.try_publish_package(package_dir.as_ref())
}
Expand Down Expand Up @@ -87,13 +103,82 @@ impl EngineInterface {
self.simulator.get_component_balance(account, resource)
}

pub fn fungible_vault_balance(&mut self, vault: NodeId) -> Decimal {
self.simulator
.inspect_vault_balance(vault)
.unwrap_or(Decimal::ZERO)
}

pub fn non_fungible_vault_balance(&mut self, vault: NodeId) -> Vec<NonFungibleLocalId> {
let tmp = self
.simulator
.inspect_non_fungible_vault(vault)
.unwrap_or((
Decimal::ZERO,
Box::new(Vec::<NonFungibleLocalId>::new().into_iter()),
))
.1;
tmp.collect()
}

pub fn new_fungible(
&mut self,
account: ComponentAddress,
initial_amount: Decimal,
divisibility: u8,
) -> ResourceAddress {
self.simulator
.create_fungible_resource(initial_amount, 18, account)
.create_fungible_resource(initial_amount, divisibility, account)
}

pub fn new_non_fungible<T: ManifestEncode + NonFungibleData>(
&mut self,
id_type: NonFungibleIdType,
) -> ResourceAddress {
let manifest = ManifestBuilder::new()
.lock_fee_from_faucet()
.create_non_fungible_resource(
OwnerRole::None,
id_type,
false,
NonFungibleResourceRoles::single_locked_rule(AccessRule::AllowAll),
metadata!(),
None::<Vec<(NonFungibleLocalId, T)>>,
)
.build();
let receipt = self.execute_manifest(manifest, false, vec![]);
receipt.expect_commit(true).new_resource_addresses()[0]
}

pub fn mint_non_fungible<T: ManifestEncode>(
&mut self,
account: ComponentAddress,
resource_address: ResourceAddress,
id: NonFungibleLocalId,
data: T,
) {
let manifest = ManifestBuilder::new()
.lock_fee_from_faucet()
.mint_non_fungible(resource_address, vec![(id, data)])
.try_deposit_entire_worktop_or_abort(account, None)
.build();

self.execute_manifest(manifest, false, vec![]);
}

pub fn mint_ruid_non_fungible<T: ManifestEncode>(
&mut self,
account: ComponentAddress,
resource_address: ResourceAddress,
data: T,
) {
let manifest = ManifestBuilder::new()
.lock_fee_from_faucet()
.mint_ruid_non_fungible(resource_address, vec![data])
.try_deposit_entire_worktop_or_abort(account, None)
.build();

self.execute_manifest(manifest, false, vec![]);
}

pub fn set_epoch(&mut self, epoch: Epoch) {
Expand Down Expand Up @@ -173,6 +258,23 @@ impl EngineInterface {
receipt.expect_commit(true).new_resource_addresses()[0]
}

pub fn get_ids_map(
&mut self,
resource_address: ResourceAddress,
) -> HashMap<ComponentAddress, Vec<NonFungibleLocalId>> {
let mut ids_map = HashMap::new();
self.simulator
.find_all_components()
.into_iter()
.for_each(|comp| {
let ids = self.nft_ids(comp, resource_address);
if !ids.is_empty() {
ids_map.insert(comp, ids);
}
});
ids_map
}

pub fn get_state<T: ScryptoDecode>(&self, component_address: ComponentAddress) -> T {
self.simulator.component_state(component_address)
}
Expand Down
Loading

0 comments on commit df21e21

Please sign in to comment.