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

3

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. Commented Feb 6, 2023 at 12:32

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.