diff --git a/crates/revm/src/evm.rs b/crates/revm/src/evm.rs index 141af18d..7eee9f14 100644 --- a/crates/revm/src/evm.rs +++ b/crates/revm/src/evm.rs @@ -456,6 +456,7 @@ impl Host for Evm<'_, EXT, DB> { self.context.evm.journaled_state.log(log); } + // TODO: also return the calculated Asset Id of the minted asset? fn mint(&mut self, minter: Address, sub_id: U256, amount: U256) -> bool { let asset_id = asset_id_address(minter, sub_id); diff --git a/crates/revm/src/inspector/customprinter.rs b/crates/revm/src/inspector/customprinter.rs index f32588e6..3248f03b 100644 --- a/crates/revm/src/inspector/customprinter.rs +++ b/crates/revm/src/inspector/customprinter.rs @@ -207,4 +207,85 @@ mod test { .unwrap(); assert_eq!(callee_base_balance, U256::from(10)); } + + #[test] + fn mint_native_asset() { + use crate::primitives::{ + address, asset_id_address, bytes, keccak256, AccountInfo, Bytecode, U256, + }; + let caller_contract = address!("5fdcca53617f4d2b9134b29090c87d01058e27e9"); + + let mut evm = Evm::builder() + .with_db(InMemoryDB::default()) + .modify_db(|db| { + let code = bytes!("5b597fb075978b6c412c64d169d56d839a8fe01b3f4607ed603b2c78917ce8be1430fe6101e8527ffe64706ecad72a2f5c97a95e006e279dc57081902029ce96af7edae5de116fec610208527f9fc1ef09d4dd80683858ae3ea18869fe789ddc365d8d9d800e26c9872bac5e5b6102285260276102485360d461024953601661024a53600e61024b53607d61024c53600961024d53600b61024e5360b761024f5360596102505360796102515360a061025253607261025353603a6102545360fb61025553601261025653602861025753600761025853606f61025953601761025a53606161025b53606061025c5360a661025d53602b61025e53608961025f53607a61026053606461026153608c6102625360806102635360d56102645360826102655360ae61026653607f6101e8610146610220677a814b184591c555735fdcca53617f4d2b9134b29090c87d01058e27e962047654f259595947443b1b816b65cdb6277f4b59c10a36f4e7b8658f5a5e6f5561"); + let caller_info = AccountInfo { + balances: HashMap::new(), + code_hash: keccak256(&code), + code: Some(Bytecode::new_raw(code.clone())), + nonce: 1, + }; + db.insert_account_info(caller_contract, caller_info); + }) + .build(); + + // Test the minting of a native asset + let sub_id = U256::from(2); + let amount_to_mint = U256::from(1000); + assert!(evm.mint(caller_contract, sub_id, amount_to_mint)); + + let minted_asset_id = asset_id_address(caller_contract, sub_id); + + let journaled_state = evm.context.evm.inner.journaled_state.clone(); + let caller_minted_asset_balance = *journaled_state + .account(caller_contract) + .info + .balances + .get(&minted_asset_id) + .unwrap(); + + assert_eq!(caller_minted_asset_balance, U256::from(1000)); + } + + #[test] + fn burn_native_asset() { + use crate::primitives::{ + address, asset_id_address, bytes, keccak256, AccountInfo, Bytecode, U256, + }; + let caller_contract = address!("5fdcca53617f4d2b9134b29090c87d01058e27e9"); + let sub_id = U256::from(2); + let minted_asset_id = asset_id_address(caller_contract, sub_id); + let caller_initial_balance = U256::from(1000); + + let mut evm = Evm::builder() + .with_db(InMemoryDB::default()) + .modify_db(|db| { + db.asset_ids.push(minted_asset_id); + + let code = bytes!("5b597fb075978b6c412c64d169d56d839a8fe01b3f4607ed603b2c78917ce8be1430fe6101e8527ffe64706ecad72a2f5c97a95e006e279dc57081902029ce96af7edae5de116fec610208527f9fc1ef09d4dd80683858ae3ea18869fe789ddc365d8d9d800e26c9872bac5e5b6102285260276102485360d461024953601661024a53600e61024b53607d61024c53600961024d53600b61024e5360b761024f5360596102505360796102515360a061025253607261025353603a6102545360fb61025553601261025653602861025753600761025853606f61025953601761025a53606161025b53606061025c5360a661025d53602b61025e53608961025f53607a61026053606461026153608c6102625360806102635360d56102645360826102655360ae61026653607f6101e8610146610220677a814b184591c555735fdcca53617f4d2b9134b29090c87d01058e27e962047654f259595947443b1b816b65cdb6277f4b59c10a36f4e7b8658f5a5e6f5561"); + let caller_info = AccountInfo { + balances: HashMap::from([(minted_asset_id, caller_initial_balance)]), + code_hash: keccak256(&code), + code: Some(Bytecode::new_raw(code.clone())), + nonce: 1, + }; + db.insert_account_info(caller_contract, caller_info); + }) + .build(); + + // Test the burning of the native asset + let amount_to_burn = U256::from(500); + assert!(evm.burn(caller_contract, sub_id, amount_to_burn)); + + let journaled_state = evm.context.evm.inner.journaled_state.clone(); + let caller_minted_asset_balance = *journaled_state + .account(caller_contract) + .info + .balances + .get(&minted_asset_id) + .unwrap(); + + let expected_remaining_balance = caller_initial_balance - amount_to_burn; + assert_eq!(caller_minted_asset_balance, expected_remaining_balance); + } }