From 0eff1711d7b5d089e2195b4630aed58642d0724c Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Wed, 12 Apr 2023 21:06:07 -0400 Subject: [PATCH 1/6] first pass --- codegen/TypeCodegen.ts | 25 ++++++++++++++- examples/sign/ed25519.eg.ts | 4 +-- examples/xcm/asset_teleportation.eg.ts | 43 ++++++++++++++++---------- fluent/ExtrinsicRune.ts | 7 ++++- 4 files changed, 59 insertions(+), 20 deletions(-) diff --git a/codegen/TypeCodegen.ts b/codegen/TypeCodegen.ts index 4fd5b0b41..282cf19ea 100644 --- a/codegen/TypeCodegen.ts +++ b/codegen/TypeCodegen.ts @@ -148,6 +148,29 @@ is${variant.tag}(value) { } } `) + .add($.object, (_codec) => (name, isTypes) => ` +${ + isTypes + ? `export type ${name} = ${this.nativeVisitor.visit(_codec)} + export function ${name}(fields: C.RunicArgs): C.ValueRune<${name}, C.RunicArgs.U>` + : `export function ${name}(fields) { + return Rune.rec(fields) +}` + }`) + .add($.array, (_codec, _element) => (name, isTypes) => + isTypes + ? `export type ${name} = ${this.nativeVisitor.visit(_codec)} + export function ${name}(elements: C.RunicArgs): C.ValueRune<${name}, C.RunicArgs.U>` + : `export function ${name}(elements) { + return Rune.array(elements) +}`) + .add($.tuple, (_codec, ..._element) => (name, isTypes) => + isTypes + ? `export type ${name} = ${this.nativeVisitor.visit(_codec)} + export function ${name}(elements: C.RunicArgs): C.ValueRune<${name}, C.RunicArgs.U>` + : `export function ${name}(elements) { + return Rune.tuple(elements) +}`) .fallback((codec) => (name, isTypes) => isTypes ? `export type ${name} = ${this.nativeVisitor.visit(codec)}` : "" ) @@ -205,7 +228,7 @@ export const $${name.replace(/^./, (x) => x.toLowerCase())}${ ${this.declVisitor.visit(codec)(name, isTypes)} `).join("\n") } - + `, ) } diff --git a/examples/sign/ed25519.eg.ts b/examples/sign/ed25519.eg.ts index 7f3dee6cb..b24b3d723 100644 --- a/examples/sign/ed25519.eg.ts +++ b/examples/sign/ed25519.eg.ts @@ -6,7 +6,7 @@ import { Balances, createUsers, MultiAddress, System } from "@capi/westend-dev" import { assert } from "asserts" -import { Rune } from "capi" +import { ExtrinsicSender, Rune } from "capi" import { signature } from "capi/patterns/signature/polkadot.ts" import * as ed from "https://esm.sh/@noble/ed25519@1.7.3" @@ -53,7 +53,7 @@ await Balances value: 12345n, dest: billy.address, }) - .signed(signature({ sender: Rune.rec({ address, sign }) })) + .signed(signature({ sender: ExtrinsicSender({ address, sign }) })) .sent() .dbgStatus("Transfer:") .finalizedEvents() diff --git a/examples/xcm/asset_teleportation.eg.ts b/examples/xcm/asset_teleportation.eg.ts index 3a182bbc1..7cf4dda54 100644 --- a/examples/xcm/asset_teleportation.eg.ts +++ b/examples/xcm/asset_teleportation.eg.ts @@ -14,6 +14,9 @@ import { XcmV2Fungibility, XcmV2Junction, XcmV2Junctions, + XcmV2MultiAsset, + XcmV2MultiAssets, + XcmV2MultiLocation, XcmV2NetworkId, XcmV2WeightLimit, } from "@capi/rococo-dev" @@ -24,7 +27,7 @@ import { System, } from "@capi/rococo-dev/statemine" import { assert } from "asserts" -import { alice, Rune } from "capi" +import { alice } from "capi" import { signature } from "capi/patterns/signature/polkadot.ts" // Reference Alice's free balance. @@ -39,22 +42,30 @@ console.log("Alice initial free:", aliceFreeInitial) XcmPallet .limitedTeleportAssets({ - dest: VersionedMultiLocation.V2(Rune.rec({ - parents: 0, - interior: XcmV2Junctions.X1(XcmV2Junction.Parachain(1000)), - })), - beneficiary: VersionedMultiLocation.V2(Rune.rec({ - parents: 0, - interior: XcmV2Junctions.X1(XcmV2Junction.AccountId32({ - id: alice.publicKey, - network: XcmV2NetworkId.Any(), - })), - })), - assets: VersionedMultiAssets.V2(Rune.array([Rune.rec({ - id: XcmV2AssetId.Concrete(Rune.rec({ + dest: VersionedMultiLocation.V2( + XcmV2MultiLocation({ parents: 0, - interior: XcmV2Junctions.Here(), - })), + interior: XcmV2Junctions.X1(XcmV2Junction.Parachain(1000)), + }), + ), + beneficiary: VersionedMultiLocation.V2( + XcmV2MultiLocation({ + parents: 0, + interior: XcmV2Junctions.X1( + XcmV2Junction.AccountId32({ + id: alice.publicKey, + network: XcmV2NetworkId.Any(), + }), + ), + }), + ), + assets: VersionedMultiAssets.V2(XcmV2MultiAssets([XcmV2MultiAsset({ + id: XcmV2AssetId.Concrete( + XcmV2MultiLocation({ + parents: 0, + interior: XcmV2Junctions.Here(), + }), + ), fun: XcmV2Fungibility.Fungible(500_000_000_000_000n), })])), feeAssetItem: 0, diff --git a/fluent/ExtrinsicRune.ts b/fluent/ExtrinsicRune.ts index d3fe430f1..38ba87be6 100644 --- a/fluent/ExtrinsicRune.ts +++ b/fluent/ExtrinsicRune.ts @@ -2,7 +2,7 @@ import { blake2_256, hex } from "../crypto/mod.ts" import * as $ from "../deps/scale.ts" import { concat } from "../deps/std/bytes.ts" import { $extrinsic, Signer } from "../frame_metadata/Extrinsic.ts" -import { Rune, ValueRune } from "../rune/mod.ts" +import { Rune, RunicArgs, ValueRune } from "../rune/mod.ts" import { Chain, ChainRune } from "./ChainRune.ts" import { CodecRune } from "./CodecRune.ts" import { PatternRune } from "./PatternRune.ts" @@ -12,6 +12,11 @@ export interface ExtrinsicSender { address: Chain.Address sign: Signer } +export function ExtrinsicSender( + extrinsicSender: RunicArgs>, +): Rune, RunicArgs.U> { + return Rune.rec(extrinsicSender) as never +} export interface SignatureData { sender: ExtrinsicSender From 1953f7833a9dc5604c1bc3a074b6102463fbf3db Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Wed, 12 Apr 2023 21:12:20 -0400 Subject: [PATCH 2/6] fix lint error --- examples/sign/ed25519.eg.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/sign/ed25519.eg.ts b/examples/sign/ed25519.eg.ts index b24b3d723..4e72d08a3 100644 --- a/examples/sign/ed25519.eg.ts +++ b/examples/sign/ed25519.eg.ts @@ -6,7 +6,7 @@ import { Balances, createUsers, MultiAddress, System } from "@capi/westend-dev" import { assert } from "asserts" -import { ExtrinsicSender, Rune } from "capi" +import { ExtrinsicSender } from "capi" import { signature } from "capi/patterns/signature/polkadot.ts" import * as ed from "https://esm.sh/@noble/ed25519@1.7.3" From 0022fc2341ed6cf8b7b547153ab8ca53f1541009 Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Fri, 14 Apr 2023 16:01:45 -0400 Subject: [PATCH 3/6] cleanup --- codegen/TypeCodegen.ts | 2 +- examples/xcm/asset_teleportation.eg.ts | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/codegen/TypeCodegen.ts b/codegen/TypeCodegen.ts index 282cf19ea..3ad2747be 100644 --- a/codegen/TypeCodegen.ts +++ b/codegen/TypeCodegen.ts @@ -167,7 +167,7 @@ ${ .add($.tuple, (_codec, ..._element) => (name, isTypes) => isTypes ? `export type ${name} = ${this.nativeVisitor.visit(_codec)} - export function ${name}(elements: C.RunicArgs): C.ValueRune<${name}, C.RunicArgs.U>` + export function ${name}(...elements: C.RunicArgs): C.ValueRune<${name}, C.RunicArgs.U>` : `export function ${name}(elements) { return Rune.tuple(elements) }`) diff --git a/examples/xcm/asset_teleportation.eg.ts b/examples/xcm/asset_teleportation.eg.ts index 7cf4dda54..f33006552 100644 --- a/examples/xcm/asset_teleportation.eg.ts +++ b/examples/xcm/asset_teleportation.eg.ts @@ -15,7 +15,6 @@ import { XcmV2Junction, XcmV2Junctions, XcmV2MultiAsset, - XcmV2MultiAssets, XcmV2MultiLocation, XcmV2NetworkId, XcmV2WeightLimit, @@ -27,7 +26,7 @@ import { System, } from "@capi/rococo-dev/statemine" import { assert } from "asserts" -import { alice } from "capi" +import { alice, Rune } from "capi" import { signature } from "capi/patterns/signature/polkadot.ts" // Reference Alice's free balance. @@ -59,7 +58,7 @@ XcmPallet ), }), ), - assets: VersionedMultiAssets.V2(XcmV2MultiAssets([XcmV2MultiAsset({ + assets: VersionedMultiAssets.V2(Rune.array([XcmV2MultiAsset({ id: XcmV2AssetId.Concrete( XcmV2MultiLocation({ parents: 0, From c417c80b757c382312278e1752dbb4bfc913fef9 Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Fri, 14 Apr 2023 16:03:13 -0400 Subject: [PATCH 4/6] t6 suggestion --- fluent/ExtrinsicRune.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fluent/ExtrinsicRune.ts b/fluent/ExtrinsicRune.ts index 38ba87be6..7eb47c757 100644 --- a/fluent/ExtrinsicRune.ts +++ b/fluent/ExtrinsicRune.ts @@ -15,7 +15,7 @@ export interface ExtrinsicSender { export function ExtrinsicSender( extrinsicSender: RunicArgs>, ): Rune, RunicArgs.U> { - return Rune.rec(extrinsicSender) as never + return Rune.rec(RunicArgs.resolve(extrinsicSender)) } export interface SignatureData { From dacc5d475d0c1f3f3cfcaca18756f891cdedc972 Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Fri, 14 Apr 2023 16:03:54 -0400 Subject: [PATCH 5/6] get rid of array factory generation --- codegen/TypeCodegen.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/codegen/TypeCodegen.ts b/codegen/TypeCodegen.ts index 3ad2747be..eefc2f3d3 100644 --- a/codegen/TypeCodegen.ts +++ b/codegen/TypeCodegen.ts @@ -157,13 +157,6 @@ ${ return Rune.rec(fields) }` }`) - .add($.array, (_codec, _element) => (name, isTypes) => - isTypes - ? `export type ${name} = ${this.nativeVisitor.visit(_codec)} - export function ${name}(elements: C.RunicArgs): C.ValueRune<${name}, C.RunicArgs.U>` - : `export function ${name}(elements) { - return Rune.array(elements) -}`) .add($.tuple, (_codec, ..._element) => (name, isTypes) => isTypes ? `export type ${name} = ${this.nativeVisitor.visit(_codec)} From 47f6cf7d70023d1e0db42379b4b7a8e5823d8e1c Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Fri, 14 Apr 2023 16:44:07 -0400 Subject: [PATCH 6/6] fixes --- codegen/TypeCodegen.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/codegen/TypeCodegen.ts b/codegen/TypeCodegen.ts index eefc2f3d3..fa314e3ee 100644 --- a/codegen/TypeCodegen.ts +++ b/codegen/TypeCodegen.ts @@ -152,7 +152,7 @@ is${variant.tag}(value) { ${ isTypes ? `export type ${name} = ${this.nativeVisitor.visit(_codec)} - export function ${name}(fields: C.RunicArgs): C.ValueRune<${name}, C.RunicArgs.U>` +export function ${name}(fields: C.RunicArgs): C.ValueRune<${name}, C.RunicArgs.U>` : `export function ${name}(fields) { return Rune.rec(fields) }` @@ -160,8 +160,8 @@ ${ .add($.tuple, (_codec, ..._element) => (name, isTypes) => isTypes ? `export type ${name} = ${this.nativeVisitor.visit(_codec)} - export function ${name}(...elements: C.RunicArgs): C.ValueRune<${name}, C.RunicArgs.U>` - : `export function ${name}(elements) { +export function ${name}(...elements: C.RunicArgs): C.ValueRune<${name}, C.RunicArgs.U>` + : `export function ${name}(...elements) { return Rune.tuple(elements) }`) .fallback((codec) => (name, isTypes) =>