|
| 1 | +import Long from 'long'; |
| 2 | +import { createProtobufRpcClient, QueryClient } from '@cosmjs/stargate'; |
| 3 | + |
| 4 | +import { SignMode } from '../codec/cosmos/tx/signing/v1beta1/signing'; |
| 5 | +import { GetTxRequest, GetTxResponse, ServiceClientImpl, SimulateRequest, SimulateResponse } from '../codec/cosmos/tx/v1beta1/service'; |
| 6 | +import { AuthInfo, Fee, Tx, TxBody } from '../codec/cosmos/tx/v1beta1/tx'; |
| 7 | +import { Message } from '../messages'; |
| 8 | +import { LumRegistry } from '../registry'; |
| 9 | +import { publicKeyToProto } from '../utils'; |
| 10 | + |
| 11 | +export interface TxExtension { |
| 12 | + readonly tx: { |
| 13 | + getTx: (txId: string) => Promise<GetTxResponse>; |
| 14 | + simulate: (messages: readonly Message[], memo: string | undefined, pubkey: Uint8Array, sequence: number) => Promise<SimulateResponse>; |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +export function setupTxExtension(base: QueryClient): TxExtension { |
| 19 | + // Use this service to get easy typed access to query methods |
| 20 | + // This cannot be used for proof verification |
| 21 | + const rpc = createProtobufRpcClient(base); |
| 22 | + const queryService = new ServiceClientImpl(rpc); |
| 23 | + |
| 24 | + return { |
| 25 | + tx: { |
| 26 | + getTx: async (hash: string) => { |
| 27 | + const request: GetTxRequest = { |
| 28 | + hash, |
| 29 | + }; |
| 30 | + const response = await queryService.GetTx(request); |
| 31 | + return response; |
| 32 | + }, |
| 33 | + simulate: async (messages: readonly Message[], memo: string | undefined, pubkey: Uint8Array, sequence: number) => { |
| 34 | + const request = SimulateRequest.fromPartial({ |
| 35 | + tx: Tx.fromPartial({ |
| 36 | + authInfo: AuthInfo.fromPartial({ |
| 37 | + fee: Fee.fromPartial({}), |
| 38 | + signerInfos: [ |
| 39 | + { |
| 40 | + publicKey: publicKeyToProto(pubkey), |
| 41 | + sequence: Long.fromNumber(sequence, true), |
| 42 | + modeInfo: { single: { mode: SignMode.SIGN_MODE_UNSPECIFIED } }, |
| 43 | + }, |
| 44 | + ], |
| 45 | + }), |
| 46 | + body: TxBody.fromPartial({ |
| 47 | + messages: messages.map((m) => { |
| 48 | + return { typeUrl: m.typeUrl, value: LumRegistry.encode(m) }; |
| 49 | + }), |
| 50 | + memo: memo, |
| 51 | + }), |
| 52 | + signatures: [new Uint8Array()], |
| 53 | + }), |
| 54 | + // Sending serialized `txBytes` is the future. But |
| 55 | + // this is not available in Comsos SDK 0.42. |
| 56 | + txBytes: undefined, |
| 57 | + }); |
| 58 | + const response = await queryService.Simulate(request); |
| 59 | + return response; |
| 60 | + }, |
| 61 | + }, |
| 62 | + }; |
| 63 | +} |
0 commit comments