@@ -13,6 +13,7 @@ The Somnia Data Streams Python SDK enables streaming data on-chain, integrated w
1313- Type-safe API with comprehensive type definitions
1414- Asynchronized architecture for better CPU utilization on high load
1515- Extensive unit tests and integration tests
16+ - Example code snippets for all functionalities
1617
1718
1819## Installation
@@ -39,6 +40,8 @@ sdk = SDK.create_for_chain(SOMNIA_TESTNET["id"], private_key="0x...")
3940### Get All Registered Schemas
4041
4142``` python
43+ import asyncio
44+
4245schemas = await sdk.streams.get_all_schemas()
4346for i, schema in enumerate (schemas):
4447 print (f " { i+ 1 } . { schema} " )
@@ -67,7 +70,7 @@ from somnia_data_streams_sdk import SchemaEncoder, SchemaItem
6770encoder = SchemaEncoder(" uint256 balance, address owner" )
6871encoded = encoder.encode_data([
6972 SchemaItem(name = " balance" , type = " uint256" , value = 666 ),
70- SchemaItem(name = " owner" , type = " address" , value = " 0x7e5f4552091a69125d5dfcb7b8c2659029395bdf " ),
73+ SchemaItem(name = " owner" , type = " address" , value = " 0x... " ),
7174])
7275print (f " Encoded Schema: { encoded} " )
7376
@@ -77,7 +80,7 @@ for item in decoded:
7780 print (f " { item.name} ( { item.type} ): { item.value.value} " )
7881```
7982
80- ### Register a Schema (Consumes Gas)
83+ ### Register a Data Schema (Consumes Gas)
8184
8285``` python
8386from somnia_data_streams_sdk import DataSchemaRegistration
@@ -136,59 +139,96 @@ if data:
136139 print (" Raw data (schema not public):" , data)
137140```
138141
139- ### Register and Emit Events (Consumes Gas)
142+ ### Register Events (Consumes Gas)
140143
141144``` python
142- from somnia_data_streams_sdk import EventSchema, EventParameter, EventStream
145+ from somnia_data_streams_sdk import EventSchema, EventParameter
143146from eth_utils import to_hex, keccak
144147
145- # First, register an event schema
146- event_signature = " Transfer(address,uint256,uint256)"
147- event_topic = to_hex(keccak(text = event_signature)) # Compute keccak256 hash
148+ event_signature = " TestV1(uint256 indexed x)"
149+ event_id = " TestV1"
148150
149151event_schemas = [
150152 EventSchema(
151153 params = [
152- EventParameter(name = " user" , param_type = " address" , is_indexed = True ),
153- EventParameter(name = " amount" , param_type = " uint256" , is_indexed = False ),
154- EventParameter(name = " timestamp" , param_type = " uint256" , is_indexed = False ),
154+ EventParameter(name = " x" , param_type = " uint256" , is_indexed = True )
155155 ],
156- event_topic = event_topic # HexStr: keccak256 hash of event signature
156+ event_topic = to_hex(keccak( text = event_signature))
157157 )
158158]
159159
160160tx_hash = await sdk.streams.register_event_schemas(
161- ids = [" my-transfer-event " ],
161+ ids = [event_id ],
162162 schemas = event_schemas
163163)
164+
164165if tx_hash and isinstance (tx_hash, str ):
165- print (f " Event schema registered! TX: { tx_hash} " )
166+ print (f " Event schema registered! TX: 0x { tx_hash} " )
166167else :
167168 print (" Event schema registration failed or already registered" )
168169
169- # Then emit events
170+ time.sleep(5 ) # gives Somnia's blockchain a short time to register the event
171+
172+ tx_hash = await sdk.streams.manage_event_emitters_for_registered_streams_event(
173+ event_id, " 0x..." , True ) # Replace 0x... with your public key
174+ print (f " Event permission added! TX: 0x { tx_hash} " )
175+ ```
176+
177+ ### Emit Events (Consumes Gas)
178+
179+ ``` python
170180from eth_abi import encode
181+ from somnia_data_streams_sdk import EventStream
171182
172183event_data = encode(
173- [" uint256" , " uint256 " ],
174- [1000 , 1234567890 ] # amount, timestamp
184+ [" uint256" ],
185+ [13 ] # Replace 13 with any unsigned int value you want to emit
175186)
176187
177188events = [
178189 EventStream(
179- id = " my-transfer-event " ,
180- argument_topics = [to_hex(keccak(hexstr = " 0x " + " 1234567890123456789012345678901234567890 " ))], # indexed user address
181- data = to_hex( event_data)
190+ id = event_id ,
191+ argument_topics = [to_hex(keccak(text = event_signature ))],
192+ data = event_data
182193 )
183194]
184195
185196tx_hash = await sdk.streams.emit_events(events)
186197if tx_hash and isinstance (tx_hash, str ):
187- print (f " Events emitted! TX: { tx_hash} " )
198+ print (f " Events emitted! TX: 0x { tx_hash} " )
188199else :
189200 print (" Event emission failed" )
190201```
191202
203+ ### Subscribe to Events
204+
205+ ``` python
206+ from somnia_data_streams_sdk import SubscriptionInitParams
207+
208+ def on_data (data ):
209+ print (data)
210+
211+ def on_error (error ):
212+ print (error)
213+
214+ subscription = await sdk.streams.subscribe(
215+ SubscriptionInitParams(
216+ somnia_streams_event_id = " TestV1" ,
217+ eth_calls = [],
218+ on_data = on_data,
219+ on_error = on_error,
220+ only_push_changes = True
221+ )
222+ )
223+
224+ print (" Subscribed to TestV1. Press Ctrl+C to stop." )
225+
226+ try :
227+ await asyncio.Future()
228+ except :
229+ await subscription.get(" unsubscribe" )
230+ ```
231+
192232
193233## API Reference
194234
@@ -220,4 +260,4 @@ If it's bug fix or code improvement (i.e. not a new feature), please make sure y
220260pytest -v -s
221261```
222262
223- If it's a new feature, don't forget to write unit tests and integration tests for it.
263+ If it's a new feature, don't forget to write examples, unit tests, and integration tests for it.
0 commit comments