Generate TypeScript SDKs for your CosmWasm smart contracts
npm install -g @cosmwasm/ts-codegen
The quickest and easiest way to interact with CosmWasm Contracts. @cosmwasm/ts-codegen
converts your CosmWasm smart contracts into dev-friendly TypeScript classes so you can focus on shipping code.
🎥 Checkout our video playlist to learn how to use ts-codegen
!
Clone your project and cd
into your contracts folder
git clone https://github.com/public-awesome/launchpad.git
cd launchpad/contracts/sg721-base/
Run cosmwasm-ts-codegen
to generate your code.
cosmwasm-ts-codegen generate \
--plugin client \
--schema ./schema \
--out ./ts \
--name SG721 \
--no-bundle
The output will be in the folder specified by --out
, enjoy!
You can get started quickly using our cli
by globally installing via npm:
npm install -g @cosmwasm/ts-codegen
For production usage, we recommend setting up a build script that uses the main entry point:
import codegen from '@cosmwasm/ts-codegen';
codegen({
contracts: [
{
name: 'SG721',
dir: './path/to/sg721/schema'
},
{
name: 'Minter',
dir: './path/to/Minter/schema'
}
],
outPath: './path/to/code/src/',
// options are completely optional ;)
options: {
bundle: {
bundleFile: 'index.ts',
scope: 'contracts'
},
types: {
enabled: true
},
client: {
enabled: true
},
reactQuery: {
enabled: true,
optionalClient: true,
version: 'v4',
mutations: true,
queryKeys: true,
queryFactory: true,
},
recoil: {
enabled: false
},
messageComposer: {
enabled: false
},
msgBuilder: {
enabled: false
}
}
}).then(() => {
console.log('✨ all done!');
});
Typescript types and interfaces are generated in separate files so they can be imported into various generated plugins.
option | description |
---|---|
types.enabled |
enable type generation |
types.aliasExecuteMsg |
generate a type alias based on the contract name |
The client
plugin will generate TS client classes for your contracts. This option generates a QueryClient
for queries as well as a Client
for queries and mutations.
option | description |
---|---|
client.enabled |
generate TS client classes for your contracts |
client.execExtendsQuery |
execute should extend query message clients |
client.noImplicitOverride |
should match your tsconfig noImplicitOverride option |
cosmwasm-ts-codegen generate \
--plugin client
--schema ./schema \
--out ./ts \
--name MyContractName
Generate react-query v3 or react-query v4 bindings for your contracts with the react-query
command.
option | description |
---|---|
reactQuery.enabled |
enable the react-query plugin |
reactQuery.optionalClient |
allows contract client to be undefined as the component renders |
reactQuery.queryKeys |
generates a const queryKeys object for use with invalidations and set values |
reactQuery.queryFactory |
generates a const queryFactory object for useQueries and prefetchQueries use |
reactQuery.version |
v4 uses @tanstack/react-query and v3 uses react-query |
reactQuery.mutations |
also generate mutations |
reactQuery.camelize |
use camelCase style for property names |
Here is an example without optional client, using v3 for react-query
, without mutations:
cosmwasm-ts-codegen generate \
--plugin client \
--plugin react-query \
--schema ./schema \
--out ./ts \
--name MyContractName \
--version v3 \
--no-optionalClient \
--no-mutations
Example with optional client, using v4, with mutations:
cosmwasm-ts-codegen generate \
--plugin react-query \
--schema ./schema \
--out ./ts \
--name MyContractName \
--optionalClient \
--version v4 \
--mutations
Generate recoil bindings for your contracts with the recoil
command.
cosmwasm-ts-codegen generate \
--plugin recoil \
--schema ./schema \
--out ./ts \
--name MyContractName
option | description |
---|---|
recoil.enabled |
enable the recoil plugin |
Generate pure message objects with the proper utf8
encoding and typeUrl
configured that you can broadcast yourself via cosmjs
with the message-composer
command.
cosmwasm-ts-codegen generate \
--plugin message-composer \
--schema ./schema \
--out ./ts \
--name MyContractName
option | description |
---|---|
messageComposer.enabled |
enable the messageComposer plugin |
Generate raw message jsons for use in your application with the msg-builder
command.
cosmwasm-ts-codegen generate \
--plugin msg-builder \
--schema ./schema \
--out ./ts \
--name MyContractName
| option | description |
-------------| ------------------------------ | ------------------------------------------------------------------- |
| msgBuilder.enabled
| enable the msgBilder plugin |
The bundler will make a nice package of all your contracts. For example:
const {
MinterQueryClient,
useMinterConfigQuery
} = contracts.Minter;
const { CwAdminFactoryClient } = contracts.CwAdminFactory;
option | description |
---|---|
bundle.enabled |
enable the bundler plugin |
bundle.scope |
name of the scope, defaults to contracts (you can use . to make more scopes) |
bundle.bundleFile |
name of the bundle file |
The CLI is interactive, and if you don't specify an option, it will interactively prompt you.
cosmwasm-ts-codegen generate
? [plugin] which plugins? (Press <space> to select, <a> to toggle all, <i> to invert selection)
❯◯ client
◯ recoil
◯ react-query
◯ message-composer
In this example, you can press space bar to select a number of plugins you wish you enable.
Additionally, it will also show you the name of the field (in this case plugin
) so you can specify the parameter (for example when using CI/CD) on the comand line. Here is an exampl with --plugin
set to client
via CLI:
cosmwasm-ts-codegen generate \
--plugin client
--schema ./schema \
--out ./ts \
--name MyContractName
You can specify multiple --plugin
options using the generate
command:
cosmwasm-ts-codegen generate \
--plugin client \
--plugin recoil \
--schema ./schema \
--out ./ts \
--name SG721
All options can be provided so you can bypass the prompt.
For confirm options, you can pass --no-<name>
to set the value to false. Here is an example without optional client, using v3 for react-query
, without mutations:
cosmwasm-ts-codegen generate \
--plugin client \
--plugin react-query \
--schema ./schema \
--out ./ts \
--name MyContractName \
--version v3 \
--no-optionalClient \
--no-mutations
Example with optional client, using v4, with mutations:
cosmwasm-ts-codegen generate \
--plugin react-query \
--schema ./schema \
--out ./ts \
--name MyContractName \
--optionalClient \
--version v4 \
--mutations
If needed, you can generate only the types with the typesOnly
option;
cosmwasm-ts-codegen generate \
--typesOnly \
--schema ./schema \
--out ./ts \
--name SG721
for lower-level access, you can import the various plugins directly:
import {
generateTypes,
generateClient,
generateReactQuery,
generateRecoil,
generateMessageComposer,
} from '@cosmwasm/ts-codegen';
cosmwasm-ts-codegen generate --typesOnly
https://gist.github.com/pyramation/107d4e8e30dc5eb3ffc07bc3000f4dd0
cosmwasm-ts-codegen generate --plugin client
https://gist.github.com/pyramation/30508678b7563e286f06ccc5ac384817
cosmwasm-ts-codegen generate --plugin react-query
https://gist.github.com/pyramation/70aef28fd3af0ee164f7711704d3dfc0
cosmwasm-ts-codegen generate --plugin recoil
https://gist.github.com/pyramation/a9520ccf131177b1841e02a97d7d3731
cosmwasm-ts-codegen generate --plugin message-composer
https://gist.github.com/pyramation/43320e8b952751a0bd5a77dbc5b601f4
cosmwasm-ts-codegen generate --plugin msg-builder
https://gist.github.com/adairrr/b394e62beb9856b0351883f776650f26
We generate code from the JSON Schema exported from CosmWasm smart contracts.
Currently you have to have the JSON Schema output. Here is an example to start.
First, get the Rust contracts and run cargo build
:
git clone [email protected]:public-awesome/stargaze-contracts.git
cd stargaze-contracts
cargo build
now build the schema with cargo schema
cd contracts/sg721/
cargo schema
Using the new write_api
method, you can export schemas:
use cosmwasm_schema::write_api;
use cw4_group::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
fn main() {
write_api! {
instantiate: InstantiateMsg,
execute: ExecuteMsg,
query: QueryMsg,
}
}
Here is a legacy example:
use cosmwasm_std::{Addr, CosmosMsg, Empty};
export_schema_with_title(&schema_for!(MinterData), &out_dir, "MinterResponse");
export_schema_with_title(&schema_for!(Addr), &out_dir, "StakingResponse");
export_schema_with_title(&schema_for!(Addr), &out_dir, "DaoResponse");
export_schema_with_title(
&schema_for!(CosmosMsg<Empty>),
&out_dir,
"CosmosMsg_for_Empty",
);
yarn
yarn bootstrap
yarn build
Then cd
into a package and run the tests
cd ./packages/wasm-ast-types
yarn test:watch
See the docs in the wasm-ast-types
package.
Checkout these related projects:
- @osmonauts/telescope a "babel for the Cosmos", Telescope is a TypeScript Transpiler for Cosmos Protobufs.
- chain-registry Cosmos chain registry and chain info.
- cosmos-kit A wallet connector for the Cosmos.
🛠 Built by Cosmology — if you like our tools, please consider delegating to our validator ⚛️