|
| 1 | +--- |
| 2 | +title: Substrate RPC |
| 3 | +--- |
| 4 | + |
| 5 | +The Substrate RPC source connects to a Substrate-based blockchain node via WebSocket RPC to subscribe to new blocks and retrieve complete block data. It works with any Substrate-based network including Polkadot, Kusama, and parachains. |
| 6 | + |
| 7 | +## Configuration |
| 8 | + |
| 9 | +The following snippet shows an example of how to set up a Substrate WebSocket source: |
| 10 | + |
| 11 | +```toml |
| 12 | +[source] |
| 13 | +type = "substrate-rpc" |
| 14 | +url = "ws://localhost:9944" |
| 15 | +``` |
| 16 | + |
| 17 | +### Section `source`: |
| 18 | + |
| 19 | +- `type`: this field must be set to the literal value `substrate-rpc`. |
| 20 | +- `url`: the WebSocket URL of the Substrate RPC endpoint (required). Must use `ws://` or `wss://` protocol. |
| 21 | + |
| 22 | +## Supported Networks |
| 23 | + |
| 24 | +The Substrate source works with any Substrate-based blockchain, including: |
| 25 | + |
| 26 | +- **Polkadot** - The main relay chain |
| 27 | +- **Kusama** - Polkadot's canary network |
| 28 | +- **Westend** - Polkadot's testnet |
| 29 | +- **Parachains** - Any parachain connected to Polkadot or Kusama |
| 30 | +- **Solo chains** - Independent Substrate-based blockchains |
| 31 | +- **Local development nodes** - For testing and development |
| 32 | + |
| 33 | +## Examples |
| 34 | + |
| 35 | +### Local Substrate Node |
| 36 | + |
| 37 | +```toml |
| 38 | +[source] |
| 39 | +type = "substrate-rpc" |
| 40 | +url = "ws://localhost:9944" |
| 41 | +``` |
| 42 | + |
| 43 | +### Polkadot Mainnet via OnFinality |
| 44 | + |
| 45 | +```toml |
| 46 | +[source] |
| 47 | +type = "substrate-rpc" |
| 48 | +url = "wss://polkadot.api.onfinality.io/public-ws" |
| 49 | +``` |
| 50 | + |
| 51 | +### Kusama Network |
| 52 | + |
| 53 | +```toml |
| 54 | +[source] |
| 55 | +type = "substrate-rpc" |
| 56 | +url = "wss://kusama-rpc.polkadot.io" |
| 57 | +``` |
| 58 | + |
| 59 | +### Westend Testnet |
| 60 | + |
| 61 | +```toml |
| 62 | +[source] |
| 63 | +type = "substrate-rpc" |
| 64 | +url = "wss://westend-rpc.polkadot.io" |
| 65 | +``` |
| 66 | + |
| 67 | +### Astar Parachain |
| 68 | + |
| 69 | +```toml |
| 70 | +[source] |
| 71 | +type = "substrate-rpc" |
| 72 | +url = "wss://rpc.astar.network" |
| 73 | +``` |
| 74 | + |
| 75 | +## How It Works |
| 76 | + |
| 77 | +The Substrate source operates using Subxt library for Substrate interaction: |
| 78 | + |
| 79 | +1. **Connection**: Connects to the Substrate node via WebSocket using the Subxt client. |
| 80 | + |
| 81 | +2. **Subscription**: Subscribes to the best block stream to receive notifications when new blocks are finalized. |
| 82 | + |
| 83 | +3. **Block Processing**: For each new block: |
| 84 | + - Retrieves the complete block header information |
| 85 | + - Parses all extrinsics (transactions) in the block |
| 86 | + - Extracts signature information for signed extrinsics |
| 87 | + - Calculates block and extrinsic hashes |
| 88 | + |
| 89 | +4. **Event Generation**: Converts the block data into Oura's internal `ChainEvent::Apply` format for processing by filters and sinks. |
| 90 | + |
| 91 | +## Output Events |
| 92 | + |
| 93 | +The Substrate source produces `ChainEvent::Apply` events containing parsed Substrate block data. Each event includes: |
| 94 | + |
| 95 | +### Block Header |
| 96 | +- Block number and hash |
| 97 | +- Parent block hash |
| 98 | +- State root and extrinsics root |
| 99 | +- Block timestamp and other metadata |
| 100 | + |
| 101 | +### Extrinsics |
| 102 | +For each extrinsic in the block: |
| 103 | +- Extrinsic index and hash |
| 104 | +- Raw bytes (hex-encoded) |
| 105 | +- Signature information (for signed extrinsics): |
| 106 | + - Signer address |
| 107 | + - Signature data |
| 108 | + - Signed extensions metadata |
| 109 | + |
| 110 | +### Additional Information |
| 111 | +- Total extrinsics count |
| 112 | +- Block processing metrics |
| 113 | +- Chain tip information |
| 114 | + |
| 115 | +## Data Structure |
| 116 | + |
| 117 | +The parsed block follows this structure: |
| 118 | + |
| 119 | +```rust |
| 120 | +ParsedBlock { |
| 121 | + header: BlockHeader { |
| 122 | + number: u64, |
| 123 | + hash: String, |
| 124 | + parent_hash: String, |
| 125 | + state_root: String, |
| 126 | + extrinsics_root: String, |
| 127 | + }, |
| 128 | + extrinsics: Vec<Extrinsic>, |
| 129 | + extrinsics_count: usize, |
| 130 | +} |
| 131 | + |
| 132 | +Extrinsic { |
| 133 | + index: u32, |
| 134 | + hash: String, |
| 135 | + bytes: Option<String>, |
| 136 | + signature: Option<SignatureInfo>, |
| 137 | +} |
| 138 | + |
| 139 | +SignatureInfo { |
| 140 | + address: String, |
| 141 | + signature: String, |
| 142 | + extra: Option<String>, |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +## Substrate Runtime Compatibility |
| 147 | + |
| 148 | +The source is designed to work with any Substrate runtime without requiring specific runtime metadata knowledge. It focuses on the core block structure that is consistent across all Substrate chains: |
| 149 | + |
| 150 | +- Block headers follow the standard Substrate format |
| 151 | +- Extrinsics use the SCALE codec for encoding |
| 152 | +- Signature extraction works with standard Substrate transaction formats |
| 153 | + |
| 154 | +This makes the source compatible with both current and future Substrate networks without requiring updates for new runtime versions. |
| 155 | + |
| 156 | +## Performance Considerations |
| 157 | + |
| 158 | +- The source subscribes to the best blocks only, ensuring reliable block ordering |
| 159 | +- Block processing is done asynchronously to maintain good throughput |
| 160 | +- Extrinsic parsing includes detailed signature extraction which may add some processing overhead |
| 161 | +- WebSocket connections are automatically managed and reconnected if needed |
| 162 | + |
| 163 | +The events follow the same pattern as other Oura sources, allowing them to be processed by standard filters and sinks in the pipeline. |
0 commit comments