2

I am trying to figure out correct way how to type MultiLocation type

I thought that I would do

import {MultiLocation} from "@polkadot/types/interfaces"
const location: MultiLocation = {
  parents: 0,
  interior: {
    x1: { generalKey: "0x0001" }
  }
};

but this does not work cause parents is u8 and number does not work there etc So I tried this solution which works

const loc: MultiLocation = api.createType(
  "MultiLocation",
  JSON.parse(
    JSON.stringify({
      parents: "0",
      interior: {
        x1: { generalKey: "0x0001" }
      }
    })
  )
);

but I do not like this solution. IS there a correct way how to type this ? Thanks

2
  • How are you getting the type MultiLocation?
    – Alex Bean
    Feb 3 at 9:25
  • I updated my question. Feb 3 at 12:36

1 Answer 1

3

So you are very close to the correct answer. Using createType will automically set the type reference to the interface you assign. So the correct code here in typescript would be:

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

Then if you want the JSON value back:

console.log(loc.toJSON());
1
  • 1
    Thank you Tarik for your help. Feb 6 at 12:32

Your Answer

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

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