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 f0f44a8..5330685 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 @@ -24,7 +25,7 @@ ERC_TOKEN_TRANSFER = 34 HEX_STR = '1111' GAS_LIMIT = 4294967 -TOKEN_NUM = 10000 * pow(10, 15) +TOKEN_NUM = 10 * 10 ** 18 ABI_FILE = 'ETH/identity/abi' TOKEN_NUM_BASE = pow(10, 18) @@ -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() @@ -93,13 +107,14 @@ def test_evm_fee(self): 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': 100 * 10 ** 9, - 'maxPriorityFeePerGas': 1 * 10 ** 9, + 'maxFeePerGas': 1000 * 10 ** 9, + 'maxPriorityFeePerGas': 1000 * 10 ** 9, 'nonce': nonce, 'chainId': self._eth_chain_id } @@ -109,6 +124,33 @@ 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) + 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) + + 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 diff --git a/tests/utils_func.py b/tests/utils_func.py index d342cf4..f1cf03c 100644 --- a/tests/utils_func.py +++ b/tests/utils_func.py @@ -42,7 +42,7 @@ def is_not_dev_chain(): def is_not_peaq_chain(): ws = SubstrateInterface(url=WS_URL) chain_name = get_chain(ws) - return chain_name not in ['peaq', 'peaq-network-fork'] + return chain_name not in ['peaq-network', 'peaq-network-fork'] def is_krest_related_chain(): diff --git a/tools/peaq_eth_utils.py b/tools/peaq_eth_utils.py index 2d18c75..cb23988 100644 --- a/tools/peaq_eth_utils.py +++ b/tools/peaq_eth_utils.py @@ -95,6 +95,7 @@ def get_eth_info(mnemonic=""): mnemonic = Keypair.generate_mnemonic() kp = Keypair.create_from_mnemonic(mnemonic, crypto_type=KeypairType.ECDSA) return { + 'mnemonic': mnemonic, 'kp': kp, 'substrate': calculate_evm_account(kp.ss58_address), 'eth': kp.ss58_address