Skip to content

Commit a3295e1

Browse files
committed
add helper creator
1 parent a340e09 commit a3295e1

File tree

5 files changed

+216
-4
lines changed

5 files changed

+216
-4
lines changed

packages/telescope/__tests__/telescope-helper-func.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ const options: TelescopeOptions = {
3333
},
3434

3535
interfaces: {
36-
enabled: false,
36+
enabled: true,
37+
useGlobalDecoderRegistry: true,
3738
useUnionTypes: false,
3839
},
3940

@@ -333,7 +334,7 @@ const options: TelescopeOptions = {
333334
}
334335
}
335336
},
336-
useLegacyInlineEncoding: true,
337+
useLegacyInlineEncoding: false,
337338
},
338339
packages: {
339340
akash: {

packages/telescope/src/generators/create-helpers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
jsonSafe,
2424
decimal,
2525
getHelperFuncTypes,
26+
getHelperFuncTypesForInterface,
2627
getReactQueryHelperHooks,
2728
getReactQueryHelperHooksIcJs,
2829
} from "../helpers";
@@ -83,7 +84,11 @@ export const plugin = (builder: TelescopeBuilder) => {
8384

8485
if (builder.options.helperFuncCreators?.enabled) {
8586
builder.files.push("helper-func-types.ts");
86-
write(builder, "helper-func-types.ts", getHelperFuncTypes(builder.options));
87+
if(builder.options.interfaces?.enabled && builder.options.interfaces?.useGlobalDecoderRegistry) {
88+
write(builder, "helper-func-types.ts", getHelperFuncTypesForInterface(builder.options));
89+
} else {
90+
write(builder, "helper-func-types.ts", getHelperFuncTypes(builder.options));
91+
}
8792
}
8893

8994
if (
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
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+
};

packages/telescope/src/helpers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ export * from './registry-helper';
1919
export * from './json-safe';
2020
export * from './decimals';
2121
export * from './helper-func-types';
22+
export * from './helper-func-types-interface';
Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)