-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e99536
commit 0018fee
Showing
11 changed files
with
461 additions
and
6 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
__fixtures__/v-next/outputinstantrpc/akash-scoped-client.ts
This file contains 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,79 @@ | ||
import { GeneratedType, Registry, OfflineSigner } from "@cosmjs/proto-signing"; | ||
import { defaultRegistryTypes, AminoTypes, SigningStargateClient } from "@cosmjs/stargate"; | ||
import { HttpEndpoint } from "@cosmjs/tendermint-rpc"; | ||
import { createRpcClient } from "./extern"; | ||
import { DeliverTxResponse, EncodeObject, StdFee, TxRpc, SigningClientParams } from "./types"; | ||
import * as akashAuditV1beta1AuditRegistry from "./akash/audit/v1beta1/audit.registry"; | ||
import * as akashAuditV1beta2AuditRegistry from "./akash/audit/v1beta2/audit.registry"; | ||
import * as akashCertV1beta2CertRegistry from "./akash/cert/v1beta2/cert.registry"; | ||
import * as akashDeploymentV1beta2ServiceRegistry from "./akash/deployment/v1beta2/service.registry"; | ||
import * as akashMarketV1beta2ServiceRegistry from "./akash/market/v1beta2/service.registry"; | ||
import * as akashProviderV1beta2ProviderRegistry from "./akash/provider/v1beta2/provider.registry"; | ||
import * as akashAuditV1beta1AuditAmino from "./akash/audit/v1beta1/audit.amino"; | ||
import * as akashAuditV1beta2AuditAmino from "./akash/audit/v1beta2/audit.amino"; | ||
import * as akashCertV1beta2CertAmino from "./akash/cert/v1beta2/cert.amino"; | ||
import * as akashDeploymentV1beta2ServiceAmino from "./akash/deployment/v1beta2/service.amino"; | ||
import * as akashMarketV1beta2ServiceAmino from "./akash/market/v1beta2/service.amino"; | ||
import * as akashProviderV1beta2ProviderAmino from "./akash/provider/v1beta2/provider.amino"; | ||
|
||
export const akashAminoConverters = { | ||
...akashAuditV1beta1AuditAmino.AminoConverter, | ||
...akashAuditV1beta2AuditAmino.AminoConverter, | ||
...akashCertV1beta2CertAmino.AminoConverter, | ||
...akashDeploymentV1beta2ServiceAmino.AminoConverter, | ||
...akashMarketV1beta2ServiceAmino.AminoConverter, | ||
...akashProviderV1beta2ProviderAmino.AminoConverter, | ||
}; | ||
export const akashProtoRegistry: ReadonlyArray<[string, GeneratedType]> = [...akashAuditV1beta1AuditRegistry.registry, ...akashAuditV1beta2AuditRegistry.registry, ...akashCertV1beta2CertRegistry.registry, ...akashDeploymentV1beta2ServiceRegistry.registry, ...akashMarketV1beta2ServiceRegistry.registry, ...akashProviderV1beta2ProviderRegistry.registry]; | ||
export const getAkashSigningClientOptions = ({ | ||
defaultTypes = defaultRegistryTypes | ||
}: { | ||
defaultTypes?: ReadonlyArray<[string, GeneratedType]>; | ||
} = {}): { | ||
registry: Registry; | ||
aminoTypes: AminoTypes; | ||
} => { | ||
const registry = new Registry([...defaultTypes, ...akashProtoRegistry]); | ||
const aminoTypes = new AminoTypes({ | ||
...akashAminoConverters | ||
}); | ||
return { | ||
registry, | ||
aminoTypes | ||
}; | ||
}; | ||
export const getAkashSigningClient = async ({ | ||
rpcEndpoint, | ||
signer, | ||
defaultTypes = defaultRegistryTypes | ||
}: { | ||
rpcEndpoint: string | HttpEndpoint; | ||
signer: OfflineSigner; | ||
defaultTypes?: ReadonlyArray<[string, GeneratedType]>; | ||
}) => { | ||
const { | ||
registry, | ||
aminoTypes | ||
} = getAkashSigningClientOptions({ | ||
defaultTypes | ||
}); | ||
const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, signer, { | ||
registry: (registry as any), | ||
aminoTypes | ||
}); | ||
return client; | ||
}; | ||
export const getAkashSigningTxRpc = async ({ | ||
rpcEndpoint, | ||
signer | ||
}: SigningClientParams) => { | ||
let txRpc = (await createRpcClient(rpcEndpoint) as TxRpc); | ||
const signingClient = await getAkashSigningClient({ | ||
rpcEndpoint, | ||
signer | ||
}); | ||
txRpc.signAndBroadcast = (signerAddress: string, messages: EncodeObject[], fee: number | StdFee | "auto", memo?: string) => { | ||
return (signingClient.signAndBroadcast(signerAddress, messages, fee, memo) as Promise<DeliverTxResponse>); | ||
}; | ||
return txRpc; | ||
}; |
This file contains 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,85 @@ | ||
import { GeneratedType, Registry, OfflineSigner } from "@cosmjs/proto-signing"; | ||
import { defaultRegistryTypes, AminoTypes, SigningStargateClient } from "@cosmjs/stargate"; | ||
import { HttpEndpoint } from "@cosmjs/tendermint-rpc"; | ||
import { createRpcClient } from "./extern"; | ||
import { DeliverTxResponse, EncodeObject, StdFee, TxRpc, SigningClientParams } from "./types"; | ||
import * as akashAuditV1beta1AuditRegistry from "./akash/audit/v1beta1/audit.registry"; | ||
import * as akashAuditV1beta2AuditRegistry from "./akash/audit/v1beta2/audit.registry"; | ||
import * as akashCertV1beta2CertRegistry from "./akash/cert/v1beta2/cert.registry"; | ||
import * as akashDeploymentV1beta2ServiceRegistry from "./akash/deployment/v1beta2/service.registry"; | ||
import * as akashMarketV1beta2ServiceRegistry from "./akash/market/v1beta2/service.registry"; | ||
import * as akashProviderV1beta2ProviderRegistry from "./akash/provider/v1beta2/provider.registry"; | ||
import * as cosmosGroupV1TxRegistry from "./cosmos/group/v1/tx.registry"; | ||
import * as cosmosNftV1beta1TxRegistry from "./cosmos/nft/v1beta1/tx.registry"; | ||
import * as akashAuditV1beta1AuditAmino from "./akash/audit/v1beta1/audit.amino"; | ||
import * as akashAuditV1beta2AuditAmino from "./akash/audit/v1beta2/audit.amino"; | ||
import * as akashCertV1beta2CertAmino from "./akash/cert/v1beta2/cert.amino"; | ||
import * as akashDeploymentV1beta2ServiceAmino from "./akash/deployment/v1beta2/service.amino"; | ||
import * as akashMarketV1beta2ServiceAmino from "./akash/market/v1beta2/service.amino"; | ||
import * as akashProviderV1beta2ProviderAmino from "./akash/provider/v1beta2/provider.amino"; | ||
import * as cosmosGroupV1TxAmino from "./cosmos/group/v1/tx.amino"; | ||
import * as cosmosNftV1beta1TxAmino from "./cosmos/nft/v1beta1/tx.amino"; | ||
export const unifiedAminoConverters = { | ||
...akashAuditV1beta1AuditAmino.AminoConverter, | ||
...akashAuditV1beta2AuditAmino.AminoConverter, | ||
...akashCertV1beta2CertAmino.AminoConverter, | ||
...akashDeploymentV1beta2ServiceAmino.AminoConverter, | ||
...akashMarketV1beta2ServiceAmino.AminoConverter, | ||
...akashProviderV1beta2ProviderAmino.AminoConverter, | ||
...cosmosGroupV1TxAmino.AminoConverter, | ||
...cosmosNftV1beta1TxAmino.AminoConverter, | ||
... | ||
}; | ||
export const unifiedProtoRegistry: ReadonlyArray<[string, GeneratedType]> = [...akashAuditV1beta1AuditRegistry.registry, ...akashAuditV1beta2AuditRegistry.registry, ...akashCertV1beta2CertRegistry.registry, ...akashDeploymentV1beta2ServiceRegistry.registry, ...akashMarketV1beta2ServiceRegistry.registry, ...akashProviderV1beta2ProviderRegistry.registry, ...cosmosGroupV1TxRegistry.registry, ...cosmosNftV1beta1TxRegistry.registry, ...]; | ||
export const getUnifiedSigningClientOptions = ({ | ||
defaultTypes = defaultRegistryTypes | ||
}: { | ||
defaultTypes?: ReadonlyArray<[string, GeneratedType]>; | ||
} = {}): { | ||
registry: Registry; | ||
aminoTypes: AminoTypes; | ||
} => { | ||
const registry = new Registry([...defaultTypes, ...unifiedProtoRegistry]); | ||
const aminoTypes = new AminoTypes({ | ||
...unifiedAminoConverters | ||
}); | ||
return { | ||
registry, | ||
aminoTypes | ||
}; | ||
}; | ||
export const getUnifiedSigningClient = async ({ | ||
rpcEndpoint, | ||
signer, | ||
defaultTypes = defaultRegistryTypes | ||
}: { | ||
rpcEndpoint: string | HttpEndpoint; | ||
signer: OfflineSigner; | ||
defaultTypes?: ReadonlyArray<[string, GeneratedType]>; | ||
}) => { | ||
const { | ||
registry, | ||
aminoTypes | ||
} = getUnifiedSigningClientOptions({ | ||
defaultTypes | ||
}); | ||
const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, signer, { | ||
registry: (registry as any), | ||
aminoTypes | ||
}); | ||
return client; | ||
}; | ||
export const getUnifiedSigningTxRpc = async ({ | ||
rpcEndpoint, | ||
signer | ||
}: SigningClientParams) => { | ||
let txRpc = (await createRpcClient(rpcEndpoint) as TxRpc); | ||
const signingClient = await getUnifiedSigningClient({ | ||
rpcEndpoint, | ||
signer | ||
}); | ||
txRpc.signAndBroadcast = (signerAddress: string, messages: EncodeObject[], fee: number | StdFee | "auto", memo?: string) => { | ||
return (signingClient.signAndBroadcast(signerAddress, messages, fee, memo) as Promise<DeliverTxResponse>); | ||
}; | ||
return txRpc; | ||
}; |
This file contains 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 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 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
128 changes: 128 additions & 0 deletions
128
packages/telescope/src/generators/create-scoped-stargate-clients.ts
This file contains 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,128 @@ | ||
import { Bundler } from '../bundler'; | ||
import { TelescopeBuilder } from '../builder'; | ||
import { join, dirname, relative } from 'path'; | ||
import { | ||
importNamespace, | ||
GenericParseContext, | ||
createStargateClient, | ||
createStargateClientOptions, | ||
createStargateClientProtoRegistry, | ||
createStargateClientAminoRegistry, | ||
createGetTxRpc | ||
} from '@cosmology/ast'; | ||
import { ProtoRef } from '@cosmology/types'; | ||
import { camel, pascal } from 'case'; | ||
import { duplicateImportPathsWithExt, variableSlug, toPosixPath, isPackageIncluded } from '@cosmology/utils'; | ||
import { buildAllImportsFromGenericContext } from '../imports'; | ||
import { writeAstToFile } from '../utils/files'; | ||
import { BundlerFile } from '../types'; | ||
import minimatch from "minimatch"; | ||
|
||
export const plugin = ( | ||
builder: TelescopeBuilder, | ||
allRegistries: BundlerFile[], | ||
allConverters: BundlerFile[] | ||
) => { | ||
builder.options.rpcClients.combinedClient.map((currentClient) => { | ||
|
||
if (!allRegistries || !allRegistries.length) { | ||
return; | ||
} | ||
|
||
const registryImports = []; | ||
const converterImports = []; | ||
|
||
const clientFile = currentClient.fileName | ||
const ctxRef: ProtoRef = { | ||
absolute: '/', | ||
filename: '/', | ||
proto: { | ||
imports: [], | ||
package: currentClient.name, | ||
root: {}, | ||
} | ||
}; | ||
const ctx = new GenericParseContext(ctxRef, null, builder.options); | ||
|
||
const registryVariables = []; | ||
const converterVariables = []; | ||
const filteredRegistries = allRegistries.filter(item => | ||
isPackageIncluded(item.package, currentClient.include.patterns) | ||
); | ||
filteredRegistries.forEach(registry => { | ||
let rel = relative(dirname(clientFile), registry.localname); | ||
if (!rel.startsWith('.')) rel = `./${rel}`; | ||
rel = toPosixPath(rel) | ||
const variable = variableSlug(registry.localname); | ||
registryVariables.push(variable); | ||
registryImports.push(importNamespace(variable, rel)); | ||
}); | ||
|
||
const filteredConverters = allConverters.filter(item => | ||
isPackageIncluded(item.package, currentClient.include.patterns) | ||
); | ||
filteredConverters.forEach(converter => { | ||
let rel = relative(dirname(clientFile), converter.localname); | ||
if (!rel.startsWith('.')) rel = `./${rel}`; | ||
rel = toPosixPath(rel) | ||
const variable = variableSlug(converter.localname); | ||
converterVariables.push(variable); | ||
converterImports.push(importNamespace(variable, rel)); | ||
}); | ||
const name = 'get' + pascal(currentClient.name + 'SigningClient'); | ||
const txRpcName = 'get' + pascal(currentClient.name + 'SigningTxRpc'); | ||
const prefix = camel(currentClient.name); | ||
const aminos = createStargateClientAminoRegistry({ | ||
context: ctx, | ||
aminos: converterVariables, | ||
aminoConverters: prefix + 'AminoConverters' | ||
}); | ||
const protos = createStargateClientProtoRegistry({ | ||
context: ctx, | ||
registries: registryVariables, | ||
protoTypeRegistry: prefix + 'ProtoRegistry' | ||
}); | ||
const clientOptions = createStargateClientOptions({ | ||
context: ctx, | ||
name: name + 'Options', | ||
protoTypeRegistry: prefix + 'ProtoRegistry', | ||
aminoConverters: prefix + 'AminoConverters' | ||
}); | ||
const clientBody = createStargateClient({ | ||
context: ctx, | ||
name, | ||
options: name + 'Options', | ||
}); | ||
|
||
let getTxRpc; | ||
|
||
if (ctx.pluginValue("stargateClients.addGetTxRpc")) { | ||
getTxRpc = createGetTxRpc(ctx, txRpcName, name); | ||
} | ||
|
||
const imports = buildAllImportsFromGenericContext(ctx, clientFile); | ||
|
||
let importDecls = [...imports, ...registryImports, ...converterImports]; | ||
|
||
importDecls = duplicateImportPathsWithExt(importDecls, builder.options.restoreImportExtension); | ||
|
||
let cProg = importDecls | ||
.concat(aminos) | ||
.concat(protos) | ||
.concat(clientOptions) | ||
.concat(clientBody); | ||
|
||
// replace all backslash path for windows | ||
for (let i = 0; i < cProg.length; i++) { | ||
if (cProg[i].source?.value) { | ||
cProg[i].source.value = toPosixPath(cProg[i].source?.value) | ||
} | ||
} | ||
|
||
if (getTxRpc) { | ||
cProg = cProg.concat(getTxRpc); | ||
} | ||
const clientOutFile = join(builder.outPath, clientFile); | ||
writeAstToFile(builder.outPath, builder.options, cProg, clientOutFile); | ||
}) | ||
}; |
Oops, something went wrong.