1- use std:: collections:: BTreeMap ;
2- use std:: path:: Path ;
3-
41use crate :: account:: Account ;
52use crate :: internal_prelude:: * ;
3+ use std:: collections:: BTreeMap ;
4+ use std:: path:: Path ;
65
76pub 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