computed bases

This commit is contained in:
tim
2025-10-25 18:18:18 -04:00
parent 2972152e58
commit ba08da2fca
14 changed files with 93 additions and 98 deletions

View File

@@ -18,7 +18,6 @@ interface IPartyPlanner is IOwnable {
/// @param name LP token name
/// @param symbol LP token symbol
/// @param tokens token addresses (n)
/// @param bases scaling bases for each token (n) - used when converting to/from internal 64.64 amounts
/// @param tradeFrac trade fraction in 64.64 fixed-point (as used by LMSR)
/// @param targetSlippage target slippage in 64.64 fixed-point (as used by LMSR)
/// @param swapFeePpm fee in parts-per-million, taken from swap input amounts before LMSR calculations
@@ -35,7 +34,6 @@ interface IPartyPlanner is IOwnable {
string memory name,
string memory symbol,
IERC20[] memory tokens,
uint256[] memory bases,
int128 tradeFrac,
int128 targetSlippage,
uint256 swapFeePpm,
@@ -53,7 +51,6 @@ interface IPartyPlanner is IOwnable {
/// @param name LP token name
/// @param symbol LP token symbol
/// @param tokens token addresses (n)
/// @param bases scaling bases for each token (n) - used when converting to/from internal 64.64 amounts
/// @param kappa liquidity parameter κ in 64.64 fixed-point used to derive b = κ * S(q)
/// @param swapFeePpm fee in parts-per-million, taken from swap input amounts before LMSR calculations
/// @param flashFeePpm fee in parts-per-million, taken for flash loans
@@ -69,7 +66,6 @@ interface IPartyPlanner is IOwnable {
string memory name,
string memory symbol,
IERC20[] memory tokens,
uint256[] memory bases,
int128 kappa,
uint256 swapFeePpm,
uint256 flashFeePpm,

View File

@@ -87,7 +87,6 @@ contract PartyPlanner is OwnableExternal, IPartyPlanner {
string memory name_,
string memory symbol_,
IERC20[] memory tokens_,
uint256[] memory bases_,
int128 kappa_,
uint256 swapFeePpm_,
uint256 flashFeePpm_,
@@ -115,7 +114,6 @@ contract PartyPlanner is OwnableExternal, IPartyPlanner {
name_,
symbol_,
tokens_,
bases_,
kappa_,
swapFeePpm_,
flashFeePpm_,
@@ -165,7 +163,6 @@ contract PartyPlanner is OwnableExternal, IPartyPlanner {
string memory name_,
string memory symbol_,
IERC20[] memory tokens_,
uint256[] memory bases_,
int128 tradeFrac_,
int128 targetSlippage_,
uint256 swapFeePpm_,
@@ -190,7 +187,6 @@ contract PartyPlanner is OwnableExternal, IPartyPlanner {
name_,
symbol_,
tokens_,
bases_,
computedKappa,
swapFeePpm_,
flashFeePpm_,

View File

@@ -99,7 +99,6 @@ contract PartyPool is PartyPoolBase, OwnableExternal, ERC20External, IPartyPool
/// @param name_ LP token name
/// @param symbol_ LP token symbol
/// @param tokens_ token addresses (n)
/// @param bases_ scaling _bases for each token (n) - used when converting to/from internal 64.64 amounts
/// @param kappa_ liquidity parameter κ (Q64.64) used to derive b = κ * S(q)
/// @param swapFeePpm_ fee in parts-per-million, taken from swap input amounts before LMSR calculations
/// @param flashFeePpm_ fee in parts-per-million, taken for flash loans
@@ -110,7 +109,6 @@ contract PartyPool is PartyPoolBase, OwnableExternal, ERC20External, IPartyPool
string memory name_,
string memory symbol_,
IERC20[] memory tokens_,
uint256[] memory bases_,
int128 kappa_,
uint256 swapFeePpm_,
uint256 flashFeePpm_,
@@ -126,9 +124,7 @@ contract PartyPool is PartyPoolBase, OwnableExternal, ERC20External, IPartyPool
{
require(owner_ != address(0));
require(tokens_.length > 1, "Pool: need >1 asset");
require(tokens_.length == bases_.length, "Pool: lengths mismatch");
_tokens = tokens_;
_bases = bases_;
KAPPA = kappa_;
require(swapFeePpm_ < 1_000_000, "Pool: fee >= ppm");
SWAP_FEE_PPM = swapFeePpm_;
@@ -153,6 +149,9 @@ contract PartyPool is PartyPoolBase, OwnableExternal, ERC20External, IPartyPool
unchecked {i++;}
}
// Allocate denominators (bases) to be computed during initialMint from initial deposits
_bases = new uint256[](n);
// Initialize caches to zero and protocol ledger
_cachedUintBalances = new uint256[](n);
_protocolFeesOwed = new uint256[](n);

View File

@@ -15,7 +15,6 @@ contract PartyPoolBalancedPair is PartyPool {
string memory name_,
string memory symbol_,
IERC20[] memory tokens_,
uint256[] memory bases_,
int128 kappa_,
uint256 swapFeePpm_,
uint256 flashFeePpm_,
@@ -25,7 +24,7 @@ contract PartyPoolBalancedPair is PartyPool {
PartyPoolSwapImpl swapMintImpl_,
PartyPoolMintImpl mintImpl_
)
PartyPool(owner_, name_, symbol_, tokens_, bases_, kappa_, swapFeePpm_, flashFeePpm_, protocolFeePpm_, protocolFeeAddress_, wrapperToken_, swapMintImpl_, mintImpl_)
PartyPool(owner_, name_, symbol_, tokens_, kappa_, swapFeePpm_, flashFeePpm_, protocolFeePpm_, protocolFeeAddress_, wrapperToken_, swapMintImpl_, mintImpl_)
{}
function _swapAmountsForExactInput(uint256 i, uint256 j, int128 a, int128 limitPrice) internal virtual override view

View File

@@ -15,7 +15,6 @@ interface IPartyPoolDeployer {
string memory name_,
string memory symbol_,
IERC20[] memory tokens_,
uint256[] memory bases_,
int128 kappa_,
uint256 swapFeePpm_,
uint256 flashFeePpm_,
@@ -33,7 +32,6 @@ contract PartyPoolDeployer is IPartyPoolDeployer {
string memory name_,
string memory symbol_,
IERC20[] memory tokens_,
uint256[] memory bases_,
int128 kappa_,
uint256 swapFeePpm_,
uint256 flashFeePpm_,
@@ -48,7 +46,6 @@ contract PartyPoolDeployer is IPartyPoolDeployer {
name_,
symbol_,
tokens_,
bases_,
kappa_,
swapFeePpm_,
flashFeePpm_,
@@ -67,7 +64,6 @@ contract PartyPoolBalancedPairDeployer is IPartyPoolDeployer {
string memory name_,
string memory symbol_,
IERC20[] memory tokens_,
uint256[] memory bases_,
int128 kappa_,
uint256 swapFeePpm_,
uint256 flashFeePpm_,
@@ -82,7 +78,6 @@ contract PartyPoolBalancedPairDeployer is IPartyPoolDeployer {
name_,
symbol_,
tokens_,
bases_,
kappa_,
swapFeePpm_,
flashFeePpm_,

View File

@@ -33,30 +33,38 @@ contract PartyPoolMintImpl is PartyPoolBase {
bool isInitialDeposit = _totalSupply == 0 || _lmsr.nAssets == 0;
require(isInitialDeposit, "initialMint: pool already initialized");
// Update cached balances for all assets
// Read initial on-chain balances, require all > 0, and compute denominators (bases) from deposits.
// We assume equal-valued deposits; set base[i] = depositAmount so internal q_i starts at 1.0.
int128[] memory newQInternal = new int128[](n);
uint256[] memory depositAmounts = new uint256[](n);
for (uint i = 0; i < n; ) {
uint256 bal = IERC20(_tokens[i]).balanceOf(address(this));
_cachedUintBalances[i] = bal;
newQInternal[i] = _uintToInternalFloor(bal, _bases[i]);
require(bal > 0, "initialMint: zero initial balance");
depositAmounts[i] = bal;
// Cache external balances
_cachedUintBalances[i] = bal;
// Set per-asset denominator to the observed deposit amount (at least 1)
_bases[i] = bal;
// Compute internal q_i = bal / base_i => ~1.0 in 64.64
newQInternal[i] = _uintToInternalFloor(bal, _bases[i]);
require(newQInternal[i] > int128(0), "initialMint: zero internal q");
unchecked { i++; }
}
// Initialize the stabilized LMSR state with provided kappa
_lmsr.init(newQInternal, KAPPA);
// Compute actual LP _tokens to mint based on size metric (scaled)
if( lpTokens != 0 )
lpMinted = lpTokens;
else {
int128 newTotal = _computeSizeMetric(newQInternal);
lpMinted = ABDKMath64x64.mulu(newTotal, LP_SCALE);
}
// Obey the passed-in initial LP amount. If 0, default to 1e18
lpMinted = lpTokens == 0 ? 1e18 : lpTokens;
require(lpMinted > 0, "initialMint: zero LP amount");
_mint(receiver, lpMinted);
if (lpMinted > 0) {
_mint(receiver, lpMinted);
}
emit IPartyPool.Mint(address(0), receiver, depositAmounts, lpMinted);
}