Skip to content

Commit 60922d2

Browse files
committed
make the reference commands into mini-tutorials
1 parent fb0e949 commit 60922d2

File tree

2 files changed

+262
-88
lines changed

2 files changed

+262
-88
lines changed

develop/toolkit/integrations/transaction-construction.md

Lines changed: 131 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: Understand how to construct, sign, and broadcast transactions in th
77

88
## Introduction
99

10-
This page will discuss the transaction format in Polkadot and how to create, sign, and broadcast transactions. Like the other pages in this guide, this page demonstrates some of the available tools.
10+
This page will discuss the transaction format in Polkadot and how to create, sign, and broadcast transactions as well as highlight some of commands and tools available for integrators.
1111

1212
**Always refer to each tool's documentation when integrating.**
1313

@@ -77,45 +77,104 @@ The typical transaction workflow is as follows:
7777
4. Serialize the signed payload into a transaction.
7878
5. Submit the serialized transaction.
7979

80-
Parity provides the following tools to help perform these steps.
80+
Parity provides several tools to help perform these steps.
8181

8282
## Polkadot-JS Tools
8383

8484
[Polkadot-JS Tools](https://github.com/polkadot-js/tools){target=\_blank} contains a set of command line tools for
85-
interacting with a Substrate client, including one called "Signer CLI" to create, sign, and
85+
interacting with a Polkadot SDK client, including one called "Signer CLI" to create, sign, and
8686
broadcast transactions.
8787

88+
!!!note "Generating SS58 Addresses"
89+
For the examples on this page, it may be helpful to generate new accounts for signing purposes.
90+
For this, you can use the [Subkey](/polkadot-protocol/basics/accounts/#using-subkey){target=\_blank} CLI tool.
91+
```bash
92+
$ subkey --network polkadot generate
93+
Secret phrase `pulp gaze fuel ... mercy inherit equal` is account:
94+
Secret seed: 0x57450b3e09ba4598 ... ... ... ... ... ... ... .. 219756eeba80bb16
95+
Public key (hex): 0x2ca17d26ca376087dc30ed52deb74bf0f64aca96fe78b05ec3e720a72adb1235
96+
Account ID: 0x2ca17d26ca376087dc30ed52deb74bf0f64aca96fe78b05ec3e720a72adb1235
97+
SS58 Address: 121X5bEgTZcGQx5NZjwuTjqqKoiG8B2wEAvrUFjuw24ZGZf2
98+
99+
$ subkey --network polkadot generate
100+
Secret phrase `exercise auction soft ... obey control easily` is account:
101+
Secret seed: 0x5f4bbb9fbb69261a ... ... ... ... ... ... ... .. 4691ed7d1130fbbd
102+
Public key (hex): 0xda04de6cd781c98acf0693dfb97c11011938ad22fcc476ed0089ac5aec3fe243
103+
Account ID: 0xda04de6cd781c98acf0693dfb97c11011938ad22fcc476ed0089ac5aec3fe243
104+
SS58 Address: 15vrtLsCQFG3qRYUcaEeeEih4JwepocNJHkpsrqojqnZPc2y
105+
```
106+
88107
This example will use the `signer submit` command, which will create and submit the transaction. The
89108
`signer sendOffline` command has the exact same API, but will not broadcast the transaction.
90109
`submit` and `sendOffline` must be connected to a node to fetch the current metadata and construct a
91-
valid transaction. Their API has the format:
110+
valid transaction.
111+
112+
Let's start by installing the Signer CLI:
92113

93114
```bash
94-
yarn run:signer <submit|sendOffline> --account <from-account-ss58> --ws <endpoint> <module.method> [param1] [...] [paramX]
115+
npm install -g @polkadot/signer-cli
95116
```
96117

97-
Signing:
118+
And spinning up a network to test our transactions:
119+
120+
```
121+
npx @acala-network/chopsticks --config=polkadot -p 9944
122+
```
123+
We should get a Polkadot network running on port 9944.
124+
125+
> If you are new to chopsticks, you can checkout the [chopsticks guide](/tutorials/polkadot-sdk/testing/fork-live-chains/){target=\_blank}.
126+
127+
To create a transaction, you need to connect to a chain, enabling the creation of a transaction using the chain's metadata.
128+
Here is the format for `submit` or `sendOffline`:
98129

99130
```bash
100-
yarn run:signer sign --account <from-account-ss58> --seed <seed> --type <sr25519|ed25519> <payload>
131+
polkadot-js-signer <submit|sendOffline> --account <from-account-ss58> --ws <endpoint> <module.method> [param1] [...] [paramX]
101132
```
102133

103-
For example, let's send 0.5 DOT from `121X5bEgTZcGQx5NZjwuTjqqKoiG8B2wEAvrUFjuw24ZGZf2` to
104-
`15vrtLsCQFG3qRYUcaEeeEih4JwepocNJHkpsrqojqnZPc2y`.
134+
And for signing a transaction:
135+
136+
```bash
137+
polkadot-js-signer sign --account <from-account-ss58> --seed <seed> --type <sr25519|ed25519> <payload>
138+
```
139+
140+
### Creating a Transaction, Signing, and Submitting
141+
142+
For example, let's send 0.5 DOT from `121X5bEgTZcGQx5NZjwuTjqqKoiG8B2wEAvrUFjuw24ZGZf2` to `15vrtLsCQFG3qRYUcaEeeEih4JwepocNJHkpsrqojqnZPc2y` using `polkadot-js-signer`.
143+
144+
First we call submit to create the transaction and give us the payload to sign:
105145

106146
```bash
107-
yarn run:signer submit --account 121X5bEgTZcGQx5NZjwuTjqqKoiG8B2wEAvrUFjuw24ZGZf2 --ws ws://127.0.0.1:9944 balances.transferKeepAlive 15vrtLsCQFG3qRYUcaEeeEih4JwepocNJHkpsrqojqnZPc2y 5000000000
147+
polkadot-js-signer submit --account 121X5bEgTZcGQx5NZjwuTjqqKoiG8B2wEAvrUFjuw24ZGZf2 --ws ws://127.0.0.1:9944 balances.transferKeepAlive 15vrtLsCQFG3qRYUcaEeeEih4JwepocNJHkpsrqojqnZPc2y 5000000000
108148
```
109149

110-
This will return a payload to sign and an input waiting for a signature. Take this payload and use
111-
your normal signing environment (e.g. air gapped machine, VM, etc.). Sign the payload:
150+
This will return a payload to sign and an input waiting for a signature. Something like this:
151+
152+
```
153+
Payload: 0x040300ff4a83f1...a8239139ff3ff7c3f6
154+
Signature>
155+
```
156+
157+
Take this payload and use your normal signing environment (e.g. air gapped machine, VM, etc.). In a separate tab in your terminal, sign the payload:
112158

113159
```bash
114-
yarn run:signer sign --account 121X5bEgTZcGQx5NZjwuTjqqKoiG8B2wEAvrUFjuw24ZGZf2 --seed "pulp gaze fuel ... mercy inherit equal" --type sr25519 0x040300ff4a83f1...a8239139ff3ff7c3f6
160+
polkadot-js-signer sign --account 121X5bEgTZcGQx5NZjwuTjqqKoiG8B2wEAvrUFjuw24ZGZf2 --seed "pulp gaze fuel ... mercy inherit equal" --type sr25519 0x040300ff4a83f1...a8239139ff3ff7c3f6
161+
```
162+
163+
This will output the signature of the transaction:
164+
```
165+
Signature: 0xe6facf194a8e...413ce3155c2d1240b
115166
```
116167

117-
Save the output and bring it to the machine that you will broadcast from, enter it into `submit`'s
118-
signature field, and send the transaction (or just return the serialized transaction if using `sendOffline`).
168+
Paste this signature into the `submit`'s signature field, and send the transaction (or just return the serialized transaction if using `sendOffline`).
169+
170+
By default, submit will create a mortal extrinsic with a lifetime of 50 blocks.
171+
Assuming a six-second block time, you will have five minutes to go offline, sign the transaction, paste the signature, and submit the signed transaction.
172+
173+
!!!note "Submitting Pre-Signed Transaction"
174+
You can also submit pre-signed transactions, e.g. generated using the `sendOffline` command.
175+
```
176+
polkadot-js-signer submit --tx <signedTransaction> --ws <endpoint>
177+
```
119178

120179
## TxWrapper
121180

@@ -126,6 +185,61 @@ Substrate-based chains will have their own `txwrapper-{chain}` implementations.
126185
[examples](https://github.com/paritytech/txwrapper-core/blob/main/packages/txwrapper-examples/README.md){target=\_blank}
127186
for a guide.
128187

188+
### Creating a Transaction, Signing, and Submitting
189+
190+
We will need a network to run test the transaction on.
191+
Let's use [chopsticks](/tutorials/polkadot-sdk/testing/fork-live-chains/){target=\_blank} for this:
192+
193+
```
194+
npx @acala-network/chopsticks --config=polkadot -p 9944
195+
```
196+
We should get a Polkadot network running on port 9944.
197+
198+
Next we will use the [`TxWrapper example script`](https://github.com/paritytech/txwrapper-core/blob/main/packages/txwrapper-examples/polkadot/src/polkadot.ts){target=\_blank} to create and sign transactions.
199+
200+
For this we will need the [`TxWrapper`](https://github.com/paritytech/txwrapper-core){target=\_blank} library. Let's clone [`TxWrapper`](https://github.com/paritytech/txwrapper-core){target=\_blank}:
201+
202+
```
203+
git clone https://github.com/paritytech/txwrapper-core
204+
cd txwrapper-core
205+
yarn install && yarn build
206+
cd packages/txwrapper-examples
207+
```
208+
209+
Build and run the [`TxWrapper Polkadot example script`](https://github.com/paritytech/txwrapper-core/blob/main/packages/txwrapper-examples/polkadot/src/polkadot.ts){target=\_blank}:
210+
```
211+
yarn run build
212+
yarn run polkadot
213+
```
214+
215+
You will get output like the following:
216+
```
217+
Alice's SS58-Encoded Address: 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
218+
219+
Decoded Transaction
220+
To: 14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3
221+
Amount: "10000000000"
222+
223+
Payload to Sign: 0xa40503008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480700e40b54028500000000b1590f001a00000091b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3843125cd049613a7edf44b55a01efbabffcd1b962068a82070cff82314b67bbc00
224+
225+
Decoded Transaction
226+
To: 14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3
227+
Amount: "10000000000"
228+
229+
Signature: 0x01ae703e667b3b444e3613a5f06d16bedf2460a18e52075b47c6442ebc1d316917c6d12e3aa7bbd2f8db76cc859b43134cecb4495613d8d504901de776d0642b82
230+
231+
Transaction to Submit: 0x45028400d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01ae703e667b3b444e3613a5f06d16bedf2460a18e52075b47c6442ebc1d316917c6d12e3aa7bbd2f8db76cc859b43134cecb4495613d8d504901de776d0642b8285000000000503008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480700e40b5402
232+
233+
Expected Tx Hash: 0xa12126a095b38f0c70331be78743329a851e33839f9b2f93a7ecc34541507891
234+
Actual Tx Hash: 0xa12126a095b38f0c70331be78743329a851e33839f9b2f93a7ecc34541507891
235+
236+
Decoded Transaction
237+
To: 14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3
238+
Amount: "10000000000"
239+
```
240+
241+
The [`TxWrapper example script`](https://github.com/paritytech/txwrapper-core/blob/main/packages/txwrapper-examples/polkadot/src/polkadot.ts){target=\_blank} includes many of the reference examples below:
242+
129243
**Import a private key**
130244

131245
```ts
@@ -219,33 +333,6 @@ import { getTxHash } from ‘@substrate/txwrapper-polkadot’;
219333
const txHash = getTxHash(signedTx);
220334
```
221335

222-
## Submitting a Signed Payload
223-
224-
There are several ways to submit a signed payload:
225-
226-
1. Signer CLI (`yarn run:signer submit --tx <signed-transaction> --ws <endpoint>`)
227-
1. [Substrate API Sidecar](https://docs.polkadot.com/develop/toolkit/api-libraries/sidecar/#sidecar-api){target=\_blank}
228-
1. [RPC](https://docs.polkadot.com/develop/toolkit/api-libraries/){target=\_blank} with `author_submitExtrinsic` or
229-
`author_submitAndWatchExtrinsic`, the latter of which will subscribe you to events to be notified
230-
as a transaction gets validated and included in the chain.
336+
## Additional Libraries for Submitting a Transaction
231337

232-
## Example Addresses
233-
234-
Some addresses to use in the examples. See
235-
[Subkey documentation](https://docs.polkadot.com/polkadot-protocol/basics/accounts/#using-subkey){target=\_blank}.
236-
237-
```bash
238-
$ subkey --network polkadot generate
239-
Secret phrase `pulp gaze fuel ... mercy inherit equal` is account:
240-
Secret seed: 0x57450b3e09ba4598 ... ... ... ... ... ... ... .. 219756eeba80bb16
241-
Public key (hex): 0x2ca17d26ca376087dc30ed52deb74bf0f64aca96fe78b05ec3e720a72adb1235
242-
Account ID: 0x2ca17d26ca376087dc30ed52deb74bf0f64aca96fe78b05ec3e720a72adb1235
243-
SS58 Address: 121X5bEgTZcGQx5NZjwuTjqqKoiG8B2wEAvrUFjuw24ZGZf2
244-
245-
$ subkey --network polkadot generate
246-
Secret phrase `exercise auction soft ... obey control easily` is account:
247-
Secret seed: 0x5f4bbb9fbb69261a ... ... ... ... ... ... ... .. 4691ed7d1130fbbd
248-
Public key (hex): 0xda04de6cd781c98acf0693dfb97c11011938ad22fcc476ed0089ac5aec3fe243
249-
Account ID: 0xda04de6cd781c98acf0693dfb97c11011938ad22fcc476ed0089ac5aec3fe243
250-
SS58 Address: 15vrtLsCQFG3qRYUcaEeeEih4JwepocNJHkpsrqojqnZPc2y
251-
```
338+
Other than Polkadot JS Tools and TXWrapper, there are several other libraries that can also be used to submit a signed payload such as the [Sidecar API](/develop/toolkit/api-libraries/sidecar/#sidecar-api){target=\_blank} or using RPC calls with `author_submitExtrinsic` or `author_submitAndWatchExtrinsic`, the latter of which will subscribe you to events to be notified as a transaction gets validated and included in the chain. You can see all the available libraries in the [API Libraries](/develop/toolkit/api-libraries/) section of the Polkadot Docs.

0 commit comments

Comments
 (0)