paritytech / asset-transfer-api

Typescript API aiming to provide clear, and simple to use tools for transferring assets across common good parachains.
Apache License 2.0
44 stars 11 forks source link

feat: support relay to bridge tx construction #409

Closed marshacb closed 5 months ago

marshacb commented 5 months ago

Description

This PR introduces the RelayToBridge tx direction for constructing BridgeHub transactions from the Relay Chain.

Changes

BridgeHub P<>K Bridge: (Rococo ROC -> Westend AssetHub)

Example of constructing a transferAssetsUsingTypeAndThen call sending ROC to Westend AssetHub from Rococo:

const { api, specName, safeXcmVersion } = await constructApiPromise('wss://rococo-rpc.polkadot.io');
const assetApi = new AssetTransferApi(api, specName, safeXcmVersion);

let callInfo: TxResult<'call'>;
try {
    callInfo = await assetApi.createTransferTransaction(
        `{"parents":"1","interior":{"X2":[{"GlobalConsensus":"Westend"},{"Parachain":"1000"}]}}`,
        '13EoPU88424tufnjevEYbbvZ7sGV3q1uhuN4ZbUaoTsnLHYt',
        [`{"parents":"0","interior":{"Here":""}}`],
        ['1000000000000'],
            {
              format: 'call',
              xcmVersion: 4,
              paysWithFeeDest: `{"parents":"0","interior":{"Here":""}}`,
              assetTransferType: 'RemoteReserve',
              remoteReserveAssetTransferTypeLocation: '{"parents":"0","interior":{"X1":{"Parachain":"1000"}}}',
              feesTransferType: 'RemoteReserve',
              remoteReserveFeesTransferTypeLocation: '{"parents":"0","interior":{"X1":{"Parachain":"1000"}}}',
                }
);

console.log(callInfo);

Expected output:

{
  origin: 'rococo',
  dest: 'westend',
  direction: 'RelayToBridge',
  xcmVersion: 4,
  method: 'transferAssetsUsingTypeAndThen',
  format: 'call',
  tx: '0x630d040102090400a10f0404000000070010a5d4e80304000100a10f0400000304000100a10f04040d0102040001010062fecf9c60d8d49d400bd86804558401ec7151fecd440041ca6bf5fd5782517700'
}

The following decoded tx:
 {
    "args": {
        "dest": {
            "V4": {
                "parents": "1",
                "interior": {
                    "X2": [
                        {
                            "GlobalConsensus": "Westend"
                        },
                        {
                            "Parachain": "1,000"
                        }
                    ]
                }
            }
        },
        "assets": {
            "V4": [
                {
                    "id": {
                        "parents": "0",
                        "interior": "Here"
                    },
                    "fun": {
                        "Fungible": "1,000,000,000,000"
                    }
                }
            ]
        },
        "assets_transfer_type": {
            "RemoteReserve": {
                "V4": {
                    "parents": "0",
                    "interior": {
                        "X1": [
                            {
                                "Parachain": "1,000"
                            }
                        ]
                    }
                }
            }
        },
        "remote_fees_id": {
            "V4": {
                "parents": "0",
                "interior": "Here"
            }
        },
        "fees_transfer_type": {
            "RemoteReserve": {
                "V4": {
                    "parents": "0",
                    "interior": {
                        "X1": [
                            {
                                "Parachain": "1,000"
                            }
                        ]
                    }
                }
            }
        },
        "custom_xcm_on_dest": {
            "V4": [
                {
                    "DepositAsset": {
                        "assets": {
                            "Wild": {
                                "AllCounted": "1"
                            }
                        },
                        "beneficiary": {
                            "parents": "0",
                            "interior": {
                                "X1": [
                                    {
                                        "AccountId32": {
                                            "network": null,
                                            "id": "0x62fecf9c60d8d49d400bd86804558401ec7151fecd440041ca6bf5fd57825177"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        },
        "weight_limit": "Unlimited"
    },
    "method": "transferAssetsUsingTypeAndThen",
    "section": "xcmPallet"
}