pumpfun-toolkit 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pumpfun_toolkit/__init__.py +79 -0
- pumpfun_toolkit/borsh.py +68 -0
- pumpfun_toolkit/constants.py +27 -0
- pumpfun_toolkit/curve.py +147 -0
- pumpfun_toolkit/errors.py +44 -0
- pumpfun_toolkit/events.py +260 -0
- pumpfun_toolkit/fees.py +64 -0
- pumpfun_toolkit/mint.py +134 -0
- pumpfun_toolkit/py.typed +0 -0
- pumpfun_toolkit/redact.py +138 -0
- pumpfun_toolkit/risk.py +375 -0
- pumpfun_toolkit/rpc.py +422 -0
- pumpfun_toolkit/text.py +12 -0
- pumpfun_toolkit-0.1.1.dist-info/METADATA +290 -0
- pumpfun_toolkit-0.1.1.dist-info/RECORD +17 -0
- pumpfun_toolkit-0.1.1.dist-info/WHEEL +4 -0
- pumpfun_toolkit-0.1.1.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""pumpfun-toolkit: read-only Python tools for the Pump.fun program on Solana.
|
|
2
|
+
|
|
3
|
+
Decode program events from transaction logs, decode bonding curve accounts and
|
|
4
|
+
compute curve prices, inspect SPL Token / Token-2022 mints for dangerous
|
|
5
|
+
authorities and extensions, and run a heuristic token risk check.
|
|
6
|
+
|
|
7
|
+
The package never signs or sends transactions.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from pumpfun_toolkit.constants import (
|
|
11
|
+
IDL_COMMIT,
|
|
12
|
+
LAMPORTS_PER_SOL,
|
|
13
|
+
PUMP_PROGRAM_ID,
|
|
14
|
+
TOKEN_2022_PROGRAM_ID,
|
|
15
|
+
TOKEN_PROGRAM_ID,
|
|
16
|
+
)
|
|
17
|
+
from pumpfun_toolkit.curve import (
|
|
18
|
+
BondingCurveAccount,
|
|
19
|
+
CurveState,
|
|
20
|
+
bonding_curve_address,
|
|
21
|
+
decode_bonding_curve,
|
|
22
|
+
)
|
|
23
|
+
from pumpfun_toolkit.errors import (
|
|
24
|
+
DecodeError,
|
|
25
|
+
InvalidRpcUrlError,
|
|
26
|
+
MethodNotAllowedError,
|
|
27
|
+
MintDecodeError,
|
|
28
|
+
PumpfunToolkitError,
|
|
29
|
+
SolanaRpcError,
|
|
30
|
+
)
|
|
31
|
+
from pumpfun_toolkit.events import (
|
|
32
|
+
CompleteEvent,
|
|
33
|
+
CreateEvent,
|
|
34
|
+
PumpEvent,
|
|
35
|
+
TradeEvent,
|
|
36
|
+
decode_event,
|
|
37
|
+
decode_logs,
|
|
38
|
+
extract_program_data,
|
|
39
|
+
)
|
|
40
|
+
from pumpfun_toolkit.mint import MintInfo, parse_mint
|
|
41
|
+
from pumpfun_toolkit.redact import redact_url
|
|
42
|
+
from pumpfun_toolkit.risk import Assessment, Finding, SecurityInputs, assess
|
|
43
|
+
from pumpfun_toolkit.rpc import SolanaRpcClient
|
|
44
|
+
|
|
45
|
+
__version__ = "0.1.1"
|
|
46
|
+
|
|
47
|
+
__all__ = [
|
|
48
|
+
"IDL_COMMIT",
|
|
49
|
+
"LAMPORTS_PER_SOL",
|
|
50
|
+
"PUMP_PROGRAM_ID",
|
|
51
|
+
"TOKEN_2022_PROGRAM_ID",
|
|
52
|
+
"TOKEN_PROGRAM_ID",
|
|
53
|
+
"Assessment",
|
|
54
|
+
"BondingCurveAccount",
|
|
55
|
+
"CompleteEvent",
|
|
56
|
+
"CreateEvent",
|
|
57
|
+
"CurveState",
|
|
58
|
+
"DecodeError",
|
|
59
|
+
"Finding",
|
|
60
|
+
"InvalidRpcUrlError",
|
|
61
|
+
"MethodNotAllowedError",
|
|
62
|
+
"MintDecodeError",
|
|
63
|
+
"MintInfo",
|
|
64
|
+
"PumpEvent",
|
|
65
|
+
"PumpfunToolkitError",
|
|
66
|
+
"SecurityInputs",
|
|
67
|
+
"SolanaRpcClient",
|
|
68
|
+
"SolanaRpcError",
|
|
69
|
+
"TradeEvent",
|
|
70
|
+
"__version__",
|
|
71
|
+
"assess",
|
|
72
|
+
"bonding_curve_address",
|
|
73
|
+
"decode_bonding_curve",
|
|
74
|
+
"decode_event",
|
|
75
|
+
"decode_logs",
|
|
76
|
+
"extract_program_data",
|
|
77
|
+
"parse_mint",
|
|
78
|
+
"redact_url",
|
|
79
|
+
]
|
pumpfun_toolkit/borsh.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""A minimal, bounds-checked Borsh reader."""
|
|
2
|
+
|
|
3
|
+
import struct
|
|
4
|
+
|
|
5
|
+
from solders.pubkey import Pubkey
|
|
6
|
+
|
|
7
|
+
from pumpfun_toolkit.errors import DecodeError
|
|
8
|
+
|
|
9
|
+
# Upper bound for a single Borsh string. Token names, symbols and URIs are far
|
|
10
|
+
# shorter; anything longer is treated as corrupt input instead of being allocated.
|
|
11
|
+
MAX_STRING_BYTES = 2048
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class BorshReader:
|
|
15
|
+
"""Reads Borsh-encoded fields one after another.
|
|
16
|
+
|
|
17
|
+
Every read checks the remaining length first and raises :class:`DecodeError`
|
|
18
|
+
instead of returning partial data.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def __init__(self, data: bytes, offset: int = 0) -> None:
|
|
22
|
+
self._data = data
|
|
23
|
+
self._offset = offset
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def remaining(self) -> int:
|
|
27
|
+
return len(self._data) - self._offset
|
|
28
|
+
|
|
29
|
+
def take(self, size: int) -> bytes:
|
|
30
|
+
"""Return the next ``size`` raw bytes."""
|
|
31
|
+
if size < 0 or self.remaining < size:
|
|
32
|
+
raise DecodeError("data ends prematurely")
|
|
33
|
+
chunk = self._data[self._offset : self._offset + size]
|
|
34
|
+
self._offset += size
|
|
35
|
+
return chunk
|
|
36
|
+
|
|
37
|
+
def u8(self) -> int:
|
|
38
|
+
return self.take(1)[0]
|
|
39
|
+
|
|
40
|
+
def bool(self) -> bool:
|
|
41
|
+
value = self.u8()
|
|
42
|
+
if value > 1:
|
|
43
|
+
raise DecodeError("invalid boolean value")
|
|
44
|
+
return value == 1
|
|
45
|
+
|
|
46
|
+
def u16(self) -> int:
|
|
47
|
+
return struct.unpack("<H", self.take(2))[0]
|
|
48
|
+
|
|
49
|
+
def u32(self) -> int:
|
|
50
|
+
return struct.unpack("<I", self.take(4))[0]
|
|
51
|
+
|
|
52
|
+
def u64(self) -> int:
|
|
53
|
+
return struct.unpack("<Q", self.take(8))[0]
|
|
54
|
+
|
|
55
|
+
def i64(self) -> int:
|
|
56
|
+
return struct.unpack("<q", self.take(8))[0]
|
|
57
|
+
|
|
58
|
+
def pubkey(self) -> str:
|
|
59
|
+
return str(Pubkey.from_bytes(self.take(32)))
|
|
60
|
+
|
|
61
|
+
def string(self) -> str:
|
|
62
|
+
length = self.u32()
|
|
63
|
+
if length > MAX_STRING_BYTES:
|
|
64
|
+
raise DecodeError("string is implausibly long")
|
|
65
|
+
try:
|
|
66
|
+
return self.take(length).decode("utf-8")
|
|
67
|
+
except UnicodeDecodeError as exc:
|
|
68
|
+
raise DecodeError("string is not valid UTF-8") from exc
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Program IDs, mints and unit constants.
|
|
2
|
+
|
|
3
|
+
Every field layout and discriminator in this package follows the official Pump.fun
|
|
4
|
+
IDL ``idl/pump.json`` in the public repository ``pump-fun/pump-public-docs``.
|
|
5
|
+
``IDL_COMMIT`` names the revision the layouts were last checked against. If Pump.fun
|
|
6
|
+
upgrades its program, ``events.py`` and ``curve.py`` must be re-checked against the
|
|
7
|
+
new IDL (see ``docs/idl-provenance.md``).
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
PUMP_PROGRAM_ID = "6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P"
|
|
11
|
+
IDL_SOURCE = "https://github.com/pump-fun/pump-public-docs/blob/main/idl/pump.json"
|
|
12
|
+
IDL_COMMIT = "e244b35ba70864c029411e81ed1bbd4dee4d2926"
|
|
13
|
+
|
|
14
|
+
# SPL Token programs (https://github.com/solana-program/token and token-2022).
|
|
15
|
+
TOKEN_PROGRAM_ID = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" # noqa: S105 - public program ID
|
|
16
|
+
TOKEN_2022_PROGRAM_ID = "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb" # noqa: S105 - public program ID
|
|
17
|
+
|
|
18
|
+
# Pump.fun tokens always use six decimals.
|
|
19
|
+
TOKEN_DECIMALS = 6
|
|
20
|
+
|
|
21
|
+
# SOL as the quote asset is encoded either as the all-zero key (older coins) or as
|
|
22
|
+
# Wrapped SOL.
|
|
23
|
+
DEFAULT_PUBKEY = "11111111111111111111111111111111"
|
|
24
|
+
WRAPPED_SOL_MINT = "So11111111111111111111111111111111111111112"
|
|
25
|
+
SOL_QUOTE_MINTS = frozenset({DEFAULT_PUBKEY, WRAPPED_SOL_MINT})
|
|
26
|
+
|
|
27
|
+
LAMPORTS_PER_SOL = 1_000_000_000
|
pumpfun_toolkit/curve.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"""The Pump.fun bonding curve: decode the account and compute key figures.
|
|
2
|
+
|
|
3
|
+
The curve is a constant product of virtual reserves:
|
|
4
|
+
``virtual_sol * virtual_token = constant``. The price of one token is
|
|
5
|
+
``virtual_sol / virtual_token``. Real reserves are what actually sits in the curve;
|
|
6
|
+
once the real token reserves are sold out, the curve is complete and the coin
|
|
7
|
+
migrates to PumpSwap.
|
|
8
|
+
|
|
9
|
+
All arithmetic uses integers and :class:`~decimal.Decimal`. Fees are not included;
|
|
10
|
+
the values are raw curve prices.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
from decimal import Decimal
|
|
15
|
+
|
|
16
|
+
from solders.pubkey import Pubkey
|
|
17
|
+
|
|
18
|
+
from pumpfun_toolkit.borsh import BorshReader
|
|
19
|
+
from pumpfun_toolkit.constants import (
|
|
20
|
+
LAMPORTS_PER_SOL,
|
|
21
|
+
PUMP_PROGRAM_ID,
|
|
22
|
+
SOL_QUOTE_MINTS,
|
|
23
|
+
TOKEN_DECIMALS,
|
|
24
|
+
)
|
|
25
|
+
from pumpfun_toolkit.errors import DecodeError
|
|
26
|
+
|
|
27
|
+
# Anchor discriminator of the ``BondingCurve`` account according to the IDL.
|
|
28
|
+
BONDING_CURVE_DISCRIMINATOR = bytes([23, 183, 248, 55, 96, 216, 172, 96])
|
|
29
|
+
|
|
30
|
+
_TOKEN_UNIT = 10**TOKEN_DECIMALS
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def bonding_curve_address(mint: str) -> str:
|
|
34
|
+
"""Address of the curve account: the PDA of ``"bonding-curve"`` and the mint."""
|
|
35
|
+
address, _bump = Pubkey.find_program_address(
|
|
36
|
+
[b"bonding-curve", bytes(Pubkey.from_string(mint))], Pubkey.from_string(PUMP_PROGRAM_ID)
|
|
37
|
+
)
|
|
38
|
+
return str(address)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass(frozen=True)
|
|
42
|
+
class CurveState:
|
|
43
|
+
"""Reserves of a curve. For SOL-quoted coins the quote side is in lamports."""
|
|
44
|
+
|
|
45
|
+
virtual_token_reserves: int
|
|
46
|
+
virtual_sol_reserves: int
|
|
47
|
+
real_token_reserves: int
|
|
48
|
+
real_sol_reserves: int
|
|
49
|
+
token_total_supply: int
|
|
50
|
+
complete: bool = False
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def price_sol(self) -> Decimal | None:
|
|
54
|
+
"""Price of one whole token in SOL."""
|
|
55
|
+
if self.virtual_token_reserves <= 0:
|
|
56
|
+
return None
|
|
57
|
+
return (Decimal(self.virtual_sol_reserves) / LAMPORTS_PER_SOL) / (
|
|
58
|
+
Decimal(self.virtual_token_reserves) / _TOKEN_UNIT
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def market_cap_sol(self) -> Decimal | None:
|
|
63
|
+
"""Price times total supply, in SOL."""
|
|
64
|
+
price = self.price_sol
|
|
65
|
+
if price is None:
|
|
66
|
+
return None
|
|
67
|
+
return price * Decimal(self.token_total_supply) / _TOKEN_UNIT
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def liquidity_lamports(self) -> int:
|
|
71
|
+
"""SOL that actually sits in the curve."""
|
|
72
|
+
return self.real_sol_reserves
|
|
73
|
+
|
|
74
|
+
def progress(self, initial_real_token_reserves: int) -> Decimal | None:
|
|
75
|
+
"""Share of the curve's tokens already sold, from 0 to 1."""
|
|
76
|
+
if initial_real_token_reserves <= 0:
|
|
77
|
+
return None
|
|
78
|
+
sold = initial_real_token_reserves - self.real_token_reserves
|
|
79
|
+
return max(Decimal(0), min(Decimal(1), Decimal(sold) / initial_real_token_reserves))
|
|
80
|
+
|
|
81
|
+
def tokens_out_for_sol(self, lamports_in: int) -> int:
|
|
82
|
+
"""Tokens (base units) received for a buy, excluding fees."""
|
|
83
|
+
if lamports_in <= 0 or self.complete:
|
|
84
|
+
return 0
|
|
85
|
+
out = self.virtual_token_reserves * lamports_in // (self.virtual_sol_reserves + lamports_in)
|
|
86
|
+
return min(out, self.real_token_reserves)
|
|
87
|
+
|
|
88
|
+
def sol_out_for_tokens(self, tokens_in: int) -> int:
|
|
89
|
+
"""Lamports received for a sell, excluding fees."""
|
|
90
|
+
if tokens_in <= 0 or self.complete:
|
|
91
|
+
return 0
|
|
92
|
+
out = self.virtual_sol_reserves * tokens_in // (self.virtual_token_reserves + tokens_in)
|
|
93
|
+
return min(out, self.real_sol_reserves)
|
|
94
|
+
|
|
95
|
+
def price_impact(self, lamports_in: int) -> Decimal | None:
|
|
96
|
+
"""How far a buy lifts the average price above the curve price (0.02 = 2 %)."""
|
|
97
|
+
tokens = self.tokens_out_for_sol(lamports_in)
|
|
98
|
+
price = self.price_sol
|
|
99
|
+
if tokens <= 0 or not price:
|
|
100
|
+
return None
|
|
101
|
+
paid = (Decimal(lamports_in) / LAMPORTS_PER_SOL) / (Decimal(tokens) / _TOKEN_UNIT)
|
|
102
|
+
return paid / price - 1
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@dataclass(frozen=True)
|
|
106
|
+
class BondingCurveAccount:
|
|
107
|
+
"""A decoded ``BondingCurve`` account."""
|
|
108
|
+
|
|
109
|
+
state: CurveState
|
|
110
|
+
creator: str
|
|
111
|
+
quote_mint: str | None
|
|
112
|
+
is_cashback: bool = False
|
|
113
|
+
|
|
114
|
+
@property
|
|
115
|
+
def quote_is_sol(self) -> bool:
|
|
116
|
+
return self.quote_mint is None or self.quote_mint in SOL_QUOTE_MINTS
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def decode_bonding_curve(data: bytes) -> BondingCurveAccount:
|
|
120
|
+
"""Decode the raw data of a curve account.
|
|
121
|
+
|
|
122
|
+
Raises :class:`~pumpfun_toolkit.errors.DecodeError` if the data is not a
|
|
123
|
+
bonding curve or is truncated.
|
|
124
|
+
"""
|
|
125
|
+
if data[:8] != BONDING_CURVE_DISCRIMINATOR:
|
|
126
|
+
raise DecodeError("account is not a bonding curve")
|
|
127
|
+
reader = BorshReader(data, 8)
|
|
128
|
+
state = CurveState(
|
|
129
|
+
virtual_token_reserves=reader.u64(),
|
|
130
|
+
virtual_sol_reserves=reader.u64(),
|
|
131
|
+
real_token_reserves=reader.u64(),
|
|
132
|
+
real_sol_reserves=reader.u64(),
|
|
133
|
+
token_total_supply=reader.u64(),
|
|
134
|
+
complete=reader.bool(),
|
|
135
|
+
)
|
|
136
|
+
creator = reader.pubkey()
|
|
137
|
+
quote_mint: str | None = None
|
|
138
|
+
is_cashback = False
|
|
139
|
+
try: # fields that older accounts do not have yet
|
|
140
|
+
reader.bool() # is_mayhem_mode
|
|
141
|
+
is_cashback = reader.bool()
|
|
142
|
+
quote_mint = reader.pubkey()
|
|
143
|
+
except DecodeError:
|
|
144
|
+
pass
|
|
145
|
+
return BondingCurveAccount(
|
|
146
|
+
state=state, creator=creator, quote_mint=quote_mint, is_cashback=is_cashback
|
|
147
|
+
)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""Exception hierarchy.
|
|
2
|
+
|
|
3
|
+
Every exception raised on purpose by this package derives from
|
|
4
|
+
:class:`PumpfunToolkitError`, so callers can catch them all with one clause.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class PumpfunToolkitError(Exception):
|
|
11
|
+
"""Base class for all errors raised by ``pumpfun_toolkit``."""
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class DecodeError(PumpfunToolkitError, ValueError):
|
|
15
|
+
"""The bytes do not match the expected layout."""
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class MintDecodeError(PumpfunToolkitError, ValueError):
|
|
19
|
+
"""The data is not a readable SPL Token or Token-2022 mint account."""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class SolanaRpcError(PumpfunToolkitError):
|
|
23
|
+
"""The RPC node is unreachable or returned an error.
|
|
24
|
+
|
|
25
|
+
``details`` holds machine-readable context (method name, RPC error code,
|
|
26
|
+
number of attempts). It never contains the endpoint URL, because that URL
|
|
27
|
+
may embed an API key.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
def __init__(self, message: str, *, details: Any = None) -> None:
|
|
31
|
+
super().__init__(message)
|
|
32
|
+
self.message = message
|
|
33
|
+
self.details = details
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class MethodNotAllowedError(PumpfunToolkitError):
|
|
37
|
+
"""The RPC method is not on the read-only allowlist."""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class InvalidRpcUrlError(PumpfunToolkitError, ValueError):
|
|
41
|
+
"""The RPC endpoint is not an absolute ``http://`` or ``https://`` URL.
|
|
42
|
+
|
|
43
|
+
The message never repeats the URL, because it may embed an API key.
|
|
44
|
+
"""
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"""Decode Pump.fun program events from transaction logs.
|
|
2
|
+
|
|
3
|
+
The program emits events as ``Program data: <base64>`` log lines. The first eight
|
|
4
|
+
bytes are the Anchor event discriminator (``sha256("event:<Name>")[:8]``); the
|
|
5
|
+
fields follow in Borsh encoding.
|
|
6
|
+
|
|
7
|
+
Two safeguards:
|
|
8
|
+
|
|
9
|
+
- Any program may write ``Program data:`` lines. :func:`extract_program_data`
|
|
10
|
+
therefore tracks the invocation stack and only accepts lines written by the
|
|
11
|
+
Pump.fun program itself. Otherwise another program in the same transaction
|
|
12
|
+
could forge events.
|
|
13
|
+
- Pump.fun appends new fields at the end of its events. The decoder reads the
|
|
14
|
+
fields it needs and tolerates unknown trailing bytes.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
import base64
|
|
18
|
+
import hashlib
|
|
19
|
+
from dataclasses import dataclass
|
|
20
|
+
|
|
21
|
+
from pumpfun_toolkit.borsh import BorshReader
|
|
22
|
+
from pumpfun_toolkit.constants import PUMP_PROGRAM_ID, SOL_QUOTE_MINTS
|
|
23
|
+
from pumpfun_toolkit.errors import DecodeError
|
|
24
|
+
|
|
25
|
+
# Upper bound for the ``shareholders`` vector in a trade event. Real events carry a
|
|
26
|
+
# handful of entries; a larger count means corrupt data.
|
|
27
|
+
MAX_SHAREHOLDERS = 64
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def event_discriminator(name: str) -> bytes:
|
|
31
|
+
"""Anchor discriminator of an event: ``sha256("event:<name>")[:8]``."""
|
|
32
|
+
return hashlib.sha256(f"event:{name}".encode()).digest()[:8]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
CREATE_EVENT = event_discriminator("CreateEvent")
|
|
36
|
+
TRADE_EVENT = event_discriminator("TradeEvent")
|
|
37
|
+
COMPLETE_EVENT = event_discriminator("CompleteEvent")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(frozen=True)
|
|
41
|
+
class CreateEvent:
|
|
42
|
+
"""A new coin was created on its bonding curve."""
|
|
43
|
+
|
|
44
|
+
name: str
|
|
45
|
+
symbol: str
|
|
46
|
+
uri: str
|
|
47
|
+
mint: str
|
|
48
|
+
bonding_curve: str
|
|
49
|
+
user: str
|
|
50
|
+
creator: str
|
|
51
|
+
timestamp: int
|
|
52
|
+
virtual_token_reserves: int
|
|
53
|
+
virtual_sol_reserves: int
|
|
54
|
+
real_token_reserves: int
|
|
55
|
+
token_total_supply: int
|
|
56
|
+
token_program: str
|
|
57
|
+
is_mayhem_mode: bool
|
|
58
|
+
is_cashback_enabled: bool
|
|
59
|
+
quote_mint: str
|
|
60
|
+
virtual_quote_reserves: int
|
|
61
|
+
|
|
62
|
+
@property
|
|
63
|
+
def quote_is_sol(self) -> bool:
|
|
64
|
+
return self.quote_mint in SOL_QUOTE_MINTS
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@dataclass(frozen=True)
|
|
68
|
+
class TradeEvent:
|
|
69
|
+
"""A buy or sell on a bonding curve. Amounts are in base units (lamports, raw tokens)."""
|
|
70
|
+
|
|
71
|
+
mint: str
|
|
72
|
+
sol_amount: int
|
|
73
|
+
token_amount: int
|
|
74
|
+
is_buy: bool
|
|
75
|
+
user: str
|
|
76
|
+
timestamp: int
|
|
77
|
+
virtual_sol_reserves: int
|
|
78
|
+
virtual_token_reserves: int
|
|
79
|
+
real_sol_reserves: int
|
|
80
|
+
real_token_reserves: int
|
|
81
|
+
creator: str
|
|
82
|
+
# Only present in newer events; ``None`` means SOL.
|
|
83
|
+
quote_mint: str | None = None
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def quote_is_sol(self) -> bool:
|
|
87
|
+
return self.quote_mint is None or self.quote_mint in SOL_QUOTE_MINTS
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@dataclass(frozen=True)
|
|
91
|
+
class CompleteEvent:
|
|
92
|
+
"""A bonding curve sold out and the coin migrates away from it."""
|
|
93
|
+
|
|
94
|
+
user: str
|
|
95
|
+
mint: str
|
|
96
|
+
bonding_curve: str
|
|
97
|
+
timestamp: int
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
PumpEvent = CreateEvent | TradeEvent | CompleteEvent
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _decode_create(reader: BorshReader) -> CreateEvent:
|
|
104
|
+
return CreateEvent(
|
|
105
|
+
name=reader.string(),
|
|
106
|
+
symbol=reader.string(),
|
|
107
|
+
uri=reader.string(),
|
|
108
|
+
mint=reader.pubkey(),
|
|
109
|
+
bonding_curve=reader.pubkey(),
|
|
110
|
+
user=reader.pubkey(),
|
|
111
|
+
creator=reader.pubkey(),
|
|
112
|
+
timestamp=reader.i64(),
|
|
113
|
+
virtual_token_reserves=reader.u64(),
|
|
114
|
+
virtual_sol_reserves=reader.u64(),
|
|
115
|
+
real_token_reserves=reader.u64(),
|
|
116
|
+
token_total_supply=reader.u64(),
|
|
117
|
+
token_program=reader.pubkey(),
|
|
118
|
+
is_mayhem_mode=reader.bool(),
|
|
119
|
+
is_cashback_enabled=reader.bool(),
|
|
120
|
+
quote_mint=reader.pubkey(),
|
|
121
|
+
virtual_quote_reserves=reader.u64(),
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _decode_trade(reader: BorshReader) -> TradeEvent:
|
|
126
|
+
mint = reader.pubkey()
|
|
127
|
+
sol_amount = reader.u64()
|
|
128
|
+
token_amount = reader.u64()
|
|
129
|
+
is_buy = reader.bool()
|
|
130
|
+
user = reader.pubkey()
|
|
131
|
+
timestamp = reader.i64()
|
|
132
|
+
virtual_sol = reader.u64()
|
|
133
|
+
virtual_token = reader.u64()
|
|
134
|
+
real_sol = reader.u64()
|
|
135
|
+
real_token = reader.u64()
|
|
136
|
+
reader.pubkey() # fee_recipient
|
|
137
|
+
reader.u64() # fee_basis_points
|
|
138
|
+
reader.u64() # fee
|
|
139
|
+
creator = reader.pubkey()
|
|
140
|
+
quote_mint = _trade_quote_mint(reader)
|
|
141
|
+
return TradeEvent(
|
|
142
|
+
mint=mint,
|
|
143
|
+
sol_amount=sol_amount,
|
|
144
|
+
token_amount=token_amount,
|
|
145
|
+
is_buy=is_buy,
|
|
146
|
+
user=user,
|
|
147
|
+
timestamp=timestamp,
|
|
148
|
+
virtual_sol_reserves=virtual_sol,
|
|
149
|
+
virtual_token_reserves=virtual_token,
|
|
150
|
+
real_sol_reserves=real_sol,
|
|
151
|
+
real_token_reserves=real_token,
|
|
152
|
+
creator=creator,
|
|
153
|
+
quote_mint=quote_mint,
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _trade_quote_mint(reader: BorshReader) -> str | None:
|
|
158
|
+
"""Read the quote mint from the tail of a trade event.
|
|
159
|
+
|
|
160
|
+
Older events end earlier; in that case the quote asset is SOL.
|
|
161
|
+
"""
|
|
162
|
+
try:
|
|
163
|
+
reader.u64() # creator_fee_basis_points
|
|
164
|
+
reader.u64() # creator_fee
|
|
165
|
+
reader.bool() # track_volume
|
|
166
|
+
reader.u64() # total_unclaimed_tokens
|
|
167
|
+
reader.u64() # total_claimed_tokens
|
|
168
|
+
reader.u64() # current_sol_volume
|
|
169
|
+
reader.i64() # last_update_timestamp
|
|
170
|
+
reader.string() # ix_name
|
|
171
|
+
reader.bool() # mayhem_mode
|
|
172
|
+
for _ in range(4): # cashback and buyback: basis points and amount each
|
|
173
|
+
reader.u64()
|
|
174
|
+
shareholders = reader.u32()
|
|
175
|
+
if shareholders > MAX_SHAREHOLDERS:
|
|
176
|
+
raise DecodeError("implausible number of shareholders")
|
|
177
|
+
for _ in range(shareholders):
|
|
178
|
+
reader.pubkey() # address
|
|
179
|
+
reader.u16() # share_bps
|
|
180
|
+
return reader.pubkey()
|
|
181
|
+
except DecodeError:
|
|
182
|
+
return None
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _decode_complete(reader: BorshReader) -> CompleteEvent:
|
|
186
|
+
return CompleteEvent(
|
|
187
|
+
user=reader.pubkey(),
|
|
188
|
+
mint=reader.pubkey(),
|
|
189
|
+
bonding_curve=reader.pubkey(),
|
|
190
|
+
timestamp=reader.i64(),
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
_DECODERS = {
|
|
195
|
+
CREATE_EVENT: _decode_create,
|
|
196
|
+
TRADE_EVENT: _decode_trade,
|
|
197
|
+
COMPLETE_EVENT: _decode_complete,
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def decode_event(data: bytes) -> PumpEvent | None:
|
|
202
|
+
"""Decode one event payload. Unknown event types return ``None``.
|
|
203
|
+
|
|
204
|
+
Raises :class:`~pumpfun_toolkit.errors.DecodeError` if a known event is
|
|
205
|
+
truncated or malformed.
|
|
206
|
+
"""
|
|
207
|
+
decoder = _DECODERS.get(data[:8])
|
|
208
|
+
return decoder(BorshReader(data, 8)) if decoder else None
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def extract_program_data(logs: list[str], program_id: str = PUMP_PROGRAM_ID) -> list[bytes]:
|
|
212
|
+
"""Return the ``Program data`` payloads written by ``program_id`` itself.
|
|
213
|
+
|
|
214
|
+
The runtime logs ``Program <id> invoke [depth]`` on entry and
|
|
215
|
+
``Program <id> success`` or ``Program <id> failed: ...`` on exit. Tracking
|
|
216
|
+
these lines tells which program is running when a data line appears.
|
|
217
|
+
Lines that are not valid base64 are skipped.
|
|
218
|
+
"""
|
|
219
|
+
stack: list[str] = []
|
|
220
|
+
found: list[bytes] = []
|
|
221
|
+
for line in logs:
|
|
222
|
+
if line.startswith("Program data: "):
|
|
223
|
+
if stack and stack[-1] == program_id:
|
|
224
|
+
try:
|
|
225
|
+
found.append(base64.b64decode(line[14:], validate=True))
|
|
226
|
+
except ValueError:
|
|
227
|
+
continue
|
|
228
|
+
continue
|
|
229
|
+
if not line.startswith("Program "):
|
|
230
|
+
continue
|
|
231
|
+
parts = line.split(" ")
|
|
232
|
+
if len(parts) >= 4 and parts[2] == "invoke" and parts[3].startswith("["):
|
|
233
|
+
stack.append(parts[1])
|
|
234
|
+
elif (
|
|
235
|
+
len(parts) >= 3
|
|
236
|
+
and parts[2] in ("success", "failed:")
|
|
237
|
+
and stack
|
|
238
|
+
and stack[-1] == parts[1]
|
|
239
|
+
):
|
|
240
|
+
stack.pop()
|
|
241
|
+
return found
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def decode_logs(logs: list[str]) -> tuple[list[PumpEvent], int]:
|
|
245
|
+
"""Decode all Pump.fun events in a transaction log.
|
|
246
|
+
|
|
247
|
+
Returns the events and the number of Pump.fun payloads that could not be
|
|
248
|
+
decoded.
|
|
249
|
+
"""
|
|
250
|
+
events: list[PumpEvent] = []
|
|
251
|
+
failures = 0
|
|
252
|
+
for data in extract_program_data(logs):
|
|
253
|
+
try:
|
|
254
|
+
event = decode_event(data)
|
|
255
|
+
except DecodeError:
|
|
256
|
+
failures += 1
|
|
257
|
+
continue
|
|
258
|
+
if event is not None:
|
|
259
|
+
events.append(event)
|
|
260
|
+
return events, failures
|
pumpfun_toolkit/fees.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""Transaction fee arithmetic and unit conversions.
|
|
2
|
+
|
|
3
|
+
A Solana transaction costs
|
|
4
|
+
|
|
5
|
+
- a base fee of 5000 lamports per signature, and
|
|
6
|
+
- optionally a priority fee: compute unit limit times price per compute unit
|
|
7
|
+
(in micro-lamports), rounded up to whole lamports.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import math
|
|
11
|
+
import statistics
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from decimal import ROUND_HALF_UP, Decimal
|
|
14
|
+
|
|
15
|
+
from pumpfun_toolkit.constants import LAMPORTS_PER_SOL
|
|
16
|
+
|
|
17
|
+
BASE_FEE_LAMPORTS_PER_SIGNATURE = 5000
|
|
18
|
+
DEFAULT_COMPUTE_UNIT_LIMIT = 200_000
|
|
19
|
+
MICRO_LAMPORTS_PER_LAMPORT = 1_000_000
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True)
|
|
23
|
+
class FeeEstimate:
|
|
24
|
+
base_fee_lamports: int
|
|
25
|
+
priority_fee_lamports: int
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def total_lamports(self) -> int:
|
|
29
|
+
return self.base_fee_lamports + self.priority_fee_lamports
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def estimate_fee(
|
|
33
|
+
*,
|
|
34
|
+
signatures: int = 1,
|
|
35
|
+
compute_unit_limit: int = DEFAULT_COMPUTE_UNIT_LIMIT,
|
|
36
|
+
compute_unit_price_micro_lamports: int = 0,
|
|
37
|
+
) -> FeeEstimate:
|
|
38
|
+
"""Estimate the fee of a transaction. Raises ``ValueError`` for invalid inputs."""
|
|
39
|
+
if signatures < 1 or compute_unit_limit < 0 or compute_unit_price_micro_lamports < 0:
|
|
40
|
+
raise ValueError("signatures must be at least 1; limit and price must not be negative.")
|
|
41
|
+
priority = math.ceil(
|
|
42
|
+
compute_unit_limit * compute_unit_price_micro_lamports / MICRO_LAMPORTS_PER_LAMPORT
|
|
43
|
+
)
|
|
44
|
+
return FeeEstimate(BASE_FEE_LAMPORTS_PER_SIGNATURE * signatures, priority)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def typical_priority_price(recent_fees: list[int]) -> int:
|
|
48
|
+
"""Median of recently paid priority prices (micro-lamports per compute unit)."""
|
|
49
|
+
return int(statistics.median(recent_fees)) if recent_fees else 0
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def lamports_to_sol(lamports: int) -> Decimal:
|
|
53
|
+
return Decimal(lamports) / LAMPORTS_PER_SOL
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def sol_to_lamports(sol: Decimal) -> int:
|
|
57
|
+
"""Convert SOL to lamports, rounding half up."""
|
|
58
|
+
return int((sol * LAMPORTS_PER_SOL).to_integral_value(rounding=ROUND_HALF_UP))
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def lamports_to_fiat(lamports: int, sol_price: Decimal, places: str = "0.01") -> Decimal:
|
|
62
|
+
"""Value of ``lamports`` at ``sol_price`` (any currency), rounded to ``places``."""
|
|
63
|
+
value = lamports_to_sol(lamports) * sol_price
|
|
64
|
+
return value.quantize(Decimal(places), rounding=ROUND_HALF_UP)
|