Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: tx-helper #2341

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/small-shirts-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@substrate/light-client-extension-helpers": patch
---

add docs for tx-helper
43 changes: 42 additions & 1 deletion packages/light-client-extension-helpers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,45 @@ client.supervise()

### tx-helper

TODO: write usage guide
The tx-helper package allows you to easily sign transactions. You just need to calldata and an implementation of the
ryanleecode marked this conversation as resolved.
Show resolved Hide resolved
`@polkadot-api/signer` interface.

#### PJS Example Implementation

```ts
import { getLightClientProvider } from "@substrate/light-client-extension-helpers/web-page"
import { connectInjectedExtension } from "@polkadot-api/pjs-signer"
import { fromHex, toHex } from "@polkadot-api/utils"
import { createTx } from "@substrate/light-client-extension-helpers/tx-helper" // 👈 create-tx import

const CHANNEL_ID = "..."
const lightClientProvider = await getLightClientProvider(CHANNEL_ID)

const createTx = async (chainId: string, from: string, callData: string) => {
const chains = Object.values(lightClientProvider.getChains())
const chain = chains.find(({ genesisHash }) => genesisHash === chainId)

if (!chain) {
throw new Error("unknown chain")
}

const injectedExt = await connectInjectedExtension("polkadot-js")

const account = injectedExt
.getAccounts()
.find((account) => toHex(account.polkadotSigner.publicKey) === from)

if (!account) {
throw new Error("no account")
}

const signer = account.polkadotSigner // 👈 @polkadot-api/signer implementation

const tx = await createTx(chain.connect)({
callData: fromHex(callData),
signer,
})

return toHex(tx)
}
```
Loading