0

I'm trying to use asset_conversion pallet to call quote prices, and get reserved rpc endpoints (defined here)

In pallets official docs there is small section about calling that endpoint, but it doesn't specify how to encode input parameters. So I wanted to check if anyone know how to encode them (preferably in js/ts)

Note: There is also this file, explaining get reserves call, but still nothing about encoding

1 Answer 1

1

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();

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.