Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/use Comet Client #503

Merged
merged 20 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should really need this case distinction long term. The first case should work everywhere. However, feel free to keep it for a trial-period.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I discussed with @Zetazzz and he wants to make it into an option instead of default behavior. wdyt @Zetazzz ?

// 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