Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ codegen({
},
messageComposer: {
enabled: false
},
msgBuilder: {
enabled: false
}
}
}).then(() => {
Expand Down Expand Up @@ -252,6 +255,28 @@ cosmwasm-ts-codegen generate \
| ------------------------------ | ------------------------------------------------------------------- |
| `messageComposer.enabled` | enable the messageComposer plugin |

### Msg Builder

Generate raw message jsons for use in your application with the `msg-builder` command.

[see example output code](https://gist.github.com/adairrr/b394e62beb9856b0351883f776650f26)

#### Msg Builder via CLI

```sh
cosmwasm-ts-codegen generate \
--plugin msg-builder \
--schema ./schema \
--out ./ts \
--name MyContractName
```
#### Message Composer Options

| option | description |
-------------| ------------------------------ | ------------------------------------------------------------------- |
| `msgBuilder.enabled` | enable the msgBilder plugin |


### Bundles

The bundler will make a nice package of all your contracts. For example:
Expand Down Expand Up @@ -389,6 +414,10 @@ https://gist.github.com/pyramation/a9520ccf131177b1841e02a97d7d3731

https://gist.github.com/pyramation/43320e8b952751a0bd5a77dbc5b601f4

- `cosmwasm-ts-codegen generate --plugin msg-builder`

https://gist.github.com/adairrr/b394e62beb9856b0351883f776650f26


### JSON Schema

Expand Down
154 changes: 154 additions & 0 deletions __fixtures__/basic/ownership.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ExecuteMsg",
"description": "Ownership Execute msg",
"oneOf": [
{
"description": "Sets a new Factory",
"type": "object",
"required": [
"set_factory"
],
"properties": {
"set_factory": {
"type": "object",
"required": [
"new_factory"
],
"properties": {
"new_factory": {
"type": "string"
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"description": "Update the contract's ownership. The `action` to be provided can be either to propose transferring ownership to an account, accept a pending ownership transfer, or renounce the ownership permanently.",
"type": "object",
"required": [
"update_ownership"
],
"properties": {
"update_ownership": {
"$ref": "#/definitions/Action"
}
},
"additionalProperties": false
}
],
"definitions": {
"Action": {
"description": "Actions that can be taken to alter the contract's ownership",
"oneOf": [
{
"description": "Propose to transfer the contract's ownership to another account, optionally with an expiry time.\n\nCan only be called by the contract's current owner.\n\nAny existing pending ownership transfer is overwritten.",
"type": "object",
"required": [
"transfer_ownership"
],
"properties": {
"transfer_ownership": {
"type": "object",
"required": [
"new_owner"
],
"properties": {
"expiry": {
"anyOf": [
{
"$ref": "#/definitions/Expiration"
},
{
"type": "null"
}
]
},
"new_owner": {
"type": "string"
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"description": "Accept the pending ownership transfer.\n\nCan only be called by the pending owner.",
"type": "string",
"enum": [
"accept_ownership"
]
},
{
"description": "Give up the contract's ownership and the possibility of appointing a new owner.\n\nCan only be invoked by the contract's current owner.\n\nAny existing pending ownership transfer is canceled.",
"type": "string",
"enum": [
"renounce_ownership"
]
}
]
},
"Expiration": {
"description": "Expiration represents a point in time when some event happens. It can compare with a BlockInfo and will return is_expired() == true once the condition is hit (and for every block in the future)",
"oneOf": [
{
"description": "AtHeight will expire when `env.block.height` >= height",
"type": "object",
"required": [
"at_height"
],
"properties": {
"at_height": {
"type": "integer",
"format": "uint64",
"minimum": 0.0
}
},
"additionalProperties": false
},
{
"description": "AtTime will expire when `env.block.time` >= time",
"type": "object",
"required": [
"at_time"
],
"properties": {
"at_time": {
"$ref": "#/definitions/Timestamp"
}
},
"additionalProperties": false
},
{
"description": "Never will never expire. Used to express the empty variant",
"type": "object",
"required": [
"never"
],
"properties": {
"never": {
"type": "object",
"additionalProperties": false
}
},
"additionalProperties": false
}
]
},
"Timestamp": {
"description": "A point in time in nanosecond precision.\n\nThis type can represent times from 1970-01-01T00:00:00Z to 2554-07-21T23:34:33Z.\n\n## Examples\n\n``` # use cosmwasm_std::Timestamp; let ts = Timestamp::from_nanos(1_000_000_202); assert_eq!(ts.nanos(), 1_000_000_202); assert_eq!(ts.seconds(), 1); assert_eq!(ts.subsec_nanos(), 202);\n\nlet ts = ts.plus_seconds(2); assert_eq!(ts.nanos(), 3_000_000_202); assert_eq!(ts.seconds(), 3); assert_eq!(ts.subsec_nanos(), 202); ```",
"allOf": [
{
"$ref": "#/definitions/Uint64"
}
]
},
"Uint64": {
"description": "A thin wrapper around u64 that is using strings for JSON encoding/decoding, such that the full u64 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u64` to get the value out:\n\n``` # use cosmwasm_std::Uint64; let a = Uint64::from(42u64); assert_eq!(a.u64(), 42);\n\nlet b = Uint64::from(70u32); assert_eq!(b.u64(), 70); ```",
"type": "string"
}
}
}
26 changes: 26 additions & 0 deletions packages/ts-codegen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The quickest and easiest way to interact with CosmWasm Contracts. `@cosmwasm/ts-
- [React Query](#react-query)
- [Recoil](#recoil)
- [Message Composer](#message-composer)
- [Msg Builder](#msg-builder)
- [Bundles](#bundles)
- [CLI Usage and Examples](#cli-usage-and-examples)
- [Advanced Usage](#advanced-usage)
Expand Down Expand Up @@ -119,6 +120,9 @@ codegen({
},
messageComposer: {
enabled: false
},
msgBuilder: {
enabled: false
}
}
}).then(() => {
Expand Down Expand Up @@ -252,6 +256,28 @@ cosmwasm-ts-codegen generate \
| ------------------------------ | ------------------------------------------------------------------- |
| `messageComposer.enabled` | enable the messageComposer plugin |

### Msg Builder

Generate raw message jsons for use in your application with the `msg-builder` command.

[see example output code](https://gist.github.com/adairrr/b394e62beb9856b0351883f776650f26)

#### Msg Builder via CLI

```sh
cosmwasm-ts-codegen generate \
--plugin msg-builder \
--schema ./schema \
--out ./ts \
--name MyContractName
```
#### Message Composer Options

| option | description |
-------------| ------------------------------ | ------------------------------------------------------------------- |
| `msgBuilder.enabled` | enable the msgBilder plugin |


### Bundles

The bundler will make a nice package of all your contracts. For example:
Expand Down
53 changes: 29 additions & 24 deletions packages/wasm-ast-types/src/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import * as t from '@babel/types';
import { camel, pascal } from 'case';
import { camel } from 'case';
import {
arrowFunctionExpression,
bindMethod,
typedIdentifier,
promiseTypeAnnotation,
classDeclaration,
classProperty,
arrowFunctionExpression,
getMessageProperties
} from '../utils'
getMessageProperties,
promiseTypeAnnotation,
typedIdentifier
} from '../utils';

import {
QueryMsg,
ExecuteMsg
} from '../types';
import { ExecuteMsg, JSONSchema, QueryMsg } from '../types';

import { getPropertyType, getType, createTypedObjectParams, getResponseType } from '../utils/types';
import { createTypedObjectParams, getPropertyType, getResponseType, getType } from '../utils/types';
import { RenderContext } from '../context';
import { JSONSchema } from '../types';
import { identifier, propertySignature } from '../utils/babel';

export const CONSTANT_EXEC_PARAMS = [
Expand Down Expand Up @@ -89,7 +85,7 @@ export const createWasmQueryMethod = (
const methodName = camel(underscoreName);
const responseType = getResponseType(context, underscoreName);

const obj = createTypedObjectParams(
const param = createTypedObjectParams(
context,
jsonschema.properties[underscoreName]
);
Expand All @@ -99,13 +95,14 @@ export const createWasmQueryMethod = (
jsonschema.properties[underscoreName]
);

const actionArg =
t.objectProperty(t.identifier(underscoreName), t.objectExpression(args));
const msgAction = t.identifier(underscoreName);
// If the param is an identifier, we can just use it as is
const msgActionValue = param?.type === 'Identifier' ? t.identifier(param.name) : t.objectExpression(args)

return t.classProperty(
t.identifier(methodName),
arrowFunctionExpression(
obj ? [obj] : [],
param ? [param] : [],
t.blockStatement(
[
t.returnStatement(
Expand All @@ -120,7 +117,7 @@ export const createWasmQueryMethod = (
[
t.memberExpression(t.thisExpression(), t.identifier('contractAddress')),
t.objectExpression([
actionArg
t.objectProperty(msgAction, msgActionValue)
])
]
)
Expand Down Expand Up @@ -237,9 +234,15 @@ export const getWasmMethodArgs = (
// only 1 degree $ref-lookup
if (!keys.length && jsonschema.$ref) {
const obj = context.refLookup(jsonschema.$ref);
// properties
if (obj) {
keys = Object.keys(obj.properties ?? {})
}

// tuple struct or otherwise, use the name of the reference
if (!keys.length && obj?.oneOf) {
// TODO????? ADAIR
}
}

const args = keys.map(prop => {
Expand All @@ -265,7 +268,7 @@ export const createWasmExecMethod = (

const underscoreName = Object.keys(jsonschema.properties)[0];
const methodName = camel(underscoreName);
const obj = createTypedObjectParams(
const param = createTypedObjectParams(
context,
jsonschema.properties[underscoreName]
);
Expand All @@ -274,12 +277,16 @@ export const createWasmExecMethod = (
jsonschema.properties[underscoreName]
);

const msgAction = t.identifier(underscoreName);
// If the param is an identifier, we can just use it as is
const msgActionValue = param?.type === 'Identifier' ? t.identifier(param.name) : t.objectExpression(args)

return t.classProperty(
t.identifier(methodName),
arrowFunctionExpression(
obj ? [
param ? [
// props
obj,
param,
...CONSTANT_EXEC_PARAMS
] : CONSTANT_EXEC_PARAMS,
t.blockStatement(
Expand All @@ -306,10 +313,8 @@ export const createWasmExecMethod = (
t.objectExpression(
[
t.objectProperty(
t.identifier(underscoreName),
t.objectExpression([
...args
])
msgAction,
msgActionValue
)

]
Expand Down
Loading