Skip to content

Commit

Permalink
Merge pull request #503 from cosmology-tech/feat/tendermint34Client_t…
Browse files Browse the repository at this point in the history
…o_cometClient

feat/use Comet Client
  • Loading branch information
hoangdv2429 authored Dec 12, 2023
2 parents 25d5b5d + c951142 commit 44144f1
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 18 deletions.
24 changes: 24 additions & 0 deletions packages/ast/src/clients/rpc/scoped/__snapshots__/rpc.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ exports[`createScopedRpcFactory 1`] = `
});"
`;

exports[`createScopedRpcFactoryWithComet 1`] = `
"export const createRpcFactorizzleWithComet = async ({
rpc
}: {
rpc: Rpc;
}) => {
return {
cosmos: {
bank: {
v1beta1: (await import("./proto/cosmos/bank/v1beta1/query.lcd")).createClientImpl(rpc)
},
gov: {
v1beta1: (await import("./proto/cosmos/bank/v1beta1/query.lcd")).createClientImpl(rpc)
}
},
osmosis: {
gamm: {
v1beta1: (await import("./proto/cosmos/bank/v1beta1/query.lcd")).createClientImpl(rpc)
}
}
};
};"
`;

exports[`createScopedRpcFactoryWithoutTm 1`] = `
"export const createRpcFactorizzleWithoutTM = async ({
rpc
Expand Down
22 changes: 22 additions & 0 deletions packages/ast/src/clients/rpc/scoped/rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,25 @@ it('createScopedRpcFactoryWithoutTm', async () => {
'createRpcFactorizzleWithoutTM',
))
});

it('createScopedRpcFactoryWithComet', async () => {
const context = getGenericParseContext();
context.options!.rpcClients!.useConnectComet = true;
expectCode(createScopedRpcTmFactory(context, {
cosmos: {
bank: {
v1beta1: "./proto/cosmos/bank/v1beta1/query.lcd"
},
gov: {
v1beta1: "./proto/cosmos/bank/v1beta1/query.lcd"
},
},
osmosis: {
gamm: {
v1beta1: "./proto/cosmos/bank/v1beta1/query.lcd"
}
}
},
'createRpcFactorizzleWithComet',
))
});
37 changes: 27 additions & 10 deletions packages/ast/src/clients/rpc/scoped/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export const createScopedRpcTmFactory = (
obj: object,
identifier: string
) => {
const newClientType = context.pluginValue('rpcClients.useConnectComet');
const extensions = context.pluginValue('rpcClients.extensions');
let functionParams;
const returnStatement = t.returnStatement(
Expand All @@ -200,10 +201,34 @@ export const createScopedRpcTmFactory = (
extensions ? 'client' : 'rpc',
)
);

let functionStatements;
let awaitClientCreation;
// TODO: remove tendermint34client and options if not needed
if (newClientType) {
// use connectComet for dynamic client
context.addUtil('connectComet');
awaitClientCreation = t.callExpression(
t.identifier('connectComet'),
[
t.identifier('rpcEndpoint')
]
)
} else {
// use tendermint34 client
context.addUtil('Tendermint34Client');
awaitClientCreation = t.callExpression(
t.memberExpression(
t.identifier('Tendermint34Client'),
t.identifier('connect')
),
[
t.identifier('rpcEndpoint')
]
)
}

if(extensions){
context.addUtil('Tendermint34Client');
context.addUtil('HttpEndpoint');
context.addUtil('QueryClient');

Expand Down Expand Up @@ -239,15 +264,7 @@ export const createScopedRpcTmFactory = (
t.variableDeclarator(
t.identifier('tmClient'),
t.awaitExpression(
t.callExpression(
t.memberExpression(
t.identifier('Tendermint34Client'),
t.identifier('connect')
),
[
t.identifier('rpcEndpoint')
]
)
awaitClientCreation
)
)
]),
Expand Down
8 changes: 4 additions & 4 deletions packages/starship/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@
"typescript": "^5.0.4"
},
"dependencies": {
"@cosmjs/amino": "0.29.3",
"@cosmjs/proto-signing": "0.29.3",
"@cosmjs/stargate": "0.29.3",
"@cosmjs/tendermint-rpc": "^0.29.3",
"@cosmjs/amino": "^0.32.0",
"@cosmjs/proto-signing": "^0.32.0",
"@cosmjs/stargate": "^0.32.0",
"@cosmjs/tendermint-rpc": "^0.32.0",
"@cosmology/core": "1.23.0",
"@cosmology/lcd": "^0.13.3",
"@keplr-wallet/unit": "0.11.56",
Expand Down
6 changes: 3 additions & 3 deletions packages/telescope/src/helpers/external.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const external = `import { QueryClient, createProtobufRpcClient, ProtobufRpcClient } from '@cosmjs/stargate'
import { Tendermint34Client, HttpEndpoint } from "@cosmjs/tendermint-rpc";
import { connectComet, HttpEndpoint } from "@cosmjs/tendermint-rpc";
const _rpcClients: Record<string, ProtobufRpcClient> = {};
Expand All @@ -18,9 +18,9 @@ export const getRpcClient = async (rpcEndpoint: string | HttpEndpoint) => {
if (_rpcClients.hasOwnProperty(key)) {
return _rpcClients[key];
}
const tmClient = await Tendermint34Client.connect(rpcEndpoint);
const cometClient = await connectComet(rpcEndpoint);
//@ts-ignore
const client = new QueryClient(tmClient);
const client = new QueryClient(cometClient);
const rpc = createProtobufRpcClient(client);
_rpcClients[key] = rpc;
return rpc;
Expand Down
1 change: 1 addition & 0 deletions packages/telescope/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const UTILS = {
base64FromBytes: '__helpers__',
bytesFromBase64: '__helpers__',
BrowserHeaders: 'browser-headers',
connectComet: '@cosmjs/tendermint-rpc',
Decimal: '@cosmjs/math',
createProtobufRpcClient: '@cosmjs/stargate',
Pubkey: '@cosmjs/amino',
Expand Down
2 changes: 1 addition & 1 deletion packages/telescope/types/helpers/external.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export declare const external = "import { QueryClient, createProtobufRpcClient, ProtobufRpcClient } from '@cosmjs/stargate'\nimport { Tendermint34Client, HttpEndpoint } from \"@cosmjs/tendermint-rpc\";\n\nconst _rpcClients: Record<string, ProtobufRpcClient> = {};\n\nexport const getRpcEndpointKey = (rpcEndpoint: string | HttpEndpoint) => {\n if (typeof rpcEndpoint === 'string') {\n return rpcEndpoint;\n } else if (!!rpcEndpoint) {\n //@ts-ignore \n return rpcEndpoint.url;\n }\n}\n\nexport const getRpcClient = async (rpcEndpoint: string | HttpEndpoint) => {\n const key = getRpcEndpointKey(rpcEndpoint);\n if (!key) return;\n if (_rpcClients.hasOwnProperty(key)) {\n return _rpcClients[key];\n }\n const tmClient = await Tendermint34Client.connect(rpcEndpoint);\n //@ts-ignore\n const client = new QueryClient(tmClient);\n const rpc = createProtobufRpcClient(client);\n _rpcClients[key] = rpc;\n return rpc;\n}\n";
export declare const external = "import { QueryClient, createProtobufRpcClient, ProtobufRpcClient } from '@cosmjs/stargate'\nimport { connectComet, HttpEndpoint } from \"@cosmjs/tendermint-rpc\";\n\nconst _rpcClients: Record<string, ProtobufRpcClient> = {};\n\nexport const getRpcEndpointKey = (rpcEndpoint: string | HttpEndpoint) => {\n if (typeof rpcEndpoint === 'string') {\n return rpcEndpoint;\n } else if (!!rpcEndpoint) {\n //@ts-ignore \n return rpcEndpoint.url;\n }\n}\n\nexport const getRpcClient = async (rpcEndpoint: string | HttpEndpoint) => {\n const key = getRpcEndpointKey(rpcEndpoint);\n if (!key) return;\n if (_rpcClients.hasOwnProperty(key)) {\n return _rpcClients[key];\n }\n const cometClient = await connectComet(rpcEndpoint);\n //@ts-ignore\n const client = new QueryClient(cometClient);\n const rpc = createProtobufRpcClient(client);\n _rpcClients[key] = rpc;\n return rpc;\n}\n";
1 change: 1 addition & 0 deletions packages/telescope/types/utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export declare const UTILS: {
base64FromBytes: string;
bytesFromBase64: string;
BrowserHeaders: string;
connectComet: string;
Decimal: string;
createProtobufRpcClient: string;
Pubkey: string;
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/telescope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ interface TelescopeOpts {
};
}
}[];
useConnectComet?: boolean;
};
reactQuery?: {
enabled: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/types/types/telescope.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ interface TelescopeOpts {
};
};
}[];
useConnectComet?: boolean;
};
reactQuery?: {
enabled: boolean;
Expand Down
Loading

0 comments on commit 44144f1

Please sign in to comment.