This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: decouple capi from presentation layer (#223)
Co-authored-by: Josep M Sobrepere <jm.sobrepere@gmail.com>
- Loading branch information
1 parent
b91eba6
commit 33bce54
Showing
20 changed files
with
456 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { hex, Rune, RunicArgs } from "capi" | ||
import { signature } from "capi/patterns/signature/polkadot" | ||
import { Setup } from "common" | ||
import { defaultAccount } from "../signals/accounts.js" | ||
import { toMultiAddressIdRune, toMultisigRune } from "../util/capi-helpers.js" | ||
import { createSender } from "./createSender.js" | ||
import { Message } from "./notificationsCb.js" | ||
|
||
export async function cancel( | ||
setup: Setup, | ||
callHash: string, | ||
cb: (value: Message) => void, | ||
) { | ||
const account = defaultAccount.value | ||
if (!account) throw new Error("No account connected!") | ||
|
||
const multisig = toMultisigRune(setup) | ||
const user = toMultiAddressIdRune(account.address) | ||
const sender = createSender(account.address) | ||
|
||
const cancelCall = multisig | ||
.cancel(user, hex.decode(callHash)) | ||
.signed(signature({ sender })) | ||
.sent() | ||
.dbgStatus("Cancel") | ||
.finalizedEvents() | ||
.unhandleFailed() | ||
.pipe(<X>(...[events]: RunicArgs<X, [any[]]>) => { | ||
cb({ type: "loading" }) | ||
return Rune.resolve(events).map((events) => { | ||
cb({ type: "success" }) | ||
const eventNames = events.map((e) => | ||
`${e.event.type}:${e.event.value.type}` | ||
) | ||
cb({ type: "info", events: eventNames }) | ||
}) | ||
}) | ||
|
||
await cancelCall.run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { westend } from "@capi/westend" | ||
import { pjsSender } from "capi/patterns/compat/pjs_sender" | ||
import { defaultExtension } from "../signals/accounts.js" | ||
|
||
export function createSender(address: string) { | ||
return pjsSender( | ||
westend, | ||
defaultExtension.value!.signer as any, | ||
)(address) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { PalletProxyEvent, RuntimeEvent, westend } from "@capi/westend" | ||
import { Rune, RunicArgs, ss58 } from "capi" | ||
import { signature } from "capi/patterns/signature/polkadot" | ||
import { createSender } from "./createSender.js" | ||
import { Message } from "./notificationsCb.js" | ||
|
||
export async function createStashCall( | ||
address: string, | ||
cb: (value: Message) => void, | ||
): Promise<string> { | ||
const sender = createSender(address) | ||
|
||
const createStashCall = westend.Proxy.createPure({ | ||
proxyType: "Any", | ||
delay: 0, | ||
index: 0, | ||
}) | ||
.signed(signature({ sender: sender })) | ||
.sent() | ||
.dbgStatus("Creating Pure Proxy:") | ||
.inBlockEvents() | ||
.unhandleFailed() | ||
.pipe(<X>(...[events]: RunicArgs<X, [any[]]>) => { | ||
cb({ type: "loading" }) | ||
return Rune.resolve(events).map((events) => { | ||
cb({ type: "success" }) | ||
const eventNames = events.map((e) => | ||
`${e.event.type}:${e.event.value.type}` | ||
) | ||
cb({ type: "info", events: eventNames }) | ||
|
||
return events | ||
.map((e) => e.event) | ||
.filter((event): event is RuntimeEvent.Proxy => | ||
event.type === "Proxy" | ||
) | ||
.map((e) => e.value) | ||
.filter((event): event is PalletProxyEvent.PureCreated => | ||
event.type === "PureCreated" | ||
) | ||
}) | ||
}) | ||
// TODO typing is broken in capi | ||
.map((events: { pure: unknown }[]) => events.map(({ pure }) => pure)) | ||
.access(0) | ||
|
||
const stashBytes = (await createStashCall.run()) as Uint8Array | ||
const stashAddress = ss58.encode( | ||
await westend.addressPrefix().run(), | ||
stashBytes, | ||
) | ||
return stashAddress | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { westend } from "@capi/westend" | ||
import { Rune, RunicArgs } from "capi" | ||
import { signature } from "capi/patterns/signature/polkadot" | ||
import { defaultAccount } from "../signals/accounts.js" | ||
import { toBalance } from "../util/balance.js" | ||
import { toMultiAddressIdRune } from "../util/capi-helpers.js" | ||
import { createSender } from "./createSender.js" | ||
import { Message } from "./notificationsCb.js" | ||
|
||
export async function fundStash( | ||
amount: number, | ||
dest: string, | ||
cb: (value: Message) => void, | ||
) { | ||
const sender = createSender(defaultAccount.value!.address) | ||
|
||
const fundStashCall = westend.Balances | ||
.transfer({ | ||
value: toBalance(amount), | ||
dest: toMultiAddressIdRune(dest), | ||
}) | ||
.signed(signature({ sender })) | ||
.sent() | ||
.dbgStatus("Transfer:") | ||
.inBlockEvents() | ||
.unhandleFailed() | ||
.pipe(<X>(...[events]: RunicArgs<X, [any[]]>) => { | ||
cb({ type: "loading" }) | ||
return Rune.resolve(events).map((events) => { | ||
cb({ type: "success" }) | ||
const eventNames = events.map((e) => | ||
`${e.event.type}:${e.event.value.type}` | ||
) | ||
cb({ type: "info", events: eventNames }) | ||
}) | ||
}) | ||
|
||
await fundStashCall.run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Westend, westend } from "@capi/westend" | ||
import { ss58 } from "capi" | ||
import { MultisigRune } from "capi/patterns/multisig" | ||
import { toPubKey } from "../util/capi-helpers.js" | ||
|
||
export async function getMultisigAddress(members: string[], threshold: number) { | ||
const signatories = members.map(toPubKey) | ||
const multisig: MultisigRune<Westend, never> = MultisigRune.from(westend, { | ||
signatories, | ||
threshold, | ||
}) | ||
const [prefix, accountId] = await Promise.all([ | ||
westend.addressPrefix().run(), | ||
multisig.accountId.run(), | ||
]) | ||
|
||
return ss58.encode(prefix, accountId) | ||
} |
Oops, something went wrong.