Skip to content

Commit 102cab5

Browse files
nhussein11eshaben
andauthored
Add send txs with sdks (#1312)
* fix: send tx with rpc * docs: update SDK transaction examples with prerequisites and ensure proper formatting * formatting edits * prettier on code files --------- Co-authored-by: Erin Shaben <[email protected]>
1 parent b91301c commit 102cab5

File tree

12 files changed

+746
-1
lines changed

12 files changed

+746
-1
lines changed
Lines changed: 302 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,302 @@
1-
TODO
1+
---
2+
title: Send Transactions with SDKs
3+
description: Learn how to construct, sign, and submit transactions using PAPI, Polkadot.js, Dedot, Python Substrate Interface, and Subxt.
4+
categories: Chain Interactions
5+
---
6+
7+
# Send Transactions with SDKs
8+
9+
## Introduction
10+
11+
Sending transactions on Polkadot SDK-based blockchains involves constructing an extrinsic (transaction), signing it with your account's private key, and submitting it to the network. Each SDK provides different methods for transaction construction, signing, and submission.
12+
13+
This guide demonstrates how to send transactions using five popular SDKs:
14+
15+
- **[Polkadot API (PAPI)](/reference/tools/papi/){target=\_blank}**: Modern TypeScript library with type-safe APIs
16+
- **[Polkadot.js API](/reference/tools/polkadot-js-api/){target=\_blank}**: Comprehensive JavaScript library (maintenance mode)
17+
- **[Dedot](/reference/tools/dedot/){target=\_blank}**: Lightweight TypeScript library optimized for performance
18+
- **[Python Substrate Interface](/reference/tools/py-substrate-interface/){target=\_blank}**: Python library for Substrate chains
19+
- **[Subxt](/reference/tools/subxt/){target=\_blank}**: Rust library with compile-time type safety
20+
21+
Select your preferred SDK below to see complete, runnable examples that send balance transfer transactions on Polkadot Hub.
22+
23+
## Prerequisites
24+
25+
- Access to a Polkadot-SDK-compatible wallet, with its mnemonic phrase or private key.
26+
- A funded account on Polkadot Hub, with some testnet tokens. You can use the [Polkadot Faucet](https://faucet.polkadot.io/?parachain=1111){target=\_blank} to obtain test tokens.
27+
28+
## Send Transactions
29+
30+
!!! warning
31+
Never share your mnemonic phrase or private keys. The examples below use mnemonics for demonstration purposes only. In production, use secure key management solutions.
32+
33+
=== "PAPI"
34+
35+
**Prerequisites**
36+
37+
- [Node.js](https://nodejs.org/){target=\_blank} v18 or higher
38+
- npm, pnpm, or yarn package manager
39+
40+
**Environment Setup**
41+
42+
1. Create and initialize a new project:
43+
44+
```bash
45+
mkdir papi-send-tx-example && cd papi-send-tx-example && \
46+
npm init -y && npm pkg set type=module
47+
```
48+
49+
2. Install dependencies:
50+
51+
```bash
52+
npm install polkadot-api @polkadot/util-crypto @polkadot/keyring && \
53+
npm install --save-dev @types/node tsx typescript
54+
```
55+
56+
3. Generate types for Polkadot Hub TestNet:
57+
58+
```bash
59+
npx papi add polkadotTestNet -w wss://asset-hub-paseo.dotters.network
60+
```
61+
62+
**Send Balance Transfer**
63+
64+
The following example constructs, signs, and submits a balance transfer transaction.
65+
66+
Create a file named `send-transfer.ts` and add the following code to it:
67+
68+
```typescript title="send-transfer.ts"
69+
--8<-- "code/chain-interactions/send-transactions/with-sdks/papi/send-transfer.ts"
70+
```
71+
72+
!!! note
73+
Ensure to replace `INSERT_WS_ENDPOINT` with the proper WebSocket endpoint, `INSERT_SENDER_MNEMONIC` with your account's mnemonic phrase, and `INSERT_DEST_ADDRESS` with the recipient address. For this example, you can use Polkadot Hub (`wss://polkadot-asset-hub-rpc.polkadot.io`).
74+
75+
Run the script:
76+
77+
```bash
78+
npx tsx send-transfer.ts
79+
```
80+
81+
You should see output similar to:
82+
83+
--8<-- 'code/chain-interactions/send-transactions/with-sdks/papi/send-transfer-ts.html'
84+
85+
=== "Polkadot.js"
86+
87+
!!! warning "Maintenance Mode Only"
88+
The Polkadot.js API is no longer actively developed. New projects should use [PAPI](/reference/tools/papi/){target=\_blank} or [Dedot](/reference/tools/dedot/){target=\_blank} as actively maintained alternatives.
89+
90+
**Prerequisites**
91+
92+
- [Node.js](https://nodejs.org/){target=\_blank} v18 or higher
93+
- npm, pnpm, or yarn package manager
94+
95+
**Environment Setup**
96+
97+
1. Create and initialize a new project:
98+
99+
```bash
100+
mkdir pjs-send-tx-example && cd pjs-send-tx-example && \
101+
npm init -y && npm pkg set type=module
102+
```
103+
104+
2. Install dependencies:
105+
106+
```bash
107+
npm install @polkadot/api @polkadot/keyring @polkadot/util-crypto
108+
```
109+
110+
**Send Balance Transfer**
111+
112+
The following example constructs, signs, and submits a balance transfer transaction.
113+
114+
Create a file named `send-transfer.js` and add the following code:
115+
116+
```javascript title="send-transfer.js"
117+
--8<-- "code/chain-interactions/send-transactions/with-sdks/pjs/send-transfer.js"
118+
```
119+
120+
!!! note
121+
Ensure to replace `INSERT_WS_ENDPOINT` with the proper WebSocket endpoint, `INSERT_SENDER_MNEMONIC` with your account's mnemonic phrase, and `INSERT_DEST_ADDRESS` with the recipient address. For this example, you can use Polkadot Hub (`wss://polkadot-asset-hub-rpc.polkadot.io`).
122+
123+
Run the script:
124+
125+
```bash
126+
node send-transfer.js
127+
```
128+
129+
You should see output similar to:
130+
131+
--8<-- 'code/chain-interactions/send-transactions/with-sdks/pjs/send-transfer-js.html'
132+
133+
=== "Dedot"
134+
135+
**Prerequisites**
136+
137+
- [Node.js](https://nodejs.org/){target=\_blank} v18 or higher
138+
- npm, pnpm, or yarn package manager
139+
140+
**Environment Setup**
141+
142+
1. Create and initialize a new project:
143+
144+
```bash
145+
mkdir dedot-send-tx-example && cd dedot-send-tx-example && \
146+
npm init -y && npm pkg set type=module
147+
```
148+
149+
2. Install dependencies:
150+
151+
```bash
152+
npm install dedot @polkadot/keyring @polkadot/util-crypto && \
153+
npm install --save-dev @dedot/chaintypes @types/node tsx typescript
154+
```
155+
156+
**Send Balance Transfer**
157+
158+
The following example constructs, signs, and submits a balance transfer transaction.
159+
160+
Create a file named `send-transfer.ts` and add the following code to it:
161+
162+
```typescript title="send-transfer.ts"
163+
--8<-- "code/chain-interactions/send-transactions/with-sdks/dedot/send-transfer.ts"
164+
```
165+
166+
!!! note
167+
Ensure to replace `INSERT_WS_ENDPOINT` with the proper WebSocket endpoint, `INSERT_SENDER_MNEMONIC` with your account's mnemonic phrase, and `INSERT_DEST_ADDRESS` with the recipient address. For this example, you can use Polkadot Hub (`wss://polkadot-asset-hub-rpc.polkadot.io`).
168+
169+
Run the script:
170+
171+
```bash
172+
npx tsx send-transfer.ts
173+
```
174+
175+
You should see output similar to:
176+
177+
--8<-- 'code/chain-interactions/send-transactions/with-sdks/dedot/send-transfer-ts.html'
178+
179+
=== "Python Substrate Interface"
180+
181+
**Prerequisites**
182+
183+
- [Python](https://www.python.org/){target=\_blank} 3.8 or higher
184+
- pip package manager
185+
186+
**Environment Setup**
187+
188+
1. Create a new project directory and set up a virtual environment:
189+
190+
```bash
191+
mkdir psi-send-tx-example && cd psi-send-tx-example && \
192+
python3 -m venv venv && source venv/bin/activate
193+
```
194+
195+
2. Install the substrate-interface package:
196+
197+
```bash
198+
pip install substrate-interface
199+
```
200+
201+
**Send Balance Transfer**
202+
203+
The following example constructs, signs, and submits a balance transfer transaction.
204+
205+
Create a file named `send_transfer.py` and add the following code to it:
206+
207+
```python title="send_transfer.py"
208+
--8<-- "code/chain-interactions/send-transactions/with-sdks/psi/send_transfer.py"
209+
```
210+
211+
!!! note
212+
Ensure to replace `INSERT_WS_ENDPOINT` with the proper WebSocket endpoint, `INSERT_SENDER_MNEMONIC` with your account's mnemonic phrase, and `INSERT_DEST_ADDRESS` with the recipient address. For this example, you can use Polkadot Hub (`wss://polkadot-asset-hub-rpc.polkadot.io`).
213+
214+
Run the script:
215+
216+
```bash
217+
python send_transfer.py
218+
```
219+
220+
You should see output similar to:
221+
222+
--8<-- 'code/chain-interactions/send-transactions/with-sdks/psi/send-transfer-py.html'
223+
224+
=== "Subxt"
225+
226+
[Subxt](/reference/tools/subxt/){target=\_blank} is a Rust library that provides compile-time type safety through code generation from chain metadata.
227+
228+
**Prerequisites**
229+
230+
- [Rust](https://rustup.rs/){target=\_blank} toolchain (latest stable)
231+
- Cargo package manager
232+
233+
**Environment Setup**
234+
235+
1. Create a new Rust project:
236+
237+
```bash
238+
cargo new subxt-send-tx-example && cd subxt-send-tx-example
239+
```
240+
241+
2. Install the Subxt CLI:
242+
243+
```bash
244+
cargo install subxt-cli@{{ dependencies.crates.subxt_cli.version }}
245+
```
246+
247+
3. Download the Polkadot Hub metadata:
248+
249+
```bash
250+
subxt metadata --url INSERT_WS_ENDPOINT -o asset_hub_metadata.scale
251+
```
252+
253+
4. Update `Cargo.toml` with the required dependencies:
254+
255+
```toml title="Cargo.toml"
256+
--8<-- "code/chain-interactions/send-transactions/with-sdks/subxt/Cargo.toml"
257+
```
258+
259+
**Send Balance Transfer**
260+
261+
The following example constructs, signs, and submits a balance transfer transaction.
262+
263+
Create a file at `src/bin/send_transfer.rs` and add the following code to it:
264+
265+
```rust title="src/bin/send_transfer.rs"
266+
--8<-- "code/chain-interactions/send-transactions/with-sdks/subxt/src/bin/send_transfer.rs"
267+
```
268+
269+
!!! note
270+
Ensure to replace `INSERT_WS_ENDPOINT` with the proper WebSocket endpoint, `INSERT_SENDER_MNEMONIC` with your account's mnemonic phrase, and `INSERT_DEST_ADDRESS` with the recipient address. For this example, you can use Polkadot Hub (`wss://polkadot-asset-hub-rpc.polkadot.io`).
271+
272+
Run the script:
273+
274+
```bash
275+
cargo run --bin send_transfer
276+
```
277+
278+
You should see output similar to:
279+
280+
--8<-- 'code/chain-interactions/send-transactions/with-sdks/subxt/send-transfer-rs.html'
281+
282+
## Where to Go Next
283+
284+
<div class="grid cards" markdown>
285+
286+
- <span class="badge guide">Guide</span> **Query On-Chain State**
287+
288+
---
289+
290+
Learn how to query storage and runtime data with the SDKs used in this guide.
291+
292+
[:octicons-arrow-right-24: Get Started](/chain-interactions/query-data/query-sdks/)
293+
294+
- <span class="badge guide">Guide</span> **Calculate Transaction Fees**
295+
296+
---
297+
298+
Estimate fees before sending transactions
299+
300+
[:octicons-arrow-right-24: Get Started](/chain-interactions/send-transactions/calculate-transaction-fees/)
301+
302+
</div>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>npx tsx send-transfer.ts</span>
3+
<span data-ty></span>Connected to Polkadot Testnet</span>
4+
<span data-ty>Sender address: 5GgbDVeKZwCmMHzn58iFSgSZDTojRMM52arXnuNXto28R7mg</span>
5+
<span data-ty>Recipient address: 5GgbDVeKZwCmMHzn58iFSgSZDTojRMM52arXnuNXto28R7mg</span>
6+
<span data-ty>Amount: 1000000000 (1 PAS)</span>
7+
<span data-ty></span>
8+
<span data-ty>Sender balance: 59868680224</span>
9+
<span data-ty></span>
10+
<span data-ty>Signing and submitting transaction...</span>
11+
<span data-ty>Transaction status: Validated</span>
12+
<span data-ty>Transaction status: Broadcasting</span>
13+
<span data-ty>Transaction status: BestChainBlockIncluded</span>
14+
<span data-ty>Transaction included in block: 0x80b039e897d8cfe4ec0c641cd17cc7a47ed4b26797b31c7d3c93c3b0b96f7b9f</span>
15+
<span data-ty>Transaction status: Finalized</span>
16+
<span data-ty>Transaction hash: 0x325a1c1cff76fb6a004190cf5ee382f89433596b3c396f10fd25ce6945f2b1df</span>
17+
<span data-ty>Transaction finalized in block: 0x80b039e897d8cfe4ec0c641cd17cc7a47ed4b26797b31c7d3c93c3b0b96f7b9f</span>
18+
<span data-ty>Transaction successful!</span>
19+
<span data-ty>Disconnected from Polkadot Testnet</span>
20+
</div>

0 commit comments

Comments
 (0)