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

fix: broken patterns + examples #786

Merged
merged 21 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 17 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
1 change: 1 addition & 0 deletions deps/std/collections/map_entries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/std@0.181.0/collections/map_entries.ts"
30 changes: 16 additions & 14 deletions examples/batch.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { Rune } from "capi"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { Balances, System, users, Utility } from "westend_dev/mod.js"
import { mapEntries } from "../deps/std/collections/map_entries.ts"

const [alexa, billy, carol, david] = await users(4)

const recipients = Object.entries({ billy, carol, david })
const balances = Rune.rec(
mapEntries({ billy, carol, david }, ([name, { publicKey }]) => {
const balance = System.Account
.value(publicKey)
.unhandle(undefined)
.access("data", "free")
return [name, balance]
}),
)

const batch = Utility
console.log("Initial balances:", await balances.run())

await Utility
.batch({
calls: Rune.tuple(recipients.map(([, { address }]) =>
calls: Rune.tuple([billy, carol, david].map(({ address }) =>
Balances.transfer({
dest: address,
value: 3_000_000_123_456_789n,
Expand All @@ -18,16 +29,7 @@ const batch = Utility
.signed(signature({ sender: alexa }))
.sent()
.dbgStatus("Batch tx:")
.finalized()

await logBalances()
.chain(() => batch)
.chain(logBalances)
.finalizedHash()
.run()

function logBalances() {
return Rune.tuple(recipients.map(([name, { publicKey }]) => {
const free = System.Account.value(publicKey).unhandle(undefined).access("data", "free")
return free.dbg(Rune.str`${name} balance:`)
}))
}
console.log("Final balances:", await balances.run())
3 changes: 1 addition & 2 deletions examples/derived.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ArrayRune, ValueRune } from "capi"
import { Paras } from "polkadot/mod.js"

const result = await Paras
.Parachains
const result = await Paras.Parachains
.value()
.unhandle(undefined)
.into(ArrayRune)
Expand Down
5 changes: 4 additions & 1 deletion examples/era_rewards.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Staking } from "westend/mod.js"

const idx = Staking.ActiveEra.value().unhandle(undefined).access("index")
const idx = Staking.ActiveEra
.value()
.unhandle(undefined)
.access("index")

const result = await Staking.ErasRewardPoints.value(idx).run()

Expand Down
1 change: 0 additions & 1 deletion examples/identity_cr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ const raw = Identity.IdentityOf
.unhandle(undefined)
.access("info")

// @ts-ignore will re-implement this pattern later anyways
console.log(await transcoders.decode(raw).run())
File renamed without changes.
55 changes: 21 additions & 34 deletions examples/multisig_pure_proxy_stash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,50 @@ const multisig = Rune
})
.into(MultisigRune, chain)

const fundMultisig = Balances
await Balances
.transfer({
value: 20_000_000_000_000n,
dest: multisig.address,
})
.signed(signature({ sender: alexa }))
.sent()
.dbgStatus("Fund Multisig:")
.finalized()
.finalizedHash()
.run()

const aliceRatify = multisig
.ratify({
call: Proxy.createPure({
proxyType: "Any",
delay: 0,
index: 0,
}),
sender: alexa.address,
})
const call = Proxy.createPure({
proxyType: "Any",
delay: 0,
index: 0,
})

await multisig
.ratify({ call, sender: alexa.address })
.signed(signature({ sender: alexa }))
.sent()
.dbgStatus("Alice Ratify:")
.finalized()
.finalizedHash()
.run()

const bobRatify = multisig
.ratify({
call: Proxy.createPure({
proxyType: "Any",
delay: 0,
index: 0,
}),
sender: billy.address,
})
const stashAddress = await multisig
.ratify({ call, sender: billy.address })
.signed(signature({ sender: billy }))
.sent()
.dbgStatus("Bob Ratify:")

const stashAddress = bobRatify
.finalizedEvents()
.pipe(filterPureCreatedEvents)
.map((events) => events[0]!)
.access("pure")
.access(0, "pure")
.run()

const fundStash = Balances
await Balances
.transfer({
value: 20_000_000_000_000n,
dest: MultiAddress.Id(stashAddress),
})
.signed(signature({ sender: alexa }))
.sent()
.dbgStatus("Fund Stash:")
.finalized()

await Rune
.chain(() => fundMultisig)
.chain(() => aliceRatify)
.chain(() => stashAddress)
.chain(() => fundStash)
.chain(() => System.Account.value(stashAddress).dbg("Stash Balance:"))
.finalizedHash()
.run()

console.log("Stash balance:", await System.Account.value(stashAddress).run())
17 changes: 4 additions & 13 deletions examples/multisig_transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ const multisig = Rune
})
.into(MultisigRune, chain)

// Read dave's initial balance (to-be changed by the call)
console.log("Dave initial balance:", await System.Account.value(david.publicKey).run())

// Transfer some funds into the multisig account
await Balances
.transfer({
value: 2_000_000_000_000n,
Expand All @@ -24,28 +22,24 @@ await Balances
.signed(signature({ sender: alexa }))
.sent()
.dbgStatus("Existential deposit:")
.finalized()
.finalizedHash()
.run()

// The to-be proposed and approved call
const call = Balances.transferKeepAlive({
dest: david.address,
value: 1_230_000_000_000n,
})

// Submit a proposal to dispatch the call
await multisig
.ratify({ call, sender: alexa.address })
.signed(signature({ sender: alexa }))
.sent()
.dbgStatus("Proposal:")
.finalized()
.finalizedHash()
.run()

// Check if the call has been proposed
console.log("Is proposed?:", await multisig.isProposed(call.hash).run())

// Send a non-executing approval
await multisig
.approve({
callHash: call.hash,
Expand All @@ -54,27 +48,24 @@ await multisig
.signed(signature({ sender: billy }))
.sent()
.dbgStatus("Vote:")
.finalized()
.finalizedHash()
.run()

// Check for existing approval(s)
console.log(
"Existing approvals:",
await multisig
.proposal(call.hash)
.unsafeAs<any>()
.into(ValueRune)
.access("approvals")
.run(),
)

// Send the executing (final) approval
await multisig
.ratify({ call, sender: carol.address })
.signed(signature({ sender: carol }))
.sent()
.dbgStatus("Approval:")
.finalized()
.finalizedHash()
.run()

// Check to see whether Dave's balance has in fact changed
Expand Down
28 changes: 11 additions & 17 deletions examples/transfer_sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@ import { signature } from "../patterns/signature/polkadot.ts"

const [alexa, billy, carol] = await users(3)

await Rune
.chain(balances)
.chain(() => transfer("bob", billy))
.chain(balances)
.chain(() => transfer("charlie", carol))
.chain(balances)
.run()
const balances = Rune.rec({
alice: balance(alexa),
bob: balance(billy),
charlie: balance(carol),
})

function balances() {
return Rune
.rec({
alice: balance(alexa),
bob: balance(billy),
charlie: balance(carol),
})
.dbg("Balances:")
}
console.log("Initial balances", await balances.run())
await transfer("bob", billy).run()
console.log("Balances after Bob transfer", await balances.run())
await transfer("carol", carol).run()
console.log("Final balances", await balances.run())

function balance(user: Sr25519) {
return System.Account.value(user.publicKey).unhandle(undefined).access("data", "free")
Expand All @@ -35,5 +29,5 @@ function transfer(name: string, user: Sr25519) {
.signed(signature({ sender: alexa }))
.sent()
.dbgStatus(`Transfer to ${name}:`)
.finalized()
.finalizedHash()
}
67 changes: 27 additions & 40 deletions examples/virtual_multisig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Rune } from "capi"
import { Rune, Sr25519 } from "capi"
import { VirtualMultisigRune } from "capi/patterns/multisig/mod.ts"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { Balances, chain, System, users, Utility } from "polkadot_dev/mod.js"
Expand All @@ -10,65 +10,52 @@ const [alexa, billy, carol, david] = await users(4)
let { state } = parse(Deno.args, { string: ["state"] })
if (!state) {
state = await VirtualMultisigRune
.deployment(chain, {
.deployment(/* TODO: simplify */ chain, {
founders: [alexa.publicKey, billy.publicKey, carol.publicKey],
threshold: 2,
deployer: alexa,
})
.hex
.run()
deployer: alexa.address,
}, signature({ sender: alexa })).hex.run()
}

console.log(`Virtual multisig state hex: ${state}`)

const vMultisig = VirtualMultisigRune.hydrate(chain, state)

const fundStash = Balances
await Balances
.transfer({
dest: MultiAddress.Id(vMultisig.stash),
value: 20_000_000_000_000n,
})
.signed(signature({ sender: alexa }))
.sent()
.dbgStatus("Fund Stash:")
.finalized()
.finalizedHash()
.run()

const daveBalance = System.Account.value(david.publicKey)

console.log("Dave balance before:", await daveBalance.run())

const proposal = Balances.transfer({
dest: david.address,
value: 1_234_000_000_000n,
})

const bobTx = Utility
.batchAll({
// @ts-ignore: fix upon #656
calls: Rune.array([
vMultisig.fundMemberProxy(billy.publicKey, 20_000_000_000_000n),
vMultisig.ratify(billy.publicKey, proposal),
]),
})
.signed(signature({ sender: billy }))
.sent()
.dbgStatus("Bob fund & ratify:")
.finalized()
await fundAndRatify("billy", carol).run()
await fundAndRatify("carol", carol).run()

const charlieTx = Utility
.batchAll({
// @ts-ignore: fix upon #656
calls: Rune.array([
vMultisig.fundMemberProxy(carol.publicKey, 20_000_000_000_000n),
vMultisig.ratify(carol.publicKey, proposal),
]),
})
.signed(signature({ sender: carol }))
.sent()
.dbgStatus("Charlie fund & ratify:")
.finalized()
console.log("Dave balance after:", await daveBalance.run())

await Rune
.chain(() => vMultisig)
.chain(() => fundStash)
.chain(() => System.Account.value(david.publicKey).dbg("Dave Balance Before:"))
.chain(() => bobTx)
.chain(() => charlieTx)
.chain(() => System.Account.value(david.publicKey).dbg("Dave Balance After:"))
.run()
function fundAndRatify(name: string, sender: Sr25519) {
return Utility
.batchAll({
calls: Rune.array([
vMultisig.fundMemberProxy(sender.publicKey, 20_000_000_000_000n),
vMultisig.ratify(sender.publicKey, proposal),
]),
})
.signed(signature({ sender }))
.sent()
.dbgStatus(`${name} fund & ratify:`)
.finalizedHash()
}
15 changes: 15 additions & 0 deletions fluent/EventsRune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ import { PatternRune } from "./PatternRune.ts"

export class EventsRune<out C extends Chain, out U> extends PatternRune<Event<C>[], C, U> {}

export interface TmpEventsChain extends Chain {
harrysolovay marked this conversation as resolved.
Show resolved Hide resolved
metadata: FrameMetadata & {
pallets: {
System: {
storage: {
Events: {
key: Codec<void>
value: Codec<_Event<any>[]>
}
}
}
}
}
}

interface _EventsChain<RE> extends Chain {
metadata: FrameMetadata & {
pallets: {
Expand Down
Loading