Verified contractPartialBUSL-1.1
PHSolver
Address0x5cbef3b831fee9ce7cc782b17edc0f5a55bada55
- Balance
0 ETH
- Transactions
0
- Compiler
solc 0.8.38
- Same code
Only this contract
Details
- Compiler
- solc 0.8.38+commit.1835d247
- Language
- solidity
- Optimization
- Yes, 30 runs
- EVM version
- shanghai
- License
- BUSL-1.1
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,483,510
- Creation transaction
- 0x08c2b3bb79…13b07c66e3
src/PHSolver.sol
// SPDX-License-Identifier: BUSL-1.1
// Copyright (c) 2026 Charu Punjani. Licensed under the Business Source License 1.1.
// PUNZI(TM) and PUNZI WARS(TM) are trademarks of Charu Punjani.
// See LICENSE for full terms.
pragma solidity 0.8.38;
/// @notice Read-only views of the token that calls the solver.
interface ICurveView {
function getBuyCost(uint256 currentK, uint256 tokenAmount) external view returns (uint256);
function cumulativeK() external view returns (uint256);
function totalSupply() external view returns (uint256);
function treasury() external view returns (uint256);
function collateralScale() external view returns (uint256);
}
/**
* @notice Interface of the sell-side book solver.
* @dev `Book` fields: `kk` the lifetime purchase position (cumulativeK), `n` circulating
* supply, `bal` the treasury in 18-decimal units, `tipLow` and `dLow` the stored tip
* position and depth, `elapsed` the time since the stored heal timestamp, scaled to
* HEAL_DURATION.
*/
interface IPHSolver {
struct Book {
uint256 kk;
uint256 n;
uint256 bal;
uint256 tipLow;
uint256 dLow;
uint256 elapsed;
}
function quote(address curve, uint256 tipLow, uint256 dLow, uint256 elapsed, uint256 amount)
external view returns (uint256 seg, uint256 nextTipLow, uint256 nextDepth, uint256 priced);
function HEAL_DURATION() external view returns (uint256);
function canonicalDomains() external view returns (string memory);
}
/**
* @title PHSolver
* @notice Computes the sell-side support book for PUNZI tokens: the tip position, the depth of
* the book below it, and the curve cost of the part of a sale inside the book.
* @dev Stateless, ownerless and read-only. A token calls it with a staticcall and prices the
* sale itself if the call fails or returns an out-of-range answer.
*/
contract PHSolver is IPHSolver {
/// @notice Heal length in seconds (30 minutes). Tokens scale the time since the last sale
/// to it.
uint256 public constant HEAL_DURATION = 1800;
/// @notice The dApp's canonical domains and ENS name, comma-separated.
/// @dev The same value on every PUNZI contract.
function canonicalDomains() external pure returns (string memory) {
return "punzi.xyz,punziwars.com,punzi.eth";
}
/// @dev Curve cost of the window of depth `d` that ends at position `t`. Returns
/// `type(uint256).max` when `d` exceeds the supply `n` or `t`, or the cost saturates.
function _bookAt(ICurveView c, uint256 t, uint256 d, uint256 n) private view returns (uint256) {
if (d > n || d > t) return type(uint256).max;
return d == 0 ? 0 : c.getBuyCost(t - d, d);
}
/// @dev True when the treasury `bal` covers 97% of a book worth `v` (the 3% spread floor).
/// Values above max/97 are not backed.
function _backed(uint256 v, uint256 bal) private pure returns (bool) {
return v <= type(uint256).max / 97 && (v * 97) / 100 <= bal;
}
/// @dev `_backed`, applied to the book with its tip at `t` and depth `d`.
function _fits(ICurveView c, uint256 t, uint256 d, uint256 n, uint256 bal) private view returns (bool) {
return _backed(_bookAt(c, t, d, n), bal);
}
/// @notice Price a sale against the book and return the state it leaves behind.
/// @param curve the calling token; its position, supply and treasury are read from it
/// @param tipLow stored tip position
/// @param dLow stored tip depth
/// @param elapsed time since the stored heal timestamp, scaled by the token to HEAL_DURATION
/// @param amount tokens being sold
/// @return seg curve cost of the part of the sale inside the book, or
/// `type(uint256).max` if it cannot be priced
/// @return nextTipLow tip position after the sale
/// @return nextDepth tip depth after the sale
/// @return priced tokens of the sale inside the book, the part `seg` prices (the
/// whole sale when the sale is larger than the supply)
function quote(address curve, uint256 tipLow, uint256 dLow, uint256 elapsed, uint256 amount)
external view override returns (uint256 seg, uint256 nextTipLow, uint256 nextDepth, uint256 priced)
{
ICurveView c = ICurveView(curve);
return quoteBook(
c,
Book({
kk: c.cumulativeK(),
n: c.totalSupply(),
bal: c.treasury() * c.collateralScale(),
tipLow: tipLow,
dLow: dLow,
elapsed: elapsed
}),
amount
);
}
/// @notice `quote`, for a caller-supplied `Book`.
/// @dev The book is not checked against the curve's state. Tokens use `quote`.
function quoteBook(ICurveView c, Book memory b, uint256 amount)
public view returns (uint256 seg, uint256 nextTipLow, uint256 nextDepth, uint256 priced)
{
(uint256 t, uint256 d) = tipAndDepth(c, b);
if (amount > b.n) {
// A sale larger than the supply cannot be priced: return the sentinel.
seg = type(uint256).max;
priced = amount;
} else {
// The sale walks down from the tip. Tokens past the book's depth are priced at zero.
priced = amount < d ? amount : d;
seg = priced == 0 ? 0 : c.getBuyCost(t - priced, priced);
}
nextTipLow = t > amount ? t - amount : 0;
nextDepth = d > amount ? d - amount : 0;
}
/// @notice The current tip position and the depth of the book below it.
/// @dev Over HEAL_DURATION the tip moves linearly from its stored position to `kk`, and the
/// depth from its stored value to the target `_dmax` gives at that position. The depth
/// takes the target at once when the target is lower or the interpolated depth is not
/// backed. The treasury limits the depth, never the tip's position. The book is not
/// checked against the curve's state.
function tipAndDepth(ICurveView c, Book memory b) public view returns (uint256 t, uint256 d) {
if (b.n == 0) return (0, 0);
if (b.tipLow >= b.kk || b.elapsed >= HEAL_DURATION) t = b.kk;
// If the interpolation would overflow, keep the stored position.
else if (b.elapsed != 0 && (b.kk - b.tipLow) > type(uint256).max / b.elapsed) t = b.tipLow;
else t = b.tipLow + ((b.kk - b.tipLow) * b.elapsed) / HEAL_DURATION;
uint256 dTarget = _dmax(c, b, t);
if (b.dLow >= dTarget || b.elapsed >= HEAL_DURATION) d = dTarget;
else {
// Interpolate; if that would overflow, keep the stored depth. Either way, fall back
// to the target when the result is not backed.
d = b.elapsed != 0 && (dTarget - b.dLow) > type(uint256).max / b.elapsed
? b.dLow
: b.dLow + ((dTarget - b.dLow) * b.elapsed) / HEAL_DURATION;
if (!_fits(c, t, d, b.n, b.bal)) d = dTarget;
}
}
/// @dev Maximum number of steps in the depth search.
uint256 private constant DEPTH_STEPS = 40;
/// @dev The tip depth at position `t`: the full depth (the smaller of the supply and `t`)
/// when that window is backed; otherwise the deepest backed depth the search finds.
/// The search keeps a backed depth `lo` (from zero, the empty window) and an unbacked
/// depth `bad` (from the full depth) and interpolates between them on book value (regula
/// falsi with the Illinois adjustment; bisection when the values are too large to
/// multiply safely). It stops when the backed book is within capB / 2^40 of `capB`, when
/// no depth lies between the two ends, or after DEPTH_STEPS steps.
function _dmax(ICurveView c, Book memory b, uint256 t) private view returns (uint256 lo) {
uint256 bad = b.n > t ? t : b.n;
uint256 vBad = _bookAt(c, t, bad, b.n);
if (_backed(vBad, b.bal)) return bad;
// capB is the largest value `_backed` accepts: `_backed(v)` holds exactly when v <= capB.
uint256 capB = b.bal > (type(uint256).max - 99) / 100 ? type(uint256).max / 97 : (b.bal * 100 + 99) / 97;
// Interpolation weights: each end's distance from capB (the empty window is worth zero).
uint256 wLo = capB;
uint256 wBad = vBad - capB;
uint256 moved; // the end the previous step moved: 1 = lo, 2 = bad
for (uint256 i; i < DEPTH_STEPS && bad - lo > 1; ++i) {
uint256 mid;
{
uint256 w = bad - lo;
// Interpolate only where the product cannot overflow; bisect otherwise.
uint256 step = (w | wLo | wBad) >> 128 == 0 ? (w * wLo) / (wLo + wBad) : w / 2;
if (step == 0) step = 1;
else if (step >= w) step = w - 1;
mid = lo + step;
}
uint256 v = _bookAt(c, t, mid, b.n);
if (_backed(v, b.bal)) {
lo = mid;
wLo = capB - v;
if (wLo <= capB >> 40) break;
if (moved == 1) wBad = (wBad + 1) / 2;
moved = 1;
} else {
bad = mid;
wBad = v - capB;
if (moved == 2) wLo = (wLo + 1) / 2;
moved = 2;
}
}
}
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": false,
"bytecodeHash": "none",
"useLiteralContent": false
},
"libraries": {},
"optimizer": {
"runs": 30,
"enabled": true
},
"evmVersion": "shanghai",
"remappings": [
"forge-std/=lib/forge-std/src/",
"@prb/math/=lib/prb-math/src/",
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/",
"prb-math/=lib/prb-math/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.