Skip to content

Commit a6c74cb

Browse files
authored
docs: tx-helper (#2341)
1 parent 444503f commit a6c74cb

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

.changeset/small-shirts-press.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@substrate/light-client-extension-helpers": patch
3+
---
4+
5+
add docs for tx-helper

packages/light-client-extension-helpers/README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,45 @@ client.supervise()
225225

226226
### tx-helper
227227

228-
TODO: write usage guide
228+
The tx-helper package allows you to easily sign transactions. You just need the calldata and an implementation of the
229+
`@polkadot-api/signer` interface.
230+
231+
#### PJS Example Implementation
232+
233+
```ts
234+
import { getLightClientProvider } from "@substrate/light-client-extension-helpers/web-page"
235+
import { connectInjectedExtension } from "@polkadot-api/pjs-signer"
236+
import { fromHex, toHex } from "@polkadot-api/utils"
237+
import { createTx } from "@substrate/light-client-extension-helpers/tx-helper" // 👈 create-tx import
238+
239+
const CHANNEL_ID = "..."
240+
const lightClientProvider = await getLightClientProvider(CHANNEL_ID)
241+
242+
const createTx = async (chainId: string, from: string, callData: string) => {
243+
const chains = Object.values(lightClientProvider.getChains())
244+
const chain = chains.find(({ genesisHash }) => genesisHash === chainId)
245+
246+
if (!chain) {
247+
throw new Error("unknown chain")
248+
}
249+
250+
const injectedExt = await connectInjectedExtension("polkadot-js")
251+
252+
const account = injectedExt
253+
.getAccounts()
254+
.find((account) => toHex(account.polkadotSigner.publicKey) === from)
255+
256+
if (!account) {
257+
throw new Error("no account")
258+
}
259+
260+
const signer = account.polkadotSigner // 👈 @polkadot-api/signer implementation
261+
262+
const tx = await createTx(chain.connect)({
263+
callData: fromHex(callData),
264+
signer,
265+
})
266+
267+
return toHex(tx)
268+
}
269+
```

0 commit comments

Comments
 (0)