Skip to content
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

Wrong capitalization in parameter when doing RPC call #5529

Closed
3 of 10 tasks
onnovisser opened this issue Mar 9, 2023 · 3 comments
Closed
3 of 10 tasks

Wrong capitalization in parameter when doing RPC call #5529

onnovisser opened this issue Mar 9, 2023 · 3 comments
Labels

Comments

@onnovisser
Copy link

  • I'm submitting a ...
  • Bug report
  • Feature request
  • Support request
  • Other
  • What is the current behavior and expected behavior?

I'm trying to do an RPC call, but it errors because there's a change in capitalization of one of the parameters when the call gets submitted.

The types:

const types = {
  PoolId: "u64",
  TrancheId: "[u8; 16]",
  RewardDomain: {
    _enum: ["Block", "Liquidity"]
  },
  CurrencyId: {
    _enum: {
      Native: "Native",
      Tranche: "(PoolId, TrancheId)",
      KSM: "KSM",
      AUSD: "AUSD",
      ForeignAsset: "u32"
    }
  }
};

const rpc = {
  rewards: {
    computeReward: {
      description: "Compute reward",
      params: [
        {
          name: "currency_id",
          type: "(RewardDomain,CurrencyId)"
        },
        {
          name: "account_id",
          type: "AccountId"
        }
      ],
      type: "u128"
    }
  }
};

The call

api.rpc.rewards.computeReward(
  [
    "Liquidity",
    { Tranche: ["872535177", "0x58c4a52c1f0d239f1ff53f7f13875b3d"] }
  ],
  "4fZ56cX6at2SCLe8NbC6o2Q13KmCCkxDTSB5Hq6woekqZDf5"
);

The rpc call that gets made

{
  "id": 8,
  "jsonrpc": "2.0",
  "method": "rewards_computeReward",
  "params": [
    ["Liquidity", { "tranche": [872535177, "0x58c4a52c1f0d239f1ff53f7f13875b3d"] }],
    "kAMN6eYXWjYWzvsRYkVw8aJuW5ADjA3Hd2zgL6vbmM72U1Jcu"
  ]
}

The error

2023-03-09 12:03:12        RPC-CORE: computeReward(currency_id: (RewardDomain,CurrencyId), account_id: AccountId): u128:: -32602: unknown variant `tranche`, expected one of `Native`, `Tranche`, `KSM`, `AUSD`, `ForeignAsset` at line 1 column 23

Tranche unexpectedly changes to tranche.

A sandbox reproducing the error: https://codesandbox.io/s/polkadotjs-rpc-xjjdhl?file=/src/index.ts

  • What is the motivation for changing the behavior?

I would expect the capitalization to remain unchanged.

  • Please tell us about your environment:
  • Version: 10.0.1

  • Environment:

    • Node.js
    • Browser
    • Other (limited support for other environments)
  • Language:

    • JavaScript
    • TypeScript (include tsc --version)
    • Other
@jacogr
Copy link
Member

jacogr commented Mar 12, 2023

Where is the Rust definition for these enums? The reason for asking, as per the substrate standard, all enums are encoded (when they have values) in camelCase via a serde attribute, see for instance

https://github.com/paritytech/substrate/blob/310fe4095bff177ddcfa660ac0ab06171a25a9c2/frame/support/src/dispatch.rs#L138

or

https://github.com/paritytech/substrate/blob/310fe4095bff177ddcfa660ac0ab06171a25a9c2/client/transaction-pool/api/src/lib.rs#L106

(picking the first enums via JSONRPC that my search picked up)

TL:DR It seems you are missing the #[serde(rename_all = "camelCase")] attribute in your type - sadly this is just a standard that needs to be followed, since encoding enums different would make the world break (aka it is applied everywhere in Substrate, so the base JS API encoding for any enums follows that de-facto standard)

PS: You are actually better off not doing RPCs and doing all via decl_runtime - lots of reasons, but basically the former cannot be supported on light clients and the former will never appear in the metadata, so it will always be manual (for the latter, both are on the radar for Substrate) and they are actually versioned, unlike RPCs where changing params is a nightmare.

@jacogr jacogr added the support label Mar 12, 2023
@onnovisser
Copy link
Author

It appears that the enums indeed don't have that attribute (here for example: https://github.com/centrifuge/centrifuge-chain/blob/0f32f7b4967c73cc38914c86c2f742f28f3da789/libs/types/src/tokens.rs#L27)
I found out that we were also defining runtime APIs besides the RPC methods, so I'll be using those instead.
Thanks and I'll close this.

@polkadot-js-bot
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators Apr 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants