Skip to content

Commit

Permalink
Merge pull request #73 from peaqnetwork/feat/1208429695031476_xcm_rem…
Browse files Browse the repository at this point in the history
…ote_execute

Feat/1208429695031476 xcm remote execute
  • Loading branch information
sfffaaa authored Oct 8, 2024
2 parents 15fecbb + 1537f23 commit ab58141
Show file tree
Hide file tree
Showing 4 changed files with 380 additions and 50 deletions.
88 changes: 59 additions & 29 deletions tests/bridge_xcmutils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
from tests.utils_func import restart_parachain_and_runtime_upgrade
from tools.constants import WS_URL, ETH_URL, ACA_WS_URL
from tools.constants import ACA_PD_CHAIN_ID
from tools.constants import PARACHAIN_WS_URL
from tools.runtime_upgrade import wait_until_block_height
from tools.peaq_eth_utils import get_contract
from tools.peaq_eth_utils import GAS_LIMIT, get_eth_info
from tools.peaq_eth_utils import get_eth_chain_id
from tools.asset import setup_asset_if_not_exist, setup_xc_register_if_not_exist
from substrateinterface import SubstrateInterface, Keypair
from peaq.utils import ExtrinsicBatch
from web3 import Web3
from tools.constants import KP_GLOBAL_SUDO
from tools.peaq_eth_utils import sign_and_submit_evm_transaction
from peaq.utils import get_account_balance
from tests import utils_func as TestUtils
from tools.asset import PEAQ_ASSET_LOCATION, PEAQ_METADATA, PEAQ_ASSET_ID
from tools.asset import wait_for_account_asset_change_wrap
from tools.asset import get_tokens_account_from_pallet_tokens
from tools.utils import get_modified_chain_spec
from peaq.utils import get_chain
import pytest


Expand All @@ -33,14 +32,30 @@ class TestBridgeXCMUtils(unittest.TestCase):
@classmethod
def setUpClass(cls):
restart_parachain_and_runtime_upgrade()
wait_until_block_height(SubstrateInterface(url=PARACHAIN_WS_URL), 1)
wait_until_block_height(SubstrateInterface(url=WS_URL), 1)
wait_until_block_height(SubstrateInterface(url=ACA_WS_URL), 1)

def setUp(self):
self.si_peaq = SubstrateInterface(url=WS_URL)
wait_until_block_height(SubstrateInterface(url=PARACHAIN_WS_URL), 1)
wait_until_block_height(SubstrateInterface(url=WS_URL), 1)
self.w3 = Web3(Web3.HTTPProvider(ETH_URL))
self.kp_eth = get_eth_info()
self.kp_eth = get_eth_info("sphere gasp actual clock wreck rural essay name claw deputy party output")
self.eth_chain_id = get_eth_chain_id(self.si_peaq)
self.setup_sibling_parachain_account()

# Just calculate the sibling parachain account by moonbeam's xcm-utils tool
# Here is the command but remember to change the parachain id
# yarn calculate-multilocation-derivative-account --a 0x55eebfdbb6af8aecbc9664bb229c48e2fceb381ce9d93a3ed698d4177da3e8e6 --p 2000 --parents
def setup_sibling_parachain_account(self):
# substrate addr is 5E1NrJDAqp5R3JDBBn2JGaHpUMo5kbCNXtV2asLzaw2MZfwr
chain_spec = get_chain(self.si_peaq)
chain = get_modified_chain_spec(chain_spec)
if chain == 'peaq-dev':
self.sibling_parachain_addr = '5HYXs9665LtfnxvBz9FrXCrUGwNTWBrkotqtdFw1VAsYAiiJ'
elif chain == 'krest':
self.sibling_parachain_addr = '5Ex5h1ExXEX3DkLc3gPr7yhCgvUmovLV16A7NATbeJH6UScH'
elif chain == 'peaq':
self.sibling_parachain_addr = '5HSpy8Hc4ciygtDTHTdj6FKpPvwXDjY8riWMaqDkCgbiHfAx'

def _fund_eth_account(self):
# transfer
Expand All @@ -55,6 +70,18 @@ def _fund_eth_account(self):
)
batch.execute()

def aca_fund(self, substrate, kp_sudo, ss58_addr, new_free):
batch = ExtrinsicBatch(substrate, kp_sudo)
batch.compose_sudo_call(
'Balances',
'force_set_balance',
{
'who': ss58_addr,
'new_free': new_free,
}
)
return batch.execute()

def _compose_xcm_execute_message(self, kp):
account = kp.public_key
instr1 = {
Expand Down Expand Up @@ -97,16 +124,16 @@ def _compose_xcm_execute_message(self, kp):
)
return encoded_tx["call_args"]["message"]

def _compose_xcm_send_message(self, kp):
account = kp.public_key
def _compose_xcm_send_message_dict(self, kp):
account = f'0x{kp.public_key.hex()}'
instr1 = {
'WithdrawAsset': [
[{
'id': {
'parents': '0',
'interior': 'Here',
},
'fun': {'Fungible': 10 ** 18},
'fun': {'Fungible': 100 * 10 ** 18},
}],
]
}
Expand All @@ -117,7 +144,7 @@ def _compose_xcm_send_message(self, kp):
'parents': '0',
'interior': 'Here',
},
'fun': {'Fungible': 10 ** 18},
'fun': {'Fungible': 100 * 10 ** 18},
},
'weight_limit': 'Unlimited',
}
Expand All @@ -138,20 +165,23 @@ def _compose_xcm_send_message(self, kp):
}
message = {'V4': [[instr1, instr2, instr3]]}

return {
'dest': {'V4': {
'parents': '1',
'interior': {
'X1': [{
'Parachain': str(ACA_PD_CHAIN_ID)
}]
},
}},
'message': message,
}

def _compose_xcm_send_message(self, kp):
encoded_tx = self.si_peaq.compose_call(
call_module='PolkadotXcm',
call_function='send',
call_params={
'dest': {'V4': {
'parents': '1',
'interior': {
'X1': [{
'Parachain': '3000'
}]
},
}},
'message': message,
}
call_params=self._compose_xcm_send_message_dict(kp)
)
return encoded_tx["call_args"]["message"]

Expand Down Expand Up @@ -192,16 +222,14 @@ def test_xcm_send(self):
self.si_peaq = SubstrateInterface(url=WS_URL)
self.si_aca = SubstrateInterface(url=ACA_WS_URL)

receipt = setup_asset_if_not_exist(
self.si_aca, KP_GLOBAL_SUDO, PEAQ_ASSET_ID['para'], PEAQ_METADATA)
self.assertTrue(receipt.is_success, f'Failed to register asset: {receipt.error_message}')
receipt = setup_xc_register_if_not_exist(
self.si_aca, KP_GLOBAL_SUDO, PEAQ_ASSET_ID['para'], PEAQ_ASSET_LOCATION['para'], 100)
self.assertTrue(receipt.is_success, f'Failed to register xc asset: {receipt.error_message}')

self._fund_eth_account()
self.aca_fund(self.si_aca, KP_GLOBAL_SUDO, self.sibling_parachain_addr, 1000 * 10 ** 18)

# compose the message
kp_dst = Keypair.create_from_mnemonic(Keypair.generate_mnemonic())
self.aca_fund(self.si_aca, KP_GLOBAL_SUDO, kp_dst.ss58_address, 1000 * 10 ** 18)
orig_balance = get_account_balance(self.si_aca, kp_dst.ss58_address)

encoded_calldata = self._compose_xcm_send_message(kp_dst).encode().data

# Use the pallet send to send it
Expand All @@ -221,7 +249,9 @@ def test_xcm_send(self):

evm_receipt = sign_and_submit_evm_transaction(tx, self.w3, kp_sign)
self.assertEqual(evm_receipt['status'], 1, f'Error: {evm_receipt}: {evm_receipt["status"]}')
# Cannot test on remote side because Acala we used cannot resolve the origin (1, parachain, account)

new_balance = get_account_balance(self.si_aca, kp_dst.ss58_address)
self.assertGreater(new_balance, orig_balance, f'Error: {new_balance}, dst: {kp_dst.ss58_address}')

@pytest.mark.xcm
def test_get_units_per_second(self):
Expand Down
Loading

0 comments on commit ab58141

Please sign in to comment.