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

Commit

Permalink
feat: simplify dynamic dx (#774)
Browse files Browse the repository at this point in the history
Co-authored-by: T6 <[email protected]>
  • Loading branch information
harrysolovay and tjjfvi authored Mar 22, 2023
1 parent 03dcc3a commit e0d3980
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 32 deletions.
5 changes: 2 additions & 3 deletions codegen/FrameCodegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@ export class FrameCodegen {
"chain.js",
`
import * as _codecs from "./codecs.js"
import { connection } from "./connection.js"
import { connectionCtor, discoveryValue } from "./connection.js"
import * as C from "./capi.js"
import * as t from "./types/mod.js"
export const metadata = ${this.codecCodegen.print(this.metadata)}
export const chain = C.Rune.rec({ metadata, connection }).into(C.ChainRune)
export const chain = C.ChainRune.from(connectionCtor, discoveryValue, metadata)
`,
)
files.set(
"chain.d.ts",
`
import * as _codecs from "./codecs.js"
import { connection } from "./connection.js"
import * as C from "./capi.js"
import * as t from "./types/mod.js"
Expand Down
11 changes: 11 additions & 0 deletions examples/dynamic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ChainRune, WsConnection } from "capi"

const chain = ChainRune.from(WsConnection, "wss://rpc.polkadot.io")

const accountInfo = await chain
.pallet("System")
.storage("Account")
.entryPage(10, null)
.run()

console.log(accountInfo)
11 changes: 0 additions & 11 deletions examples/dynamic/balance.ts

This file was deleted.

31 changes: 23 additions & 8 deletions fluent/ChainRune.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { hex } from "../crypto/mod.ts"
import * as $ from "../deps/scale.ts"
import { FrameMetadata } from "../frame_metadata/mod.ts"
import { Connection } from "../rpc/mod.ts"
import { decodeMetadata, FrameMetadata } from "../frame_metadata/mod.ts"
import { Connection, ConnectionCtorLike } from "../rpc/mod.ts"
import { Rune, RunicArgs, ValueRune } from "../rune/mod.ts"
import { BlockRune } from "./BlockRune.ts"
import { ConnectionRune } from "./ConnectionRune.ts"
Expand Down Expand Up @@ -50,6 +51,21 @@ export namespace Chain {

// TODO: do we want to represent the discovery value and conn type within the type system?
export class ChainRune<out C extends Chain, out U> extends Rune<C, U> {
static from<D, M extends FrameMetadata>(
connectionCtor: ConnectionCtorLike<D>,
discovery: D,
staticMetadata?: M,
) {
const connection = ConnectionRune.from(async (signal) =>
connectionCtor.connect(discovery, signal)
)
const metadata = staticMetadata ?? Rune
.fn(hex.decode)
.call(connection.call("state_getMetadata"))
.map(decodeMetadata)
return Rune.rec({ connection, metadata }).into(this)
}

connection = this.into(ValueRune<Chain, U>).access("connection").into(ConnectionRune)

metadata = this.into(ValueRune).access("metadata")
Expand All @@ -76,12 +92,11 @@ export class ChainRune<out C extends Chain, out U> extends Rune<C, U> {
return call.into(ExtrinsicRune, this.as(ChainRune))
}

pallet<P extends Chain.PalletName<C>, X>(...args: RunicArgs<X, [palletName: P]>) {
const [palletName] = RunicArgs.resolve(args)
return this
.into(ValueRune)
.access("metadata", "pallets", palletName.as(Rune))
.into(PalletRune, this)
pallet<P extends Chain.PalletName<C>, X>(...[palletName]: RunicArgs<X, [P]>) {
return this.metadata
.access("pallets", palletName)
.unsafeAs<Chain.Pallet<C, P>>()
.into(PalletRune, this.as(ChainRune))
}

addressPrefix(this: ChainRune<AddressPrefixChain, U>) {
Expand Down
8 changes: 4 additions & 4 deletions fluent/ConnectionRune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class RunConnection extends Run<Connection, never> {
}
}

export function connection(init: (signal: AbortSignal) => Promise<Connection>) {
return Rune.new(RunConnection, init).into(ConnectionRune)
}

export class ConnectionRune<U> extends Rune<Connection, U> {
static from(init: (signal: AbortSignal) => Promise<Connection>) {
return Rune.new(RunConnection, init).into(ConnectionRune)
}

call<K extends keyof Calls, X>(
callMethod: K,
...args: RunicArgs<X, [...Parameters<Calls[K]>]>
Expand Down
7 changes: 1 addition & 6 deletions providers/frame/FrameProxyProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,8 @@ export abstract class FrameProxyProvider extends FrameProvider {
return `
import * as C from "./capi.js"
export const connectionCtor ${isTypes ? `: typeof C.WsConnection` : `= C.WsConnection`}
export const discoveryValue ${isTypes ? ":" : "="} "${url}"
export const connection ${
isTypes
? ": C.ConnectionRune<never>"
: "= C.connection((signal) => C.WsConnection.connect(discoveryValue, signal))"
}
`
}
}
5 changes: 5 additions & 0 deletions rpc/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { RpcCallMessage, RpcIngressMessage, RpcSubscriptionHandler } from "./rpc

const connectionMemos = new Map<new(discovery: any) => Connection, Map<unknown, Connection>>()

export interface ConnectionCtorLike<D> {
new(discovery: D): Connection
connect: (discovery: D, signal: AbortSignal) => Connection
}

export abstract class Connection {
nextId = 0
references = 0
Expand Down

0 comments on commit e0d3980

Please sign in to comment.