From 4a8c4c8f32fd4e5f1a7c65603305e21fb5ea444b Mon Sep 17 00:00:00 2001 From: jaypan Date: Fri, 18 Oct 2024 15:28:21 +0200 Subject: [PATCH] Add the EVM ED/API change --- tests/bridge_parachain_staking_test.py | 37 ++++++++++++++++++++++ tests/evm_eth_rpc_test.py | 43 ++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/tests/bridge_parachain_staking_test.py b/tests/bridge_parachain_staking_test.py index bd6ee50..d31d987 100644 --- a/tests/bridge_parachain_staking_test.py +++ b/tests/bridge_parachain_staking_test.py @@ -149,6 +149,43 @@ def get_event(self, block_hash, module, event): return {'attributes': e.value['event']['attributes']} return None + # Make sure the lock token cannot be transfered + def test_evm_api_cannot_transfer_over_stake(self): + contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE) + out = contract.functions.getCollatorList().call() + + collator_eth_addr = out[0][0] + collator_num = out[0][1] + receipt = self._fund_users(collator_num * 2) + self.assertEqual(receipt.is_success, True, f'fund_users fails, receipt: {receipt}') + + evm_receipt = self.evm_join_delegators(contract, self._kp_moon['kp'], collator_eth_addr, collator_num) + self.assertEqual(evm_receipt['status'], 1, f'join fails, evm_receipt: {evm_receipt}') + bl_hash = get_block_hash(self._substrate, evm_receipt['blockNumber']) + event = self.get_event(bl_hash, 'ParachainStaking', 'Delegation') + self.assertEqual( + event['attributes'], + (self._kp_moon['substrate'], collator_num, KP_COLLATOR.ss58_address, 2 * collator_num), + f'join fails, event: {event}') + + stake = self.get_stake_number(self._kp_moon['substrate']) + self.assertEqual(stake['total'], collator_num, f'join fails, stake: {stake}, collator_num: {collator_num}') + + transfer_token = int(stake['delegations'][0]['amount'] * 1.5) + tx = { + 'to': self._kp_mars['eth'], + 'value': transfer_token, + 'nonce': self._w3.eth.get_transaction_count(self._kp_moon['eth']), + 'chainId': self._eth_chain_id, + 'gas': 21000, + 'maxFeePerGas': 1000 * 10 ** 9, + 'maxPriorityFeePerGas': 1000 * 10 ** 9, + } + with self.assertRaises(ValueError) as tx_info: + sign_and_submit_evm_transaction(tx, self._w3, self._kp_moon['kp']) + + self.assertIn('insufficient funds', tx_info.exception.args[0]['message']) + def test_delegator_join_more_less_leave(self): contract = get_contract(self._w3, PARACHAIN_STAKING_ADDR, PARACHAIN_STAKING_ABI_FILE) out = contract.functions.getCollatorList().call() diff --git a/tests/evm_eth_rpc_test.py b/tests/evm_eth_rpc_test.py index 84603c7..561234f 100644 --- a/tests/evm_eth_rpc_test.py +++ b/tests/evm_eth_rpc_test.py @@ -12,6 +12,7 @@ from tools.peaq_eth_utils import call_eth_transfer_a_lot from tools.peaq_eth_utils import get_eth_balance, get_contract from tools.peaq_eth_utils import TX_SUCCESS_STATUS +from peaq.utils import get_account_balance from tests import utils_func as TestUtils from tools.peaq_eth_utils import get_eth_info from tools.utils import batch_fund @@ -82,6 +83,19 @@ def setUp(self): self._w3 = Web3(Web3.HTTPProvider(ETH_URL)) self._eth_deposited_src = calculate_evm_account(self._eth_src) + def test_evm_api_balance_same(self): + self._kp_moon = get_eth_info() + self._kp_mars = get_eth_info() + + batch = ExtrinsicBatch(self._conn, KP_GLOBAL_SUDO) + batch_fund(batch, self._kp_moon['substrate'], int(1.05 * 10 ** 18)) + receipt = batch.execute() + self.assertTrue(receipt.is_success) + + sub_balance = get_account_balance(self._conn, self._kp_moon['substrate']) + eth_balance = self._w3.eth.get_balance(self._kp_moon['kp'].ss58_address) + self.assertEqual(sub_balance, eth_balance, f"sub: {sub_balance} != eth: {eth_balance}") + @pytest.mark.skipif(TestUtils.is_not_peaq_chain() is True, reason='Only peaq chain evm tx change') def test_evm_fee(self): self._kp_moon = get_eth_info() @@ -110,6 +124,35 @@ def test_evm_fee(self): new_balance = self._w3.eth.get_balance(self._kp_moon['kp'].ss58_address) self.assertGreater(prev_balance - new_balance - 1 * 10 ** 18, 0.002 * 10 ** 18) + @pytest.mark.skipif(TestUtils.is_not_peaq_chain() is True, reason='Only peaq chain evm ED') + def test_evm_remaining_without_ed(self): + self._kp_moon = get_eth_info() + self._kp_mars = get_eth_info() + + batch = ExtrinsicBatch(self._conn, KP_GLOBAL_SUDO) + batch_fund(batch, self._kp_moon['substrate'], int(1.05 * 10 ** 18)) + receipt = batch.execute() + self.assertTrue(receipt.is_success) + + prev_balance = self._w3.eth.get_balance(self._kp_moon['kp'].ss58_address) + nonce = self._w3.eth.get_transaction_count(self._kp_moon['kp'].ss58_address) + # gas/maxFeePerGas/maxPriorityFeePerGas is decided by metamask's value + tx = { + 'from': self._kp_moon['kp'].ss58_address, + 'to': self._kp_mars['kp'].ss58_address, + 'value': 1 * 10 ** 18, + 'gas': 21000, + 'maxFeePerGas': 1000 * 10 ** 9, + 'maxPriorityFeePerGas': 1000 * 10 ** 9, + 'nonce': nonce, + 'chainId': self._eth_chain_id + } + response = sign_and_submit_evm_transaction(tx, self._w3, self._kp_moon['kp']) + self.assertTrue(response['status'] == TX_SUCCESS_STATUS, f'failed: {response}') + + new_balance = self._w3.eth.get_balance(self._kp_moon['kp'].ss58_address) + self.assertGreater(10 ** 18, new_balance) + def test_evm_rpc_transfer(self): conn = self._conn eth_chain_id = self._eth_chain_id