@polkadot/types#Raw TypeScript Examples

The following examples show how to use @polkadot/types#Raw. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: metadata.ts    From atlas with GNU General Public License v3.0 6 votes vote down vote up
parseMemberExtrinsicInput: ParseExtrinsicInputFn<MemberInputMetadata, undefined> = async (
  api,
  inputMetadata
) => {
  const memberProperties: IMembershipMetadata = {}
  if (inputMetadata.name != null) {
    memberProperties.name = inputMetadata.name
  }
  if (inputMetadata.about != null) {
    memberProperties.about = inputMetadata.about
  }
  if (inputMetadata.avatarUri != null) {
    memberProperties.avatarUri = inputMetadata.avatarUri
  }

  const serializedMemberMetadata = MembershipMetadata.encode(memberProperties).finish()
  const memberMetadataRaw = new Raw(api.registry, serializedMemberMetadata)
  const memberMetadataBytes = new Bytes(api.registry, memberMetadataRaw)
  const optionalMemberMetadataBytes = new Option(api.registry, Bytes, memberMetadataBytes)

  return [optionalMemberMetadataBytes, undefined]
}
Example #2
Source File: Logs.tsx    From crust-apps with Apache License 2.0 6 votes vote down vote up
function formatU8a (value: Raw): React.ReactNode {
  return (
    <Params
      isDisabled
      params={[{ type: getTypeDef('Bytes') }]}
      values={[{ isValid: true, value }]}
    />
  );
}
Example #3
Source File: Logs.tsx    From crust-apps with Apache License 2.0 6 votes vote down vote up
function formatItem (item: DigestItem): React.ReactNode {
  if (item.value instanceof Struct) {
    return formatStruct(item.value);
  } else if (item.value instanceof Tuple) {
    return formatTuple(item.value);
  } else if (item.value instanceof Vec) {
    return formatVector(item.value);
  } else if (item.value instanceof Raw) {
    return formatU8a(item.value);
  }

  return <div>{item.value.toString().split(',').join(', ')}</div>;
}
Example #4
Source File: valueToText.tsx    From crust-apps with Apache License 2.0 6 votes vote down vote up
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export default function valueToText (type: string, value: Codec | undefined | null, swallowError = true, contentShorten = true): React.ReactNode {
  if (isNull(value) || isUndefined(value)) {
    return div({}, '<unknown>');
  }

  return div(
    {},
    ['Bytes', 'Raw', 'Option<Keys>', 'Keys'].includes(type)
      ? u8aToHex(value.toU8a(true), contentShorten ? 512 : -1)
      // HACK Handle Keys as hex-only (this should go away once the node value is
      // consistently swapped to `Bytes`)
      : type === 'Vec<(ValidatorId,Keys)>'
        ? toString(formatKeys(value as unknown as [ValidatorId, Keys][]))
        : value instanceof Raw
          ? value.isEmpty
            ? '<empty>'
            : value.toString()
          : (value instanceof Option) && value.isNone
            ? '<none>'
            : toString(toHuman(value))
  );
}
Example #5
Source File: Storage.ts    From gear-js with GNU General Public License v3.0 6 votes vote down vote up
/**
   * Get program from chain
   * @param programId
   * @returns
   */
  async gProg(programId: ProgramId): Promise<IActiveProgram> {
    const storage = (await this.api.rpc.state.getStorage(`0x${PREFIXES.prog}${programId.slice(2)}`)) as Option<Raw>;
    const program = this.api.createType('Program', storage.unwrap()) as IProgram;
    return program.isActive ? program.asActive : program.asTerminated;
  }
Example #6
Source File: valueToText.tsx    From subscan-multisig-react with Apache License 2.0 6 votes vote down vote up
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// eslint-disable-next-line complexity
export default function valueToText(
  type: string,
  value: Codec | undefined | null,
  _swallowError = true,
  contentShorten = true
): React.ReactNode {
  if (isNull(value) || isUndefined(value)) {
    return div({}, '<unknown>');
  }

  return div(
    {},
    ['Bytes', 'Raw', 'Option<Keys>', 'Keys'].includes(type) && isFunction(value.toU8a)
      ? // eslint-disable-next-line no-magic-numbers
        u8aToHex(value.toU8a(true), contentShorten ? 512 : -1)
      : // HACK Handle Keys as hex-only (this should go away once the node value is
      // consistently swapped to `Bytes`)
      type === 'Vec<(ValidatorId,Keys)>'
      ? toString(formatKeys(value as unknown as [ValidatorId, Keys][]))
      : value instanceof Raw
      ? value.isEmpty
        ? '<empty>'
        : value.toString()
      : value instanceof Option && value.isNone
      ? '<none>'
      : toString(toHuman(value))
  );
}
Example #7
Source File: metadata.ts    From atlas with GNU General Public License v3.0 5 votes vote down vote up
parseChannelExtrinsicInput: ParseExtrinsicInputFn<ChannelInputMetadata, ChannelInputAssets> = async (
  api,
  inputMetadata,
  inputAssets
) => {
  const channelProperties: IChannelMetadata = {}

  // prepare data objects and assign proper indexes in metadata
  const channelDataObjectsMetadata: DataObjectMetadata[] = [
    ...(inputAssets.avatarPhoto ? [inputAssets.avatarPhoto] : []),
    ...(inputAssets.coverPhoto ? [inputAssets.coverPhoto] : []),
  ]
  const channelStorageAssets = await prepareAssetsForExtrinsic(api, channelDataObjectsMetadata)
  if (inputAssets.avatarPhoto) {
    channelProperties.avatarPhoto = 0
  }
  if (inputAssets.coverPhoto) {
    channelProperties.coverPhoto = inputAssets.avatarPhoto ? 1 : 0
  }

  if (inputMetadata.title != null) {
    channelProperties.title = inputMetadata.title
  }
  if (inputMetadata.description != null) {
    channelProperties.description = inputMetadata.description
  }
  if (inputMetadata.isPublic != null) {
    channelProperties.isPublic = inputMetadata.isPublic
  }
  if (inputMetadata.language != null) {
    channelProperties.language = inputMetadata.language
  }

  const serializedChannelMetadata = ChannelMetadata.encode(channelProperties).finish()
  const channelMetadataRaw = new Raw(api.registry, serializedChannelMetadata)
  const channelMetadataBytes = new Bytes(api.registry, channelMetadataRaw)
  const optionalChannelMetadataBytes = new Option(api.registry, Bytes, channelMetadataBytes)

  const optionalChannelStorageAssets = new Option(api.registry, StorageAssets, channelStorageAssets)

  return [optionalChannelMetadataBytes, optionalChannelStorageAssets]
}
Example #8
Source File: Query.tsx    From crust-apps with Apache License 2.0 5 votes vote down vote up
function getCachedComponent (query: QueryTypes): CacheInstance {
  const { id, isConst, key, params = [] } = query as StorageModuleQuery;

  if (!cache[id]) {
    let renderHelper;
    let type: string;

    if (isConst) {
      const { meta, method, section } = key as unknown as ConstValue;

      renderHelper = withCallDiv(`consts.${section}.${method}`, { withIndicator: true });
      type = meta.type.toString();
    } else {
      if (isU8a(key)) {
        // subscribe to the raw key here
        renderHelper = withCallDiv('rpc.state.subscribeStorage', {
          paramName: 'params',
          paramValid: true,
          params: [[key]],
          transform: ([data]: Option<Raw>[]): Option<Raw> => data,
          withIndicator: true
        });
      } else {
        const values: unknown[] = params.map(({ value }) => value);
        const { creator: { meta: { type } } } = key;
        const allCount = type.isPlain
          ? 0
          : type.isMap
            ? 1
            : 2;

        renderHelper = withCallDiv('subscribe', {
          paramName: 'params',
          paramValid: true,
          params: values.length === allCount
            ? [key, ...values]
            : [key.entries, ...values],
          withIndicator: true
        });
      }

      type = key.creator && key.creator.meta
        ? typeToString(key)
        : 'Raw';
    }

    const defaultProps = { className: 'ui--output' };
    const Component = renderHelper(
      // By default we render a simple div node component with the query results in it
      (value: any) => <pre>{valueToText(type, value, true, true)}</pre>,
      defaultProps
    );

    cache[query.id] = createComponent(type, Component, defaultProps, renderHelper);
  }

  return cache[id];
}
Example #9
Source File: metadata.ts    From atlas with GNU General Public License v3.0 4 votes vote down vote up
parseVideoExtrinsicInput: ParseExtrinsicInputFn<VideoInputMetadata, VideoInputAssets> = async (
  api,
  inputMetadata,
  inputAssets
) => {
  const videoProperties: IVideoMetadata = {}

  // prepare data objects and assign proper indexes in metadata
  const videoDataObjectsMetadata: DataObjectMetadata[] = [
    ...(inputAssets.media ? [inputAssets.media] : []),
    ...(inputAssets.thumbnailPhoto ? [inputAssets.thumbnailPhoto] : []),
  ]
  const videoStorageAssets = await prepareAssetsForExtrinsic(api, videoDataObjectsMetadata)
  if (inputAssets.media) {
    videoProperties.video = 0
  }
  if (inputAssets.thumbnailPhoto) {
    videoProperties.thumbnailPhoto = inputAssets.media ? 1 : 0
  }

  if (inputMetadata.title != null) {
    videoProperties.title = inputMetadata.title
  }
  if (inputMetadata.description != null) {
    videoProperties.description = inputMetadata.description
  }
  if (inputMetadata.isPublic != null) {
    videoProperties.isPublic = inputMetadata.isPublic
  }
  if (inputMetadata.language != null) {
    videoProperties.language = inputMetadata.language
  }
  if (inputMetadata.isExplicit != null) {
    videoProperties.isExplicit = inputMetadata.isExplicit
  }
  if (inputMetadata.category != null) {
    videoProperties.category = Long.fromInt(inputMetadata.category)
  }
  if (inputMetadata.duration != null) {
    videoProperties.duration = inputMetadata.duration
  }
  if (inputMetadata.mediaPixelHeight != null) {
    videoProperties.mediaPixelHeight = inputMetadata.mediaPixelHeight
  }
  if (inputMetadata.mediaPixelWidth != null) {
    videoProperties.mediaPixelWidth = inputMetadata.mediaPixelWidth
  }
  if (inputMetadata.hasMarketing != null) {
    videoProperties.hasMarketing = inputMetadata.hasMarketing
  }

  if (inputMetadata.license) {
    const videoLicenseProperties: ILicense = {}
    if (inputMetadata.license.code != null) {
      videoLicenseProperties.code = inputMetadata.license.code
    }
    if (inputMetadata.license.attribution != null) {
      videoLicenseProperties.attribution = inputMetadata.license.attribution
    }
    if (inputMetadata.license.customText != null) {
      videoLicenseProperties.customText = inputMetadata.license.customText
    }

    videoProperties.license = videoLicenseProperties
  }

  if (inputMetadata.mimeMediaType != null) {
    const videoMediaTypeProperties: IMediaType = {}
    videoMediaTypeProperties.mimeMediaType = inputMetadata.mimeMediaType
    videoProperties.mediaType = videoMediaTypeProperties
  }

  if (inputMetadata.publishedBeforeJoystream != null) {
    const videoPublishedBeforeProperties: IPublishedBeforeJoystream = {}
    videoPublishedBeforeProperties.isPublished = true
    videoPublishedBeforeProperties.date = inputMetadata.publishedBeforeJoystream
    videoProperties.publishedBeforeJoystream = videoPublishedBeforeProperties
  }

  const serializedVideoMetadata = VideoMetadata.encode(videoProperties).finish()
  const videoMetadataRaw = new Raw(api.registry, serializedVideoMetadata)
  const videoMetadataBytes = new Bytes(api.registry, videoMetadataRaw)
  const optionalVideoMetadataBytes = new Option(api.registry, Bytes, videoMetadataBytes)

  const optionalVideoStorageAssets = new Option(api.registry, StorageAssets, videoStorageAssets)

  return [optionalVideoMetadataBytes, optionalVideoStorageAssets]
}