Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
refactor: decouple capi from presentation layer (#223)
Browse files Browse the repository at this point in the history
Co-authored-by: Josep M Sobrepere <jm.sobrepere@gmail.com>
  • Loading branch information
statictype and josepot authored Jul 5, 2023
1 parent b91eba6 commit 33bce54
Show file tree
Hide file tree
Showing 20 changed files with 456 additions and 319 deletions.
9 changes: 0 additions & 9 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
{
"[css]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[html]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[yaml]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"css.lint.unknownAtRules": "ignore",
"editor.defaultFormatter": "dprint.dprint",
"editor.formatOnSave": true,
Expand Down
94 changes: 69 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
"react-router-dom": "^6.12.1",
"react-tooltip": "^5.13.2",
"tailwind-merge": "^1.13.1",
"uuid": "^9.0.0",
"vite-tsconfig-paths": "^4.2.0"
},
"devDependencies": {
"@babel/core": ">=7.12.10 <8.0.0",
"@babel/plugin-transform-react-jsx": "^7.22.5",
"@preact/preset-vite": "^2.5.0",
"@tailwindcss/forms": "^0.5.3",
"@types/uuid": "^9.0.2",
"autoprefixer": "^10.4.14",
"clsx": "^1.2.1",
"postcss": "^8.4.24",
Expand Down
40 changes: 40 additions & 0 deletions www/src/api/cancel.ts
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()
}
10 changes: 10 additions & 0 deletions www/src/api/createSender.ts
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)
}
53 changes: 53 additions & 0 deletions www/src/api/createStashCall.ts
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
}
39 changes: 39 additions & 0 deletions www/src/api/fundStash.ts
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()
}
18 changes: 18 additions & 0 deletions www/src/api/getMultisigAddress.ts
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)
}
Loading

0 comments on commit 33bce54

Please sign in to comment.