export const registryHelper = `import { BinaryReader } from "./binary"; import { Any, AnyAmino } from "./google/protobuf/any"; import { IProtoType, TelescopeGeneratedCodec } from "./types"; export class GlobalDecoderRegistry { static registry: { [key: string]: TelescopeGeneratedCodec; } = {}; static aminoProtoMapping: { [key: string]: string; } = {}; static registerAminoProtoMapping(aminoType: string, typeUrl: string) { GlobalDecoderRegistry.aminoProtoMapping[aminoType] = typeUrl; } static register( key: string, decoder: TelescopeGeneratedCodec ) { GlobalDecoderRegistry.registry[key] = decoder; } static getDecoder( key: string ): TelescopeGeneratedCodec { return GlobalDecoderRegistry.registry[key]; } static getDecoderByInstance( obj: unknown ): TelescopeGeneratedCodec | null { if (obj === undefined || obj === null) { return null; } const protoType = obj as IProtoType; if (protoType.$typeUrl) { return GlobalDecoderRegistry.getDecoder( protoType.$typeUrl ); } for (const key in GlobalDecoderRegistry.registry) { if ( Object.prototype.hasOwnProperty.call( GlobalDecoderRegistry.registry, key ) ) { const element = GlobalDecoderRegistry.registry[key]; if (element.is!(obj)) { return element; } if (element.isSDK && element.isSDK(obj)) { return element; } if (element.isAmino && element.isAmino(obj)) { return element; } } } return null; } static getDecoderByAminoType( type: string ): TelescopeGeneratedCodec | null { if (type === undefined || type === null) { return null; } const typeUrl = GlobalDecoderRegistry.aminoProtoMapping[type]; if (!typeUrl) { return null; } return GlobalDecoderRegistry.getDecoder(typeUrl); } static wrapAny(obj: unknown): Any { if(Any.is(obj)){ return obj; } const decoder = getDecoderByInstance(obj); return { typeUrl: decoder.typeUrl, value: decoder.encode(obj).finish(), }; } static unwrapAny(input: BinaryReader | Uint8Array | Any) { let data; if (Any.is(input)) { data = input; } else { const reader = input instanceof BinaryReader ? input : new BinaryReader(input); data = Any.decode(reader, reader.uint32()); } const decoder = GlobalDecoderRegistry.getDecoder( data.typeUrl ); if (!decoder) { return data; } return decoder.decode(data.value); } static fromJSON(object: any): T { const decoder = getDecoderByInstance(object); return decoder.fromJSON!(object); } static toJSON(message: T): any { const decoder = getDecoderByInstance(message); return decoder.toJSON!(message); } static fromPartial(object: unknown): T { const decoder = getDecoderByInstance(object); return decoder ? decoder.fromPartial(object) : (object as T); } static fromSDK(object: SDK): T { const decoder = getDecoderByInstance(object); return decoder.fromSDK!(object); } static fromSDKJSON(object: any): SDK { const decoder = getDecoderByInstance(object); return decoder.fromSDKJSON!(object); } static toSDK(object: T): SDK { const decoder = getDecoderByInstance(object); return decoder.toSDK!(object); } static fromAmino(object: Amino): T { const decoder = getDecoderByInstance(object); return decoder.fromAmino!(object); } static fromAminoMsg(object: AnyAmino): T { const decoder = GlobalDecoderRegistry.getDecoderByAminoType< T, unknown, Amino >(object.type); if (!decoder) { throw new Error(\`There's no decoder for the amino type \${object.type}\`); } return decoder.fromAminoMsg!(object); } static toAmino(object: T): Amino { let data: any; let decoder: TelescopeGeneratedCodec; if (Any.is(object)) { data = GlobalDecoderRegistry.unwrapAny(object); decoder = GlobalDecoderRegistry.getDecoder(object.typeUrl); if (!decoder) { decoder = Any; } } else { data = object; decoder = getDecoderByInstance(object); } return decoder.toAmino!(data); } static toAminoMsg(object: T): AnyAmino { let data: any; let decoder: TelescopeGeneratedCodec; if (Any.is(object)) { data = GlobalDecoderRegistry.unwrapAny(object); decoder = GlobalDecoderRegistry.getDecoder(object.typeUrl); if (!decoder) { decoder = Any; } } else { data = object; decoder = getDecoderByInstance(object); } return decoder.toAminoMsg!(data); } } function getDecoderByInstance( obj: unknown ): TelescopeGeneratedCodec { const decoder = GlobalDecoderRegistry.getDecoderByInstance( obj ); if (!decoder) { throw new Error( \`There's no decoder for the instance \${JSON.stringify(obj)}\` ); } return decoder; } GlobalDecoderRegistry.register(Any.typeUrl, Any); `;