CosmWasm / ts-codegen

Convert your CosmWasm smart contracts into dev-friendly TypeScript classes so you can focus on shipping code.
https://cosmology.zone/products/ts-codegen
Apache License 2.0
116 stars 29 forks source link

Mutation property type error in generated react query file #80

Closed dadamu closed 2 years ago

dadamu commented 2 years ago

The generated react query from cw721-base has the error in useCw721BaseMintMutation function from the Cw721BaseMintMutation with wrong type msg property. The msg in Cw721BaseMintMutation is using generated msg whose properties are in snake-case, it mismatches the client mint function to use the object with camel-case naming:

Here is an example:

export interface Cw721BaseMintMutation {
  client: Cw721BaseClient;
  msg: MintMsgForNullable_Empty;
  args?: {
    fee?: number | StdFee | "auto";
    memo?: string;
    funds?: Coin[];
  };
}
export function useCw721BaseMintMutation(options?: Omit<UseMutationOptions<ExecuteResult, Error, Cw721BaseMintMutation>, "mutationFn">) {
  return useMutation<ExecuteResult, Error, Cw721BaseMintMutation>(({
    client,
    msg,
    args: {
      fee,
      memo,
      funds
    } = {}
  }) => client.mint(msg, fee, memo, funds), options);

The structure of msg are snake case naming:

export interface MintMsgForNullable_Empty {
  extension?: Empty | null;
  owner: string;
  token_id: string;
  token_uri?: string | null;
}

The client mint required args are camel case naming:

mint = async ({
    extension,
    owner,
    tokenId,
    tokenUri
  }: {
    extension?: Empty;
    owner: string;
    tokenId: string;
    tokenUri?: string;
  }, fee: number | StdFee | "auto" = "auto", memo?: string, funds?: Coin[]): Promise<ExecuteResult> => {
    return await this.client.execute(this.sender, this.contractAddress, {
      mint: {
        extension,
        owner,
        token_id: tokenId,
        token_uri: tokenUri
      }
    }, fee, memo, funds);
  };
pyramation commented 2 years ago

so the client is ok, it's a mismatch between the react-query interface and implementation?

@adairrr have you experienced this? I think it could be a relatively simple fix. We may need to duplicate the interface if we're reusing it, and make a snake case version.

also, could be a use-case for https://github.com/sindresorhus/type-fest and we may not even need to duplicate the interface if I understand it correctly.

dadamu commented 2 years ago

@pyramation yes, client is ok. The error happens in react-query files.

adairrr commented 2 years ago

@dadamu can you send me the json schemas from which you're generating the code?

dadamu commented 2 years ago

@adairrr sure, here it is https://github.com/CosmWasm/cw-nfts/tree/main/contracts/cw721-base/schema

adairrr commented 2 years ago

I was able to repro the issue - should be an easy fix. It's interesting to me that the msg uses the actual type instead of expanding it like everywhere else.

The schema references the type that's used:

    {
      "description": "Mint a new NFT, can only be called by the contract minter",
      "type": "object",
      "required": [
        "mint"
      ],
      "properties": {
        "mint": {
          "$ref": "#/definitions/MintMsg_for_Nullable_Empty"
        }
      },
      "additionalProperties": false
    },
adairrr commented 2 years ago

At the revoke mutation, it uses the pascal case as well:

export interface Test721RevokeMutation {
  client: Test721Client;
  msg: {
    spender: string;
    tokenId: string;
  };
  args?: {...};
}

Ah, but there's no ref here.

    {
      "description": "Remove previously granted Approval",
      "type": "object",
      "required": [
        "revoke"
      ],
      "properties": {
        "revoke": {
          "type": "object",
          "required": [
            "spender",
            "token_id"
          ],
          "properties": {
            "spender": {
              "type": "string"
            },
            "token_id": {
              "type": "string"
            }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    }, 
pyramation commented 2 years ago

@dadamu try this

Successfully published:
 - @cosmwasm/ts-codegen@0.23.0
dadamu commented 2 years ago

@pyramation Thanks for the help! However, after this changes, non-cw721 schema codegen will be error:

  var msgType = (0, _utils.createTypedObjectParams)(context, jsonschema).typeAnnotation;
                                                                        ^
TypeError: Cannot read properties of undefined (reading 'typeAnnotation')
    at createReactQueryMutationArgsInterface (~/node_modules/wasm-ast-types/main/react-query/react-query.js:209:73)
    at ~/node_modules/wasm-ast-types/main/react-query/react-query.js:250:13
    at Array.reduce (<anonymous>)
...

The whole schemas I used are here, both cw721-schemas and schemas.

In addition, StdFee and Coin are not imported properly after the last changes, I guess it is because the context.addUtil are removed here.

adairrr commented 2 years ago

Oof @dadamu that was totally my fault, apologies. Opened a new PR with the fixed changes. https://github.com/CosmWasm/ts-codegen/pull/86