Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

passing dynamic data to codegened API methods #1186

Open
harrysolovay opened this issue Jul 14, 2023 · 0 comments
Open

passing dynamic data to codegened API methods #1186

harrysolovay opened this issue Jul 14, 2023 · 0 comments
Labels
api needs input We need input from the team and/or others request

Comments

@harrysolovay
Copy link
Contributor

harrysolovay commented Jul 14, 2023

Problem Statement

Developers will want to colocate static and dynamic usage of a given chain. Examples of both:

Static

chain.Balances

Dynamic

const palletName = await fetch("...").then((v) => v.text())

chain.pallet(palletName)

In the dynamic case, devs may run into issues when supplying the dynamic data to methods of the fluent API.

chain.pallet(palletName)
//           ~~~~~~~~~~
//           ^
//           `string` is not assignable to Chain.PalletName<Chain>

Possible Solutions

1. chain.untyped()

We could introduce a dynamic method to ChainRune. This would allow devs to opt out of the narrow, chain-specific constraints.

myChain.untyped().pallet(palletName) // no more type error

2. Generate Guards + Assertions

Alternatively, we could generate guards that do this narrowing.

import { chain, assertPallet, isStorage } from "@capi/chain"

palletName // `string`
assertPallet(palletName)
palletName // `Chain.PalletName<Chain>`

const pallet = chain.pallet(palletName) // no more type error

declare const storageName: string

if (isStorage(pallet, storageName)) {
  const storageName = pallet.storage(storageName)
  // ...
}

Tradeoffs

The first solution probably lends itself to faster prototyping. The second solution probably results in nicer error messages and safer code (albeit more code). We could also have both, and prefix untyped with unsafe to discourage devs from shipping prod code that uses it. unsafeUntyped sounds scary indeed!


Thank you @arrudagates for pointing out this use case!

@harrysolovay harrysolovay changed the title opting out of narrow chain type passing dynamic data to codegened API methods Jul 14, 2023
@vjjft vjjft added needs input We need input from the team and/or others api request labels Jul 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api needs input We need input from the team and/or others request
Projects
Status: No status
Development

No branches or pull requests

2 participants