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

Commit

Permalink
chore: transfer sequence example (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
harrysolovay authored Feb 21, 2023
1 parent 26454dc commit c340142
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/transfer_sequence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { alice, bob, charlie, Rune, Sr25519 } from "capi"
import { Balances, System } from "westend_dev/mod.ts"

await Rune
.chain(balances)
.chain(() => transfer("bob", bob))
.chain(balances)
.chain(() => transfer("charlie", charlie))
.chain(balances)
.run()

function balances() {
return Rune
.rec({
alice: balance(alice),
bob: balance(bob),
charlie: balance(charlie),
})
.log("Balances:")
}

function balance(user: Sr25519) {
return System.Account.entry([user.publicKey]).access("data", "free")
}

function transfer(name: string, user: Sr25519) {
return Balances
.transfer({
dest: user.address,
value: 1000123n,
})
.signed({ sender: alice })
.sent()
.logStatus(`Transfer to ${name}:`)
.finalized()
}
4 changes: 4 additions & 0 deletions rune/Rune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export class Rune<out T, out U = never> {
return new Rune((batch) => new ctor(batch, ...args))
}

static chain<T, U>(fn: () => Rune<T, U>): ValueRune<T, U> {
return fn().into(ValueRune)
}

async run(batch = new Batch()): Promise<T> {
for await (const value of this.iter(batch)) {
return value
Expand Down

0 comments on commit c340142

Please sign in to comment.