2024-09-09 09:04:23 +07:00
|
|
|
import asyncio
|
2025-03-05 07:03:32 +00:00
|
|
|
import os
|
2024-09-09 09:04:23 +07:00
|
|
|
import struct
|
|
|
|
|
from typing import Final
|
|
|
|
|
|
2025-11-18 13:09:37 +01:00
|
|
|
from construct import Bytes, Flag, Int64ul, Struct
|
2024-09-09 09:04:23 +07:00
|
|
|
from solana.rpc.async_api import AsyncClient
|
|
|
|
|
from solders.pubkey import Pubkey
|
|
|
|
|
|
|
|
|
|
LAMPORTS_PER_SOL: Final[int] = 1_000_000_000
|
|
|
|
|
TOKEN_DECIMALS: Final[int] = 6
|
2025-11-18 13:09:37 +01:00
|
|
|
CURVE_ADDRESS: Final[str] = "..." # Replace with actual bonding curve address
|
2024-09-09 09:04:23 +07:00
|
|
|
|
2025-06-04 16:55:58 +08:00
|
|
|
# Here and later all the discriminators are precalculated. See learning-examples/calculate_discriminator.py
|
2024-09-09 09:04:23 +07:00
|
|
|
EXPECTED_DISCRIMINATOR: Final[bytes] = struct.pack("<Q", 6966180631402821399)
|
|
|
|
|
|
2025-03-19 16:01:46 +00:00
|
|
|
RPC_ENDPOINT = os.environ.get("SOLANA_NODE_RPC_ENDPOINT")
|
|
|
|
|
|
2025-03-05 07:03:32 +00:00
|
|
|
|
2024-09-09 09:04:23 +07:00
|
|
|
class BondingCurveState:
|
2025-11-18 13:09:37 +01:00
|
|
|
"""Parse bonding curve account data - supports all versions."""
|
|
|
|
|
|
|
|
|
|
_STRUCT_V1 = Struct(
|
|
|
|
|
"virtual_token_reserves" / Int64ul,
|
|
|
|
|
"virtual_sol_reserves" / Int64ul,
|
|
|
|
|
"real_token_reserves" / Int64ul,
|
|
|
|
|
"real_sol_reserves" / Int64ul,
|
|
|
|
|
"token_total_supply" / Int64ul,
|
|
|
|
|
"complete" / Flag,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
_STRUCT_V2 = Struct(
|
2024-09-09 09:04:23 +07:00
|
|
|
"virtual_token_reserves" / Int64ul,
|
|
|
|
|
"virtual_sol_reserves" / Int64ul,
|
|
|
|
|
"real_token_reserves" / Int64ul,
|
|
|
|
|
"real_sol_reserves" / Int64ul,
|
|
|
|
|
"token_total_supply" / Int64ul,
|
2025-03-05 07:03:32 +00:00
|
|
|
"complete" / Flag,
|
2025-11-18 13:09:37 +01:00
|
|
|
"creator" / Bytes(32),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
_STRUCT_V3 = Struct(
|
|
|
|
|
"virtual_token_reserves" / Int64ul,
|
|
|
|
|
"virtual_sol_reserves" / Int64ul,
|
|
|
|
|
"real_token_reserves" / Int64ul,
|
|
|
|
|
"real_sol_reserves" / Int64ul,
|
|
|
|
|
"token_total_supply" / Int64ul,
|
|
|
|
|
"complete" / Flag,
|
|
|
|
|
"creator" / Bytes(32),
|
|
|
|
|
"is_mayhem_mode" / Flag,
|
2024-09-09 09:04:23 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def __init__(self, data: bytes) -> None:
|
2025-11-18 13:09:37 +01:00
|
|
|
"""Parse bonding curve data - auto-detects version."""
|
|
|
|
|
data_length = len(data) - 8
|
|
|
|
|
|
|
|
|
|
if data_length < 73: # V1: without creator and mayhem mode
|
|
|
|
|
parsed = self._STRUCT_V1.parse(data[8:])
|
|
|
|
|
self.__dict__.update(parsed)
|
|
|
|
|
self.creator = None
|
|
|
|
|
self.is_mayhem_mode = False
|
|
|
|
|
elif data_length == 73: # V2: with creator, without mayhem mode
|
|
|
|
|
parsed = self._STRUCT_V2.parse(data[8:])
|
|
|
|
|
self.__dict__.update(parsed)
|
|
|
|
|
if isinstance(self.creator, bytes):
|
|
|
|
|
self.creator = Pubkey.from_bytes(self.creator)
|
|
|
|
|
self.is_mayhem_mode = False
|
|
|
|
|
else: # V3: with creator and mayhem mode
|
|
|
|
|
parsed = self._STRUCT_V3.parse(data[8:])
|
|
|
|
|
self.__dict__.update(parsed)
|
|
|
|
|
if isinstance(self.creator, bytes):
|
|
|
|
|
self.creator = Pubkey.from_bytes(self.creator)
|
2024-09-09 09:04:23 +07:00
|
|
|
|
2025-03-05 07:03:32 +00:00
|
|
|
|
|
|
|
|
async def get_bonding_curve_state(
|
|
|
|
|
conn: AsyncClient, curve_address: Pubkey
|
|
|
|
|
) -> BondingCurveState:
|
2025-04-01 14:57:01 +00:00
|
|
|
response = await conn.get_account_info(curve_address, encoding="base64")
|
2024-09-09 09:04:23 +07:00
|
|
|
if not response.value or not response.value.data:
|
|
|
|
|
raise ValueError("Invalid curve state: No data")
|
|
|
|
|
|
|
|
|
|
data = response.value.data
|
|
|
|
|
if data[:8] != EXPECTED_DISCRIMINATOR:
|
|
|
|
|
raise ValueError("Invalid curve state discriminator")
|
|
|
|
|
|
|
|
|
|
return BondingCurveState(data)
|
|
|
|
|
|
2025-03-05 07:03:32 +00:00
|
|
|
|
2024-09-09 09:04:23 +07:00
|
|
|
def calculate_bonding_curve_price(curve_state: BondingCurveState) -> float:
|
|
|
|
|
if curve_state.virtual_token_reserves <= 0 or curve_state.virtual_sol_reserves <= 0:
|
|
|
|
|
raise ValueError("Invalid reserve state")
|
|
|
|
|
|
2025-03-05 07:03:32 +00:00
|
|
|
return (curve_state.virtual_sol_reserves / LAMPORTS_PER_SOL) / (
|
|
|
|
|
curve_state.virtual_token_reserves / 10**TOKEN_DECIMALS
|
|
|
|
|
)
|
|
|
|
|
|
2024-09-09 09:04:23 +07:00
|
|
|
|
|
|
|
|
async def main() -> None:
|
|
|
|
|
try:
|
|
|
|
|
async with AsyncClient(RPC_ENDPOINT) as conn:
|
|
|
|
|
curve_address = Pubkey.from_string(CURVE_ADDRESS)
|
|
|
|
|
bonding_curve_state = await get_bonding_curve_state(conn, curve_address)
|
|
|
|
|
token_price_sol = calculate_bonding_curve_price(bonding_curve_state)
|
|
|
|
|
|
|
|
|
|
print("Token price:")
|
|
|
|
|
print(f" {token_price_sol:.10f} SOL")
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
print(f"Error: {e}")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"An unexpected error occurred: {e}")
|
|
|
|
|
|
2025-03-05 07:03:32 +00:00
|
|
|
|
2024-09-09 09:04:23 +07:00
|
|
|
if __name__ == "__main__":
|
2025-03-05 07:03:32 +00:00
|
|
|
asyncio.run(main())
|