paritytech / txwrapper

Helper funtions for offline transaction generation.
Apache License 2.0
58 stars 27 forks source link

Unable to run example with node-template #349

Closed lovesh closed 3 years ago

lovesh commented 3 years ago

While trying to run example substrateMaster.ts, i get the following error

Error: 1002 Verification Error: Execution: Could not convert parameter `tx` between node and runtime: No such variant in enum MultiSignature: "RuntimeApi(\"Execution: Could not convert parameter `tx` between node and runtime: No such variant in enum MultiSignature\")"

It originates at await rpcToNode('author_submitExtrinsic', [tx]); in the example.

I tried setting the types as below but same error.

registry.setKnownTypes({
    types: {
      "Address": "AccountId",
      "LookupSource": "AccountId",
      "Balance": "u64",
      "BlockNumber": "u32",
      ............
      ................

I see a similar issue from the past but apparently, its cause is already fixed.

emostov commented 3 years ago

@lovesh I will look further into this, but soon we will be release a new version of txwrapper in a new repo that will much more friendly for general substrate chains. The repo is currently private because it is still a WIP,

lovesh commented 3 years ago

Thanks @emostov

lovesh commented 3 years ago

Rather than setKnownTypes, I required registry.register({Address: 'AccountId', LookupSource: 'AccountId'}) and the example works.

emostov commented 3 years ago

Ok, good to know and thanks for follow up.

I have not had the chance to test this out in the exact situation you mention, but one way to use setKnownTypes, I think you need to have some keys for the type definitions to indicate the chain the types are associated with. For example if you keyed it by spec types you could use (getSpecTypes) to identify the types: https://github.com/polkadot-js/api/blob/8e6a1c8fb1c42225e814d586ea1d7942ba5fca6f/packages/types-known/src/index.ts#L43

And then you would still have to call registry.register(getSpecTypes(..)) like the API does here: https://github.com/polkadot-js/api/blob/04340de6ae0fecbfdf4bb4f321bb0cdc69966217/packages/api/src/base/Init.ts#L85

For example, to create a registry getter that can use the known types from @polkadot/apps-config you could have something like:

import { typesBundle, typesChain, typesSpec } from '@polkadot/apps-config/api';
// Need some other imports as well

/**
 * Create a registry with `knownTypes` set with types from @polkadot/apps-config.
 */
function getAppsConfigRegistry(): TypeRegistry {
    const registry = new TypeRegistry();
    registry.setKnownTypes({
        typesBundle,
        typesChain,
        typesSpec,
    });

    return registry;
}

/**
 * Create a registry based on specName, chainName, specVersion and metadataRPC. This should work for
 * Polkadot, Kusama, Westend and any chain which has up-to-date types in @polkadot/apps-config.
 *
 * @param GetRegistryOptions specName, chainName, specVersion, and metadataRpc of the current runtime
 */
export function getRegistry({
    specName,
    chainName,
    specVersion,
    metadataRpc,
    properties,
}: GetRegistryOpts): TypeRegistry {
    // Polkadot, kusama, and westend have known types in the default polkadot-js registry. If we are
    // dealing with another network, use the apps-config types to fill the registry.
    const registry = ['polkadot', 'kusama', 'westend'].includes(specName)
        ? new TypeRegistry()
        : getAppsConfigRegistry();

    return getRegistryBase({
        chainProperties: properties || knownChainProperties[specName],
        // `getSpecTypes` is used to extract the chain specific types from the registries `knownTypes`
        specTypes: getSpecTypes(registry, chainName, specName, specVersion),
        metadataRpc,
    });
}

/**
 * Create a type registry given chainProperties, specTypes, and metadataRpc.
 */
export function getRegistryBase({
    chainProperties,
    specTypes,
    metadataRpc,
}: GetRegistryBaseArgs): TypeRegistry {
    const registry = new TypeRegistry();

    const metadata = createMetadata(registry, metadataRpc);

    registry.register(specTypes);

    registry.setMetadata(metadata);

    // Register the chain properties for this registry
    registry.setChainProperties(
        registry.createType('ChainProperties', chainProperties)
    );

    return registry;
}
emostov commented 3 years ago

@lovesh Just to circle back to my early comment about a "new" txwrapper, we just made a new repo, txwrapp-core, public: https://github.com/paritytech/txwrapper-core#txwrapper-core