Install polkadot/api and use api.createType and u8aToHex to encode the parameters
Code Example:
Installation:
yarn add @polkadot/api
Usage
import { ApiPromise, WsProvider } from '@polkadot/api';
import { u8aToHex } from '@polkadot/util';
const main = async () => {
const wsProvider = new WsProvider('wss://westend-asset-hub-rpc.polkadot.io');
const api = await ApiPromise.create({ provider: wsProvider });
await api.isReady;
// get input parameters as encoded SCALE Uint8Array
// asset in asset hub with id 1
const multiLocation = api.createType('MultiLocation', {
parent: 0,
interior: {
X2: [
{ PalletInstance: 50 },
{ GeneralIndex: 4200 }
]
}
}).toU8a();
const multiLocation2 = api.createType('MultiLocation', {
parent: 0,
interior: {
here: null
}
}).toU8a();
const amount = api.createType('u128', 2).toU8a();
const bool = api.createType('bool', false).toU8a();
// concatenate Uint8Arrays of input parameters
const encodedInput = new Uint8Array(multiLocation.length + multiLocation2.length);
encodedInput.set(multiLocation, 0); // Set array1 starting from index 0
encodedInput.set(multiLocation2, multiLocation.length); // Set array2 starting from the end of array1
encodedInput.set(amount, multiLocation.length + multiLocation2.length); // Set array3 starting from the end of array1 + array2
encodedInput.set(bool, multiLocation.length + multiLocation2.length + amount.length); // Set array3 starting from the end of array1 + array2
// create Hex from concatenated u8a
const encodedInputHex = u8aToHex(encodedInput);
// call rpc state call where first parameter is method to be called and second one is Hex representation of encoded input parameters
const response = await api.rpc.state.call('AssetConversionApi_quote_price_exact_tokens_for_tokens', encodedInputHex)
// decode response
const decodedprice = api.createType('Option<u128>', response);
console.log(decodedprice.toHuman());
};
main();