2

I would like to create a type called XcmVersionedMultiLocation. It can be imported using the following code:

import { XcmVersionedMultiLocation } from "@polkadot/types/lookup";

To create an instance of this type, you can use the following code:

const loc = api.createType("XcmVersionedMultiLocation", {
  isV3: true,
  type: "V3",
  asV3: {
    parents: 1,
    interior: {
      isX2: true,
      asX2: [
        { isParachain: true, asParachain: 2100 },
        {
          isAccountId32: true,
          asAccountId32: {
            id: api.createType("AccountId32", address).toHex()
          }
        }
      ]
    }
  }
});

However, the loc variable here still has the type Codec. I am not sure why this is happening. Is there something else we need to do in order to get the correct type for XcmVersionedMultiLocation?

Let's consider an example:

const account = api.createType("AccountId32", address)

This correctly assigns the type "AccountId32" to the account variable.

Similarly, for the following code:

const loc = api.createType(
"MultiLocation",
{
    parents: "0",
    interior: {
        x1: { generalKey: "0x0001" }
    }
}

);

The loc variable correctly has the type MultiLocation.

I'm not sure why it's not working for XcmVersionedMultiLocation.

Thanks for your help!

1 Answer 1

4

The XcmVersionedMultiLocation type-example you shared can be created as follows :

 const loc =  api.createType("XcmVersionedMultiLocation", {
                V3: {
                    parents: "1",
                    interior: {
                        X2: [
                         { parachain: "2100"},
                         { AccountId32: { id: address } }
                        ],
                    },
                },
            });

A complete example/code snippet would be something like the following (using a random account) :

const { ApiPromise, WsProvider } = require('@polkadot/api');

async function main() {
    const wsProvider = new WsProvider('wss://rpc.polkadot.io');
    const api = await ApiPromise.create({ provider: wsProvider });

    const xcmVersionedMultiLocation = {
        V3: {
          parents: "1",
          interior: {
            X2: [ 
                { parachain: "2100"},
                { AccountId32: 
                    {
                      id: "0x0987654309876543098765430987654309876543098765430987654309876543"        
                    }
                 } 
                ]
          },
        },
      };
    
    const loc = api.createType(
    "XcmVersionedMultiLocation",
    xcmVersionedMultiLocation
    );

    console.log(loc.toHex());  
}

main().catch(console.error).finally(() => process.exit());

For the id make sure to use the public key. If you have the address in another format you can use the subscan tool or sidecar's convert endpoint to change it.

Regarding your question on why the loc variable might still have the type Codec, there is a similar question here and a possible explanation can be found here : Since upgrading to the 7.x series, TypeScript augmentation is missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.