How to use the @polkadot/types/codec.createTypeUnsafe function in @polkadot/types

To help you get started, we’ve selected a few @polkadot/types examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github polkadot-js / api / packages / api / src / checkTypes.manual.ts View on Github external
function types (): void {
  // check correct types with `createType`
  const balance = createType('Balance', 2);
  const gas = createType('Gas', 2);
  const compact = createType('Compact', 2);
  // const random = createType('RandomType', 2); // This one should deliberately show a TS error

  const gasUnsafe = createTypeUnsafe('Gas', [2]);
  const overriddenUnsafe = createTypeUnsafe<header>('Gas', [2]);

  console.log(balance, gas, compact, gasUnsafe, overriddenUnsafe);
}
</header>
github polkadot-js / api / packages / rpc-core / src / index.ts View on Github external
//   - Codec - There is a valid value, non-empty
      //   - null - The storage key is empty
      return keys.reduce((results, key: StorageKey): Codec[] => {
        try {
          results.push(this.formatStorageSet(key, changes, withCache));
        } catch (error) {
          console.error(`Unable to decode storage ${key.section}.${key.method}:`, error.message);

          throw error;
        }

        return results;
      }, [] as Codec[]);
    }

    return createTypeUnsafe(this.registry, method.type, [result]);
  }
github polkadot-js / api / packages / api / src / checkTypes.manual.ts View on Github external
function types (): void {
  // check correct types with `createType`
  const balance = createType('Balance', 2);
  const gas = createType('Gas', 2);
  const compact = createType('Compact', 2);
  // const random = createType('RandomType', 2); // This one should deliberately show a TS error

  const gasUnsafe = createTypeUnsafe('Gas', [2]);
  const overriddenUnsafe = createTypeUnsafe<header>('Gas', [2]);

  console.log(balance, gas, compact, gasUnsafe, overriddenUnsafe);
}
</header>
github polkadot-js / api / packages / metadata / src / Decorated / storage / fromMetadata / createFunction.ts View on Github external
function createKeyDoubleMap (registry: Registry, { meta: { name, type } }: CreateItemFn, rawKey: Uint8Array, args: [CreateArgType, CreateArgType], [hasher, key2Hasher]: [HasherFunction, HasherFunction?]): Uint8Array {
  // since we are passing an almost-unknown through, trust, but verify
  assert(
    Array.isArray(args) && !isUndefined(args[0]) && !isNull(args[0]) && !isUndefined(args[1]) && !isNull(args[1]),
    `${name} is a DoubleMap and requires two arguments`
  );

  const [key1, key2] = args;
  const type1 = type.asDoubleMap.key1.toString();
  const type2 = type.asDoubleMap.key2.toString();
  const param1Encoded = u8aConcat(rawKey, createTypeUnsafe(registry, type1, [key1]).toU8a(true));
  const param1Hashed = hasher(param1Encoded);

  // If this fails it means the getHashers function failed - and we have much bigger issues
  const param2Hashed = (key2Hasher as HasherFunction)(createTypeUnsafe(registry, type2, [key2]).toU8a(true));

  // as per createKey, always add the length prefix (underlying it is Bytes)
  return Compact.addLengthPrefix(u8aConcat(param1Hashed, param2Hashed));
}
github polkadot-js / api / packages / rpc-core / src / index.ts View on Github external
// we convert to Uint8Array since it maps to the raw encoding, all
    // data will be correctly encoded (incl. numbers, excl. :code)
    const input = isEmpty
      ? null
      : this.treatAsHex(key)
        ? value
        : u8aToU8a(value);

    if (meta.modifier.isOptional) {
      return new Option(
        this.registry,
        createClass(this.registry, type),
        isEmpty
          ? null
          : createTypeUnsafe(this.registry, type, [input], true)
      );
    }

    return createTypeUnsafe(this.registry, type, [isEmpty ? meta.fallback : input], true);
  }
github polkadot-js / api / packages / rpc-core / src / index.ts View on Github external
? null
      : this.treatAsHex(key)
        ? value
        : u8aToU8a(value);

    if (meta.modifier.isOptional) {
      return new Option(
        this.registry,
        createClass(this.registry, type),
        isEmpty
          ? null
          : createTypeUnsafe(this.registry, type, [input], true)
      );
    }

    return createTypeUnsafe(this.registry, type, [isEmpty ? meta.fallback : input], true);
  }
github polkadot-js / api / packages / api-contract / src / util.ts View on Github external
export function formatData (registry: Registry, data: Raw, { info, type }: TypeDef): Codec {
  if (info === TypeDefInfo.Option) {
    return new Option(
      registry,
      createClass(registry, type),
      createTypeUnsafe(registry, type, [data], true)
    );
  }

  return createTypeUnsafe(registry, type, [data], true);
}
github polkadot-js / api / packages / api-contract / src / util.ts View on Github external
export function formatData (registry: Registry, data: Raw, { info, type }: TypeDef): Codec {
  if (info === TypeDefInfo.Option) {
    return new Option(
      registry,
      createClass(registry, type),
      createTypeUnsafe(registry, type, [data], true)
    );
  }

  return createTypeUnsafe(registry, type, [data], true);
}