Skip to content

Commit 9cc17d8

Browse files
committed
docs: tx-helper
1 parent 6ca75f4 commit 9cc17d8

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-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: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,41 @@ client.supervise()
211211

212212
### tx-helper
213213

214-
TODO: write usage guide
214+
The tx-helper package allows you to easily sign transactions. You just need to calldata and an implementation of the
215+
`@polkadot-api/signer` interface.
216+
217+
#### PJS Example Implementation
218+
219+
```ts
220+
import { getLightClientProvider } from '@substrate/light-client-extension-helpers/web-page';
221+
import { connectInjectedExtension } from '@polkadot-api/pjs-signer';
222+
import { fromHex, toHex } from '@polkadot-api/utils';
223+
import { createTx } from '@substrate/light-client-extension-helpers/tx-helper'; // 👈 create-tx import
224+
225+
const CHANNEL_ID = "..."
226+
const lightClientProvider = await getLightClientProvider(CHANNEL_ID)
227+
228+
const createTx = async (chainId: string, from: string, callData: string) => {
229+
const chains = Object.values(lightClientProvider.getChains());
230+
const chain = chains.find(({ genesisHash }) => genesisHash === chainId);
231+
232+
if (!chain) {
233+
throw new Error('unknown chain');
234+
}
235+
236+
const injectedExt = await connectInjectedExtension('polkadot-js');
237+
238+
const account = injectedExt.getAccounts()
239+
.find((account) => toHex(account.polkadotSigner.publicKey) === from);
240+
241+
if (!account) {
242+
throw new Error('no account');
243+
}
244+
245+
const signer = account.polkadotSigner; // 👈 @polkadot-api/signer implementation
246+
247+
const tx = await createTx(chain.connect)({ callData: fromHex(callData), signer });
248+
249+
return toHex(tx);
250+
}
251+
```

0 commit comments

Comments
 (0)