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

feat: flat codegen #867

Merged
merged 9 commits into from
Apr 11, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: deno run -A _tasks/use_remote.ts
- run: deno task run _tasks/star.ts
# Attempt caching thrice; #856
- run: deno cache target/star.ts || deno cache target/star.ts || deno cache target/star.ts
# Attempt caching many times; #856
- run: deno cache target/star.ts || deno cache target/star.ts || deno cache target/star.ts || deno cache target/star.ts || deno cache target/star.ts || deno cache target/star.ts

sync:
name: Sync
Expand Down
2 changes: 0 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ import { System } from "@capi/polkadot"
const accounts = await System.Account.entryPage(10, null).run()
```

> Note: to run with Deno, import from `@capi/polkadot/mod.js`

## Running Examples

Within a fresh clone of this repository...
Expand Down
8 changes: 4 additions & 4 deletions codegen/FrameCodegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class FrameCodegen {
import * as _codecs from "./codecs.js"
import { connect } from "./connection.js"
import * as C from "./capi.js"
import * as t from "./types/mod.js"
import * as t from "./types.js"

export const metadata = ${this.codecCodegen.print(this.metadata)}

Expand All @@ -38,7 +38,7 @@ export const chain = C.ChainRune.from(connect, metadata)
`
import * as _codecs from "./codecs.js"
import * as C from "./capi.js"
import * as t from "./types/mod.js"
import * as t from "./types.js"

export const metadata: ${this.typeCodegen.print(this.metadata)}

Expand All @@ -58,7 +58,7 @@ export const chain: C.ChainRune<${this.chainName}, never>
export * from "./connection.js"
export * from "./chain.js"
export * from "./pallets.js"
export * as types from "./types/mod.js"
export * from "./types.js"
`,
)

Expand All @@ -68,7 +68,7 @@ export * as types from "./types/mod.js"
import { chain, ${isTypes ? this.chainName : ""} } from "./chain.js"
import * as C from "./capi.js"
import * as _codecs from "./codecs.js"
import * as t from "./types/mod.js"
import * as t from "./types.js"

${
Object.values(this.metadata.pallets).map((pallet) => {
Expand Down
41 changes: 8 additions & 33 deletions codegen/TypeCodegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
$storageKey,
} from "../frame_metadata/key_codecs.ts"
import { $era, $null, ChainError } from "../scale_info/mod.ts"
import { getOrInit, stringifyKey, stringifyPropertyAccess } from "../util/mod.ts"
import { stringifyKey, stringifyPropertyAccess } from "../util/mod.ts"
import { CodecCodegen } from "./CodecCodegen.ts"

export class TypeCodegen {
Expand Down Expand Up @@ -187,38 +187,18 @@ is${variant.tag}(value) {
} }`
}

_write(files: Map<string, string>, path: string[], entries: [string[], $.Codec<any>][]): boolean {
const groups = new Map<string, [string[], $.Codec<any>][]>()
const codecs = []
for (const entry of entries) {
const name = entry[0][path.length]!
if (entry[0].length === path.length + 1) {
codecs.push([name, entry[1]] as const)
} else {
getOrInit(groups, name, () => []).push(entry)
}
}
const isFolder = !!groups.size
const rootDir = "../".repeat(path.length + +isFolder)
write(files: Map<string, string>) {
for (const isTypes of [false, true]) {
const ext = isTypes ? "d.ts" : "js"
files.set(
`${["types", ...path].join("/")}${isFolder ? "/mod" : ""}.${ext}`,
`types.${ext}`,
`
import * as C from "${rootDir}capi.js"
import * as _codecs from "${rootDir}codecs.js"
import * as t from "${rootDir}types/mod.js"

${
[...groups].map(([name, entries]) => {
const isFolder = this._write(files, [...path, name], entries)
return `
export * as ${name} from ${JSON.stringify(`./${name}${isFolder ? "/mod" : ""}.js`)}`
}).join("\n")
}
import * as C from "./capi.js"
import * as _codecs from "./codecs.js"
import * as t from "./types.js"

${
codecs.map(([name, codec]) => `
Object.entries(this.types).map(([name, codec]) => `
export const $${name.replace(/^./, (x) => x.toLowerCase())}${
isTypes ? `: C.$.Codec<${name}>` : `= ${this.codecCodegen.print(codec)}`
}
Expand All @@ -229,16 +209,11 @@ ${this.declVisitor.visit(codec)(name, isTypes)}
`,
)
}
return isFolder
}

write(files: Map<string, string>) {
this._write(files, [], Object.entries(this.types).map((x) => [x[0].split("."), x[1]]))
files.set(
"codecs.d.ts",
`
import * as C from "./capi.js"
import * as t from "./types/mod.js"
import * as t from "./types.js"

${
[...this.codecCodegen.codecIds.entries()].filter((x) => x[1] != null).map(([codec, id]) => `
Expand Down
4 changes: 2 additions & 2 deletions examples/blocks.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* various pieces of data pertaining to that block.
*/

import { chain, metadata, types } from "@capi/polkadot/mod.js"
import { $eventRecord, chain, metadata } from "@capi/polkadot"
import { $, $extrinsic, known, Rune } from "capi"
import { babeBlockAuthor } from "capi/patterns/consensus/mod.ts"

Expand Down Expand Up @@ -48,7 +48,7 @@ $.assert(
$.field("block", known.$signedBlock),
$.field("extrinsics", $.array($extrinsic(metadata))),
$.field("extrinsicsRaw", $.array($.str)),
$.field("events", $.array(types.frame_system.$eventRecord)),
$.field("events", $.array($eventRecord)),
$.field("author", $.str),
),
collection,
Expand Down
2 changes: 1 addition & 1 deletion examples/dev/metadata.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* chances are that you don't need to work with the metadata directly.
*/

import { chain } from "@capi/polkadot-dev/mod.js"
import { chain } from "@capi/polkadot-dev"

// Execute the metadata Rune.
const metadata = await chain.metadata.run()
Expand Down
2 changes: 1 addition & 1 deletion examples/dev/storage_sizes.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* this can be helpful in the context of chain development.
*/

import { chain } from "@capi/polkadot-dev/mod.js"
import { chain } from "@capi/polkadot-dev"
import { $ } from "capi"
import { storageSizes } from "capi/patterns/storage_sizes.ts"

Expand Down
2 changes: 1 addition & 1 deletion examples/dev/test_users.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* funds. This simplifies signing extrinsics for submission to the given test chain.
*/

import { createUsers } from "@capi/polkadot-dev/mod.js"
import { createUsers } from "@capi/polkadot-dev"
import { $, $sr25519 } from "capi"

// Test users can be initialized with no count. The resulting collection is
Expand Down
2 changes: 1 addition & 1 deletion examples/ink/deploy.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @todo utilize `createUsers` instead of `alice`
*/

import { chain, System } from "@capi/contracts-dev/mod.js"
import { chain, System } from "@capi/contracts-dev"
import { $, alice, ss58 } from "capi"
import { InkMetadataRune } from "capi/patterns/ink/mod.ts"
import { signature } from "capi/patterns/signature/polkadot.ts"
Expand Down
2 changes: 1 addition & 1 deletion examples/ink/interact.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @todo utilize `createUsers` instead of `alice` and `bob`.
*/

import { chain } from "@capi/contracts-dev/mod.js"
import { chain } from "@capi/contracts-dev"
import { assert } from "asserts"
import { $, alice, bob } from "capi"
import { InkMetadataRune } from "capi/patterns/ink/mod.ts"
Expand Down
2 changes: 1 addition & 1 deletion examples/misc/identity.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @description Set a user's identity, potentially with metadata of arbitrary user-defined shape.
*/

import { createUsers, Identity } from "@capi/polkadot-dev/mod.js"
import { createUsers, Identity } from "@capi/polkadot-dev"
import { $ } from "capi"
import { IdentityInfoTranscoders } from "capi/patterns/identity.ts"
import { signature } from "capi/patterns/signature/polkadot.ts"
Expand Down
2 changes: 1 addition & 1 deletion examples/misc/indices.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* the user's account id using the index.
*/

import { createUsers, Indices } from "@capi/polkadot-dev/mod.js"
import { createUsers, Indices } from "@capi/polkadot-dev"
import { assertEquals } from "asserts"
import { signature } from "capi/patterns/signature/polkadot.ts"

Expand Down
2 changes: 1 addition & 1 deletion examples/multisig/basic.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* that multisig.
*/

import { Balances, chain, createUsers, System } from "@capi/polkadot-dev/mod.js"
import { Balances, chain, createUsers, System } from "@capi/polkadot-dev"
import { assert } from "asserts"
import { $ } from "capi"
import { MultisigRune } from "capi/patterns/multisig/mod.ts"
Expand Down
3 changes: 1 addition & 2 deletions examples/multisig/stash.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* three signatories.
*/

import { Balances, chain, createUsers, Proxy, System } from "@capi/polkadot-dev/mod.js"
import { MultiAddress } from "@capi/polkadot-dev/types/sp_runtime/multiaddress.js"
import { Balances, chain, createUsers, MultiAddress, Proxy, System } from "@capi/polkadot-dev"
import { assert } from "asserts"
import { MultisigRune } from "capi/patterns/multisig/mod.ts"
import { filterPureCreatedEvents } from "capi/patterns/proxy/mod.ts"
Expand Down
3 changes: 1 addition & 2 deletions examples/multisig/virtual.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
* members ratify a call to give ownership of the stash account to the new multisig.
*/

import { Balances, chain, createUsers, System, Utility } from "@capi/polkadot-dev/mod.js"
import { MultiAddress } from "@capi/polkadot-dev/types/sp_runtime/multiaddress.js"
import { Balances, chain, createUsers, MultiAddress, System, Utility } from "@capi/polkadot-dev"
import { assert } from "asserts"
import { $, Rune, Sr25519 } from "capi"
import { VirtualMultisigRune } from "capi/patterns/multisig/mod.ts"
Expand Down
4 changes: 2 additions & 2 deletions examples/paginate.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @description Read pages (either of keys or entries) from storage maps.
*/

import { System, types } from "@capi/polkadot-dev/mod.js"
import { $accountInfo, System } from "@capi/polkadot-dev"
import { $ } from "capi"

// Reference the first 10 keys of a polkadot dev chain's system account map.
Expand All @@ -20,6 +20,6 @@ const accountEntries = await System.Account.entryPage(10, null).run()
// Each entry should be of type `[Uint8Array, AccountInfo]`
console.log("Account entries:", accountEntries)
$.assert(
$.sizedArray($.tuple($.uint8Array, types.frame_system.$accountInfo), 10),
$.sizedArray($.tuple($.uint8Array, $accountInfo), 10),
accountEntries,
)
2 changes: 1 addition & 1 deletion examples/raw_rpc/call.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @description Interact directly with the RPC node's call methods.
*/

import { chain } from "@capi/polkadot-dev/mod.js"
import { chain } from "@capi/polkadot-dev"
import { $ } from "capi"

// Make a call.
Expand Down
2 changes: 1 addition & 1 deletion examples/raw_rpc/subscription.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @description Interact directly with the RPC node's subscription methods.
*/

import { chain } from "@capi/polkadot-dev/mod.js"
import { chain } from "@capi/polkadot-dev"
import { $, known } from "capi"

// Get an async iterator, which yields subscription events.
Expand Down
4 changes: 2 additions & 2 deletions examples/read/account_info.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* @description Read the value (an `AccountInfo`) from the system account map.
*/

import { createUsers, System, types } from "@capi/polkadot-dev/mod.js"
import { $accountInfo, createUsers, System } from "@capi/polkadot-dev"
import { $ } from "capi"

const { alexa } = await createUsers()

const accountInfo = await System.Account.value(alexa.publicKey).run()

console.log("Account info:", accountInfo)
$.assert(types.frame_system.$accountInfo, accountInfo)
$.assert($accountInfo, accountInfo)
4 changes: 2 additions & 2 deletions examples/read/era_reward_points.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* reward points).
*/

import { Staking, types } from "@capi/westend/mod.js"
import { $eraRewardPoints, Staking } from "@capi/westend"
import { $ } from "capi"

const idx = Staking.ActiveEra
Expand All @@ -18,4 +18,4 @@ const points = await Staking.ErasRewardPoints.value(idx).run()
console.log("Era reward points:", points)

// Ensure the era reward points is of the correct shape.
$.assert(types.pallet_staking.$eraRewardPoints, points)
$.assert($eraRewardPoints, points)
2 changes: 1 addition & 1 deletion examples/read/now.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @description Read the current timestamp as agreed upon by validators.
*/

import { Timestamp } from "@capi/polkadot/mod.js"
import { Timestamp } from "@capi/polkadot"
import { $ } from "capi"

const now = await Timestamp.Now.value().run()
Expand Down
2 changes: 1 addition & 1 deletion examples/read/para_heads.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* (the corresponding parachain heads).
*/

import { Paras } from "@capi/polkadot/mod.js"
import { Paras } from "@capi/polkadot"
import { $, ArrayRune, ValueRune } from "capi"

const heads = await Paras.Parachains
Expand Down
5 changes: 2 additions & 3 deletions examples/sign/ed25519.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @description Utilize an Ed25519 library for signing.
*/

import { Balances, createUsers, System, types } from "@capi/westend-dev/mod.js"
import { Balances, createUsers, MultiAddress, System } from "@capi/westend-dev"
import { assert } from "asserts"
import { Rune } from "capi"
import { signature } from "capi/patterns/signature/polkadot.ts"
Expand All @@ -24,8 +24,7 @@ console.log("Billy free initial:", billyFreeInitial)
const secret = crypto.getRandomValues(new Uint8Array(32))

// Get a Rune of the secret-corresponding multiaddress.
const address = types.sp_runtime.multiaddress.MultiAddress
.Id(await ed.getPublicKey(secret))
const address = MultiAddress.Id(await ed.getPublicKey(secret))

// Define a `sign` function for later use.
async function sign(msg: Uint8Array) {
Expand Down
2 changes: 1 addition & 1 deletion examples/sign/offline.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Finally, rehydrate the extrinsic and submit it.
*/

import { Balances, chain, createUsers } from "@capi/westend-dev/mod.js"
import { Balances, chain, createUsers } from "@capi/westend-dev"
import { $, SignedExtrinsicRune } from "capi"
import { signature } from "capi/patterns/signature/polkadot.ts"

Expand Down
2 changes: 1 addition & 1 deletion examples/sign/pjs.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* extension) to sign a Capi extrinsic.
*/

import { Balances, chain, createUsers, System } from "@capi/polkadot-dev/mod.js"
import { Balances, chain, createUsers, System } from "@capi/polkadot-dev"
import { ss58 } from "capi"
import { pjsSender, PjsSigner } from "capi/patterns/compat/pjs_sender.ts"
import { signature } from "capi/patterns/signature/polkadot.ts"
Expand Down
2 changes: 1 addition & 1 deletion examples/tx/balances_transfer.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @description Transfer some funds from one user to another.
*/

import { Balances, createUsers, System } from "@capi/westend-dev/mod.js"
import { Balances, createUsers, System } from "@capi/westend-dev"
import { assert } from "asserts"
import { signature } from "capi/patterns/signature/polkadot.ts"

Expand Down
2 changes: 1 addition & 1 deletion examples/tx/handle_errors.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* easy decoding (and handling) of dispatch errors.
*/

import { Balances } from "@capi/contracts-dev/mod.js"
import { Balances } from "@capi/contracts-dev"
import { assertInstanceOf } from "asserts"
import { alice, bob, ExtrinsicError } from "capi"
import { signature } from "capi/patterns/signature/polkadot.ts"
Expand Down
2 changes: 1 addition & 1 deletion examples/tx/utility_batch.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @description Sign and submit multiple calls within a single extrinsic.
*/

import { Balances, createUsers, System, Utility } from "@capi/westend-dev/mod.js"
import { Balances, createUsers, System, Utility } from "@capi/westend-dev"
import { assert } from "asserts"
import { Rune } from "capi"
import { signature } from "capi/patterns/signature/polkadot.ts"
Expand Down
2 changes: 1 addition & 1 deletion examples/watch.eg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* produce promises resolving to subsequent states.
*/

import { chain, Timestamp } from "@capi/polkadot/mod.js"
import { chain, Timestamp } from "@capi/polkadot"
import { $ } from "capi"

// Specifying `chain.latestBlockHash` indicates that (A) this Rune tree
Expand Down
Loading