Skip to content

Commit df21e21

Browse files
authored
Merge pull request #12 from BeakerTools/develop
Develop
2 parents 9ab3679 + 11c65d0 commit df21e21

12 files changed

Lines changed: 590 additions & 191 deletions

File tree

test-engine/src/call_builder.rs

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub struct CallBuilder<'a> {
2323
output_manifest: Option<(String, String)>,
2424
admin_badge: Vec<(ResourceAddress, Option<BTreeSet<NonFungibleLocalId>>)>,
2525
with_trace: bool,
26+
with_tx_fee: bool,
27+
log_title: Option<&'a str>,
2628
deposit_destination: ComponentAddress,
2729
manifest_data: Option<TransactionManifestData>,
2830
}
@@ -42,9 +44,27 @@ impl<'a> CallBuilder<'a> {
4244
admin_badge: vec![],
4345
with_trace: false,
4446
manifest_data: None,
47+
with_tx_fee: false,
48+
log_title: None,
4549
}
4650
}
4751

52+
pub fn with_test_engine<F, R>(&mut self, f: F) -> R
53+
where
54+
F: FnOnce(&mut TestEngine) -> R,
55+
{
56+
f(self.test_engine)
57+
}
58+
59+
pub fn with_manifest_builder<F>(mut self, f: F) -> Self
60+
where
61+
F: FnOnce(ManifestBuilder) -> ManifestBuilder,
62+
{
63+
self.manifest_builder = f(self.manifest_builder);
64+
65+
self
66+
}
67+
4868
/// Creates a call builder for a method call of the current component and skip the transaction execution.
4969
///
5070
/// # Arguments
@@ -61,7 +81,7 @@ impl<'a> CallBuilder<'a> {
6181
/// * `entity_name`: reference name or address of the entity to call the method on.
6282
/// * `method_name`: name of the method.
6383
/// * `args`: environment arguments to call the method.
64-
pub fn call_from_component<G: GlobalReference>(
84+
pub fn call_from<G: GlobalReference>(
6585
self,
6686
entity_name: G,
6787
method_name: &str,
@@ -101,11 +121,27 @@ impl<'a> CallBuilder<'a> {
101121
true,
102122
);
103123

124+
if let Some(title) = self.log_title {
125+
Self::output_log_title(title);
126+
}
127+
104128
Self::output_logs(&receipt);
105129

130+
if self.with_tx_fee {
131+
Self::output_tx_fee(&receipt);
132+
}
133+
106134
receipt
107135
}
108136

137+
pub fn execute_and_expect_success(self) -> CommitResult {
138+
self.execute().expect_commit_success().clone()
139+
}
140+
141+
pub fn execute_and_expect_failure(self) -> CommitResult {
142+
self.execute().expect_commit_failure().clone()
143+
}
144+
109145
/// Deposits the batch to the given account.
110146
///
111147
/// # Arguments
@@ -142,21 +178,21 @@ impl<'a> CallBuilder<'a> {
142178
pub fn transfer<
143179
E: ReferenceName,
144180
R: ReferenceName + Clone + 'static,
145-
D: TryInto<Decimal> + Clone + 'static,
181+
// D: TryInto<Decimal> + Clone + 'static,
146182
>(
147183
self,
148184
recipient: E,
149185
resource: R,
150-
amount: D,
186+
amount: Decimal,
151187
) -> Self
152-
where
153-
<D as TryInto<Decimal>>::Error: std::fmt::Debug,
188+
// where
189+
// <D as TryInto<Decimal>>::Error: std::fmt::Debug,
154190
{
155-
self.call_from_component(
191+
self.call_from(
156192
recipient,
157193
"try_deposit_or_abort",
158194
vec![
159-
Box::new(Fungible::Bucket(resource.clone(), amount)),
195+
Box::new(Fungible::FromAccount(resource.clone(), amount)),
160196
Box::new(None::<u64>),
161197
],
162198
)
@@ -174,11 +210,11 @@ impl<'a> CallBuilder<'a> {
174210
resource: R,
175211
ids: Vec<T>,
176212
) -> Self {
177-
self.call_from_component(
213+
self.call_from(
178214
recipient,
179215
"try_deposit_or_abort",
180216
vec![
181-
Box::new(NonFungible::Bucket(
217+
Box::new(NonFungible::FromAccount(
182218
resource,
183219
ids.into_iter().map(|id| id.to_id()).collect(),
184220
)),
@@ -240,6 +276,20 @@ impl<'a> CallBuilder<'a> {
240276
self
241277
}
242278

279+
/// Displays tx fee or not.
280+
///
281+
/// # Arguments
282+
/// * `trace`:
283+
pub fn with_log_tx_fee(mut self) -> Self {
284+
self.with_tx_fee = true;
285+
self
286+
}
287+
288+
pub fn with_log_title(mut self, title: &'a str) -> Self {
289+
self.log_title = Some(title);
290+
self
291+
}
292+
243293
pub(crate) fn call_method_internal(
244294
mut self,
245295
component: impl ResolvableGlobalAddress,
@@ -294,8 +344,16 @@ impl<'a> CallBuilder<'a> {
294344
false,
295345
);
296346

347+
if let Some(title) = self.log_title {
348+
Self::output_log_title(title);
349+
}
350+
297351
Self::output_logs(&receipt);
298352

353+
if self.with_tx_fee {
354+
Self::output_tx_fee(&receipt);
355+
}
356+
299357
receipt
300358
}
301359

@@ -418,6 +476,14 @@ impl<'a> CallBuilder<'a> {
418476
}
419477
}
420478
}
479+
480+
fn output_log_title(title: &str) {
481+
println!("\nTX: {title}");
482+
}
483+
484+
fn output_tx_fee(receipt: &TransactionReceipt) {
485+
println!("Transaction fees:{:?}", receipt.fee_summary.total_cost());
486+
}
421487
}
422488

423489
impl SimpleMethodCaller for CallBuilder<'_> {

test-engine/src/engine_interface.rs

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use std::collections::BTreeMap;
2-
use std::path::Path;
3-
41
use crate::account::Account;
52
use crate::internal_prelude::*;
3+
use std::collections::BTreeMap;
4+
use std::path::Path;
65

76
pub struct EngineInterface {
87
simulator: DefaultLedgerSimulator,
@@ -23,6 +22,23 @@ impl EngineInterface {
2322
}
2423
}
2524

25+
pub fn new_with_custom_genesis(genesis: CustomGenesis) -> Self {
26+
let test_runner_builder = LedgerSimulatorBuilder::new()
27+
.with_custom_genesis(genesis)
28+
.without_kernel_trace()
29+
.build();
30+
Self {
31+
simulator: test_runner_builder,
32+
}
33+
}
34+
35+
pub fn with_simulator<F, R>(&mut self, action: F) -> R
36+
where
37+
F: FnOnce(&mut DefaultLedgerSimulator) -> R,
38+
{
39+
action(&mut self.simulator)
40+
}
41+
2642
pub fn publish_package<P: AsRef<Path>>(&mut self, package_dir: P) -> TransactionReceipt {
2743
self.simulator.try_publish_package(package_dir.as_ref())
2844
}
@@ -87,13 +103,82 @@ impl EngineInterface {
87103
self.simulator.get_component_balance(account, resource)
88104
}
89105

106+
pub fn fungible_vault_balance(&mut self, vault: NodeId) -> Decimal {
107+
self.simulator
108+
.inspect_vault_balance(vault)
109+
.unwrap_or(Decimal::ZERO)
110+
}
111+
112+
pub fn non_fungible_vault_balance(&mut self, vault: NodeId) -> Vec<NonFungibleLocalId> {
113+
let tmp = self
114+
.simulator
115+
.inspect_non_fungible_vault(vault)
116+
.unwrap_or((
117+
Decimal::ZERO,
118+
Box::new(Vec::<NonFungibleLocalId>::new().into_iter()),
119+
))
120+
.1;
121+
tmp.collect()
122+
}
123+
90124
pub fn new_fungible(
91125
&mut self,
92126
account: ComponentAddress,
93127
initial_amount: Decimal,
128+
divisibility: u8,
94129
) -> ResourceAddress {
95130
self.simulator
96-
.create_fungible_resource(initial_amount, 18, account)
131+
.create_fungible_resource(initial_amount, divisibility, account)
132+
}
133+
134+
pub fn new_non_fungible<T: ManifestEncode + NonFungibleData>(
135+
&mut self,
136+
id_type: NonFungibleIdType,
137+
) -> ResourceAddress {
138+
let manifest = ManifestBuilder::new()
139+
.lock_fee_from_faucet()
140+
.create_non_fungible_resource(
141+
OwnerRole::None,
142+
id_type,
143+
false,
144+
NonFungibleResourceRoles::single_locked_rule(AccessRule::AllowAll),
145+
metadata!(),
146+
None::<Vec<(NonFungibleLocalId, T)>>,
147+
)
148+
.build();
149+
let receipt = self.execute_manifest(manifest, false, vec![]);
150+
receipt.expect_commit(true).new_resource_addresses()[0]
151+
}
152+
153+
pub fn mint_non_fungible<T: ManifestEncode>(
154+
&mut self,
155+
account: ComponentAddress,
156+
resource_address: ResourceAddress,
157+
id: NonFungibleLocalId,
158+
data: T,
159+
) {
160+
let manifest = ManifestBuilder::new()
161+
.lock_fee_from_faucet()
162+
.mint_non_fungible(resource_address, vec![(id, data)])
163+
.try_deposit_entire_worktop_or_abort(account, None)
164+
.build();
165+
166+
self.execute_manifest(manifest, false, vec![]);
167+
}
168+
169+
pub fn mint_ruid_non_fungible<T: ManifestEncode>(
170+
&mut self,
171+
account: ComponentAddress,
172+
resource_address: ResourceAddress,
173+
data: T,
174+
) {
175+
let manifest = ManifestBuilder::new()
176+
.lock_fee_from_faucet()
177+
.mint_ruid_non_fungible(resource_address, vec![data])
178+
.try_deposit_entire_worktop_or_abort(account, None)
179+
.build();
180+
181+
self.execute_manifest(manifest, false, vec![]);
97182
}
98183

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

261+
pub fn get_ids_map(
262+
&mut self,
263+
resource_address: ResourceAddress,
264+
) -> HashMap<ComponentAddress, Vec<NonFungibleLocalId>> {
265+
let mut ids_map = HashMap::new();
266+
self.simulator
267+
.find_all_components()
268+
.into_iter()
269+
.for_each(|comp| {
270+
let ids = self.nft_ids(comp, resource_address);
271+
if !ids.is_empty() {
272+
ids_map.insert(comp, ids);
273+
}
274+
});
275+
ids_map
276+
}
277+
176278
pub fn get_state<T: ScryptoDecode>(&self, component_address: ComponentAddress) -> T {
177279
self.simulator.component_state(component_address)
178280
}

0 commit comments

Comments
 (0)