// This file is part of Bizinikiwi. // Copyright (C) Parity Technologies (UK) Ltd. and Dijital Kurdistan Tech Institute // SPDX-License-Identifier: Apache-2.0 // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use super::*; use crate::{ alloy::hex, mock::{new_test_ext, Assets, Balances, RuntimeEvent, RuntimeOrigin, System, Test}, }; use alloy::primitives::U256; use pezframe_support::{assert_ok, traits::Currency}; use pezpallet_revive::ExecConfig; use pezsp_core::H160; use pezsp_runtime::Weight; fn assert_contract_event(contract: H160, event: IERC20Events) { let (topics, data) = event.into_log_data().split(); let topics = topics.into_iter().map(|v| H256(v.0)).collect::>(); System::assert_has_event(RuntimeEvent::Revive(pezpallet_revive::Event::ContractEmitted { contract, data: data.to_vec(), topics, })); } #[test] fn asset_id_extractor_works() { let address: [u8; 20] = hex::const_decode_to_array(b"0000053900000000000000000000000001200000").unwrap(); assert!(InlineIdConfig::<0x0120>::MATCHER.matches(&address)); assert_eq!( as AssetPrecompileConfig>::AssetIdExtractor::asset_id_from_address( &address ) .unwrap(), 1337u32 ); } #[test] fn precompile_transfer_works() { new_test_ext().execute_with(|| { let asset_id = 0u32; let asset_addr = H160::from( hex::const_decode_to_array(b"0000000000000000000000000000000001200000").unwrap(), ); let from = 123456789; let to = 987654321; Balances::make_free_balance_be(&from, 100); Balances::make_free_balance_be(&to, 100); let from_addr = ::AddressMapper::to_address(&from); let to_addr = ::AddressMapper::to_address(&to); assert_ok!(Assets::force_create(RuntimeOrigin::root(), asset_id, from, true, 1)); assert_ok!(Assets::mint(RuntimeOrigin::signed(from), asset_id, from, 100)); let data = IERC20::transferCall { to: to_addr.0.into(), value: U256::from(10) }.abi_encode(); pezpallet_revive::Pezpallet::::bare_call( RuntimeOrigin::signed(from), H160::from(asset_addr), 0u32.into(), Weight::MAX, u64::MAX, data, ExecConfig::new_bizinikiwi_tx(), ); assert_contract_event( asset_addr, IERC20Events::Transfer(IERC20::Transfer { from: from_addr.0.into(), to: to_addr.0.into(), value: U256::from(10), }), ); assert_eq!(Assets::balance(asset_id, from), 90); assert_eq!(Assets::balance(asset_id, to), 10); }); } #[test] fn total_supply_works() { new_test_ext().execute_with(|| { let asset_id = 0u32; let asset_addr = hex::const_decode_to_array(b"0000000000000000000000000000000001200000").unwrap(); let owner = 123456789; Balances::make_free_balance_be(&owner, 100); assert_ok!(Assets::force_create(RuntimeOrigin::root(), asset_id, owner, true, 1)); assert_ok!(Assets::mint(RuntimeOrigin::signed(owner), asset_id, owner, 1000)); let data = IERC20::totalSupplyCall {}.abi_encode(); let data = pezpallet_revive::Pezpallet::::bare_call( RuntimeOrigin::signed(owner), H160::from(asset_addr), 0u32.into(), Weight::MAX, u64::MAX, data, ExecConfig::new_bizinikiwi_tx(), ) .result .unwrap() .data; let ret = IERC20::totalSupplyCall::abi_decode_returns(&data).unwrap(); assert_eq!(ret, U256::from(1000)); }); } #[test] fn balance_of_works() { new_test_ext().execute_with(|| { let asset_id = 0u32; let asset_addr = hex::const_decode_to_array(b"0000000000000000000000000000000001200000").unwrap(); let owner = 123456789; assert_ok!(Assets::force_create(RuntimeOrigin::root(), asset_id, owner, true, 1)); assert_ok!(Assets::mint(RuntimeOrigin::signed(owner), asset_id, owner, 1000)); let account = ::AddressMapper::to_address(&owner).0.into(); let data = IERC20::balanceOfCall { account }.abi_encode(); let data = pezpallet_revive::Pezpallet::::bare_call( RuntimeOrigin::signed(owner), H160::from(asset_addr), 0u32.into(), Weight::MAX, u64::MAX, data, ExecConfig::new_bizinikiwi_tx(), ) .result .unwrap() .data; let ret = IERC20::balanceOfCall::abi_decode_returns(&data).unwrap(); assert_eq!(ret, U256::from(1000)); }); } #[test] fn approval_works() { use pezframe_support::traits::fungibles::approvals::Inspect; new_test_ext().execute_with(|| { let asset_id = 0u32; let asset_addr = H160::from( hex::const_decode_to_array(b"0000000000000000000000000000000001200000").unwrap(), ); let owner = 123456789; let spender = 987654321; let other = 1122334455; Balances::make_free_balance_be(&owner, 100); Balances::make_free_balance_be(&spender, 100); Balances::make_free_balance_be(&other, 100); let owner_addr = ::AddressMapper::to_address(&owner); let spender_addr = ::AddressMapper::to_address(&spender); let other_addr = ::AddressMapper::to_address(&other); assert_ok!(Assets::force_create(RuntimeOrigin::root(), asset_id, owner, true, 1)); assert_ok!(Assets::mint(RuntimeOrigin::signed(owner), asset_id, owner, 100)); let data = IERC20::approveCall { spender: spender_addr.0.into(), value: U256::from(25) } .abi_encode(); pezpallet_revive::Pezpallet::::bare_call( RuntimeOrigin::signed(owner), H160::from(asset_addr), 0u32.into(), Weight::MAX, u64::MAX, data, ExecConfig::new_bizinikiwi_tx(), ); assert_contract_event( asset_addr, IERC20Events::Approval(IERC20::Approval { owner: owner_addr.0.into(), spender: spender_addr.0.into(), value: U256::from(25), }), ); let data = IERC20::allowanceCall { owner: owner_addr.0.into(), spender: spender_addr.0.into() } .abi_encode(); let data = pezpallet_revive::Pezpallet::::bare_call( RuntimeOrigin::signed(owner), H160::from(asset_addr), 0u32.into(), Weight::MAX, u64::MAX, data, ExecConfig::new_bizinikiwi_tx(), ) .result .unwrap() .data; let ret = IERC20::allowanceCall::abi_decode_returns(&data).unwrap(); assert_eq!(ret, U256::from(25)); let data = IERC20::transferFromCall { from: owner_addr.0.into(), to: other_addr.0.into(), value: U256::from(10), } .abi_encode(); pezpallet_revive::Pezpallet::::bare_call( RuntimeOrigin::signed(spender), H160::from(asset_addr), 0u32.into(), Weight::MAX, u64::MAX, data, ExecConfig::new_bizinikiwi_tx(), ); assert_eq!(Assets::balance(asset_id, owner), 90); assert_eq!(Assets::allowance(asset_id, &owner, &spender), 15); assert_eq!(Assets::balance(asset_id, other), 10); assert_contract_event( asset_addr, IERC20Events::Transfer(IERC20::Transfer { from: owner_addr.0.into(), to: other_addr.0.into(), value: U256::from(10), }), ); }); }