Transaction Construction
This page will discuss the transaction format in Acala and how to create, sign, and broadcast transactions.
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. Always refer to each tool's documentation when integrating.

Transaction Format

Polkadot has some basic transaction information that is common to all transactions.
  • Address: The SS58-encoded address of the sending account.
  • Block Hash: The hash of the checkpoint block.
  • Block Number: The number of the checkpoint block.
  • Genesis Hash: The genesis hash of the chain.
  • Metadata: The SCALE-encoded metadata for the runtime when submitted.
  • Nonce: The nonce for this transaction.*
  • Spec Version: The current spec version for the runtime.
  • Transaction Version: The current version for transaction format.
  • Tip: Optional, the tip to increase transaction priority.
  • Era Period: Optional, the number of blocks after the checkpoint for which a transaction is valid. If zero, the transaction is immortal.
*The nonce queried from the System module does not account for pending transactions. You must track and increment the nonce manually if you want to submit multiple valid transactions at the same time.
Each transaction will have its own (or no) parameters to add. For example, the transferKeepAlive function from the Balances pallet will take:
  • dest: Destination address
  • #[compact] value: Number of tokens (compact encoding)
Once you have all the necessary information, you will need to:
  1. 1.
    Construct an unsigned transaction.
  2. 2.
    Create a signing payload.
  3. 3.
    Sign the payload.
  4. 4.
    Serialize the signed payload into a transaction.
  5. 5.
    Submit the serialized transaction.
Parity provides the following tools to help perform these steps.

Acala JS

[TODO]

Tx Wrapper

If you do not want to use the CLI for signing operations, Parity provides an SDK called TxWrapper to generate and sign transactions offline. See the examples for a guide.
Import a private key
1
import { importPrivateKey } from '@substrate/txwrapper';
2
3
const keypair = importPrivateKey(“pulp gaze fuel ... mercy inherit equal”);
Copied!
Derive an address from a public key
1
import { deriveAddress } from '@substrate/txwrapper';
2
3
// Public key, can be either hex string, or Uint8Array
4
const publicKey = “0x2ca17d26ca376087dc30ed52deb74bf0f64aca96fe78b05ec3e720a72adb1235”;
5
const address = deriveAddress(publicKey);
Copied!
Construct a transaction offline
1
import { methods } from "@substrate/txwrapper";
2
3
const unsigned = methods.balances.transferKeepAlive(
4
{
5
dest: "15vrtLsCQFG3qRYUcaEeeEih4JwepocNJHkpsrqojqnZPc2y",
6
value: 500000000000,
7
},
8
{
9
address: "121X5bEgTZcGQx5NZjwuTjqqKoiG8B2wEAvrUFjuw24ZGZf2",
10
blockHash: "0x1fc7493f3c1e9ac758a183839906475f8363aafb1b1d3e910fe16fab4ae1b582",
11
blockNumber: 4302222,
12
genesisHash: "0xe3777fa922cafbff200cadeaea1a76bd7898ad5b89f7848999058b50e715f636",
13
metadataRpc, // must import from client RPC call state_getMetadata
14
nonce: 2,
15
specVersion: 1019,
16
tip: 0,
17
eraPeriod: 64, // number of blocks from checkpoint that transaction is valid
18
transactionVersion: 1,
19
},
20
{
21
metadataRpc,
22
registry, // Type registry
23
}
24
);
Copied!
Construct a signing payload
1
import { methods, createSigningPayload } from '@substrate/txwrapper';
2
3
// See "Construct a transaction offline" for "{...}"
4
const unsigned = methods.balances.transferKeepAlive({...}, {...}, {...});
5
const signingPayload = createSigningPayload(unsigned, { registry });
Copied!
Serialize a signed transaction
1
import { createSignedTx } from "@substrate/txwrapper";
2
3
// Example code, replace `signWithAlice` with actual remote signer.
4
// An example is given here:
5
// https://github.com/paritytech/txwrapper/blob/630c38d/examples/index.ts#L50-L68
6
const signature = await signWithAlice(signingPayload);
7
const signedTx = createSignedTx(unsigned, signature, { metadataRpc, registry });
Copied!
Decode payload types
You may want to decode payloads to verify their contents prior to submission.
1
import { decode } from "@substrate/txwrapper";
2
3
// Decode an unsigned tx
4
const txInfo = decode(unsigned, { metadataRpc, registry });
5
6
// Decode a signing payload
7
const txInfo = decode(signingPayload, { metadataRpc, registry });
8
9
// Decode a signed tx
10
const txInfo = decode(signedTx, { metadataRpc, registry });
Copied!
Check a transaction's hash
1
import { getTxHash } from ‘@substrate/txwrapper’;
2
const txHash = getTxHash(signedTx);
Copied!

Submitting a Signed Payload

There are several ways to submit a signed payload:
  1. 1.
    Signer CLI (yarn run:signer submit --tx <signed-transaction> --ws <endpoint>)
  2. 3.
    RPC 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.

Notes

Some addresses to use in the examples. See Subkey documentation.
1
$ subkey --network polkadot generate
2
Secret phrase `pulp gaze fuel ... mercy inherit equal` is account:
3
Secret seed: 0x57450b3e09ba4598 ... ... ... ... ... ... ... .. 219756eeba80bb16
4
Public key (hex): 0x2ca17d26ca376087dc30ed52deb74bf0f64aca96fe78b05ec3e720a72adb1235
5
Account ID: 0x2ca17d26ca376087dc30ed52deb74bf0f64aca96fe78b05ec3e720a72adb1235
6
SS58 Address: 121X5bEgTZcGQx5NZjwuTjqqKoiG8B2wEAvrUFjuw24ZGZf2
7
8
$ subkey --network polkadot generate
9
Secret phrase `exercise auction soft ... obey control easily` is account:
10
Secret seed: 0x5f4bbb9fbb69261a ... ... ... ... ... ... ... .. 4691ed7d1130fbbd
11
Public key (hex): 0xda04de6cd781c98acf0693dfb97c11011938ad22fcc476ed0089ac5aec3fe243
12
Account ID: 0xda04de6cd781c98acf0693dfb97c11011938ad22fcc476ed0089ac5aec3fe243
13
SS58 Address: 15vrtLsCQFG3qRYUcaEeeEih4JwepocNJHkpsrqojqnZPc2y
Copied!
Last modified 1yr ago