|
| 1 | +import { TelescopeOptions } from "@cosmology/types"; |
| 2 | + |
| 3 | +export const getHelperFuncTypesForInterface = (options: TelescopeOptions) => { |
| 4 | + return ` |
| 5 | +import { HttpEndpoint } from "@interchainjs/types"; |
| 6 | +import { BinaryReader, BinaryWriter } from "./binary${options.restoreImportExtension ?? ""}"; |
| 7 | +import { getRpcClient } from "./extern${options.restoreImportExtension ?? ""}"; |
| 8 | +import { isRpc, Rpc } from "./helpers${options.restoreImportExtension ?? ""}"; |
| 9 | +import { TelescopeGeneratedCodec } from "./types"; |
| 10 | +import { GlobalDecoderRegistry } from "./registry"; |
| 11 | +
|
| 12 | +export interface QueryBuilderOptions<TReq, TRes> { |
| 13 | + encode: (request: TReq, writer?: BinaryWriter) => BinaryWriter |
| 14 | + decode: (input: BinaryReader | Uint8Array, length?: number) => TRes |
| 15 | + service: string, |
| 16 | + method: string, |
| 17 | + clientResolver?: RpcResolver, |
| 18 | + deps?: TelescopeGeneratedCodec<any, any, any>[], |
| 19 | +} |
| 20 | +
|
| 21 | +export function buildQuery<TReq, TRes>(opts: QueryBuilderOptions<TReq, TRes>) { |
| 22 | + return async (request: TReq) => { |
| 23 | + let rpc: Rpc | undefined; |
| 24 | +
|
| 25 | + if(isRpc(opts.clientResolver)) { |
| 26 | + rpc = opts.clientResolver; |
| 27 | + } else { |
| 28 | + rpc = opts.clientResolver ? await getRpcClient(opts.clientResolver) : undefined; |
| 29 | + } |
| 30 | +
|
| 31 | + if (!rpc) throw new Error("Query Rpc is not initialized"); |
| 32 | +
|
| 33 | + registerDependencies(opts.deps ?? []); |
| 34 | +
|
| 35 | + const data = opts.encode(request).finish(); |
| 36 | + const response = await rpc.request(opts.service, opts.method, data); |
| 37 | + return opts.decode(response); |
| 38 | + }; |
| 39 | +} |
| 40 | +
|
| 41 | +export interface ITxArgs<TMsg> { |
| 42 | + signerAddress: string; |
| 43 | + message: TMsg; |
| 44 | + fee: StdFee | 'auto'; |
| 45 | + memo: string; |
| 46 | +} |
| 47 | +
|
| 48 | +export function isISigningClient(client: unknown): client is ISigningClient { |
| 49 | + return client !== null && client !== undefined |
| 50 | + && typeof (client as ISigningClient).signAndBroadcast === 'function' |
| 51 | + && typeof (client as ISigningClient).addConverters === 'function' |
| 52 | + && typeof (client as ISigningClient).addEncoders === 'function'; |
| 53 | +} |
| 54 | +
|
| 55 | +export interface ISigningClient { |
| 56 | + /** |
| 57 | + * register converters |
| 58 | + */ |
| 59 | + addConverters: (converters: AminoConverter[]) => void; |
| 60 | + /** |
| 61 | + * register encoders |
| 62 | + */ |
| 63 | + addEncoders: (encoders: Encoder[]) => void; |
| 64 | +
|
| 65 | + signAndBroadcast: ( |
| 66 | + signerAddress: string, |
| 67 | + message: Message[], |
| 68 | + fee: StdFee | 'auto', |
| 69 | + memo: string |
| 70 | + ) => Promise<DeliverTxResponse>; |
| 71 | +} |
| 72 | +
|
| 73 | +export interface TxBuilderOptions { |
| 74 | + clientResolver?: SigningClientResolver, |
| 75 | + typeUrl: string, |
| 76 | + encoders?: Encoder[], |
| 77 | + converters?: AminoConverter[], |
| 78 | + deps?: TelescopeGeneratedCodec<any, any, any>[], |
| 79 | +} |
| 80 | +
|
| 81 | +export function buildTx<TMsg>(opts: TxBuilderOptions) { |
| 82 | + return async ( |
| 83 | + signerAddress: string, |
| 84 | + message: TMsg, |
| 85 | + fee: StdFee | 'auto', |
| 86 | + memo: string |
| 87 | + ): Promise<DeliverTxResponse> => { |
| 88 | + let client: ISigningClient | undefined; |
| 89 | +
|
| 90 | + // if opts.getSigningClient is a function, call it to get the SigningClient instance |
| 91 | + if(isISigningClient(opts.clientResolver)) { |
| 92 | + client = opts.clientResolver; |
| 93 | + } |
| 94 | +
|
| 95 | + if (!client) throw new Error("SigningClient is not initialized"); |
| 96 | +
|
| 97 | + //register all related encoders and converters |
| 98 | + client.addEncoders(opts.encoders ?? []); |
| 99 | + client.addConverters(opts.converters ?? []); |
| 100 | + registerDependencies(opts.deps ?? []); |
| 101 | +
|
| 102 | + const data = [ |
| 103 | + { |
| 104 | + typeUrl: opts.typeUrl, |
| 105 | + value: message, |
| 106 | + }, |
| 107 | + ]; |
| 108 | + return client.signAndBroadcast!(signerAddress, data, fee, memo); |
| 109 | + }; |
| 110 | +} |
| 111 | +
|
| 112 | +export interface Coin { |
| 113 | + denom: string; |
| 114 | + amount: string; |
| 115 | +} |
| 116 | +
|
| 117 | +export interface StdFee { |
| 118 | + amount: Coin[]; |
| 119 | + gas: string; |
| 120 | + /** The granter address that is used for paying with feegrants */ |
| 121 | + granter?: string; |
| 122 | + /** The fee payer address. The payer must have signed the transaction. */ |
| 123 | + payer?: string; |
| 124 | +} |
| 125 | +
|
| 126 | +/** |
| 127 | + * The response after successfully broadcasting a transaction. |
| 128 | + * Success or failure refer to the execution result. |
| 129 | + */ |
| 130 | +export interface DeliverTxResponse { |
| 131 | + height: number; |
| 132 | + /** The position of the transaction within the block. This is a 0-based index. */ |
| 133 | + txIndex: number; |
| 134 | + /** Error code. The transaction suceeded if and only if code is 0. */ |
| 135 | + code: number; |
| 136 | + transactionHash: string; |
| 137 | + events: Event[]; |
| 138 | + /** |
| 139 | + * A string-based log document. |
| 140 | + * |
| 141 | + * This currently seems to merge attributes of multiple events into one event per type |
| 142 | + * (https://github.com/tendermint/tendermint/issues/9595). You might want to use the \`events\` |
| 143 | + * field instead. |
| 144 | + */ |
| 145 | + rawLog?: string; |
| 146 | + /** @deprecated Use \`msgResponses\` instead. */ |
| 147 | + data?: MsgData[]; |
| 148 | + /** |
| 149 | + * The message responses of the [TxMsgData](https://github.com/cosmos/cosmos-sdk/blob/v0.46.3/proto/cosmos/base/abci/v1beta1/abci.proto#L128-L140) |
| 150 | + * as \`Any\`s. |
| 151 | + * This field is an empty list for chains running Cosmos SDK < 0.46. |
| 152 | + */ |
| 153 | + msgResponses: Array<{ |
| 154 | + typeUrl: string; |
| 155 | + value: Uint8Array; |
| 156 | + }>; |
| 157 | + gasUsed: bigint; |
| 158 | + gasWanted: bigint; |
| 159 | +} |
| 160 | +
|
| 161 | +export interface MsgData { |
| 162 | + msgType: string; |
| 163 | + data: Uint8Array; |
| 164 | +} |
| 165 | +
|
| 166 | +export interface Attribute { |
| 167 | + key: string; |
| 168 | + value: string; |
| 169 | + index: boolean; |
| 170 | +} |
| 171 | +export interface Event { |
| 172 | + type: string; |
| 173 | + attributes: Attribute[]; |
| 174 | +} |
| 175 | +
|
| 176 | +export interface Message<T = any> { |
| 177 | + typeUrl: string; |
| 178 | + value: T; |
| 179 | +} |
| 180 | +
|
| 181 | +export interface Encoder { |
| 182 | + typeUrl: string; |
| 183 | + fromPartial: (data: any) => any; |
| 184 | + encode: (data: any) => Uint8Array; |
| 185 | +} |
| 186 | +
|
| 187 | +export interface AminoConverter { |
| 188 | + typeUrl: string; |
| 189 | + aminoType: string; |
| 190 | + fromAmino: (data: any) => any; |
| 191 | + toAmino: (data: any) => any; |
| 192 | +} |
| 193 | +
|
| 194 | +export type SigningClientResolver = string | HttpEndpoint | ISigningClient; |
| 195 | +export type RpcResolver = string | HttpEndpoint | Rpc ; |
| 196 | +
|
| 197 | +function registerDependencies(deps: TelescopeGeneratedCodec<any, any, any>[]) { |
| 198 | + for (const dep of deps) { |
| 199 | + GlobalDecoderRegistry.register(dep.typeUrl, dep); |
| 200 | + if (dep.aminoType) { |
| 201 | + GlobalDecoderRegistry.registerAminoProtoMapping(dep.aminoType, dep.typeUrl); |
| 202 | + } |
| 203 | + } |
| 204 | +} |
| 205 | +`; |
| 206 | +}; |
0 commit comments