Skip to content

Commit 3988897

Browse files
author
Anton Sauchyk
committed
chore: format examples
1 parent 37c9d22 commit 3988897

9 files changed

+177
-109
lines changed

learning-examples/accounts_advanced_filter.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import hashlib
66
import base58
77

8-
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
8+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
99

1010
from dotenv import load_dotenv
1111
from generated import geyser_pb2, geyser_pb2_grpc
@@ -19,48 +19,52 @@
1919
# Pump.fun program ID - this is the main program that manages all pump.fun tokens
2020
PUMP_FUN_PROGRAM_ID = "6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P"
2121

22+
2223
def calculate_discriminator(account_name: str) -> int:
2324
"""
2425
Calculate the 8-byte discriminator for an Anchor account type.
25-
26+
2627
The discriminator is the first 8 bytes of SHA-256 hash of "account:{account_name}"
2728
This is used to identify different types of accounts in Anchor programs.
28-
29+
2930
Args:
3031
account_name: The name of the account struct (e.g., "BondingCurve")
31-
32+
3233
Returns:
3334
The discriminator as a little-endian integer
3435
"""
3536
hash_input = f"account:{account_name}"
3637
hash_result = hashlib.sha256(hash_input.encode()).digest()
3738
discriminator_bytes = hash_result[:8]
38-
return int.from_bytes(discriminator_bytes, 'little')
39+
return int.from_bytes(discriminator_bytes, "little")
40+
3941

4042
# Calculate the bonding curve discriminator
4143
BONDING_CURVE_DISCRIMINATOR = calculate_discriminator("BondingCurve")
4244
print(f"Bonding Curve Discriminator: {BONDING_CURVE_DISCRIMINATOR}")
4345

46+
4447
async def main():
4548
"""
4649
Main function that connects to Geyser and monitors pump.fun bonding curve updates.
47-
50+
4851
This will receive updates for:
4952
- New token creation (when bonding curves are created)
5053
- Trading activity (buys/sells that change bonding curve state)
5154
- Token progression through the bonding curve
5255
"""
53-
56+
5457
async with grpc.aio.secure_channel(
5558
GEYSER_ENDPOINT,
5659
grpc.composite_channel_credentials(
5760
grpc.ssl_channel_credentials(),
5861
grpc.metadata_call_credentials(
59-
lambda context, callback: callback((('x-token', GEYSER_API_TOKEN),), None)
60-
)
61-
)
62+
lambda context, callback: callback(
63+
(("x-token", GEYSER_API_TOKEN),), None
64+
)
65+
),
66+
),
6267
) as channel:
63-
6468
# Create the gRPC stub for making requests
6569
stub = geyser_pb2_grpc.GeyserStub(channel)
6670

@@ -74,10 +78,10 @@ async def main():
7478
geyser_pb2.SubscribeRequestFilterAccountsFilter(
7579
memcmp=geyser_pb2.SubscribeRequestFilterAccountsFilterMemcmp(
7680
offset=0, # Discriminator is at the beginning of account data
77-
bytes=BONDING_CURVE_DISCRIMINATOR.to_bytes(8, "little")
81+
bytes=BONDING_CURVE_DISCRIMINATOR.to_bytes(8, "little"),
7882
)
7983
),
80-
]
84+
],
8185
)
8286
},
8387
# Use PROCESSED commitment for faster updates (vs CONFIRMED or FINALIZED)
@@ -91,32 +95,37 @@ async def main():
9195
update_count = 0
9296
async for response in stub.Subscribe(iter([request])):
9397
update_count += 1
94-
98+
9599
if response.account:
96100
account_info = response.account.account
97-
101+
98102
print(f"📊 Update #{update_count}")
99-
print(f" Account: {base58.b58encode(account_info.pubkey).decode('utf-8')}")
103+
print(
104+
f" Account: {base58.b58encode(account_info.pubkey).decode('utf-8')}"
105+
)
100106
print(f" Lamports: {account_info.lamports:,}")
101107
print(f" Data Size: {len(account_info.data)} bytes")
102-
print(f" Owner: {base58.b58encode(account_info.owner).decode('utf-8')}")
103-
108+
print(
109+
f" Owner: {base58.b58encode(account_info.owner).decode('utf-8')}"
110+
)
111+
104112
# You can decode the bonding curve data here to get:
105113
# - Virtual SOL reserves
106114
# - Virtual token reserves
107115
# - Real token reserves
108116
# - Token mint address
109117
# - Bonding curve progress
110-
118+
111119
print("---")
112120
else:
113121
print(f"⚠️ Received non-account update: {response}")
114122
print("---")
115123

124+
116125
if __name__ == "__main__":
117126
try:
118127
asyncio.run(main())
119128
except KeyboardInterrupt:
120129
print("\n🛑 Stopping monitor...")
121130
except Exception as e:
122-
print(f"❌ Error: {e}")
131+
print(f"❌ Error: {e}")

learning-examples/accounts_basic_filter.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import grpc
55
import base58
66

7-
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
7+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
88

99
from dotenv import load_dotenv
1010
from generated import geyser_pb2, geyser_pb2_grpc
@@ -19,20 +19,23 @@
1919
# We will subscribe to updates for this specific account.
2020
RAYDIUM_MARKET_ADDRESS = "2AXXcN6oN9bBT5owwmTH53C7QHUXvhLeu718Kqt8rvY2"
2121

22+
2223
async def main():
2324
"""
2425
Main function that connects to Geyser and monitors a specific account.
25-
26+
2627
This will receive updates whenever the specified account's data changes.
2728
"""
2829
async with grpc.aio.secure_channel(
2930
GEYSER_ENDPOINT,
3031
grpc.composite_channel_credentials(
3132
grpc.ssl_channel_credentials(),
3233
grpc.metadata_call_credentials(
33-
lambda context, callback: callback((('x-token', GEYSER_API_TOKEN),), None)
34-
)
35-
)
34+
lambda context, callback: callback(
35+
(("x-token", GEYSER_API_TOKEN),), None
36+
)
37+
),
38+
),
3639
) as channel:
3740
stub = geyser_pb2_grpc.GeyserStub(channel)
3841

@@ -41,7 +44,7 @@ async def main():
4144
accounts={
4245
"accounts_filter": geyser_pb2.SubscribeRequestFilterAccounts(
4346
# Provide the specific account public key to monitor
44-
account=[RAYDIUM_MARKET_ADDRESS]
47+
account=[RAYDIUM_MARKET_ADDRESS]
4548
)
4649
},
4750
# Use PROCESSED commitment for faster updates (vs CONFIRMED or FINALIZED)
@@ -55,20 +58,25 @@ async def main():
5558
update_count = 0
5659
async for response in stub.Subscribe(iter([request])):
5760
update_count += 1
58-
61+
5962
if response.account:
6063
account_info = response.account.account
61-
64+
6265
print(f"📊 Update #{update_count}")
63-
print(f" Account: {base58.b58encode(account_info.pubkey).decode('utf-8')}")
66+
print(
67+
f" Account: {base58.b58encode(account_info.pubkey).decode('utf-8')}"
68+
)
6469
print(f" Lamports: {account_info.lamports:,}")
6570
print(f" Data Size: {len(account_info.data)} bytes")
66-
print(f" Owner: {base58.b58encode(account_info.owner).decode('utf-8')}")
71+
print(
72+
f" Owner: {base58.b58encode(account_info.owner).decode('utf-8')}"
73+
)
6774
print("---")
6875
else:
6976
print(f"⚠️ Received non-account update: {response}")
7077
print("---")
7178

79+
7280
if __name__ == "__main__":
7381
try:
7482
asyncio.run(main())

learning-examples/blocks_meta_subscription.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
import grpc
55

6-
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
6+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
77

88
from dotenv import load_dotenv
99
from generated import geyser_pb2, geyser_pb2_grpc
@@ -14,27 +14,30 @@
1414
GEYSER_ENDPOINT = os.getenv("GEYSER_ENDPOINT")
1515
GEYSER_API_TOKEN = os.getenv("GEYSER_API_TOKEN")
1616

17+
1718
async def main():
1819
"""
1920
Main function that connects to Geyser and monitors block metadata for all blocks.
20-
21+
2122
This will receive metadata for every block processed by the Geyser node.
2223
"""
2324
async with grpc.aio.secure_channel(
2425
GEYSER_ENDPOINT,
2526
grpc.composite_channel_credentials(
2627
grpc.ssl_channel_credentials(),
2728
grpc.metadata_call_credentials(
28-
lambda context, callback: callback((('x-token', GEYSER_API_TOKEN),), None)
29-
)
30-
)
29+
lambda context, callback: callback(
30+
(("x-token", GEYSER_API_TOKEN),), None
31+
)
32+
),
33+
),
3134
) as channel:
3235
stub = geyser_pb2_grpc.GeyserStub(channel)
3336

3437
# Basic blocks_meta subscription for all blocks
3538
request = geyser_pb2.SubscribeRequest(
3639
blocks_meta={
37-
"blocks_meta_filter": geyser_pb2.SubscribeRequestFilterBlocksMeta() # Don't have filters
40+
"blocks_meta_filter": geyser_pb2.SubscribeRequestFilterBlocksMeta() # Don't have filters
3841
},
3942
commitment=geyser_pb2.CommitmentLevel.PROCESSED,
4043
)
@@ -46,21 +49,24 @@ async def main():
4649
update_count = 0
4750
async for response in stub.Subscribe(iter([request])):
4851
update_count += 1
49-
52+
5053
if response.block_meta:
5154
block_meta_info = response.block_meta
52-
55+
5356
print(f"📦 Block Meta Update #{update_count}")
5457
print(f" Slot: {block_meta_info.slot}")
5558
print(f" Blockhash: {block_meta_info.blockhash}")
5659
print(f" Parent Slot: {block_meta_info.parent_slot}")
5760
print(f" Block Time: {block_meta_info.block_time.timestamp}")
58-
print(f" Transaction Count: {block_meta_info.executed_transaction_count}")
61+
print(
62+
f" Transaction Count: {block_meta_info.executed_transaction_count}"
63+
)
5964
print("---")
6065
else:
6166
print(f"⚠️ Received non-block_meta update: {response}")
6267
print("---")
6368

69+
6470
if __name__ == "__main__":
6571
try:
6672
asyncio.run(main())

learning-examples/blocks_subscription.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
import grpc
55

6-
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
6+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
77

88
from dotenv import load_dotenv
99
from generated import geyser_pb2, geyser_pb2_grpc
@@ -17,11 +17,12 @@
1717
# Pump.fun program ID - we will subscribe to blocks containing its transactions
1818
PUMP_FUN_PROGRAM_ID = "6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P"
1919

20+
2021
async def main():
2122
"""
2223
Main function that connects to Geyser and monitors blocks containing transactions
2324
for the specified pump.fun program.
24-
25+
2526
This will receive updates for any block that includes a transaction interacting
2627
with the PUMP_FUN_PROGRAM_ID.
2728
"""
@@ -30,9 +31,11 @@ async def main():
3031
grpc.composite_channel_credentials(
3132
grpc.ssl_channel_credentials(),
3233
grpc.metadata_call_credentials(
33-
lambda context, callback: callback((('x-token', GEYSER_API_TOKEN),), None)
34-
)
35-
)
34+
lambda context, callback: callback(
35+
(("x-token", GEYSER_API_TOKEN),), None
36+
)
37+
),
38+
),
3639
) as channel:
3740
stub = geyser_pb2_grpc.GeyserStub(channel)
3841

@@ -42,8 +45,8 @@ async def main():
4245
"blocks_filter": geyser_pb2.SubscribeRequestFilterBlocks(
4346
account_include=[PUMP_FUN_PROGRAM_ID],
4447
include_transactions=True, # Include transactions in which the account is involved
45-
include_accounts=False, # Not allowed
46-
include_entries=False # Not allowed
48+
include_accounts=False, # Not allowed
49+
include_entries=False, # Not allowed
4750
)
4851
},
4952
commitment=geyser_pb2.CommitmentLevel.PROCESSED,
@@ -56,22 +59,25 @@ async def main():
5659
update_count = 0
5760
async for response in stub.Subscribe(iter([request])):
5861
update_count += 1
59-
62+
6063
if response.block:
6164
block_info = response.block
62-
65+
6366
print(f"📦 Block Update #{update_count}")
6467
print(f" Slot: {block_info.slot}")
6568
print(f" Blockhash: {block_info.blockhash}")
66-
69+
6770
if block_info.transactions:
68-
print(f" Found {len(block_info.transactions)} transactions involving the program.")
69-
71+
print(
72+
f" Found {len(block_info.transactions)} transactions involving the program."
73+
)
74+
7075
print("---")
7176
else:
7277
print(f"⚠️ Received non-block update: {response}")
7378
print("---")
7479

80+
7581
if __name__ == "__main__":
7682
try:
7783
asyncio.run(main())

0 commit comments

Comments
 (0)