-
Notifications
You must be signed in to change notification settings - Fork 565
feat(cosmogen): implement 3rd party generation root template #4737
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
Merged
+686
−52
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7237844
feat(cosmogen): implement 3rd party generation
julienrbrt 3e1d676
cl
julienrbrt 9d15b29
hardcode module path
julienrbrt cbecb98
simplify
julienrbrt 4a65cf6
updates
julienrbrt fa97462
lint
julienrbrt ae34d73
feat: Filter protos to module being processed
clockworkgr 79fb14c
put test protos in correct folder
julienrbrt 50c5752
remove todo
julienrbrt 3433d30
prep v29.1.0 cl
julienrbrt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
ignite/pkg/cosmosgen/testdata/expected_files/ts-client/client.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/// <reference path="./types.d.ts" /> | ||
import { | ||
GeneratedType, | ||
OfflineSigner, | ||
EncodeObject, | ||
Registry, | ||
} from "@cosmjs/proto-signing"; | ||
import { SigningStargateClient, StdFee } from "@cosmjs/stargate"; | ||
import { Env } from "./env"; | ||
import { UnionToIntersection, Return, Constructor } from "./helpers"; | ||
import { IgntModule } from "./modules"; | ||
import { EventEmitter } from "events"; | ||
import { ChainInfo } from "@keplr-wallet/types"; | ||
|
||
const defaultFee = { | ||
amount: [], | ||
gas: "200000", | ||
}; | ||
|
||
export class IgniteClient extends EventEmitter { | ||
static plugins: IgntModule[] = []; | ||
env: Env; | ||
signer?: OfflineSigner; | ||
registry: Array<[string, GeneratedType]> = []; | ||
static plugin<T extends IgntModule | IgntModule[]>(plugin: T) { | ||
const currentPlugins = this.plugins; | ||
|
||
class AugmentedClient extends this { | ||
static plugins = currentPlugins.concat(plugin); | ||
} | ||
|
||
if (Array.isArray(plugin)) { | ||
type Extension = UnionToIntersection<Return<T>['module']> | ||
return AugmentedClient as typeof IgniteClient & Constructor<Extension>; | ||
} | ||
|
||
type Extension = Return<T>['module'] | ||
return AugmentedClient as typeof IgniteClient & Constructor<Extension>; | ||
} | ||
|
||
async signAndBroadcast(msgs: EncodeObject[], fee: StdFee, memo: string) { | ||
if (this.signer) { | ||
const { address } = (await this.signer.getAccounts())[0]; | ||
const signingClient = await SigningStargateClient.connectWithSigner(this.env.rpcURL, this.signer, { registry: new Registry(this.registry) }); | ||
return await signingClient.signAndBroadcast(address, msgs, fee ? fee : defaultFee, memo) | ||
} else { | ||
throw new Error(" Signer is not present."); | ||
} | ||
} | ||
|
||
constructor(env: Env, signer?: OfflineSigner) { | ||
super(); | ||
this.env = env; | ||
this.setMaxListeners(0); | ||
this.signer = signer; | ||
const classConstructor = this.constructor as typeof IgniteClient; | ||
classConstructor.plugins.forEach(plugin => { | ||
const pluginInstance = plugin(this); | ||
Object.assign(this, pluginInstance.module) | ||
if (this.registry) { | ||
this.registry = this.registry.concat(pluginInstance.registry) | ||
} | ||
}); | ||
} | ||
useSigner(signer: OfflineSigner) { | ||
this.signer = signer; | ||
this.emit("signer-changed", this.signer); | ||
} | ||
removeSigner() { | ||
this.signer = undefined; | ||
this.emit("signer-changed", this.signer); | ||
} | ||
async useKeplr(keplrChainInfo: Partial<ChainInfo> = {}) { | ||
// Using queryClients directly because BaseClient has no knowledge of the modules at this stage | ||
try { | ||
const queryClient = ( | ||
await import("./cosmos.base.tendermint.v1beta1/module") | ||
).queryClient; | ||
const bankQueryClient = (await import("./cosmos.bank.v1beta1/module")) | ||
.queryClient; | ||
const stakingQueryClient = (await import("./cosmos.staking.v1beta1/module")).queryClient; | ||
const stakingqc = stakingQueryClient({ addr: this.env.apiURL }); | ||
const staking = await (await stakingqc.queryParams()).data; | ||
const qc = queryClient({ addr: this.env.apiURL }); | ||
const node_info = await (await qc.serviceGetNodeInfo()).data; | ||
const chainId = node_info.default_node_info?.network ?? ""; | ||
const chainName = chainId?.toUpperCase() + " Network"; | ||
const bankqc = bankQueryClient({ addr: this.env.apiURL }); | ||
const tokens = await (await bankqc.queryTotalSupply()).data; | ||
const addrPrefix = this.env.prefix ?? "cosmos"; | ||
const rpc = this.env.rpcURL; | ||
const rest = this.env.apiURL; | ||
|
||
let bip44 = { | ||
coinType: 118, | ||
}; | ||
|
||
let bech32Config = { | ||
bech32PrefixAccAddr: addrPrefix, | ||
bech32PrefixAccPub: addrPrefix + "pub", | ||
bech32PrefixValAddr: addrPrefix + "valoper", | ||
bech32PrefixValPub: addrPrefix + "valoperpub", | ||
bech32PrefixConsAddr: addrPrefix + "valcons", | ||
bech32PrefixConsPub: addrPrefix + "valconspub", | ||
}; | ||
|
||
let currencies = | ||
tokens.supply?.map((x) => { | ||
const y = { | ||
coinDenom: x.denom?.toUpperCase() ?? "", | ||
coinMinimalDenom: x.denom ?? "", | ||
coinDecimals: 0, | ||
}; | ||
return y; | ||
}) ?? []; | ||
|
||
let stakeCurrency = { | ||
coinDenom: staking.params?.bond_denom?.toUpperCase() ?? "", | ||
coinMinimalDenom: staking.params?.bond_denom ?? "", | ||
coinDecimals: 0, | ||
}; | ||
|
||
let feeCurrencies = | ||
tokens.supply?.map((x) => { | ||
const y = { | ||
coinDenom: x.denom?.toUpperCase() ?? "", | ||
coinMinimalDenom: x.denom ?? "", | ||
coinDecimals: 0, | ||
}; | ||
return y; | ||
}) ?? []; | ||
|
||
if (chainId) { | ||
const suggestOptions: ChainInfo = { | ||
chainId, | ||
chainName, | ||
rpc, | ||
rest, | ||
stakeCurrency, | ||
bip44, | ||
bech32Config, | ||
currencies, | ||
feeCurrencies, | ||
...keplrChainInfo, | ||
}; | ||
await window.keplr.experimentalSuggestChain(suggestOptions); | ||
|
||
window.keplr.defaultOptions = { | ||
sign: { | ||
preferNoSetFee: true, | ||
preferNoSetMemo: true, | ||
}, | ||
}; | ||
} | ||
await window.keplr.enable(chainId); | ||
this.signer = window.keplr.getOfflineSigner(chainId); | ||
this.emit("signer-changed", this.signer); | ||
} catch (e) { | ||
throw new Error( | ||
"Could not load tendermint, staking and bank modules. Please ensure your client loads them to use useKeplr()" | ||
); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
ignite/pkg/cosmosgen/testdata/expected_files/ts-client/env.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { OfflineSigner } from "@cosmjs/proto-signing"; | ||
|
||
export interface Env { | ||
apiURL: string | ||
rpcURL: string | ||
prefix?: string | ||
} |
32 changes: 32 additions & 0 deletions
32
ignite/pkg/cosmosgen/testdata/expected_files/ts-client/helpers.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export type Constructor<T> = new (...args: any[]) => T; | ||
|
||
export type AnyFunction = (...args: any) => any; | ||
|
||
export type UnionToIntersection<Union> = | ||
(Union extends any | ||
? (argument: Union) => void | ||
: never | ||
) extends (argument: infer Intersection) => void | ||
? Intersection | ||
: never; | ||
|
||
export type Return<T> = | ||
T extends AnyFunction | ||
? ReturnType<T> | ||
: T extends AnyFunction[] | ||
? UnionToIntersection<ReturnType<T[number]>> | ||
: never | ||
|
||
|
||
export const MissingWalletError = new Error("wallet is required"); | ||
|
||
export function getStructure(template) { | ||
let structure = { fields: [] as Array<unknown>} | ||
for (const [key, value] of Object.entries(template)) { | ||
let field: any = {} | ||
field.name = key | ||
field.type = typeof value | ||
structure.fields.push(field) | ||
} | ||
return structure | ||
} |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...data/testchain/ts-client/mars/registry.ts → .../ts-client/ignite.planet.mars/registry.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 10 additions & 10 deletions
20
...testdata/testchain/ts-client/mars/rest.ts → ...iles/ts-client/ignite.planet.mars/rest.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
ignite/pkg/cosmosgen/testdata/expected_files/ts-client/ignite.planet.mars/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { AnotherType } from "./types/ignite/planet/mars/mars" | ||
|
||
|
||
export { | ||
AnotherType, | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same needs to be done on generate openapi
doesnt affect ts-client generation but it will be nice to polish everything
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure thing!