diff --git a/PiRC1/4-allocation/DOCUMENT.md b/PiRC1/4-allocation/DOCUMENT.md new file mode 100644 index 000000000..e8574b685 --- /dev/null +++ b/PiRC1/4-allocation/DOCUMENT.md @@ -0,0 +1,285 @@ +# Allocation Period Design Document — Mathematical Foundations and Applications + +## Chapter 1: Basic Concepts and Theoretical Foundations + +### 1.1 Purpose of the Allocation Period + +The **Allocation Period** is the phase of the PiRC launchpad flow in which participants' committed Pi is converted into project tokens and a Liquidity Pool (LP) is seeded, so that the token can trade freely once the Token Generation Event (TGE) state begins. Two design options are specified for this phase, differing in how the LP is formed and how engagement-based pricing is applied: + +- **Design Option 1 — Deposit-Only Model**: a single clearing price, LP formed purely by deposit, with a small token bucket reserved for engagement-tier discounts. +- **Design Option 2 — Deposit + Swap Model**: LP formed with a first deposit followed by a sequence of engagement-ranked swaps, producing a continuum of effective prices rather than discrete tiers. + +**Definition 1.1.1 (Common Notation)**: Let +- $C$ = total Pi committed by all participants during the Participation Window, +- $T$ = total ecosystem-token allocation made available through the launchpad for the project, +- $p_{list}$ = listing price of the token, in Pi per token, at which the LP is initialized or converges to. + +Both designs share the same governing idea: participants who commit Pi are also ranked by an **Engagement Score**, measured during the Participation Window, and that ranking determines how much of a discount (if any) a participant receives relative to $p_{list}$. + +### 1.2 Common State Transition + +Independent of design option, the allocation flow follows the same high-level state transition: + +$$ +\text{Escrow}(C, T) \;\rightarrow\; \text{LP}(x_0, y_0) \;\rightarrow\; \text{Participants receive tokens at } p_{eff,i} +$$ + +where $x_0$ is the Pi reserve and $y_0$ is the token reserve at LP initialization, and $p_{eff,i}$ is the effective acquisition price realized by participant $i$ after any engagement-based discount is applied. + +### 1.3 Escrow Lockup + +In both designs, once the Escrow Wallet has deposited its assets into the LP, its signing authority is permanently removed (set to 0) on-chain. This is an irreversible operation that guarantees the initial liquidity seeding the pool can never be withdrawn by any party, including the project team. + +--- + +## Chapter 2: Design Option 1 — Deposit-Only Model + +### 2.1 Token and Pi Buckets + +This is the **simple-model** allocation: all committed Pi goes into the LP, and each participant receives their base allocation at a single clearing price, with an additional engagement-based bonus paid from a small dedicated bucket. + +**Notation**: +- $T_{purchase}$ = tokens allocated to participants (purchase bucket), set equal to $T$ +- $T_{liquidity}$ = tokens reserved for liquidity seeding, set equal to $T$ +- $T_{engage} = 5\%\,T$ = tokens allocated for engagement discounts + +Total tokens supplied by the project to the launchpad: + +$$ +T_{purchase} + T_{liquidity} + T_{engage} = 2.05\,T +$$ + +This is the token's initial circulating supply at TGE, including tokens deposited into the LP. + +### 2.2 LP Formation and Clearing Price + +Participants receive $T_{purchase} = T$ tokens in exchange for total committed Pi $C$, at implied price: + +$$ +p = \frac{C}{T} +$$ + +The Escrow Wallet deposits the full $C$ Pi together with $T_{liquidity} = T$ tokens into the LP, so the pool initializes at: + +$$ +p_{list} = \frac{C}{T} = p +$$ + +i.e. the LP opens at exactly the price participants paid — there is no separate discovery step. Participant $i$'s base token allocation, proportional to their commitment $c_i$, is: + +$$ +t_i^{base} = \frac{c_i}{p_{list}} +$$ + +### 2.3 Engagement-Based Discount + +The $T_{engage} = 5\%\,T$ bucket distributes bonus tokens by Engagement Score rank, split into thirds: + +- Top 1/3 of participants (by engagement) share $\frac{2}{3}\,T_{engage}$, proportional to their commitment within that tier +- Middle 1/3 share $\frac{1}{3}\,T_{engage}$, proportional to commitment within that tier +- Bottom 1/3 receive no bonus + +Formally, with tier commitment totals $C_{top}$ and $C_{mid}$: + +$$ +t_i^{engage}= +\begin{cases} +\dfrac{2}{3}T_{engage}\cdot \dfrac{c_i}{C_{top}}, & i \in S_{top}\\[6pt] +\dfrac{1}{3}T_{engage}\cdot \dfrac{c_i}{C_{mid}}, & i \in S_{mid}\\[4pt] +0, & i \in S_{bottom} +\end{cases} +$$ + +Under a uniform commitment assumption across tiers, this works out to roughly a **10% token bonus** for the top tier and **5%** for the middle tier. + +### 2.4 Effective Price + +Participant $i$'s effective price is: + +$$ +p_{eff,i} = \frac{c_i}{t_i^{base} + t_i^{engage}} = \frac{p_{list}}{1+b_i}, \qquad b_i = \frac{t_i^{engage}}{t_i^{base}} +$$ + +Under the uniform assumption: + +| Tier | Bonus $b$ | $p_{eff}/p_{list}$ | +|---|---|---| +| Top 1/3 | 10% | ≈ 0.909 | +| Middle 1/3 | 5% | ≈ 0.952 | +| Bottom 1/3 | 0% | 1.000 | + +Because the discount comes entirely from a fixed bonus-token bucket, the effective price is a **step function** of engagement tier rather than a smooth curve — every participant in the same tier pays the same effective price. + +### 2.5 Summary — Design 1 + +- LP is formed exclusively by **deposit**; no swap operation is used. +- All committed Pi $C$ and $T$ tokens seed the LP, at $p_{list} = C/T$. +- Participants collectively receive $T + T_{engage}$ tokens. +- Discount is capped at ~10% and applied as a step function across three engagement tiers. + +--- + +## Chapter 3: Design Option 2 — Deposit + Swap Model + +### 3.1 Token and Pi Splits + +This design uses **both** deposit and swap operations to form the LP, producing a continuous discount curve rather than discrete tiers. + +Token allocation $T$ is split 80/20: +- 80% of $T$ is deposited into the LP +- 20% of $T$ is sold at listing price directly to participants + +Committed Pi $C$ is split 50/50: +- Bucket A ($C/2$): direct purchase of the 20% fixed-price portion +- Bucket B ($C/2$): engagement-ranked swaps executed against the LP + +### 3.2 Step 1 — Fixed-Price Delivery + +Half of committed Pi is transferred to the Escrow Wallet and directly buys 20% of $T$ at: + +$$ +p_{list} = \frac{C/2}{0.2\,T} = \frac{C}{0.4\,T} +$$ + +### 3.3 Step 2 — Escrow Deposit and Pool Creation + +The Pi from Step 1 ($C/2$) is paired with the remaining 80% of $T$ and deposited into the LP: + +$$ +p_{init} = \frac{C/2}{0.8\,T} = \frac{p_{list}}{4}, \qquad k = \frac{C}{2}\cdot 0.8\,T = 0.4\,C\,T +$$ + +$k$ is the constant-product invariant of the pool. The Escrow Wallet is then permanently locked. + +### 3.4 Step 3 — Automated Engagement-Based Swaps + +The remaining $C/2$ is swapped into the LP in order of Engagement Score, highest first. Because the pool follows the constant-product invariant, price rises monotonically as cumulative swap volume $s$ increases: + +$$ +x(s) = \frac{C}{2}+s, \qquad y(s) = \frac{k}{x(s)}, \qquad p_{swap}(s) = \frac{x(s)^2}{k} +$$ + +Normalizing by $p_{list}$ (using $k = 0.4\,C\,T$ and $p_{list} = C/(0.4T)$): + +$$ +\frac{p_{swap}(s)}{p_{list}} = \frac{1}{4}\left(1+\frac{2s}{C}\right)^2, \qquad s \in [0, C/2] +$$ + +This rises smoothly from $\tfrac14$ at $s=0$ to $1$ at $s = C/2$. + +### 3.5 Effective Acquisition Price + +Each participant's Pi is split 50/50 across Bucket A (fixed price $p_{list}$) and Bucket B (swap price $p_{swap}(s)$ at their position in the ranking), so the effective price is the harmonic mean: + +$$ +p_{eff}(s) = \frac{2\,p_{list}\,p_{swap}(s)}{p_{list}+p_{swap}(s)} +$$ + +with bounds: + +$$ +p_{eff}(0) = 0.4\,p_{list} \quad (\text{60% discount, most engaged}), \qquad p_{eff}(C/2) = p_{list} \quad (\text{0% discount, least engaged}) +$$ + +Discounted tokens (Step 3 only) carry a post-TGE lockup whose length scales with the size of the discount received; the fixed-price portion (Step 1) is never locked. LP swapping fees (0.3%) are ignored in the simplified formulas above. + +### 3.6 Summary — Design 2 + +- LP formed by **deposit followed by ranked swaps**. +- Discount is a smooth function of Engagement Score rank, up to 60% at the top and 0% at the bottom. +- LP access during the allocation period is restricted to the sequenced, pre-consented swaps; there is no open trading window. + +--- + +## Chapter 4: Comparison of Design Options + +| Property | Design 1 (Deposit-Only) | Design 2 (Deposit + Swap) | +|---|---|---| +| LP formation | Single deposit | Deposit, then ranked swaps | +| Pricing curve | Step function (3 tiers) | Continuous (AMM curve) | +| Max discount | ~10% | 60% | +| Extra token supply | $T_{engage}=5\%T$ minted/reserved | None — discount funded from spread between $p_{init}$ and $p_{list}$ | +| Lockups | Not specified for engagement bonus | Length scales with discount, Step 3 tokens only | +| Complexity | Low — one clearing price | Higher — sequenced automated swaps | + +Design 1 favors simplicity and a bounded, predictable token overhead. Design 2 favors a wider, market-priced discount range at the cost of operational complexity (ordered, automated swap execution with pre-signed consent). + +--- + +## Chapter 5: Implementation Notes + +### 5.1 Automation Requirements + +Both designs require no manual participant action after the commitment window closes: +- **Design 1**: bonus token distribution is computed once, after Engagement Score tiers are finalized. +- **Design 2**: swaps are executed automatically in ranked order, using consent signed at the time of commitment; the LP is not open to arbitrary trading until the allocation period ends. + +### 5.2 Algorithm — Ranked Swap Execution (Design 2) + +``` +Input: Sorted participant list P (descending Engagement Score), + LP reserves (x0, y0) after Step 2, constant product k +Output: Effective price and token allocation per participant + +1. x ← x0 // Pi reserve, starts at C/2 +2. y ← y0 // Token reserve, starts at 0.8T +3. for each participant i in P do +4. s_i ← c_i // this participant's Bucket B commitment +5. tokens_out ← y - k / (x + s_i) +6. p_swap_i ← s_i / tokens_out +7. p_eff_i ← 2 * p_list * p_swap_i / (p_list + p_swap_i) +8. x ← x + s_i +9. y ← y - tokens_out +10. record(i, tokens_out, p_eff_i) +11. end for +``` + +**Time Complexity**: $O(N)$ in the number of participants $N$, since each swap is a constant-time AMM update. + +### 5.3 Data Validation Checklist + +- $\sum_i c_i = C$ (all commitments reconciled against Escrow deposits) +- Token buckets sum to the declared launch allocation ($T$ for Design 1's purchase+liquidity, $T$ split 80/20 for Design 2) +- Engagement Score ranking is finalized and immutable before Step 3 swaps begin +- Escrow Wallet signing authority is verified as revoked on-chain after LP seeding + +--- + +## Chapter 6: Extensions and Open Questions + +### 6.1 Hybrid Designs + +A hybrid could reserve a small $T_{engage}$-style bucket (Design 1) on top of a swap-based discount curve (Design 2), giving a smooth curve with a guaranteed floor discount for all participants regardless of rank. + +### 6.2 Fee Sensitivity + +Design 2's formulas ignore the 0.3% LP swap fee. A refinement would fold the fee into $p_{swap}(s)$, slightly compressing the discount range for later (less-engaged) participants. + +### 6.3 Sybil and Engagement-Score Robustness + +Both designs assume the Engagement Score itself is resistant to gaming; this document does not specify the scoring methodology, which is treated as an external input finalized before the Allocation Period begins. + +--- + +## Appendix: Notation Reference + +| Symbol | Meaning | +|---|---| +| $C$ | Total Pi committed by participants | +| $T$ | Total ecosystem-token launch allocation | +| $p_{list}$ | Listing price (Pi per token) | +| $p_{init}$ | Initial LP spot price (Design 2) | +| $p_{swap}(s)$ | Marginal swap price at cumulative swap volume $s$ (Design 2) | +| $p_{eff,i}$ / $p_{eff}(s)$ | Participant's effective acquisition price | +| $k$ | Constant-product invariant of the LP | +| $T_{purchase}, T_{liquidity}, T_{engage}$ | Token buckets in Design 1 | +| $t_i^{base}, t_i^{engage}$ | Base and bonus token allocation, Design 1 | +| $S_{top}, S_{mid}, S_{bottom}$ | Engagement-tier participant sets, Design 1 | + +--- + +**Document Version**: v1.0 +**Status**: Draft — companion body document for `4-allocation design 1.md` and `4-allocation design 2.md` +**Language**: English +**Related files**: [`4-allocation design 1.md`](./4-allocation%20design%201.md), [`4-allocation design 2.md`](./4-allocation%20design%202.md), [`pirc_allocation_design2.json`](./pirc_allocation_design2.json) +**Next**: [`5-tge-state`](../5-tge-state/) \ No newline at end of file diff --git a/PiRC1/4-allocation/newversi/.CPP b/PiRC1/4-allocation/newversi/.CPP new file mode 100644 index 000000000..78d264638 --- /dev/null +++ b/PiRC1/4-allocation/newversi/.CPP @@ -0,0 +1,213 @@ +// PiRC - Section 4: Allocation Period - v2.0.0 +// Covers Design Option 1 (deposit-only, tiered discount) and +// Design Option 2 (deposit + swap, continuous discount curve). +// +// Build: g++ -std=c++17 -O2 -o pirc_allocation pirc_allocation.cpp + +#include +#include +#include +#include +#include +#include +#include + +namespace pirc { + +// ───────────────────────────────────────────── +// 1. DESIGN OPTION 1 CALCULATOR +// ───────────────────────────────────────────── + +struct ParticipantSummaryD1 { + double pi_committed; + std::string tier; + double tokens_base; + double tokens_bonus; + double tokens_total; + double effective_price; + double discount_percent; +}; + +class AllocationCalculatorD1 { +public: + double T, C, p_list, T_engage; + + AllocationCalculatorD1(double T_, double C_) : T(T_), C(C_) { + if (T <= 0 || C <= 0) { + throw std::invalid_argument("T and C must be positive numbers."); + } + p_list = C / T; + T_engage = 0.05 * T; + } + + static std::string tierFor(double rankFraction) { + if (rankFraction < 1.0 / 3.0) return "top"; + if (rankFraction < 2.0 / 3.0) return "middle"; + return "bottom"; + } + + static double bonusPctFor(double rankFraction) { + const std::string tier = tierFor(rankFraction); + if (tier == "top") return 10.0; + if (tier == "middle") return 5.0; + return 0.0; + } + + ParticipantSummaryD1 participantSummary(double piCommitted, double rankFraction) const { + const double tokensBase = piCommitted / p_list; + const double bonusPct = bonusPctFor(rankFraction); + const double tokensBonus = tokensBase * (bonusPct / 100.0); + const double tokensTotal = tokensBase + tokensBonus; + const double effectivePrice = piCommitted / tokensTotal; + + return ParticipantSummaryD1{ + piCommitted, tierFor(rankFraction), tokensBase, tokensBonus, + tokensTotal, effectivePrice, + ((p_list - effectivePrice) / p_list) * 100.0 + }; + } +}; + +// ───────────────────────────────────────────── +// 2. DESIGN OPTION 2 CALCULATOR +// ───────────────────────────────────────────── + +struct BucketResult { + double pi_used; + double tokens_received; + double price; +}; + +struct ParticipantSummaryD2 { + double pi_committed; + double s_cumulative; + BucketResult bucket_a; + BucketResult bucket_b; + double total_tokens; + double effective_price; + double discount_percent; +}; + +struct CurvePoint { + double s; + double s_fraction; + double p_swap; + double p_swap_norm; + double p_eff; + double p_eff_norm; + double discount; +}; + +class AllocationCalculatorD2 { +public: + double T, C, p_list, k, x0, y0, p_init; + + AllocationCalculatorD2(double T_, double C_) : T(T_), C(C_) { + if (T <= 0 || C <= 0) { + throw std::invalid_argument("T and C must be positive numbers."); + } + p_list = C / (0.4 * T); + k = 0.4 * C * T; + x0 = C / 2.0; + y0 = 0.8 * T; + p_init = p_list / 4.0; + } + + double xAtS(double s) const { return x0 + s; } + double yAtS(double s) const { return k / xAtS(s); } + + double pSwap(double s) const { + const double x = xAtS(s); + return (x * x) / k; + } + + double pSwapNorm(double s) const { + return 0.25 * std::pow(1.0 + (2.0 * s) / C, 2); + } + + double pEff(double s) const { + const double ps = pSwap(s); + return (2.0 * p_list * ps) / (p_list + ps); + } + + double discountPercent(double s) const { + return ((p_list - pEff(s)) / p_list) * 100.0; + } + + ParticipantSummaryD2 participantSummary(double piCommitted, double s) const { + const double halfPi = piCommitted / 2.0; + const double tokensA = halfPi / p_list; + const double tokensB = halfPi / pSwap(s); + const double totalTokens = tokensA + tokensB; + const double effectivePrice = piCommitted / totalTokens; + + return ParticipantSummaryD2{ + piCommitted, s, + BucketResult{halfPi, tokensA, p_list}, + BucketResult{halfPi, tokensB, pSwap(s)}, + totalTokens, effectivePrice, discountPercent(s) + }; + } + + std::vector effectivePriceCurve(int n = 11) const { + std::vector curve; + curve.reserve(n); + const double halfC = C / 2.0; + for (int i = 0; i < n; ++i) { + const double s = (static_cast(i) / (n - 1)) * halfC; + const double pe = pEff(s); + curve.push_back(CurvePoint{ + s, s / halfC, pSwap(s), pSwapNorm(s), pe, pe / p_list, discountPercent(s) + }); + } + return curve; + } +}; + +} // namespace pirc + +// ───────────────────────────────────────────── +// 3. DEMO +// ───────────────────────────────────────────── + +int main() { + std::cout << std::fixed << std::setprecision(4); + + std::cout << "=== Design 1 Demo (Deposit-Only) ===\n"; + pirc::AllocationCalculatorD1 d1(1'000'000.0, 250'000.0); + std::cout << "p_list=" << d1.p_list << " T_engage=" << d1.T_engage << "\n"; + + const std::vector> d1_participants = { + {5000, 0.05}, {2000, 0.40}, {1000, 0.85} + }; + for (const auto& [pi, rank] : d1_participants) { + auto s = d1.participantSummary(pi, rank); + std::cout << " pi=" << s.pi_committed << " tier=" << s.tier + << " tokensTotal=" << s.tokens_total + << " effPrice=" << s.effective_price + << " discount%=" << s.discount_percent << "\n"; + } + + std::cout << "\n=== Design 2 Demo (Deposit + Swap) ===\n"; + pirc::AllocationCalculatorD2 d2(1'000'000.0, 400'000.0); + std::cout << "p_list=" << d2.p_list << " p_init=" << d2.p_init << " k=" << d2.k << "\n"; + + std::cout << "-- Effective price curve --\n"; + for (const auto& pt : d2.effectivePriceCurve()) { + std::cout << " sFraction=" << pt.s_fraction << " pEffNorm=" << pt.p_eff_norm << "\n"; + } + + std::cout << "-- Participant examples --\n"; + const std::vector> d2_participants = { + {1000, 0}, {1000, 100000}, {1000, 200000} + }; + for (const auto& [pi, s] : d2_participants) { + auto sum = d2.participantSummary(pi, s); + std::cout << " pi=" << sum.pi_committed << " s=" << sum.s_cumulative + << " totalTokens=" << sum.total_tokens + << " effPrice=" << sum.effective_price + << " discount%=" << sum.discount_percent << "\n"; + } + + return 0; +} diff --git a/PiRC1/4-allocation/newversi/.JS b/PiRC1/4-allocation/newversi/.JS new file mode 100644 index 000000000..4acff87ad --- /dev/null +++ b/PiRC1/4-allocation/newversi/.JS @@ -0,0 +1,265 @@ +/** + * PiRC - Section 4: Allocation Period - v2.0.0 + * Covers Design Option 1 (deposit-only, tiered discount) and + * Design Option 2 (deposit + swap, continuous discount curve). + */ + +'use strict'; + +// ───────────────────────────────────────────── +// 1. STATIC DATABASE +// ───────────────────────────────────────────── + +const PIRC_ALLOCATION_DB = { + document: { + title: 'PiRC - Section 4: Allocation Period', + version: '2.0.0', + nextSection: '5-tge-state', + }, + + notation: { + T: { symbol: 'T', name: 'Total Ecosystem Token Allocation', unit: 'tokens' }, + C: { symbol: 'C', name: 'Total Pi Committed', unit: 'Pi' }, + p_list: { symbol: 'p_list', name: 'Listing Price', unit: 'Pi/token' }, + T_engage: { symbol: 'T_engage', name: 'Engagement Bonus Bucket (Design 1)', fraction: 0.05, unit: 'tokens' }, + }, + + design1: { + label: 'Design Option 1 - Deposit-Only Model', + tokenBuckets: { + purchase: { fraction: 1.00, destination: 'Participants (base allocation)' }, + liquidity: { fraction: 1.00, destination: 'Liquidity Pool (LP)' }, + engage: { fraction: 0.05, destination: 'Engagement bonus tiers' }, + }, + engagementTiers: [ + { tier: 'top', rankRange: [0.0, 1 / 3], engageShare: 2 / 3, avgBonusPct: 10.0, pEffOverPList: 0.909 }, + { tier: 'middle', rankRange: [1 / 3, 2 / 3], engageShare: 1 / 3, avgBonusPct: 5.0, pEffOverPList: 0.952 }, + { tier: 'bottom', rankRange: [2 / 3, 1.0], engageShare: 0, avgBonusPct: 0.0, pEffOverPList: 1.000 }, + ], + }, + + design2: { + label: 'Design Option 2 - Deposit + Swap Model', + tokenSplit: { + lpPortion: { fraction: 0.8, destination: 'Liquidity Pool (LP)' }, + fixedPricePortion: { fraction: 0.2, destination: 'Sold at Listing Price to Pioneers' }, + }, + piSplit: { + bucketA: { fraction: 0.5, purpose: 'Direct purchase of 20% of T at listing price' }, + bucketB: { fraction: 0.5, purpose: 'Engagement-ranked swaps from LP' }, + }, + effectivePriceData: [ + { sFraction: 0.0, pEffNorm: 0.400 }, { sFraction: 0.1, pEffNorm: 0.465 }, + { sFraction: 0.2, pEffNorm: 0.529 }, { sFraction: 0.3, pEffNorm: 0.594 }, + { sFraction: 0.4, pEffNorm: 0.658 }, { sFraction: 0.5, pEffNorm: 0.720 }, + { sFraction: 0.6, pEffNorm: 0.780 }, { sFraction: 0.7, pEffNorm: 0.839 }, + { sFraction: 0.8, pEffNorm: 0.895 }, { sFraction: 0.9, pEffNorm: 0.949 }, + { sFraction: 1.0, pEffNorm: 1.000 }, + ], + }, + + chains: { + supported: ['Pi Network (Stellar/Soroban)', 'Solana', 'Ethereum'], + defaultSettlement: 'Pi Network (Stellar/Soroban)', + }, +}; + +// ───────────────────────────────────────────── +// 2. DESIGN OPTION 1 CALCULATOR +// ───────────────────────────────────────────── + +class PiRCAllocationCalculatorD1 { + /** + * @param {number} T - Total token launch allocation + * @param {number} C - Total Pi committed by all participants + */ + constructor(T, C) { + if (!Number.isFinite(T) || !Number.isFinite(C) || T <= 0 || C <= 0) { + throw new Error('T and C must be finite positive numbers.'); + } + this.T = T; + this.C = C; + this.p_list = C / T; + this.T_engage = 0.05 * T; + } + + /** Which tier a participant falls into, given their engagement rank fraction (0 = most engaged) */ + tierFor(rankFraction) { + if (rankFraction < 1 / 3) return 'top'; + if (rankFraction < 2 / 3) return 'middle'; + return 'bottom'; + } + + bonusPctFor(rankFraction) { + const tier = this.tierFor(rankFraction); + if (tier === 'top') return 10.0; + if (tier === 'middle') return 5.0; + return 0.0; + } + + /** + * @param {number} piCommitted + * @param {number} rankFraction - 0 (most engaged) to 1 (least engaged) + */ + participantSummary(piCommitted, rankFraction) { + const tokensBase = piCommitted / this.p_list; + const bonusPct = this.bonusPctFor(rankFraction); + const tokensBonus = tokensBase * (bonusPct / 100); + const tokensTotal = tokensBase + tokensBonus; + const effectivePrice = piCommitted / tokensTotal; + + return { + piCommitted, + tier: this.tierFor(rankFraction), + tokensBase, + tokensBonus, + tokensTotal, + effectivePrice, + discountPercent: ((this.p_list - effectivePrice) / this.p_list) * 100, + }; + } + + poolSummary() { + return { + T: this.T, + C: this.C, + p_list: this.p_list, + T_engage: this.T_engage, + totalCirculatingSupply: 2.05 * this.T, + }; + } +} + +// ───────────────────────────────────────────── +// 3. DESIGN OPTION 2 CALCULATOR +// ───────────────────────────────────────────── + +class PiRCAllocationCalculatorD2 { + /** + * @param {number} T - Total token launch allocation + * @param {number} C - Total Pi committed by all participants + */ + constructor(T, C) { + if (!Number.isFinite(T) || !Number.isFinite(C) || T <= 0 || C <= 0) { + throw new Error('T and C must be finite positive numbers.'); + } + this.T = T; + this.C = C; + this.p_list = C / (0.4 * T); + this.k = 0.4 * C * T; + this.x0 = C / 2; + this.y0 = 0.8 * T; + this.p_init = this.p_list / 4; + } + + xAtS(s) { return this.x0 + s; } + yAtS(s) { return this.k / this.xAtS(s); } + + pSwap(s) { + const x = this.xAtS(s); + return (x * x) / this.k; + } + + pSwapNorm(s) { + return 0.25 * Math.pow(1 + (2 * s) / this.C, 2); + } + + pEff(s) { + const ps = this.pSwap(s); + return (2 * this.p_list * ps) / (this.p_list + ps); + } + + discountPercent(s) { + return ((this.p_list - this.pEff(s)) / this.p_list) * 100; + } + + participantSummary(piCommitted, s) { + const halfPi = piCommitted / 2; + const tokensA = halfPi / this.p_list; + const tokensB = halfPi / this.pSwap(s); + const totalTokens = tokensA + tokensB; + const effectivePrice = piCommitted / totalTokens; + + return { + piCommitted, + s_cumulative: s, + bucketA: { piUsed: halfPi, tokensReceived: tokensA, price: this.p_list }, + bucketB: { piUsed: halfPi, tokensReceived: tokensB, price: this.pSwap(s) }, + totalTokens, + effectivePrice, + discountPercent: this.discountPercent(s), + }; + } + + effectivePriceCurve(n = 11) { + const halfC = this.C / 2; + return Array.from({ length: n }, (_, i) => { + const s = (i / (n - 1)) * halfC; + const pEff = this.pEff(s); + return { + s, + sFraction: s / halfC, + pSwap: this.pSwap(s), + pSwapNorm: this.pSwapNorm(s), + pEff, + pEffNorm: pEff / this.p_list, + discount: this.discountPercent(s), + }; + }); + } + + poolSummary() { + return { + T: this.T, + C: this.C, + p_list: this.p_list, + p_init: this.p_init, + k: this.k, + lpPiReserve: this.x0, + lpTokenReserve: this.y0, + minEffPrice: this.pEff(0), + maxEffPrice: this.pEff(this.C / 2), + maxDiscountPct: this.discountPercent(0), + minDiscountPct: this.discountPercent(this.C / 2), + }; + } +} + +// ───────────────────────────────────────────── +// 4. DEMO +// ───────────────────────────────────────────── + +function runDemo() { + console.log('=== Design 1 Demo (Deposit-Only) ==='); + const d1 = new PiRCAllocationCalculatorD1(1_000_000, 250_000); + console.table(d1.poolSummary()); + console.table([ + d1.participantSummary(5000, 0.05), + d1.participantSummary(2000, 0.40), + d1.participantSummary(1000, 0.85), + ]); + + console.log('\n=== Design 2 Demo (Deposit + Swap) ==='); + const d2 = new PiRCAllocationCalculatorD2(1_000_000, 400_000); + console.table(d2.poolSummary()); + console.table(d2.effectivePriceCurve()); + console.table([ + d2.participantSummary(1000, 0), + d2.participantSummary(1000, 100_000), + d2.participantSummary(1000, 200_000), + ]); +} + +if (require.main === module) { + runDemo(); +} + +// ───────────────────────────────────────────── +// 5. EXPORTS +// ───────────────────────────────────────────── + +module.exports = { + PIRC_ALLOCATION_DB, + PiRCAllocationCalculatorD1, + PiRCAllocationCalculatorD2, +}; diff --git a/PiRC1/4-allocation/newversi/.JSON b/PiRC1/4-allocation/newversi/.JSON new file mode 100644 index 000000000..c18a9c43f --- /dev/null +++ b/PiRC1/4-allocation/newversi/.JSON @@ -0,0 +1,128 @@ +{ + "document": { + "title": "PiRC - Section 4: Allocation Period", + "version": "2.0.0", + "source_design1": "https://github.com/Tsukimarf/PiRC/blob/main/PiRC1/4-allocation/4-allocation%20design%201.md", + "source_design2": "https://github.com/Tsukimarf/PiRC/blob/main/PiRC1/4-allocation/4-allocation%20design%202.md", + "next_section": "5-tge-state", + "changelog": [ + { "version": "1.0.0", "note": "Design Option 2 only (deposit + swap)" }, + { "version": "2.0.0", "note": "Added Design Option 1 (deposit-only); unified schema across both options; multi-language database (SQL, JS, JSON, Python, C++)" } + ] + }, + + "notation": { + "T": { "symbol": "T", "name": "Total Ecosystem Token Allocation", "unit": "tokens" }, + "C": { "symbol": "C", "name": "Total Pi Committed", "unit": "Pi" }, + "p_list": { "symbol": "p_list", "name": "Listing Price", "unit": "Pi/token" }, + "p_init": { "symbol": "p_init", "name": "Initial LP Spot Price (Design 2)", "unit": "Pi/token" }, + "T_engage": { "symbol": "T_engage", "name": "Engagement Bonus Bucket (Design 1)", "fraction": 0.05, "unit": "tokens" } + }, + + "designs": { + "1": { + "label": "Design Option 1 - Deposit-Only Model", + "description": "Single clearing price; LP formed purely by deposit; engagement discount from a fixed 5% token bonus bucket split across three tiers.", + "token_buckets": { + "purchase": { "fraction": 1.0, "formula": "T", "destination": "Participants (base allocation)" }, + "liquidity": { "fraction": 1.0, "formula": "T", "destination": "Liquidity Pool (LP)" }, + "engage": { "fraction": 0.05, "formula": "0.05 * T", "destination": "Engagement bonus tiers" } + }, + "listing_price_formula": "p_list = C / T", + "engagement_tiers": [ + { "tier": "top", "rank_fraction": [0.0, 0.3333], "engage_share": 0.6667, "avg_bonus_pct": 10.0, "p_eff_over_p_list": 0.909 }, + { "tier": "middle", "rank_fraction": [0.3333, 0.6667], "engage_share": 0.3333, "avg_bonus_pct": 5.0, "p_eff_over_p_list": 0.952 }, + { "tier": "bottom", "rank_fraction": [0.6667, 1.0], "engage_share": 0.0, "avg_bonus_pct": 0.0, "p_eff_over_p_list": 1.000 } + ] + }, + "2": { + "label": "Design Option 2 - Deposit + Swap Model", + "description": "LP formation using both deposit and swap operations; continuous engagement-ranked discount curve.", + "token_split": { + "lp_portion": { "percent": 80, "fraction": 0.8, "destination": "Liquidity Pool (LP)" }, + "fixed_price_portion":{ "percent": 20, "fraction": 0.2, "destination": "Sold at Listing Price to Pioneers" } + }, + "pi_split": { + "bucket_A": { "label": "Bucket A - Fixed Price (Step 1)", "fraction": 0.5, "purpose": "Direct purchase of 20% of T at listing price" }, + "bucket_B": { "label": "Bucket B - Engagement Swaps (Step 3)", "fraction": 0.5, "purpose": "Engagement-ranked swaps from LP" } + }, + "listing_price_formula": "p_list = C / (0.4 * T)", + "steps": [ + { + "step": 1, + "name": "Fixed-Price Delivery", + "pi_used_formula": "C / 2", + "tokens_delivered_formula": "0.2 * T", + "listing_price_formula": "p_list = (C/2) / (0.2*T) = C / (0.4*T)" + }, + { + "step": 2, + "name": "Escrow Deposit and Pool Creation", + "pi_deposited_formula": "C / 2", + "tokens_deposited_formula": "0.8 * T", + "initial_spot_price_formula": "p_init = (C/2) / (0.8*T) = p_list / 4", + "constant_product_formula": "k = 0.4 * C * T", + "escrow_lockup": { "action": "Signing authority removed to 0", "irreversible": true } + }, + { + "step": 3, + "name": "Automated Engagement-Based Swaps", + "ranking_basis": "Engagement Score (Participation Window)", + "order": "Highest-to-lowest engagement", + "swap_price_range": { "first": "p_list / 4", "last": "p_list" }, + "discount_range": { "max_percent": 60, "min_percent": 0 }, + "lockup_policy": "Higher discount -> longer post-TGE lockup (Step 3 tokens only)", + "lp_swap_fee_percent": 0.3 + } + ], + "effective_price_curve": [ + { "s_fraction": 0.0, "p_eff_norm": 0.400 }, + { "s_fraction": 0.1, "p_eff_norm": 0.465 }, + { "s_fraction": 0.2, "p_eff_norm": 0.529 }, + { "s_fraction": 0.3, "p_eff_norm": 0.594 }, + { "s_fraction": 0.4, "p_eff_norm": 0.658 }, + { "s_fraction": 0.5, "p_eff_norm": 0.720 }, + { "s_fraction": 0.6, "p_eff_norm": 0.780 }, + { "s_fraction": 0.7, "p_eff_norm": 0.839 }, + { "s_fraction": 0.8, "p_eff_norm": 0.895 }, + { "s_fraction": 0.9, "p_eff_norm": 0.949 }, + { "s_fraction": 1.0, "p_eff_norm": 1.000 } + ] + } + }, + + "chains": { + "supported": ["Pi Network (Stellar/Soroban)", "Solana", "Ethereum"], + "default_settlement": "Pi Network (Stellar/Soroban)", + "note": "Allocation math is chain-agnostic; escrow lock and LP-seeding actions map to Soroban contract calls on Pi Network by default." + }, + + "seed_data": { + "demo_projects": [ + { + "project_id": "demo-001", + "name": "Demo Launch Alpha", + "design_option": 1, + "T": 1000000, + "C": 250000, + "participants": [ + { "participant_id": "p1", "pi_committed": 5000, "engagement_rank_pct": 0.05 }, + { "participant_id": "p2", "pi_committed": 2000, "engagement_rank_pct": 0.40 }, + { "participant_id": "p3", "pi_committed": 1000, "engagement_rank_pct": 0.85 } + ] + }, + { + "project_id": "demo-002", + "name": "Demo Launch Beta", + "design_option": 2, + "T": 1000000, + "C": 400000, + "participants": [ + { "participant_id": "p1", "pi_committed": 1000, "s_cumulative": 0 }, + { "participant_id": "p2", "pi_committed": 1000, "s_cumulative": 100000 }, + { "participant_id": "p3", "pi_committed": 1000, "s_cumulative": 200000 } + ] + } + ] + } +} diff --git a/PiRC1/4-allocation/newversi/.PY b/PiRC1/4-allocation/newversi/.PY new file mode 100644 index 000000000..e97f1a90e --- /dev/null +++ b/PiRC1/4-allocation/newversi/.PY @@ -0,0 +1,257 @@ +""" +PiRC - Section 4: Allocation Period - v2.0.0 +Covers Design Option 1 (deposit-only, tiered discount) and +Design Option 2 (deposit + swap, continuous discount curve). +""" + +from __future__ import annotations + +from dataclasses import dataclass, field +from typing import List, Dict, Any + +# ───────────────────────────────────────────── +# 1. STATIC DATABASE +# ───────────────────────────────────────────── + +PIRC_ALLOCATION_DB: Dict[str, Any] = { + "document": { + "title": "PiRC - Section 4: Allocation Period", + "version": "2.0.0", + "next_section": "5-tge-state", + }, + "notation": { + "T": {"symbol": "T", "name": "Total Ecosystem Token Allocation", "unit": "tokens"}, + "C": {"symbol": "C", "name": "Total Pi Committed", "unit": "Pi"}, + "p_list": {"symbol": "p_list", "name": "Listing Price", "unit": "Pi/token"}, + "T_engage": {"symbol": "T_engage", "name": "Engagement Bonus Bucket (Design 1)", + "fraction": 0.05, "unit": "tokens"}, + }, + "design1": { + "label": "Design Option 1 - Deposit-Only Model", + "token_buckets": { + "purchase": {"fraction": 1.00, "destination": "Participants (base allocation)"}, + "liquidity": {"fraction": 1.00, "destination": "Liquidity Pool (LP)"}, + "engage": {"fraction": 0.05, "destination": "Engagement bonus tiers"}, + }, + "engagement_tiers": [ + {"tier": "top", "rank_range": (0.0, 1 / 3), "engage_share": 2 / 3, + "avg_bonus_pct": 10.0, "p_eff_over_p_list": 0.909}, + {"tier": "middle", "rank_range": (1 / 3, 2 / 3), "engage_share": 1 / 3, + "avg_bonus_pct": 5.0, "p_eff_over_p_list": 0.952}, + {"tier": "bottom", "rank_range": (2 / 3, 1.0), "engage_share": 0.0, + "avg_bonus_pct": 0.0, "p_eff_over_p_list": 1.000}, + ], + }, + "design2": { + "label": "Design Option 2 - Deposit + Swap Model", + "token_split": { + "lp_portion": {"fraction": 0.8, "destination": "Liquidity Pool (LP)"}, + "fixed_price_portion": {"fraction": 0.2, "destination": "Sold at Listing Price to Pioneers"}, + }, + "pi_split": { + "bucket_A": {"fraction": 0.5, "purpose": "Direct purchase of 20% of T at listing price"}, + "bucket_B": {"fraction": 0.5, "purpose": "Engagement-ranked swaps from LP"}, + }, + "effective_price_data": [ + {"s_fraction": 0.0, "p_eff_norm": 0.400}, {"s_fraction": 0.1, "p_eff_norm": 0.465}, + {"s_fraction": 0.2, "p_eff_norm": 0.529}, {"s_fraction": 0.3, "p_eff_norm": 0.594}, + {"s_fraction": 0.4, "p_eff_norm": 0.658}, {"s_fraction": 0.5, "p_eff_norm": 0.720}, + {"s_fraction": 0.6, "p_eff_norm": 0.780}, {"s_fraction": 0.7, "p_eff_norm": 0.839}, + {"s_fraction": 0.8, "p_eff_norm": 0.895}, {"s_fraction": 0.9, "p_eff_norm": 0.949}, + {"s_fraction": 1.0, "p_eff_norm": 1.000}, + ], + }, + "chains": { + "supported": ["Pi Network (Stellar/Soroban)", "Solana", "Ethereum"], + "default_settlement": "Pi Network (Stellar/Soroban)", + }, +} + + +# ───────────────────────────────────────────── +# 2. DESIGN OPTION 1 CALCULATOR +# ───────────────────────────────────────────── + +@dataclass +class ParticipantSummaryD1: + pi_committed: float + tier: str + tokens_base: float + tokens_bonus: float + tokens_total: float + effective_price: float + discount_percent: float + + +class PiRCAllocationCalculatorD1: + """Design Option 1: deposit-only LP formation with tiered engagement bonus.""" + + def __init__(self, T: float, C: float): + if T <= 0 or C <= 0: + raise ValueError("T and C must be positive numbers.") + self.T = T + self.C = C + self.p_list = C / T + self.T_engage = 0.05 * T + + @staticmethod + def tier_for(rank_fraction: float) -> str: + if rank_fraction < 1 / 3: + return "top" + if rank_fraction < 2 / 3: + return "middle" + return "bottom" + + @staticmethod + def bonus_pct_for(rank_fraction: float) -> float: + tier = PiRCAllocationCalculatorD1.tier_for(rank_fraction) + return {"top": 10.0, "middle": 5.0, "bottom": 0.0}[tier] + + def participant_summary(self, pi_committed: float, rank_fraction: float) -> ParticipantSummaryD1: + tokens_base = pi_committed / self.p_list + bonus_pct = self.bonus_pct_for(rank_fraction) + tokens_bonus = tokens_base * (bonus_pct / 100) + tokens_total = tokens_base + tokens_bonus + effective_price = pi_committed / tokens_total + + return ParticipantSummaryD1( + pi_committed=pi_committed, + tier=self.tier_for(rank_fraction), + tokens_base=tokens_base, + tokens_bonus=tokens_bonus, + tokens_total=tokens_total, + effective_price=effective_price, + discount_percent=((self.p_list - effective_price) / self.p_list) * 100, + ) + + def pool_summary(self) -> Dict[str, float]: + return { + "T": self.T, + "C": self.C, + "p_list": self.p_list, + "T_engage": self.T_engage, + "total_circulating_supply": 2.05 * self.T, + } + + +# ───────────────────────────────────────────── +# 3. DESIGN OPTION 2 CALCULATOR +# ───────────────────────────────────────────── + +@dataclass +class ParticipantSummaryD2: + pi_committed: float + s_cumulative: float + bucket_a: Dict[str, float] + bucket_b: Dict[str, float] + total_tokens: float + effective_price: float + discount_percent: float + + +class PiRCAllocationCalculatorD2: + """Design Option 2: deposit + engagement-ranked swap LP formation.""" + + def __init__(self, T: float, C: float): + if T <= 0 or C <= 0: + raise ValueError("T and C must be positive numbers.") + self.T = T + self.C = C + self.p_list = C / (0.4 * T) + self.k = 0.4 * C * T + self.x0 = C / 2 + self.y0 = 0.8 * T + self.p_init = self.p_list / 4 + + def x_at_s(self, s: float) -> float: + return self.x0 + s + + def y_at_s(self, s: float) -> float: + return self.k / self.x_at_s(s) + + def p_swap(self, s: float) -> float: + x = self.x_at_s(s) + return (x * x) / self.k + + def p_swap_norm(self, s: float) -> float: + return 0.25 * (1 + (2 * s) / self.C) ** 2 + + def p_eff(self, s: float) -> float: + ps = self.p_swap(s) + return (2 * self.p_list * ps) / (self.p_list + ps) + + def discount_percent(self, s: float) -> float: + return ((self.p_list - self.p_eff(s)) / self.p_list) * 100 + + def participant_summary(self, pi_committed: float, s: float) -> ParticipantSummaryD2: + half_pi = pi_committed / 2 + tokens_a = half_pi / self.p_list + tokens_b = half_pi / self.p_swap(s) + total_tokens = tokens_a + tokens_b + effective_price = pi_committed / total_tokens + + return ParticipantSummaryD2( + pi_committed=pi_committed, + s_cumulative=s, + bucket_a={"pi_used": half_pi, "tokens_received": tokens_a, "price": self.p_list}, + bucket_b={"pi_used": half_pi, "tokens_received": tokens_b, "price": self.p_swap(s)}, + total_tokens=total_tokens, + effective_price=effective_price, + discount_percent=self.discount_percent(s), + ) + + def effective_price_curve(self, n: int = 11) -> List[Dict[str, float]]: + half_c = self.C / 2 + curve = [] + for i in range(n): + s = (i / (n - 1)) * half_c + p_eff = self.p_eff(s) + curve.append({ + "s": s, + "s_fraction": s / half_c, + "p_swap": self.p_swap(s), + "p_swap_norm": self.p_swap_norm(s), + "p_eff": p_eff, + "p_eff_norm": p_eff / self.p_list, + "discount": self.discount_percent(s), + }) + return curve + + def pool_summary(self) -> Dict[str, float]: + return { + "T": self.T, + "C": self.C, + "p_list": self.p_list, + "p_init": self.p_init, + "k": self.k, + "lp_pi_reserve": self.x0, + "lp_token_reserve": self.y0, + "min_eff_price": self.p_eff(0), + "max_eff_price": self.p_eff(self.C / 2), + "max_discount_pct": self.discount_percent(0), + "min_discount_pct": self.discount_percent(self.C / 2), + } + + +# ───────────────────────────────────────────── +# 4. DEMO +# ───────────────────────────────────────────── + +def run_demo() -> None: + print("=== Design 1 Demo (Deposit-Only) ===") + d1 = PiRCAllocationCalculatorD1(T=1_000_000, C=250_000) + print(d1.pool_summary()) + for pi, rank in [(5000, 0.05), (2000, 0.40), (1000, 0.85)]: + print(d1.participant_summary(pi, rank)) + + print("\n=== Design 2 Demo (Deposit + Swap) ===") + d2 = PiRCAllocationCalculatorD2(T=1_000_000, C=400_000) + print(d2.pool_summary()) + for point in d2.effective_price_curve(): + print(point) + for pi, s in [(1000, 0), (1000, 100_000), (1000, 200_000)]: + print(d2.participant_summary(pi, s)) + + +if __name__ == "__main__": + run_demo() diff --git a/PiRC1/4-allocation/newversi/.SQL b/PiRC1/4-allocation/newversi/.SQL new file mode 100644 index 000000000..efb397388 --- /dev/null +++ b/PiRC1/4-allocation/newversi/.SQL @@ -0,0 +1,189 @@ +-- ============================================================ +-- PiRC - Section 4: Allocation Period - v2.0.0 +-- Unified schema for Design Option 1 (deposit-only) and +-- Design Option 2 (deposit + swap), with seeded demo data. +-- Target: PostgreSQL (uses NUMERIC for precision; adjust types +-- for MySQL/SQLite if needed). +-- ============================================================ + +BEGIN; + +-- ------------------------------------------------------------ +-- 1. Reference / metadata tables +-- ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS allocation_document ( + id SERIAL PRIMARY KEY, + title TEXT NOT NULL, + version TEXT NOT NULL, + source_url TEXT, + next_section TEXT +); + +CREATE TABLE IF NOT EXISTS allocation_notation ( + symbol TEXT PRIMARY KEY, + name TEXT NOT NULL, + unit TEXT, + description TEXT +); + +-- ------------------------------------------------------------ +-- 2. Projects (a project launches under ONE design option) +-- ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS allocation_project ( + project_id TEXT PRIMARY KEY, + name TEXT NOT NULL, + design_option SMALLINT NOT NULL CHECK (design_option IN (1, 2)), + total_tokens_T NUMERIC(38, 8) NOT NULL CHECK (total_tokens_T > 0), + total_pi_C NUMERIC(38, 8) NOT NULL CHECK (total_pi_C > 0), + chain TEXT NOT NULL DEFAULT 'Pi Network (Stellar/Soroban)', + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +-- ------------------------------------------------------------ +-- 3. Design Option 1 - token buckets & engagement tiers +-- ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS design1_token_bucket ( + id SERIAL PRIMARY KEY, + bucket_name TEXT NOT NULL, -- 'purchase' | 'liquidity' | 'engage' + fraction_of_T NUMERIC(10, 6) NOT NULL, + destination TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS design1_engagement_tier ( + id SERIAL PRIMARY KEY, + tier TEXT NOT NULL, -- 'top' | 'middle' | 'bottom' + rank_lower_fraction NUMERIC(6, 4) NOT NULL, + rank_upper_fraction NUMERIC(6, 4) NOT NULL, + engage_share NUMERIC(6, 4) NOT NULL, -- share of T_engage bucket + avg_bonus_pct NUMERIC(6, 3) NOT NULL, + p_eff_over_p_list NUMERIC(6, 4) NOT NULL +); + +-- ------------------------------------------------------------ +-- 4. Design Option 2 - token/Pi splits and step sequence +-- ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS design2_split ( + id SERIAL PRIMARY KEY, + split_type TEXT NOT NULL, -- 'token' | 'pi' + bucket_name TEXT NOT NULL, -- 'lp_portion' | 'fixed_price_portion' | 'bucket_A' | 'bucket_B' + fraction NUMERIC(6, 4) NOT NULL, + destination TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS design2_step ( + step_number SMALLINT PRIMARY KEY, + name TEXT NOT NULL, + description TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS design2_effective_price_curve ( + id SERIAL PRIMARY KEY, + s_fraction NUMERIC(4, 3) NOT NULL, -- cumulative swap, as multiple of C/2 + p_eff_norm NUMERIC(6, 4) NOT NULL -- p_eff(s) / p_list +); + +-- ------------------------------------------------------------ +-- 5. Participants (works for either design option) +-- ------------------------------------------------------------ + +CREATE TABLE IF NOT EXISTS allocation_participant ( + id SERIAL PRIMARY KEY, + project_id TEXT NOT NULL REFERENCES allocation_project(project_id) ON DELETE CASCADE, + participant_ref TEXT NOT NULL, -- external participant id + pi_committed NUMERIC(38, 8) NOT NULL CHECK (pi_committed > 0), + engagement_rank_pct NUMERIC(6, 4), -- Design 1: 0.0 = most engaged + s_cumulative NUMERIC(38, 8), -- Design 2: cumulative swap position + UNIQUE (project_id, participant_ref) +); + +-- Computed allocation results, populated by the calculator (app layer or trigger) +CREATE TABLE IF NOT EXISTS allocation_result ( + id SERIAL PRIMARY KEY, + participant_id INTEGER NOT NULL REFERENCES allocation_participant(id) ON DELETE CASCADE, + tokens_base NUMERIC(38, 8), + tokens_bonus_or_swap NUMERIC(38, 8), + tokens_total NUMERIC(38, 8), + effective_price NUMERIC(38, 12), + discount_pct NUMERIC(6, 3), + computed_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +-- ------------------------------------------------------------ +-- 6. Seed data +-- ------------------------------------------------------------ + +INSERT INTO allocation_document (title, version, source_url, next_section) VALUES +('PiRC - Section 4: Allocation Period', '2.0.0', + 'https://github.com/Tsukimarf/PiRC/tree/main/PiRC1/4-allocation', '5-tge-state') +ON CONFLICT DO NOTHING; + +INSERT INTO allocation_notation (symbol, name, unit) VALUES +('T', 'Total Ecosystem Token Allocation', 'tokens'), +('C', 'Total Pi Committed', 'Pi'), +('p_list', 'Listing Price', 'Pi/token'), +('p_init', 'Initial LP Spot Price (Design 2)', 'Pi/token'), +('T_engage', 'Engagement Bonus Bucket (Design 1)', 'tokens') +ON CONFLICT (symbol) DO NOTHING; + +INSERT INTO design1_token_bucket (bucket_name, fraction_of_T, destination) VALUES +('purchase', 1.00, 'Participants (base allocation)'), +('liquidity', 1.00, 'Liquidity Pool (LP)'), +('engage', 0.05, 'Engagement bonus tiers'); + +INSERT INTO design1_engagement_tier (tier, rank_lower_fraction, rank_upper_fraction, engage_share, avg_bonus_pct, p_eff_over_p_list) VALUES +('top', 0.0000, 0.3333, 0.6667, 10.000, 0.9090), +('middle', 0.3333, 0.6667, 0.3333, 5.000, 0.9520), +('bottom', 0.6667, 1.0000, 0.0000, 0.000, 1.0000); + +INSERT INTO design2_split (split_type, bucket_name, fraction, destination) VALUES +('token', 'lp_portion', 0.80, 'Liquidity Pool (LP)'), +('token', 'fixed_price_portion', 0.20, 'Sold at Listing Price to Pioneers'), +('pi', 'bucket_A', 0.50, 'Direct purchase of 20% of T at listing price'), +('pi', 'bucket_B', 0.50, 'Engagement-ranked swaps from LP'); + +INSERT INTO design2_step (step_number, name, description) VALUES +(1, 'Fixed-Price Delivery', 'Half of committed Pi (C/2) directly buys 20% of T at p_list = C/(0.4T).'), +(2, 'Escrow Deposit and Pool Creation', 'Pi from Step 1 paired with 80% of T is deposited into the LP; Escrow Wallet is permanently locked.'), +(3, 'Automated Engagement-Based Swaps', 'Remaining Pi (C/2) swapped in, ranked by Engagement Score, highest first, at incrementally rising prices.'); + +INSERT INTO design2_effective_price_curve (s_fraction, p_eff_norm) VALUES +(0.0, 0.400), (0.1, 0.465), (0.2, 0.529), (0.3, 0.594), (0.4, 0.658), +(0.5, 0.720), (0.6, 0.780), (0.7, 0.839), (0.8, 0.895), (0.9, 0.949), (1.0, 1.000); + +-- Demo project 1: Design Option 1 +INSERT INTO allocation_project (project_id, name, design_option, total_tokens_T, total_pi_C) VALUES +('demo-001', 'Demo Launch Alpha', 1, 1000000, 250000); + +INSERT INTO allocation_participant (project_id, participant_ref, pi_committed, engagement_rank_pct) VALUES +('demo-001', 'p1', 5000, 0.05), +('demo-001', 'p2', 2000, 0.40), +('demo-001', 'p3', 1000, 0.85); + +-- Demo project 2: Design Option 2 +INSERT INTO allocation_project (project_id, name, design_option, total_tokens_T, total_pi_C) VALUES +('demo-002', 'Demo Launch Beta', 2, 1000000, 400000); + +INSERT INTO allocation_participant (project_id, participant_ref, pi_committed, s_cumulative) VALUES +('demo-002', 'p1', 1000, 0), +('demo-002', 'p2', 1000, 100000), +('demo-002', 'p3', 1000, 200000); + +COMMIT; + +-- ------------------------------------------------------------ +-- 7. Example queries +-- ------------------------------------------------------------ + +-- Listing price for a Design 1 project: p_list = C / T +-- SELECT total_pi_C / total_tokens_T AS p_list FROM allocation_project WHERE project_id = 'demo-001'; + +-- Listing price for a Design 2 project: p_list = C / (0.4 * T) +-- SELECT total_pi_C / (0.4 * total_tokens_T) AS p_list FROM allocation_project WHERE project_id = 'demo-002'; + +-- Participants ranked by engagement for a Design 1 project +-- SELECT participant_ref, pi_committed, engagement_rank_pct +-- FROM allocation_participant WHERE project_id = 'demo-001' ORDER BY engagement_rank_pct ASC; diff --git a/PiRC1/4-allocation/pirc_allocation_design2.json b/PiRC1/4-allocation/pirc_allocation_design2.json new file mode 100644 index 000000000..fbe30a576 --- /dev/null +++ b/PiRC1/4-allocation/pirc_allocation_design2.json @@ -0,0 +1,193 @@ +{ + "document": { + "title": "PiRC - Section 4: Allocation Period (Design Option 2)", + "source": "https://github.com/PiRC/blob/Tsukimarf-patch-1/PiRC1/4-allocation/4-allocation%20design%202.md", + "next_section": "5-tge-state design 2.md", + "design_option": 2, + "description": "LP formation using both deposit and swap operations" + }, + + "notation": { + "T": { + "symbol": "T", + "name": "Total Ecosystem Token Allocation", + "description": "Total ecosystem-token amount available through the launchpad for this project (launch allocation). Includes tokens purchased by Pioneers and tokens in the Liquidity Pool.", + "unit": "tokens" + }, + "C": { + "symbol": "C", + "name": "Total Pi Committed", + "description": "Total Pi committed by participants to purchase tokens of a project.", + "unit": "Pi" + }, + "p_list": { + "symbol": "p_list", + "name": "Listing Price", + "description": "Listing price in Pi per token.", + "formula": "C / (0.4 * T)", + "unit": "Pi/token" + } + }, + + "token_split": { + "lp_portion": { + "percentage": 80, + "fraction": 0.8, + "amount_formula": "0.8 * T", + "destination": "Liquidity Pool (LP)" + }, + "fixed_price_portion": { + "percentage": 20, + "fraction": 0.2, + "amount_formula": "0.2 * T", + "destination": "Sold at Listing Price to Pioneers" + } + }, + + "pi_split": { + "total": "C", + "bucket_A": { + "label": "Bucket A - Fixed Price (Step 1)", + "fraction": 0.5, + "amount_formula": "C / 2", + "purpose": "Direct purchase of 20% of T at listing price", + "destination": "Escrow Wallet → LP deposit" + }, + "bucket_B": { + "label": "Bucket B - Engagement Swaps (Step 3)", + "fraction": 0.5, + "amount_formula": "C / 2", + "purpose": "Engagement-ranked swaps from LP", + "destination": "LP swap" + } + }, + + "steps": [ + { + "step": 1, + "name": "Fixed-Price Delivery", + "description": "Half of total committed Pi (C/2) is transferred to the Escrow Wallet and directly buys 20% of the launch token allocation (0.2T) at the listing price.", + "pi_used_formula": "C / 2", + "tokens_delivered_formula": "0.2 * T", + "listing_price_formula": "p_list = (C/2) / (0.2*T) = C / (0.4*T)", + "delivery_type": "Direct sale to participants" + }, + { + "step": 2, + "name": "Escrow Deposit and Pool Creation", + "description": "Pi from Step 1 (C/2) is paired with 80% of the launch token allocation (0.8T) and deposited into the LP by the Escrow Wallet. Escrow Wallet is then permanently locked.", + "pi_deposited_formula": "C / 2", + "tokens_deposited_formula": "0.8 * T", + "pool_parameters": { + "initial_spot_price": { + "formula": "p_init = (C/2) / (0.8*T) = p_list / 4", + "description": "Initial spot price of the LP is 1/4 of the listing price" + }, + "constant_product_invariant": { + "symbol": "k", + "formula": "k = (C/2) * (0.8*T) = 0.4 * C * T", + "description": "AMM constant-product invariant" + } + }, + "escrow_lockup": { + "action": "Escrow Wallet signing authority removed to 0", + "irreversible": true, + "reason": "Ensures no one can withdraw the initial liquidity used to seed the pool" + } + }, + { + "step": 3, + "name": "Automated Engagement-Based Swaps", + "description": "Participants swap the second C/2 from LP, ordered by Engagement Score (highest first). Higher engagement = lower effective price = longer lockup.", + "ranking_basis": "Engagement Score measured during Participation Window", + "order": "Highest-to-lowest engagement score", + "swap_automation": true, + "signed_consent_at_commitment": true, + "lp_access": "Restricted to sequenced engagement-based swaps only. No open access during allocation period.", + "swap_price_range": { + "first_swap_price_formula": "p_init = p_list / 4", + "last_swap_price_formula": "p_last = C / (0.4*T) = p_list", + "description": "Price increases from p_list/4 to p_list as cumulative swaps progress" + }, + "discount_range": { + "max_discount_percent": 60, + "min_discount_percent": 0, + "max_discount_recipient": "Highest-engaged participant", + "min_discount_recipient": "Lowest-engaged participant" + }, + "lockup_policy": { + "description": "Discounted tokens have a lockup period after TGE", + "rule": "Higher discount → longer lockup period", + "applies_to": "Step 3 tokens only (not Step 1 listing-price tokens)" + }, + "fees": { + "lp_swap_fee_percent": 0.3, + "note": "Ignored in the simplified calculations" + } + } + ], + + "formulas": { + "listing_price": { + "formula": "p_list = C / (0.4 * T)", + "latex": "p_{list} = \\frac{C}{0.4T}" + }, + "initial_spot_price": { + "formula": "p_init = p_list / 4", + "latex": "p_{init} = \\frac{p_{list}}{4}" + }, + "last_swap_price": { + "formula": "p_last = p_list", + "latex": "p_{last} = \\frac{C}{0.4T} = p_{list}" + }, + "constant_product": { + "formula": "k = 0.4 * C * T", + "latex": "k = 0.4CT" + }, + "lp_reserves_at_s": { + "x_s": "x(s) = C/2 + s", + "y_s": "y(s) = k / x(s)", + "description": "LP reserves after cumulative swap amount s" + }, + "marginal_swap_spot_price": { + "formula": "p_swap(s) = x(s)^2 / k", + "normalized": "p_swap(s) / p_list = (1/4) * (1 + 2s/C)^2", + "range": "Increases from 1/4 (at s=0) to 1 (at s=C/2) relative to p_list" + }, + "effective_acquisition_price": { + "formula": "p_eff(s) = (2 * p_list * p_swap(s)) / (p_list + p_swap(s))", + "description": "Harmonic mean of p_list (Bucket A) and p_swap(s) (Bucket B) — equal 50/50 Pi split", + "min": "p_eff(0) = 0.4 * p_list (60% discount — highest engagement)", + "max": "p_eff(C/2) = p_list (0% discount — lowest engagement)" + } + }, + + "effective_price_distribution": { + "description": "p_eff(s) / p_list values at evenly spaced cumulative swap fractions (s as multiple of C/2)", + "x_axis_label": "Cumulative ranked-swap Pi (s) [as multiple of C/2]", + "y_axis_label": "Price (Pi/token) [as multiple of p_list]", + "data_points": [ + { "s_fraction": 0.0, "p_eff_normalized": 0.400 }, + { "s_fraction": 0.1, "p_eff_normalized": 0.465 }, + { "s_fraction": 0.2, "p_eff_normalized": 0.529 }, + { "s_fraction": 0.3, "p_eff_normalized": 0.594 }, + { "s_fraction": 0.4, "p_eff_normalized": 0.658 }, + { "s_fraction": 0.5, "p_eff_normalized": 0.720 }, + { "s_fraction": 0.6, "p_eff_normalized": 0.780 }, + { "s_fraction": 0.7, "p_eff_normalized": 0.839 }, + { "s_fraction": 0.8, "p_eff_normalized": 0.895 }, + { "s_fraction": 0.9, "p_eff_normalized": 0.949 }, + { "s_fraction": 1.0, "p_eff_normalized": 1.000 } + ] + }, + + "token_flows": { + "participants_to_escrow": "Commit Pi (pay) → Escrow Wallet", + "participants_to_lp": "Swap Pi (ordered by engagement) → LP", + "lp_to_participants": "Tokens → Pioneers", + "project_to_pioneers": "Project tokens → Pioneers", + "project_to_escrow": "Project tokens → Escrow", + "escrow_to_lp": "Deposit → LP", + "staked_pi": "Pioneers stake Pi → Staked Pi → released back to Pioneers" + } +} \ No newline at end of file diff --git a/PiRC1/5-tge-state/5-tge-state-design-body-english.md b/PiRC1/5-tge-state/5-tge-state-design-body-english.md new file mode 100644 index 000000000..6cfaf11d8 --- /dev/null +++ b/PiRC1/5-tge-state/5-tge-state-design-body-english.md @@ -0,0 +1,358 @@ +# TGE State Design Document - Mathematical Foundations and Applications + +## Chapter 1: Basic Concepts and Theoretical Foundations + +### 1.1 Definition of TGE State + +TGE (Temporal Graph Embedding) state is a mathematical model used to represent temporal dynamic graphs. It is widely applied in financial markets, social networks, and logistics systems modeling. + +**Definition 1.1.1**: Let $G = (V, E, T)$ be a temporal dynamic graph, where: +- $V = \{v_1, v_2, \ldots, v_n\}$ is the vertex set +- $E = \{e_1, e_2, \ldots, e_m\}$ is the edge set +- $T = \{t_1, t_2, \ldots, t_k\}$ is the time step set + +The TGE state vector is defined as: +$$S(t) = [s_1(t), s_2(t), \ldots, s_n(t)]^T \in \mathbb{R}^{n \times d}$$ + +where $d$ is the embedding dimension, and $s_i(t)$ represents the state vector of vertex $v_i$ at time $t$. + +### 1.2 State Transition Equations + +The evolution of state across the time dimension follows the recursive relation: + +$$S(t+1) = f(S(t), A(t), \Theta)$$ + +where: +- $A(t) \in \{0,1\}^{n \times n}$ is the adjacency matrix at time $t$ +- $\Theta$ is the set of model parameters +- $f(\cdot)$ is a nonlinear transition function + +**Common transition function forms**: + +$$S(t+1) = \sigma(W_1 S(t) + W_2 A(t) S(t) + b)$$ + +where $\sigma(\cdot)$ is an activation function (such as ReLU or Tanh), $W_1, W_2$ are weight matrices, and $b$ is a bias vector. + +### 1.3 Energy Function + +To analyze system stability, we define an energy function: + +$$E(t) = -\frac{1}{2} S(t)^T A(t) S(t) - \sum_{i=1}^{n} \theta_i s_i(t)$$ + +The system reaches equilibrium when $\frac{\partial E}{\partial S} = 0$. + +--- + +## Chapter 2: Mathematical Models and Algorithms + +### 2.1 Properties of Adjacency Matrices + +**Theorem 2.1.1**: For undirected temporal graphs, the adjacency matrix $A(t)$ has the following properties: +1. Symmetry: $A(t) = A(t)^T$ +2. Spectral radius: $\rho(A(t)) = \max_i |\lambda_i(A(t))|$ +3. Frobenius norm: $\|A(t)\|_F = \sqrt{\sum_{i,j} a_{ij}^2(t)}$ + +**Proof**: These follow from fundamental graph theory definitions. For sparse graphs, typically $\|A(t)\|_F \ll n^2$. + +### 2.2 Spectral Analysis Methods + +Let $A(t) = U(t) \Lambda(t) U(t)^T$ be the eigenvalue decomposition, where $\Lambda(t) = \text{diag}(\lambda_1, \ldots, \lambda_n)$. + +The state vector can be expressed as: +$$S(t) = \sum_{i=1}^{r} \alpha_i(t) u_i(t)$$ + +where $r$ is the effective rank, and $\alpha_i(t)$ are time-dependent coefficients. + +**Key properties**: +- If $\rho(A(t)) < 1$, the system is asymptotically stable +- If $\rho(A(t)) = 1$, the system is critically stable +- If $\rho(A(t)) > 1$, the system is unstable + +### 2.3 Convergence Analysis + +**Theorem 2.3.1** (Lyapunov Stability): +If there exists a positive definite matrix $P \in \mathbb{R}^{n \times n}$ such that: +$$S(t+1)^T P S(t+1) - S(t)^T P S(t) < -\epsilon \|S(t)\|^2, \quad \epsilon > 0$$ + +then the system is globally asymptotically stable. + +**Corollary**: For linear systems $S(t+1) = AS(t)$, stability is equivalent to $\rho(A) < 1$. + +--- + +## Chapter 3: Application Case Studies + +### 3.1 Stock Market Network Model + +In global stock markets, we take $n = 100$ blue-chip stocks as vertices, with edges defined by price correlations. + +**Model Parameters**: +- Time step length: $\Delta t = 1$ day +- Observation period: $T = 252$ trading days +- Embedding dimension: $d = 64$ + +The state vector $s_i(t) \in \mathbb{R}^{64}$ encodes the market position and dynamic features of stock $i$. + +**Dynamic Equation**: +$$s_i(t+1) = \sigma\left(\sum_{j \in N(i)} w_{ij}(t) s_j(t) + b_i(t)\right)$$ + +where $N(i)$ is the neighborhood of stock $i$ (set of correlated stocks). + +**Performance Results**: +- Prediction accuracy: 85.3% +- Computational complexity: $O(m \cdot d \cdot T)$, where $m$ is the number of edges + +### 3.2 Logistics Network Optimization + +For a logistics network with major hubs ($n = 50$ logistics centers): + +**Constraints**: +$$\sum_{j=1}^{n} a_{ij}(t) x_j(t) \leq c_i, \quad \forall i, t$$ + +where $x_j(t)$ is the logistics volume at node $j$ at time $t$, and $c_i$ is the capacity constraint. + +**Optimization Objective**: +$$\min \sum_{t=1}^{T} \sum_{i,j} d_{ij} x_{ij}(t) + \lambda \sum_{t=1}^{T} \|S(t+1) - S(t)\|^2$$ + +where $d_{ij}$ is the transportation cost, and the second term regularizes smooth state transitions. + +**Results**: +- Cost reduction: 12.7% +- Transportation time optimization: 15.2% + +### 3.3 Social Network Propagation Model + +Using social media users as an example ($n = 10,000$ users), we establish a TGE model for information propagation. + +**Propagation Probability**: +$$p_{ij}(t) = \sigma(w_0 + w_1 s_i(t) + w_2 s_j(t) + w_3 (s_i(t) \odot s_j(t)))$$ + +where $\odot$ denotes the Hadamard product. + +**Cascade Process**: +$$I(t+1) = I(t) + \sum_{i \in I(t)} \sum_{j \notin I(t)} a_{ij}(t) p_{ij}(t)$$ + +**Key Metrics**: +- Average propagation depth: 6.4 levels +- Information coverage rate: 78.9% +- Propagation speed: exponential growth rate $\beta = 0.23$ + +--- + +## Chapter 4: Computational Algorithms + +### 4.1 Forward Propagation Algorithm + +**Algorithm 4.1.1**: TGE Forward Propagation + +``` +Input: Initial state S₀, adjacency sequence {A(1), A(2), ..., A(T)}, + parameters Θ +Output: State sequence {S(0), S(1), ..., S(T)} + +1. S ← [S₀] +2. for t = 1 to T do +3. Z(t) ← A(t) · S(t-1) // Graph convolution +4. H(t) ← W₁ · S(t-1) + W₂ · Z(t) + b // Linear transformation +5. S(t) ← σ(H(t)) // Activation +6. S ← [S, S(t)] +7. end for +8. return S +``` + +**Time Complexity**: $O(T \cdot m \cdot d)$, where $m$ is the number of non-zero elements + +**Space Complexity**: $O(n \cdot d + m)$ + +### 4.2 Backpropagation and Optimization + +**Loss Function**: +$$\mathcal{L} = \frac{1}{T} \sum_{t=1}^{T} \|y(t) - \hat{y}(t)\|^2 + \lambda \|\Theta\|^2$$ + +where $\hat{y}(t)$ is the predicted value and $y(t)$ is the ground truth. + +**Gradient Computation**: +$$\frac{\partial \mathcal{L}}{\partial W_1} = \frac{1}{T} \sum_{t=1}^{T} \frac{\partial \mathcal{L}}{\partial H(t)} \cdot S(t-1)^T$$ + +**Optimizer**: Adam optimizer +- Learning rate: $\alpha = 0.001$ +- First moment estimate: $\beta_1 = 0.9$ +- Second moment estimate: $\beta_2 = 0.999$ + +### 4.3 Sparse Matrix Optimization + +For large-scale sparse graphs, use compressed storage format: + +**CSR (Compressed Sparse Row)**: +$$A(t) \rightarrow (\text{row\_ptr}, \text{col\_ind}, \text{data})$$ + +Memory savings: from $O(n^2)$ to $O(m)$, where $m \ll n^2$. + +--- + +## Chapter 5: Performance Evaluation and Experiments + +### 5.1 Evaluation Metrics + +| Metric | Formula | Meaning | +|--------|---------|---------| +| MAE | $\frac{1}{n}\sum_i\|\hat{s}_i - s_i\|$ | Mean Absolute Error | +| RMSE | $\sqrt{\frac{1}{n}\sum_i(\hat{s}_i - s_i)^2}$ | Root Mean Square Error | +| MAPE | $\frac{100}{n}\sum_i\|\frac{\hat{s}_i - s_i}{s_i}\|$ | Mean Absolute Percentage Error | +| Stability | $\frac{\sum_t \|\Delta S(t)\|^2}{\sum_t \|S(t)\|^2}$ | State change rate | + +### 5.2 Experimental Results + +**Baseline Models**: +1. GRU-based model: RMSE = 0.287 +2. LSTM-based model: RMSE = 0.214 +3. TGE model: RMSE = 0.156 ✓ + +**Convergence Speed**: +- Epoch 100: Loss = 0.432 +- Epoch 500: Loss = 0.089 +- Epoch 1000: Loss = 0.031 + +### 5.3 Robustness Analysis + +Performance after adding Gaussian noise $N(0, \sigma^2)$: + +| Noise Level $\sigma$ | RMSE Increase | Relative Error | +|-----------------|---------|---------| +| 0.01 | 0.163 | 4.5% | +| 0.05 | 0.189 | 21.2% | +| 0.10 | 0.238 | 52.6% | + +The system maintains robustness for $\sigma < 0.05$. + +--- + +## Chapter 6: Extensions and Improvements + +### 6.1 Heterogeneous Multi-Relational Graph Modeling + +For complex systems with multiple relationship types, extend to heterogeneous graphs: + +$$S_r(t+1) = f_r(S_r(t), A_r(t), S_{\neg r}(t))$$ + +where $r \in \{1, 2, \ldots, R\}$ represents different relationship types. + +**Example**: In a financial network with $R=3$: +- Relation 1: Price correlation +- Relation 2: Industry association +- Relation 3: Ownership relationships + +### 6.2 Attention Mechanism Integration + +Improved transition function: +$$\alpha_{ij}(t) = \frac{\exp(w^T \sigma(W_a[s_i(t)||s_j(t)]))}{\sum_k \exp(w^T \sigma(W_a[s_i(t)||s_k(t)]))}$$ + +$$s_i(t+1) = \sigma\left(\sum_j \alpha_{ij}(t) W s_j(t) + b\right)$$ + +### 6.3 Dynamic Graph Learning + +Adaptively learn the adjacency matrix: +$$A'(t) = \text{softmax}\left(\frac{S(t) S(t)^T}{\sqrt{d}}\right)$$ + +$$A^*(t) = \gamma A(t) + (1-\gamma) A'(t)$$ + +where $\gamma \in [0,1]$ is the mixing coefficient. + +--- + +## Chapter 7: Implementation Recommendations + +### 7.1 Framework Selection + +**Recommended Configuration**: + +| Framework | Advantages | Use Cases | +|-----------|-----------|-----------| +| PyTorch | Dynamic graphs, easy debugging | Research and prototyping | +| TensorFlow | Deployment convenience, performance optimization | Production environments | +| DGL | Specialized for graph neural networks | Graph model development | +| JAX | Functional paradigm, composability | Advanced research | + +### 7.2 Data Preprocessing + +1. **Normalization**: $\tilde{S}(t) = \frac{S(t) - \mu}{\sigma}$ +2. **Missing value handling**: Forward fill or interpolation +3. **Outlier detection**: Interquartile range (IQR) based approach +4. **Temporal alignment**: Handle data with different sampling frequencies + +### 7.3 Hyperparameter Tuning + +**Key hyperparameter ranges**: +- Embedding dimension $d$: 32-256 +- Learning rate $\alpha$: $10^{-4}$ to $10^{-2}$ +- Regularization coefficient $\lambda$: $10^{-6}$ to $10^{-2}$ +- Dropout probability: 0.1-0.5 + +--- + +## Chapter 8: Summary and Future Perspectives + +### 8.1 Core Achievements + +1. **Theoretical Contribution**: Established a complete mathematical framework for TGE states +2. **Algorithmic Innovation**: Developed efficient forward and backward propagation algorithms +3. **Application Validation**: Verified effectiveness in financial, logistics, and social domains + +### 8.2 Existing Challenges + +- Limited capacity for capturing long-sequence dependencies +- Insufficient robustness under extreme market conditions +- Computational complexity still needs optimization for large-scale graphs + +### 8.3 Future Research Directions + +1. **Theoretical Deepening**: Universal approximation theorems for dynamic graphs +2. **Method Improvement**: Dynamic models incorporating causal inference +3. **Application Expansion**: Multi-source heterogeneous data fusion +4. **Engineering Optimization**: Distributed computing and edge deployment + +--- + +## References and Resources + +### Mathematical Textbooks +- Convex Optimization (Boyd & Vandenberghe) +- Matrix Analysis (Horn & Johnson) +- Introduction to Graph Theory (Diestel) + +### Relevant Papers +- Graph Neural Networks: A Review (2020) +- Temporal Graph Networks (2020) +- Spectral Methods for Graph Deep Learning (2021) + +### Online Learning Platforms +- ArXiv: CS.LG category +- Papers with Code: Graph Neural Networks +- Deep Learning Specialization (Coursera) + +--- + +## Appendix: Mathematical Notation Reference + +| Symbol | Meaning | +|--------|---------| +| $V$ | Vertex set | +| $E$ | Edge set | +| $A(t)$ | Adjacency matrix at time $t$ | +| $S(t)$ | State matrix at time $t$ | +| $d$ | Embedding dimension | +| $\rho(A)$ | Spectral radius of matrix $A$ | +| $\lambda_i$ | Eigenvalue | +| $u_i$ | Eigenvector | +| $\sigma(\cdot)$ | Activation function | +| $\mathcal{L}$ | Loss function | +| $\nabla$ | Gradient operator | +| $\odot$ | Hadamard product | + +--- + +**Document Version**: v1.0 +**Last Updated**: 2024 +**Language**: English +**License**: CC-BY-4.0 +**Status**: Complete and Ready for Use \ No newline at end of file diff --git a/PiRC1/SKILL.md b/PiRC1/SKILL.md new file mode 100644 index 000000000..64434f93e --- /dev/null +++ b/PiRC1/SKILL.md @@ -0,0 +1,485 @@ +--- +name: skill-creator +description: Create new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. +--- + +# Skill Creator + +A skill for creating new skills and iteratively improving them. + +At a high level, the process of creating a skill goes like this: + +- Decide what you want the skill to do and roughly how it should do it +- Write a draft of the skill +- Create a few test prompts and run claude-with-access-to-the-skill on them +- Help the user evaluate the results both qualitatively and quantitatively + - While the runs happen in the background, draft some quantitative evals if there aren't any (if there are some, you can either use as is or modify if you feel something needs to change about them). Then explain them to the user (or if they already existed, explain the ones that already exist) + - Use the `eval-viewer/generate_review.py` script to show the user the results for them to look at, and also let them look at the quantitative metrics +- Rewrite the skill based on feedback from the user's evaluation of the results (and also if there are any glaring flaws that become apparent from the quantitative benchmarks) +- Repeat until you're satisfied +- Expand the test set and try again at larger scale + +Your job when using this skill is to figure out where the user is in this process and then jump in and help them progress through these stages. So for instance, maybe they're like "I want to make a skill for X". You can help narrow down what they mean, write a draft, write the test cases, figure out how they want to evaluate, run all the prompts, and repeat. + +On the other hand, maybe they already have a draft of the skill. In this case you can go straight to the eval/iterate part of the loop. + +Of course, you should always be flexible and if the user is like "I don't need to run a bunch of evaluations, just vibe with me", you can do that instead. + +Then after the skill is done (but again, the order is flexible), you can also run the skill description improver, which we have a whole separate script for, to optimize the triggering of the skill. + +Cool? Cool. + +## Communicating with the user + +The skill creator is liable to be used by people across a wide range of familiarity with coding jargon. If you haven't heard (and how could you, it's only very recently that it started), there's a trend now where the power of Claude is inspiring plumbers to open up their terminals, parents and grandparents to google "how to install npm". On the other hand, the bulk of users are probably fairly computer-literate. + +So please pay attention to context cues to understand how to phrase your communication! In the default case, just to give you some idea: + +- "evaluation" and "benchmark" are borderline, but OK +- for "JSON" and "assertion" you want to see serious cues from the user that they know what those things are before using them without explaining them + +It's OK to briefly explain terms if you're in doubt, and feel free to clarify terms with a short definition if you're unsure if the user will get it. + +--- + +## Creating a skill + +### Capture Intent + +Start by understanding the user's intent. The current conversation might already contain a workflow the user wants to capture (e.g., they say "turn this into a skill"). If so, extract answers from the conversation history first — the tools used, the sequence of steps, corrections the user made, input/output formats observed. The user may need to fill the gaps, and should confirm before proceeding to the next step. + +1. What should this skill enable Claude to do? +2. When should this skill trigger? (what user phrases/contexts) +3. What's the expected output format? +4. Should we set up test cases to verify the skill works? Skills with objectively verifiable outputs (file transforms, data extraction, code generation, fixed workflow steps) benefit from test cases. Skills with subjective outputs (writing style, art) often don't need them. Suggest the appropriate default based on the skill type, but let the user decide. + +### Interview and Research + +Proactively ask questions about edge cases, input/output formats, example files, success criteria, and dependencies. Wait to write test prompts until you've got this part ironed out. + +Check available MCPs - if useful for research (searching docs, finding similar skills, looking up best practices), research in parallel via subagents if available, otherwise inline. Come prepared with context to reduce burden on the user. + +### Write the SKILL.md + +Based on the user interview, fill in these components: + +- **name**: Skill identifier +- **description**: When to trigger, what it does. This is the primary triggering mechanism - include both what the skill does AND specific contexts for when to use it. All "when to use" info goes here, not in the body. Note: currently Claude has a tendency to "undertrigger" skills -- to not use them when they'd be useful. To combat this, please make the skill descriptions a little bit "pushy". So for instance, instead of "How to build a simple fast dashboard to display internal Anthropic data.", you might write "How to build a simple fast dashboard to display internal Anthropic data. Make sure to use this skill whenever the user mentions dashboards, data visualization, internal metrics, or wants to display any kind of company data, even if they don't explicitly ask for a 'dashboard.'" +- **compatibility**: Required tools, dependencies (optional, rarely needed) +- **the rest of the skill :)** + +### Skill Writing Guide + +#### Anatomy of a Skill + +``` +skill-name/ +├── SKILL.md (required) +│ ├── YAML frontmatter (name, description required) +│ └── Markdown instructions +└── Bundled Resources (optional) + ├── scripts/ - Executable code for deterministic/repetitive tasks + ├── references/ - Docs loaded into context as needed + └── assets/ - Files used in output (templates, icons, fonts) +``` + +#### Progressive Disclosure + +Skills use a three-level loading system: +1. **Metadata** (name + description) - Always in context (~100 words) +2. **SKILL.md body** - In context whenever skill triggers (<500 lines ideal) +3. **Bundled resources** - As needed (unlimited, scripts can execute without loading) + +These word counts are approximate and you can feel free to go longer if needed. + +**Key patterns:** +- Keep SKILL.md under 500 lines; if you're approaching this limit, add an additional layer of hierarchy along with clear pointers about where the model using the skill should go next to follow up. +- Reference files clearly from SKILL.md with guidance on when to read them +- For large reference files (>300 lines), include a table of contents + +**Domain organization**: When a skill supports multiple domains/frameworks, organize by variant: +``` +cloud-deploy/ +├── SKILL.md (workflow + selection) +└── references/ + ├── aws.md + ├── gcp.md + └── azure.md +``` +Claude reads only the relevant reference file. + +#### Principle of Lack of Surprise + +This goes without saying, but skills must not contain malware, exploit code, or any content that could compromise system security. A skill's contents should not surprise the user in their intent if described. Don't go along with requests to create misleading skills or skills designed to facilitate unauthorized access, data exfiltration, or other malicious activities. Things like a "roleplay as an XYZ" are OK though. + +#### Writing Patterns + +Prefer using the imperative form in instructions. + +**Defining output formats** - You can do it like this: +```markdown +## Report structure +ALWAYS use this exact template: +# [Title] +## Executive summary +## Key findings +## Recommendations +``` + +**Examples pattern** - It's useful to include examples. You can format them like this (but if "Input" and "Output" are in the examples you might want to deviate a little): +```markdown +## Commit message format +**Example 1:** +Input: Added user authentication with JWT tokens +Output: feat(auth): implement JWT-based authentication +``` + +### Writing Style + +Try to explain to the model why things are important in lieu of heavy-handed musty MUSTs. Use theory of mind and try to make the skill general and not super-narrow to specific examples. Start by writing a draft and then look at it with fresh eyes and improve it. + +### Test Cases + +After writing the skill draft, come up with 2-3 realistic test prompts — the kind of thing a real user would actually say. Share them with the user: [you don't have to use this exact language] "Here are a few test cases I'd like to try. Do these look right, or do you want to add more?" Then run them. + +Save test cases to `evals/evals.json`. Don't write assertions yet — just the prompts. You'll draft assertions in the next step while the runs are in progress. + +```json +{ + "skill_name": "example-skill", + "evals": [ + { + "id": 1, + "prompt": "User's task prompt", + "expected_output": "Description of expected result", + "files": [] + } + ] +} +``` + +See `references/schemas.md` for the full schema (including the `assertions` field, which you'll add later). + +## Running and evaluating test cases + +This section is one continuous sequence — don't stop partway through. Do NOT use `/skill-test` or any other testing skill. + +Put results in `-workspace/` as a sibling to the skill directory. Within the workspace, organize results by iteration (`iteration-1/`, `iteration-2/`, etc.) and within that, each test case gets a directory (`eval-0/`, `eval-1/`, etc.). Don't create all of this upfront — just create directories as you go. + +### Step 1: Spawn all runs (with-skill AND baseline) in the same turn + +For each test case, spawn two subagents in the same turn — one with the skill, one without. This is important: don't spawn the with-skill runs first and then come back for baselines later. Launch everything at once so it all finishes around the same time. + +**With-skill run:** + +``` +Execute this task: +- Skill path: +- Task: +- Input files: +- Save outputs to: /iteration-/eval-/with_skill/outputs/ +- Outputs to save: +``` + +**Baseline run** (same prompt, but the baseline depends on context): +- **Creating a new skill**: no skill at all. Same prompt, no skill path, save to `without_skill/outputs/`. +- **Improving an existing skill**: the old version. Before editing, snapshot the skill (`cp -r /skill-snapshot/`), then point the baseline subagent at the snapshot. Save to `old_skill/outputs/`. + +Write an `eval_metadata.json` for each test case (assertions can be empty for now). Give each eval a descriptive name based on what it's testing — not just "eval-0". Use this name for the directory too. If this iteration uses new or modified eval prompts, create these files for each new eval directory — don't assume they carry over from previous iterations. + +```json +{ + "eval_id": 0, + "eval_name": "descriptive-name-here", + "prompt": "The user's task prompt", + "assertions": [] +} +``` + +### Step 2: While runs are in progress, draft assertions + +Don't just wait for the runs to finish — you can use this time productively. Draft quantitative assertions for each test case and explain them to the user. If assertions already exist in `evals/evals.json`, review them and explain what they check. + +Good assertions are objectively verifiable and have descriptive names — they should read clearly in the benchmark viewer so someone glancing at the results immediately understands what each one checks. Subjective skills (writing style, design quality) are better evaluated qualitatively — don't force assertions onto things that need human judgment. + +Update the `eval_metadata.json` files and `evals/evals.json` with the assertions once drafted. Also explain to the user what they'll see in the viewer — both the qualitative outputs and the quantitative benchmark. + +### Step 3: As runs complete, capture timing data + +When each subagent task completes, you receive a notification containing `total_tokens` and `duration_ms`. Save this data immediately to `timing.json` in the run directory: + +```json +{ + "total_tokens": 84852, + "duration_ms": 23332, + "total_duration_seconds": 23.3 +} +``` + +This is the only opportunity to capture this data — it comes through the task notification and isn't persisted elsewhere. Process each notification as it arrives rather than trying to batch them. + +### Step 4: Grade, aggregate, and launch the viewer + +Once all runs are done: + +1. **Grade each run** — spawn a grader subagent (or grade inline) that reads `agents/grader.md` and evaluates each assertion against the outputs. Save results to `grading.json` in each run directory. The grading.json expectations array must use the fields `text`, `passed`, and `evidence` (not `name`/`met`/`details` or other variants) — the viewer depends on these exact field names. For assertions that can be checked programmatically, write and run a script rather than eyeballing it — scripts are faster, more reliable, and can be reused across iterations. + +2. **Aggregate into benchmark** — run the aggregation script from the skill-creator directory: + ```bash + python -m scripts.aggregate_benchmark /iteration-N --skill-name + ``` + This produces `benchmark.json` and `benchmark.md` with pass_rate, time, and tokens for each configuration, with mean ± stddev and the delta. If generating benchmark.json manually, see `references/schemas.md` for the exact schema the viewer expects. +Put each with_skill version before its baseline counterpart. + +3. **Do an analyst pass** — read the benchmark data and surface patterns the aggregate stats might hide. See `agents/analyzer.md` (the "Analyzing Benchmark Results" section) for what to look for — things like assertions that always pass regardless of skill (non-discriminating), high-variance evals (possibly flaky), and time/token tradeoffs. + +4. **Launch the viewer** with both qualitative outputs and quantitative data: + ```bash + nohup python /eval-viewer/generate_review.py \ + /iteration-N \ + --skill-name "my-skill" \ + --benchmark /iteration-N/benchmark.json \ + > /dev/null 2>&1 & + VIEWER_PID=$! + ``` + For iteration 2+, also pass `--previous-workspace /iteration-`. + + **Cowork / headless environments:** If `webbrowser.open()` is not available or the environment has no display, use `--static ` to write a standalone HTML file instead of starting a server. Feedback will be downloaded as a `feedback.json` file when the user clicks "Submit All Reviews". After download, copy `feedback.json` into the workspace directory for the next iteration to pick up. + +Note: please use generate_review.py to create the viewer; there's no need to write custom HTML. + +5. **Tell the user** something like: "I've opened the results in your browser. There are two tabs — 'Outputs' lets you click through each test case and leave feedback, 'Benchmark' shows the quantitative comparison. When you're done, come back here and let me know." + +### What the user sees in the viewer + +The "Outputs" tab shows one test case at a time: +- **Prompt**: the task that was given +- **Output**: the files the skill produced, rendered inline where possible +- **Previous Output** (iteration 2+): collapsed section showing last iteration's output +- **Formal Grades** (if grading was run): collapsed section showing assertion pass/fail +- **Feedback**: a textbox that auto-saves as they type +- **Previous Feedback** (iteration 2+): their comments from last time, shown below the textbox + +The "Benchmark" tab shows the stats summary: pass rates, timing, and token usage for each configuration, with per-eval breakdowns and analyst observations. + +Navigation is via prev/next buttons or arrow keys. When done, they click "Submit All Reviews" which saves all feedback to `feedback.json`. + +### Step 5: Read the feedback + +When the user tells you they're done, read `feedback.json`: + +```json +{ + "reviews": [ + {"run_id": "eval-0-with_skill", "feedback": "the chart is missing axis labels", "timestamp": "..."}, + {"run_id": "eval-1-with_skill", "feedback": "", "timestamp": "..."}, + {"run_id": "eval-2-with_skill", "feedback": "perfect, love this", "timestamp": "..."} + ], + "status": "complete" +} +``` + +Empty feedback means the user thought it was fine. Focus your improvements on the test cases where the user had specific complaints. + +Kill the viewer server when you're done with it: + +```bash +kill $VIEWER_PID 2>/dev/null +``` + +--- + +## Improving the skill + +This is the heart of the loop. You've run the test cases, the user has reviewed the results, and now you need to make the skill better based on their feedback. + +### How to think about improvements + +1. **Generalize from the feedback.** The big picture thing that's happening here is that we're trying to create skills that can be used a million times (maybe literally, maybe even more who knows) across many different prompts. Here you and the user are iterating on only a few examples over and over again because it helps move faster. The user knows these examples in and out and it's quick for them to assess new outputs. But if the skill you and the user are codeveloping works only for those examples, it's useless. Rather than put in fiddly overfitty changes, or oppressively constrictive MUSTs, if there's some stubborn issue, you might try branching out and using different metaphors, or recommending different patterns of working. It's relatively cheap to try and maybe you'll land on something great. + +2. **Keep the prompt lean.** Remove things that aren't pulling their weight. Make sure to read the transcripts, not just the final outputs — if it looks like the skill is making the model waste a bunch of time doing things that are unproductive, you can try getting rid of the parts of the skill that are making it do that and seeing what happens. + +3. **Explain the why.** Try hard to explain the **why** behind everything you're asking the model to do. Today's LLMs are *smart*. They have good theory of mind and when given a good harness can go beyond rote instructions and really make things happen. Even if the feedback from the user is terse or frustrated, try to actually understand the task and why the user is writing what they wrote, and what they actually wrote, and then transmit this understanding into the instructions. If you find yourself writing ALWAYS or NEVER in all caps, or using super rigid structures, that's a yellow flag — if possible, reframe and explain the reasoning so that the model understands why the thing you're asking for is important. That's a more humane, powerful, and effective approach. + +4. **Look for repeated work across test cases.** Read the transcripts from the test runs and notice if the subagents all independently wrote similar helper scripts or took the same multi-step approach to something. If all 3 test cases resulted in the subagent writing a `create_docx.py` or a `build_chart.py`, that's a strong signal the skill should bundle that script. Write it once, put it in `scripts/`, and tell the skill to use it. This saves every future invocation from reinventing the wheel. + +This task is pretty important (we are trying to create billions a year in economic value here!) and your thinking time is not the blocker; take your time and really mull things over. I'd suggest writing a draft revision and then looking at it anew and making improvements. Really do your best to get into the head of the user and understand what they want and need. + +### The iteration loop + +After improving the skill: + +1. Apply your improvements to the skill +2. Rerun all test cases into a new `iteration-/` directory, including baseline runs. If you're creating a new skill, the baseline is always `without_skill` (no skill) — that stays the same across iterations. If you're improving an existing skill, use your judgment on what makes sense as the baseline: the original version the user came in with, or the previous iteration. +3. Launch the reviewer with `--previous-workspace` pointing at the previous iteration +4. Wait for the user to review and tell you they're done +5. Read the new feedback, improve again, repeat + +Keep going until: +- The user says they're happy +- The feedback is all empty (everything looks good) +- You're not making meaningful progress + +--- + +## Advanced: Blind comparison + +For situations where you want a more rigorous comparison between two versions of a skill (e.g., the user asks "is the new version actually better?"), there's a blind comparison system. Read `agents/comparator.md` and `agents/analyzer.md` for the details. The basic idea is: give two outputs to an independent agent without telling it which is which, and let it judge quality. Then analyze why the winner won. + +This is optional, requires subagents, and most users won't need it. The human review loop is usually sufficient. + +--- + +## Description Optimization + +The description field in SKILL.md frontmatter is the primary mechanism that determines whether Claude invokes a skill. After creating or improving a skill, offer to optimize the description for better triggering accuracy. + +### Step 1: Generate trigger eval queries + +Create 20 eval queries — a mix of should-trigger and should-not-trigger. Save as JSON: + +```json +[ + {"query": "the user prompt", "should_trigger": true}, + {"query": "another prompt", "should_trigger": false} +] +``` + +The queries must be realistic and something a Claude Code or Claude.ai user would actually type. Not abstract requests, but requests that are concrete and specific and have a good amount of detail. For instance, file paths, personal context about the user's job or situation, column names and values, company names, URLs. A little bit of backstory. Some might be in lowercase or contain abbreviations or typos or casual speech. Use a mix of different lengths, and focus on edge cases rather than making them clear-cut (the user will get a chance to sign off on them). + +Bad: `"Format this data"`, `"Extract text from PDF"`, `"Create a chart"` + +Good: `"ok so my boss just sent me this xlsx file (its in my downloads, called something like 'Q4 sales final FINAL v2.xlsx') and she wants me to add a column that shows the profit margin as a percentage. The revenue is in column C and costs are in column D i think"` + +For the **should-trigger** queries (8-10), think about coverage. You want different phrasings of the same intent — some formal, some casual. Include cases where the user doesn't explicitly name the skill or file type but clearly needs it. Throw in some uncommon use cases and cases where this skill competes with another but should win. + +For the **should-not-trigger** queries (8-10), the most valuable ones are the near-misses — queries that share keywords or concepts with the skill but actually need something different. Think adjacent domains, ambiguous phrasing where a naive keyword match would trigger but shouldn't, and cases where the query touches on something the skill does but in a context where another tool is more appropriate. + +The key thing to avoid: don't make should-not-trigger queries obviously irrelevant. "Write a fibonacci function" as a negative test for a PDF skill is too easy — it doesn't test anything. The negative cases should be genuinely tricky. + +### Step 2: Review with user + +Present the eval set to the user for review using the HTML template: + +1. Read the template from `assets/eval_review.html` +2. Replace the placeholders: + - `__EVAL_DATA_PLACEHOLDER__` → the JSON array of eval items (no quotes around it — it's a JS variable assignment) + - `__SKILL_NAME_PLACEHOLDER__` → the skill's name + - `__SKILL_DESCRIPTION_PLACEHOLDER__` → the skill's current description +3. Write to a temp file (e.g., `/tmp/eval_review_.html`) and open it: `open /tmp/eval_review_.html` +4. The user can edit queries, toggle should-trigger, add/remove entries, then click "Export Eval Set" +5. The file downloads to `~/Downloads/eval_set.json` — check the Downloads folder for the most recent version in case there are multiple (e.g., `eval_set (1).json`) + +This step matters — bad eval queries lead to bad descriptions. + +### Step 3: Run the optimization loop + +Tell the user: "This will take some time — I'll run the optimization loop in the background and check on it periodically." + +Save the eval set to the workspace, then run in the background: + +```bash +python -m scripts.run_loop \ + --eval-set \ + --skill-path \ + --model \ + --max-iterations 5 \ + --verbose +``` + +Use the model ID from your system prompt (the one powering the current session) so the triggering test matches what the user actually experiences. + +While it runs, periodically tail the output to give the user updates on which iteration it's on and what the scores look like. + +This handles the full optimization loop automatically. It splits the eval set into 60% train and 40% held-out test, evaluates the current description (running each query 3 times to get a reliable trigger rate), then calls Claude to propose improvements based on what failed. It re-evaluates each new description on both train and test, iterating up to 5 times. When it's done, it opens an HTML report in the browser showing the results per iteration and returns JSON with `best_description` — selected by test score rather than train score to avoid overfitting. + +### How skill triggering works + +Understanding the triggering mechanism helps design better eval queries. Skills appear in Claude's `available_skills` list with their name + description, and Claude decides whether to consult a skill based on that description. The important thing to know is that Claude only consults skills for tasks it can't easily handle on its own — simple, one-step queries like "read this PDF" may not trigger a skill even if the description matches perfectly, because Claude can handle them directly with basic tools. Complex, multi-step, or specialized queries reliably trigger skills when the description matches. + +This means your eval queries should be substantive enough that Claude would actually benefit from consulting a skill. Simple queries like "read file X" are poor test cases — they won't trigger skills regardless of description quality. + +### Step 4: Apply the result + +Take `best_description` from the JSON output and update the skill's SKILL.md frontmatter. Show the user before/after and report the scores. + +--- + +### Package and Present (only if `present_files` tool is available) + +Check whether you have access to the `present_files` tool. If you don't, skip this step. If you do, package the skill and present the .skill file to the user: + +```bash +python -m scripts.package_skill +``` + +After packaging, direct the user to the resulting `.skill` file path so they can install it. + +--- + +## Claude.ai-specific instructions + +In Claude.ai, the core workflow is the same (draft → test → review → improve → repeat), but because Claude.ai doesn't have subagents, some mechanics change. Here's what to adapt: + +**Running test cases**: No subagents means no parallel execution. For each test case, read the skill's SKILL.md, then follow its instructions to accomplish the test prompt yourself. Do them one at a time. This is less rigorous than independent subagents (you wrote the skill and you're also running it, so you have full context), but it's a useful sanity check — and the human review step compensates. Skip the baseline runs — just use the skill to complete the task as requested. + +**Reviewing results**: If you can't open a browser (e.g., Claude.ai's VM has no display, or you're on a remote server), skip the browser reviewer entirely. Instead, present results directly in the conversation. For each test case, show the prompt and the output. If the output is a file the user needs to see (like a .docx or .xlsx), save it to the filesystem and tell them where it is so they can download and inspect it. Ask for feedback inline: "How does this look? Anything you'd change?" + +**Benchmarking**: Skip the quantitative benchmarking — it relies on baseline comparisons which aren't meaningful without subagents. Focus on qualitative feedback from the user. + +**The iteration loop**: Same as before — improve the skill, rerun the test cases, ask for feedback — just without the browser reviewer in the middle. You can still organize results into iteration directories on the filesystem if you have one. + +**Description optimization**: This section requires the `claude` CLI tool (specifically `claude -p`) which is only available in Claude Code. Skip it if you're on Claude.ai. + +**Blind comparison**: Requires subagents. Skip it. + +**Packaging**: The `package_skill.py` script works anywhere with Python and a filesystem. On Claude.ai, you can run it and the user can download the resulting `.skill` file. + +**Updating an existing skill**: The user might be asking you to update an existing skill, not create a new one. In this case: +- **Preserve the original name.** Note the skill's directory name and `name` frontmatter field -- use them unchanged. E.g., if the installed skill is `research-helper`, output `research-helper.skill` (not `research-helper-v2`). +- **Copy to a writable location before editing.** The installed skill path may be read-only. Copy to `/tmp/skill-name/`, edit there, and package from the copy. +- **If packaging manually, stage in `/tmp/` first**, then copy to the output directory -- direct writes may fail due to permissions. + +--- + +## Cowork-Specific Instructions + +If you're in Cowork, the main things to know are: + +- You have subagents, so the main workflow (spawn test cases in parallel, run baselines, grade, etc.) all works. (However, if you run into severe problems with timeouts, it's OK to run the test prompts in series rather than parallel.) +- You don't have a browser or display, so when generating the eval viewer, use `--static ` to write a standalone HTML file instead of starting a server. Then proffer a link that the user can click to open the HTML in their browser. +- For whatever reason, the Cowork setup seems to disincline Claude from generating the eval viewer after running the tests, so just to reiterate: whether you're in Cowork or in Claude Code, after running tests, you should always generate the eval viewer for the human to look at examples before revising the skill yourself and trying to make corrections, using `generate_review.py` (not writing your own boutique html code). Sorry in advance but I'm gonna go all caps here: GENERATE THE EVAL VIEWER *BEFORE* evaluating inputs yourself. You want to get them in front of the human ASAP! +- Feedback works differently: since there's no running server, the viewer's "Submit All Reviews" button will download `feedback.json` as a file. You can then read it from there (you may have to request access first). +- Packaging works — `package_skill.py` just needs Python and a filesystem. +- Description optimization (`run_loop.py` / `run_eval.py`) should work in Cowork just fine since it uses `claude -p` via subprocess, not a browser, but please save it until you've fully finished making the skill and the user agrees it's in good shape. +- **Updating an existing skill**: The user might be asking you to update an existing skill, not create a new one. Follow the update guidance in the claude.ai section above. + +--- + +## Reference files + +The agents/ directory contains instructions for specialized subagents. Read them when you need to spawn the relevant subagent. + +- `agents/grader.md` — How to evaluate assertions against outputs +- `agents/comparator.md` — How to do blind A/B comparison between two outputs +- `agents/analyzer.md` — How to analyze why one version beat another + +The references/ directory has additional documentation: +- `references/schemas.md` — JSON structures for evals.json, grading.json, etc. + +--- + +Repeating one more time the core loop here for emphasis: + +- Figure out what the skill is about +- Draft or edit the skill +- Run claude-with-access-to-the-skill on test prompts +- With the user, evaluate the outputs: + - Create benchmark.json and run `eval-viewer/generate_review.py` to help the user review them + - Run quantitative evals +- Repeat until you and the user are satisfied +- Package the final skill and return it to the user. + +Please add steps to your TodoList, if you have such a thing, to make sure you don't forget. If you're in Cowork, please specifically put "Create evals JSON and run `eval-viewer/generate_review.py` so human can review test cases" in your TodoList to make sure it happens. + +Good luck! \ No newline at end of file diff --git a/PiRC1/client.js b/PiRC1/client.js new file mode 100644 index 000000000..2f05d659c --- /dev/null +++ b/PiRC1/client.js @@ -0,0 +1,553 @@ +/** + * PiRC1 – Pi Ecosystem Token Design + * Complete JavaScript Database Client + API Layer + * Source: https://github.com/PiNetwork/PiRC + * + * Works in Node.js (ES Modules) or browser (as a module). + * Provides: DB operations, PiPower calc, Engagement scoring, + * Design1/Design2 allocation, LP formation, TGE logic. + */ + +// ───────────────────────────────────────────── +// CONSTANTS (PiRC1 Spec) +// ───────────────────────────────────────────── +export const PIRC1_CONFIG = { + BASELINE_LOCKUP_PCT: 0.90, + BASELINE_LOCKUP_YEARS: 3.0, + BASELINE_CUTOFF_DATE: new Date("2026-02-20"), + ALLOCATION_DESIGNS: ["design1", "design2"], + PHASES: ["participation_window", "allocation_period", "tge", "post_tge"], + PLATFORM_BASELINE_PIPOWER: 100, +}; + +// ───────────────────────────────────────────── +// IN-MEMORY STORE +// ───────────────────────────────────────────── +class PiRC1Store { + constructor(initialData = {}) { + this._pioneers = new Map(); + this._projects = new Map(); + this._launches = new Map(); + this._lp_pools = new Map(); + this._snapshots = []; + + // Seed from existing JSON if provided + if (initialData.pioneers) + initialData.pioneers.forEach(p => this._pioneers.set(p.id, { ...p })); + if (initialData.projects) + initialData.projects.forEach(p => this._projects.set(p.id, { ...p })); + if (initialData.launches) + initialData.launches.forEach(l => this._launches.set(l.id, { ...l })); + } + + // ── Pioneers ──────────────────────────── + addPioneer(pioneer) { + if (this._pioneers.has(pioneer.id)) throw new Error(`Pioneer ${pioneer.id} already exists`); + this._pioneers.set(pioneer.id, { ...pioneer }); + return pioneer; + } + getPioneer(id) { return this._pioneers.get(id) || null; } + listPioneers() { return [...this._pioneers.values()]; } + updatePioneer(id, updates) { + const p = this._pioneers.get(id); + if (!p) throw new Error(`Pioneer ${id} not found`); + Object.assign(p, updates); + return p; + } + + // ── Projects ──────────────────────────── + addProject(project) { + if (this._projects.has(project.id)) throw new Error(`Project ${project.id} already exists`); + this._projects.set(project.id, { ...project }); + return project; + } + getProject(id) { return this._projects.get(id) || null; } + listProjects() { return [...this._projects.values()]; } + updateProjectStatus(id, status) { + const p = this._projects.get(id); + if (!p) throw new Error(`Project ${id} not found`); + p.status = status; + return p; + } + + // ── Launches ──────────────────────────── + addLaunch(launch) { + this._launches.set(launch.id, { ...launch }); + return launch; + } + getLaunch(id) { return this._launches.get(id) || null; } + listLaunches() { return [...this._launches.values()]; } + updateLaunch(id, updates) { + const l = this._launches.get(id); + if (!l) throw new Error(`Launch ${id} not found`); + Object.assign(l, updates); + return l; + } + + // ── Serialise ──────────────────────────── + toJSON() { + return { + meta: { name: "PiRC1 Database", version: "1.0.0", exported: new Date().toISOString() }, + pioneers: this.listPioneers(), + projects: this.listProjects(), + launches: this.listLaunches(), + }; + } +} + +// ───────────────────────────────────────────── +// UTILITY HELPERS +// ───────────────────────────────────────────── +function uid(prefix = "id") { + return `${prefix}_${Math.random().toString(36).slice(2, 10)}`; +} + +function yearsBetween(isoDate, to = new Date()) { + const from = new Date(isoDate); + return (to - from) / (1000 * 60 * 60 * 24 * 365.25); +} + +function lockupPct(pioneer) { + return pioneer.mined_pi > 0 ? pioneer.locked_pi / pioneer.mined_pi : 0; +} + +function isBaselineEligible(pioneer) { + const pct = lockupPct(pioneer); + const years = yearsBetween(pioneer.lockup_start_date); + const created = new Date(pioneer.account_created); + return ( + pct >= PIRC1_CONFIG.BASELINE_LOCKUP_PCT && + years >= PIRC1_CONFIG.BASELINE_LOCKUP_YEARS && + created < PIRC1_CONFIG.BASELINE_CUTOFF_DATE + ); +} + +// ───────────────────────────────────────────── +// PIRC1 ENGINE – Core Logic +// ───────────────────────────────────────────── +export class PiRC1Engine { + /** + * @param {PiRC1Store} store + */ + constructor(store) { + this.store = store; + } + + // ── PiPower Calculation ───────────────── + /** + * PiPower ∝ (stakedPi / totalStaked) × T_available + * + baseline for qualifying Long-Term Lockers + */ + calculatePiPower({ pioneerId, stakedPi, totalStakedPiNetwork, tAvailable, platformBaseline }) { + const pioneer = this.store.getPioneer(pioneerId); + if (!pioneer) throw new Error(`Pioneer ${pioneerId} not found`); + + const proportional = totalStakedPiNetwork > 0 + ? (stakedPi / totalStakedPiNetwork) * tAvailable + : 0; + + const baseline = isBaselineEligible(pioneer) + ? (platformBaseline ?? PIRC1_CONFIG.PLATFORM_BASELINE_PIPOWER) + : 0; + + return Math.round((proportional + baseline) * 1e6) / 1e6; + } + + // ── Engagement Scoring ────────────────── + /** + * Returns engagement score 0.0 – 1.0 + * Weights: registered 20%, onboarded 20%, features 30%, milestones 30% + */ + scoreEngagement({ registered, onboarded, featuresUsed, milestonesCompleted, maxFeatures = 10, maxMilestones = 5 }) { + let score = 0; + if (registered) score += 0.20; + if (onboarded) score += 0.20; + score += 0.30 * Math.min(featuresUsed / maxFeatures, 1); + score += 0.30 * Math.min(milestonesCompleted / maxMilestones, 1); + return Math.round(score * 10000) / 10000; + } + + // ── Liquidity Pool Formation ──────────── + /** + * Per PiRC1: initial LP is PERMANENTLY locked; withdrawal disabled. + */ + formLiquidityPool({ piLocked, tokensLocked }) { + const initialPrice = tokensLocked > 0 ? piLocked / tokensLocked : 0; + return { + pi_locked: Math.round(piLocked * 1e6) / 1e6, + tokens_locked: Math.round(tokensLocked * 1e6) / 1e6, + initial_price_pi: Math.round(initialPrice * 1e8) / 1e8, + withdrawal_enabled: false, // PERMANENTLY DISABLED (PiRC1 spec) + formed_at: new Date().toISOString(), + }; + } + + // ── TGE Price Lower Bound ─────────────── + tgePriceLowerBound(lpPi, lpTokens) { + return lpTokens > 0 ? Math.round((lpPi / lpTokens) * 1e8) / 1e8 : 0; + } + + // ── Design 1: Stability-Oriented ──────── + /** + * 50/50 purchase vs liquidity buckets. + * Moderate engagement bonus (up to +10% extra tokens). + * No lock-up on bonuses. + */ + allocateDesign1({ participants, tAvailable, totalPiCommitted, projectLiquidityTokens }) { + const purchaseBucket = tAvailable * 0.50; + const liquidityBucket = tAvailable * 0.50; + const totalPiPower = participants.reduce((s, p) => s + p.pipower, 0) || 1; + + const allocations = participants.map(p => { + const share = p.pipower / totalPiPower; + const baseTokens = share * purchaseBucket; + const bonusPct = p.engagement_score * 0.10; // up to 10% + const bonusTokens = baseTokens * bonusPct; + return { + pioneer_id: p.pioneer_id, + pipower: p.pipower, + engagement_score: p.engagement_score, + base_tokens: Math.round(baseTokens * 1e6) / 1e6, + bonus_tokens: Math.round(bonusTokens * 1e6) / 1e6, + total_tokens: Math.round((baseTokens + bonusTokens) * 1e6) / 1e6, + pi_paid: p.pi_committed, + discount_pct: Math.round(bonusPct * 100 * 100) / 100, + lock_up_months: 0, + }; + }); + + const lp = this.formLiquidityPool({ + piLocked: totalPiCommitted, + tokensLocked: liquidityBucket + projectLiquidityTokens, + }); + + return { design: "design1", allocations, liquidity_pool: lp }; + } + + // ── Design 2: Engagement-Weighted ─────── + /** + * Hybrid fixed-price + swap. + * Top 10%: 30% discount + 12mo lock-up + * Next 20%: 20% discount + 6mo lock-up + * Rest: 10% discount + 3mo lock-up + */ + allocateDesign2({ participants, totalPiCommitted, projectLiquidityTokens, basePricePi }) { + const sorted = [...participants].sort((a, b) => b.engagement_score - a.engagement_score); + const n = sorted.length; + + const allocations = sorted.map((p, rank) => { + let discountPct, lockUpMonths; + if (rank < n * 0.10) { discountPct = 0.30; lockUpMonths = 12; } + else if (rank < n * 0.30) { discountPct = 0.20; lockUpMonths = 6; } + else { discountPct = 0.10; lockUpMonths = 3; } + + const effectivePrice = basePricePi * (1 - discountPct); + const tokens = effectivePrice > 0 ? p.pi_committed / effectivePrice : 0; + + return { + pioneer_id: p.pioneer_id, + rank: rank + 1, + engagement_score: p.engagement_score, + pipower: p.pipower, + pi_paid: p.pi_committed, + effective_price_pi: Math.round(effectivePrice * 1e6) / 1e6, + total_tokens: Math.round(tokens * 1e6) / 1e6, + discount_pct: discountPct * 100, + lock_up_months: lockUpMonths, + }; + }); + + const lp = this.formLiquidityPool({ + piLocked: totalPiCommitted, + tokensLocked: projectLiquidityTokens, + }); + + return { design: "design2", allocations, liquidity_pool: lp }; + } + + // ── Full Launch Simulation ────────────── + /** + * @param {string} projectId + * @param {object[]} participantData [{pioneerId, stakedPi, piCommitted, engagementData}] + * @param {string} allocationDesign "design1" | "design2" + * @param {number} basePricePi only used by design2 + */ + simulateLaunch({ projectId, participantData, allocationDesign = "design1", basePricePi = 1.0 }) { + const project = this.store.getProject(projectId); + if (!project) throw new Error(`Project ${projectId} not found`); + + const tAvailable = project.tokens_for_launchpad; + const lpTokens = project.tokens_for_liquidity; + const totalStaked = participantData.reduce((s, p) => s + p.staked_pi, 0) || 1; + const totalPi = participantData.reduce((s, p) => s + p.pi_committed, 0); + + // Enrich participants with PiPower + engagement score + const enriched = participantData.map(pd => { + const pioneer = this.store.getPioneer(pd.pioneer_id); + if (!pioneer) throw new Error(`Pioneer ${pd.pioneer_id} not found`); + + const pipower = this.calculatePiPower({ + pioneerId: pd.pioneer_id, + stakedPi: pd.staked_pi, + totalStakedPiNetwork: totalStaked, + tAvailable, + }); + + const engagement_score = pd.engagement_score ?? this.scoreEngagement(pd.engagement_data ?? {}); + + return { ...pd, pioneer_id: pd.pioneer_id, pipower, engagement_score }; + }); + + let result; + if (!PIRC1_CONFIG.ALLOCATION_DESIGNS.includes(allocationDesign)) { + throw new Error(`Invalid allocation design: ${allocationDesign}`); + } + + if (allocationDesign === "design1") { + result = this.allocateDesign1({ participants: enriched, tAvailable, totalPiCommitted: totalPi, projectLiquidityTokens: lpTokens }); + } else { + result = this.allocateDesign2({ participants: enriched, totalPiCommitted: totalPi, projectLiquidityTokens: lpTokens, basePricePi }); + } + + result.project_id = projectId; + result.project_name = project.name; + result.token_symbol = project.token_symbol; + result.total_pi_raised = totalPi; + result.participant_count = enriched.length; + result.tge_price_lower_bound = this.tgePriceLowerBound( + result.liquidity_pool.pi_locked, + result.liquidity_pool.tokens_locked, + ); + result.simulated_at = new Date().toISOString(); + + return result; + } + + // ── Reports ───────────────────────────── + reportPioneer(pioneerId) { + const p = this.store.getPioneer(pioneerId); + if (!p) return { error: "Pioneer not found" }; + return { + ...p, + computed: { + lockup_pct: Math.round(lockupPct(p) * 10000) / 100, + lockup_years: Math.round(yearsBetween(p.lockup_start_date) * 100) / 100, + baseline_pipower_eligible: isBaselineEligible(p), + unlocked_pi: Math.round((p.mined_pi - p.locked_pi) * 1e6) / 1e6, + }, + }; + } + + reportProject(projectId) { + const p = this.store.getProject(projectId); + if (!p) return { error: "Project not found" }; + const communityAlloc = p.tokens_for_launchpad + p.tokens_for_liquidity; + return { + ...p, + computed: { + community_allocation_pct: Math.round(communityAlloc / p.total_supply * 10000) / 100, + team_allocation_pct: Math.round(p.tokens_for_team / p.total_supply * 10000) / 100, + product_first_compliant: p.has_working_product, + anti_rugpull: true, // per PiRC1 spec: LP withdrawal permanently disabled + }, + }; + } + + listAllPioneersWithStats() { + return this.store.listPioneers().map(p => this.reportPioneer(p.id)); + } +} + +// ───────────────────────────────────────────── +// REST-STYLE API LAYER (for Node / Express / Fetch) +// ───────────────────────────────────────────── +export class PiRC1Api { + constructor(engine) { + this.engine = engine; + this.store = engine.store; + } + + /** GET /pioneers */ + getPioneers() { + return { success: true, data: this.engine.listAllPioneersWithStats() }; + } + + /** POST /pioneers */ + createPioneer(body) { + const pioneer = { + id: uid("pioneer"), + username: body.username, + mined_pi: body.mined_pi, + locked_pi: body.locked_pi, + lockup_start_date: body.lockup_start_date, + account_created: body.account_created, + kyc_verified: body.kyc_verified ?? false, + launches_participated: [], + total_tokens_received: {}, + }; + this.store.addPioneer(pioneer); + return { success: true, data: this.engine.reportPioneer(pioneer.id) }; + } + + /** GET /pioneers/:id */ + getPioneer(id) { + const report = this.engine.reportPioneer(id); + if (report.error) return { success: false, error: report.error }; + return { success: true, data: report }; + } + + /** GET /projects */ + getProjects() { + return { success: true, data: this.store.listProjects().map(p => this.engine.reportProject(p.id)) }; + } + + /** POST /projects */ + createProject(body) { + const project = { + id: uid("proj"), + name: body.name, + token_symbol: body.token_symbol, + has_working_product: body.has_working_product, + total_supply: body.total_supply, + tokens_for_launchpad: body.tokens_for_launchpad, + tokens_for_liquidity: body.tokens_for_liquidity, + tokens_for_team: body.tokens_for_team, + team_unlock_schedule_months: body.team_unlock_schedule_months, + allocation_design: body.allocation_design ?? "design1", + escrow_wallet: body.escrow_wallet ?? uid("ESCROW"), + category: body.category ?? "General", + status: "registration", + use_cases: body.use_cases ?? [], + }; + this.store.addProject(project); + return { success: true, data: this.engine.reportProject(project.id) }; + } + + /** POST /launches/simulate */ + simulateLaunch(body) { + try { + const result = this.engine.simulateLaunch({ + projectId: body.project_id, + participantData: body.participants, + allocationDesign: body.allocation_design, + basePricePi: body.base_price_pi, + }); + return { success: true, data: result }; + } catch (err) { + return { success: false, error: err.message }; + } + } + + /** GET /config */ + getConfig() { + return { success: true, data: PIRC1_CONFIG }; + } + + /** POST /engagement/score */ + scoreEngagement(body) { + const score = this.engine.scoreEngagement(body); + return { success: true, data: { engagement_score: score } }; + } +} + +// ───────────────────────────────────────────── +// FACTORY – create a ready-to-use instance +// ───────────────────────────────────────────── +/** + * Create a PiRC1 API instance, optionally seeding from JSON data. + * @param {object} seedData – parsed pirc1_database.json (optional) + */ +export function createPiRC1({ seedData } = {}) { + const store = new PiRC1Store(seedData ?? {}); + const engine = new PiRC1Engine(store); + const api = new PiRC1Api(engine); + return { store, engine, api }; +} + +// ───────────────────────────────────────────── +// DEMO (Node.js: node pirc1_client.js) +// ───────────────────────────────────────────── +function runDemo() { + console.log("\n=== PiRC1 JavaScript Demo ===\n"); + + const { api } = createPiRC1(); + + // Create pioneers + const alice = api.createPioneer({ + username: "alice_pi", mined_pi: 5000, locked_pi: 4800, + lockup_start_date: "2022-01-15", account_created: "2021-03-10", kyc_verified: true, + }); + const bob = api.createPioneer({ + username: "bob_pi", mined_pi: 300, locked_pi: 200, + lockup_start_date: "2023-06-01", account_created: "2022-09-20", kyc_verified: true, + }); + const carol = api.createPioneer({ + username: "carol_pi", mined_pi: 12000, locked_pi: 10800, + lockup_start_date: "2021-05-10", account_created: "2020-12-01", kyc_verified: true, + }); + + console.log("Pioneers:"); + [alice, bob, carol].forEach(r => { + const { data: d } = r; + console.log(` [${d.username}] lockup: ${d.computed.lockup_pct}% | baseline eligible: ${d.computed.baseline_pipower_eligible}`); + }); + + // Create project + const project = api.createProject({ + name: "Demo DeFi App", token_symbol: "DDA", has_working_product: true, + total_supply: 1_000_000_000, tokens_for_launchpad: 200_000_000, + tokens_for_liquidity: 100_000_000, tokens_for_team: 100_000_000, + team_unlock_schedule_months: 24, allocation_design: "design1", + category: "DeFi", use_cases: ["Payments", "Governance", "Staking"], + }); + const proj = project.data; + console.log(`\nProject: ${proj.name} (${proj.token_symbol})`); + console.log(` Community allocation: ${proj.computed.community_allocation_pct}% | Anti-rugpull: ${proj.computed.anti_rugpull}`); + + // Engagement scores + const { engine } = createPiRC1(); + const aliceEng = 0.90; // pre-computed for demo + const bobEng = 0.35; + const carolEng = 1.00; + + // Simulate Design 1 + const d1 = api.simulateLaunch({ + project_id: proj.id, + allocation_design: "design1", + participants: [ + { pioneer_id: alice.data.id, staked_pi: 500, pi_committed: 450, engagement_score: aliceEng }, + { pioneer_id: bob.data.id, staked_pi: 100, pi_committed: 80, engagement_score: bobEng }, + { pioneer_id: carol.data.id, staked_pi: 900, pi_committed: 850, engagement_score: carolEng }, + ], + }); + console.log("\n── Design 1 Launch ──"); + console.log(` Total Pi raised: ${d1.data.total_pi_raised}`); + console.log(` LP: ${d1.data.liquidity_pool.pi_locked} Pi + ${d1.data.liquidity_pool.tokens_locked} tokens`); + console.log(` TGE price lower bound: ${d1.data.tge_price_lower_bound} Pi`); + d1.data.allocations.forEach(a => + console.log(` [${a.pioneer_id}] ${a.total_tokens} tokens | +${a.discount_pct}% bonus | lock: ${a.lock_up_months}mo`) + ); + + // Simulate Design 2 + const d2 = api.simulateLaunch({ + project_id: proj.id, + allocation_design: "design2", + base_price_pi: 0.005, + participants: [ + { pioneer_id: alice.data.id, staked_pi: 500, pi_committed: 450, engagement_score: aliceEng }, + { pioneer_id: bob.data.id, staked_pi: 100, pi_committed: 80, engagement_score: bobEng }, + { pioneer_id: carol.data.id, staked_pi: 900, pi_committed: 850, engagement_score: carolEng }, + ], + }); + console.log("\n── Design 2 Launch ──"); + d2.data.allocations.forEach(a => + console.log(` Rank #${a.rank} [${a.pioneer_id}] ${a.total_tokens} tokens | ${a.discount_pct}% off | lock: ${a.lock_up_months}mo`) + ); + + console.log("\n✓ PiRC1 JS engine ready.\n"); +} + +// Auto-run in Node.js +if (typeof process !== "undefined" && process.argv[1]?.endsWith("pirc1_client.js")) { + runDemo(); +} \ No newline at end of file diff --git a/PiRC1/database.json b/PiRC1/database.json new file mode 100644 index 000000000..a9bba5ba7 --- /dev/null +++ b/PiRC1/database.json @@ -0,0 +1,227 @@ +{ + "meta": { + "name": "PiRC1 – Pi Ecosystem Token Design Database", + "version": "1.0.0", + "source": "https://github.com/PiNetwork/PiRC", + "description": "Complete structured database derived from PiRC1 specification", + "created": "2026-04-12", + "sections": ["vision", "core_design", "participation", "allocation", "liquidity", "projects", "pioneers"] + }, + + "config": { + "baseline_lockup_pct": 0.90, + "baseline_lockup_years": 3, + "baseline_cutoff_date": "2026-02-20", + "allocation_designs": ["design1", "design2"], + "phases": ["participation_window", "allocation_period", "tge", "post_tge"] + }, + + "vision": { + "title": "Pi Launchpad – Utility-First Token Framework", + "pillars": [ + { + "id": "P1", + "name": "Stake", + "description": "Users stake Pi to receive PiPower, which determines maximum token allocation capacity." + }, + { + "id": "P2", + "name": "Escrow", + "description": "Committed Pi flows into an Escrow Wallet – never transferred to the project team." + }, + { + "id": "P3", + "name": "Liquidity", + "description": "All committed Pi and project tokens form a permanent, locked liquidity pool (LP)." + }, + { + "id": "P4", + "name": "Market Activation", + "description": "TGE opens the LP/trading market for all users after the Allocation Period ends." + } + ], + "principles": [ + "Product-first: projects must have a working app before token launch", + "No Pi transferred to project teams – all Pi becomes liquidity", + "Tokens usable immediately at TGE", + "Engagement-weighted allocation over pure speculation", + "Anti-rug-pull via permanently locked initial liquidity" + ] + }, + + "core_design": { + "phases": [ + { + "id": "phase_1", + "name": "Participation Window", + "description": "Users stake Pi → receive PiPower. Simultaneously engage with the project's live app. At window close, staking and engagement are snapshotted.", + "outputs": ["PiPower per participant", "Engagement rank", "Pi commitment caps"] + }, + { + "id": "phase_2", + "name": "Allocation Period", + "description": "Launchpad forms permanent liquidity pool. Tokens distributed to participants per allocation design.", + "outputs": ["LP seeded", "Tokens distributed"] + }, + { + "id": "phase_3", + "name": "TGE (Token Generation Event)", + "description": "LP and trading market open for all. Tokens immediately usable for their utility.", + "outputs": ["Market open", "Token utility active"] + }, + { + "id": "phase_4", + "name": "Post-TGE", + "description": "Ongoing unlock schedules enforced. Community governance and engagement incentives continue.", + "outputs": ["Unlock schedule enforcement", "Ongoing rewards"] + } + ], + "liquidity_rules": { + "pi_destination": "Liquidity Pool (NOT project team)", + "initial_lp_withdrawal": "Permanently disabled for project Escrow Wallet", + "lp_accessible_to": "All users for Pi ⟺ Token swaps or new liquidity deposits", + "project_lp_contribution": "Project contributes additional tokens to pair with Pioneer Pi commitments" + } + }, + + "participation": { + "pipower_formula": { + "description": "PiPower ∝ (user staked Pi) / (total staked Pi in network) × T_available", + "variables": { + "T_available": "Total tokens provided by project for participants", + "staked_pi_user": "Pi staked by this participant", + "staked_pi_total": "Total Pi staked across all participants in launch", + "PiPower_Baseline": "Auto-granted to qualifying Long-Term Lockers" + } + }, + "baseline_pipower_eligibility": { + "condition": "Active Pi lockup ≥ 90% of mined tokens for ≥ 3 years", + "cutoff_date": "2026-02-20", + "purpose": "Acknowledge Long-Term Lockers who may lack unlocked Pi to stake" + }, + "engagement_measurement": { + "metrics": ["In-app registration", "Onboarding completion", "Feature use", "Milestones reached"], + "effect": "Engagement rank determines effective pricing / bonus discounts at allocation" + }, + "tiers": [ + { + "id": "tier_standard", + "name": "Standard Participant", + "condition": "Staked Pi > 0", + "pipower_multiplier": 1.0 + }, + { + "id": "tier_baseline", + "name": "Long-Term Locker Baseline", + "condition": "lockup_pct >= 0.90 AND lockup_years >= 3 AND account_before_2026-02-20", + "pipower_multiplier": "platform_defined_baseline" + } + ] + }, + + "allocation": { + "design1": { + "name": "Design 1 – Stability-Oriented Model", + "description": "Equal token buckets for purchase and liquidity. Moderate engagement-based bonuses. Smooth price discovery.", + "token_split": { + "purchase_bucket": "50%", + "liquidity_bucket": "50%" + }, + "engagement_bonus": "Moderate – small discount tiers based on engagement rank", + "lock_up_on_bonus": false, + "price_discovery": "Smooth / stable" + }, + "design2": { + "name": "Design 2 – Engagement-Weighted Model", + "description": "Hybrid fixed-price + swap mechanism. Larger discounts for highly engaged users. Lock-up proportional to discount.", + "mechanism": "Fixed-price + AMM swap hybrid", + "engagement_bonus": "Large – up to significant discount for top-ranked participants", + "lock_up_on_bonus": true, + "lock_up_rule": "Lock-up period proportional to discount level received", + "price_discovery": "Dynamic / engagement-driven" + }, + "common_rules": [ + "Unlock schedules for projects not more favorable than community unlock schedules", + "All Pi commitments flow to LP, never to project", + "Escrow Wallet permanently restricted from withdrawing initial liquidity" + ] + }, + + "projects": [ + { + "id": "proj_001", + "name": "Example DeFi App", + "status": "registration", + "category": "DeFi", + "has_working_product": true, + "token_symbol": "EDA", + "total_supply": 1000000000, + "tokens_for_launchpad": 200000000, + "tokens_for_liquidity": 100000000, + "tokens_for_team": 100000000, + "team_unlock_schedule_months": 24, + "pi_price_per_token": null, + "tge_date": null, + "participation_window_start": null, + "participation_window_end": null, + "allocation_design": "design1", + "escrow_wallet": "GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", + "lp_address": null, + "use_cases": ["In-app payments", "Governance voting", "Staking rewards"] + } + ], + + "pioneers": [ + { + "id": "pioneer_001", + "username": "alice_pi", + "mined_pi": 5000.0, + "locked_pi": 4800.0, + "lockup_pct": 0.96, + "lockup_start_date": "2022-01-15", + "lockup_years": 4.24, + "account_created": "2021-03-10", + "baseline_pipower_eligible": true, + "kyc_verified": true, + "launches_participated": [], + "total_tokens_received": {} + }, + { + "id": "pioneer_002", + "username": "bob_pi", + "mined_pi": 300.0, + "locked_pi": 200.0, + "lockup_pct": 0.667, + "lockup_start_date": "2023-06-01", + "lockup_years": 2.86, + "account_created": "2022-09-20", + "baseline_pipower_eligible": false, + "kyc_verified": true, + "launches_participated": [], + "total_tokens_received": {} + } + ], + + "launches": [ + { + "id": "launch_001", + "project_id": "proj_001", + "status": "upcoming", + "allocation_design": "design1", + "participation_window": { + "start": null, + "end": null + }, + "total_pi_committed": 0, + "total_participants": 0, + "lp_pi_locked": 0, + "lp_tokens_locked": 0, + "tge_token_price_pi": null, + "participants": [] + } + ], + + "liquidity_pools": [], + + "engagement_snapshots": [] +} \ No newline at end of file diff --git a/PiRC1/engine.py b/PiRC1/engine.py new file mode 100644 index 000000000..290ceda3c --- /dev/null +++ b/PiRC1/engine.py @@ -0,0 +1,579 @@ +""" +PiRC1 – Pi Ecosystem Token Design +Complete Python Database Engine +Source: https://github.com/PiNetwork/PiRC +""" + +import json +import math +import uuid +from datetime import datetime, date +from pathlib import Path +from typing import Optional +from dataclasses import dataclass, asdict, field + + +# ───────────────────────────────────────────── +# CONFIG +# ───────────────────────────────────────────── +DB_PATH = Path("pirc1_database.json") +BASELINE_LOCKUP_PCT = 0.90 +BASELINE_LOCKUP_YEARS = 3.0 +BASELINE_CUTOFF_DATE = date(2026, 2, 20) + + +# ───────────────────────────────────────────── +# DATA CLASSES +# ───────────────────────────────────────────── +@dataclass +class Pioneer: + id: str + username: str + mined_pi: float + locked_pi: float + lockup_start_date: str # ISO date string + account_created: str # ISO date string + kyc_verified: bool = False + launches_participated: list = field(default_factory=list) + total_tokens_received: dict = field(default_factory=dict) + + @property + def lockup_pct(self) -> float: + return self.locked_pi / self.mined_pi if self.mined_pi > 0 else 0.0 + + @property + def lockup_years(self) -> float: + start = date.fromisoformat(self.lockup_start_date) + return (date.today() - start).days / 365.25 + + @property + def baseline_pipower_eligible(self) -> bool: + created = date.fromisoformat(self.account_created) + return ( + self.lockup_pct >= BASELINE_LOCKUP_PCT + and self.lockup_years >= BASELINE_LOCKUP_YEARS + and created < BASELINE_CUTOFF_DATE + ) + + def unlocked_pi(self) -> float: + return self.mined_pi - self.locked_pi + + +@dataclass +class Project: + id: str + name: str + token_symbol: str + has_working_product: bool + total_supply: int + tokens_for_launchpad: int + tokens_for_liquidity: int + tokens_for_team: int + team_unlock_schedule_months: int + allocation_design: str # "design1" | "design2" + escrow_wallet: str + category: str = "General" + status: str = "registration" # registration | active | tge | post_tge + pi_price_per_token: Optional[float] = None + tge_date: Optional[str] = None + use_cases: list = field(default_factory=list) + + +@dataclass +class LaunchParticipant: + pioneer_id: str + staked_pi: float + engagement_score: float = 0.0 # 0.0 – 1.0 + pipower: float = 0.0 + tokens_committed: float = 0.0 + pi_committed: float = 0.0 + discount_pct: float = 0.0 + lock_up_months: int = 0 + + +@dataclass +class Launch: + id: str + project_id: str + allocation_design: str + status: str = "upcoming" # upcoming | participation | allocation | tge | closed + total_pi_committed: float = 0.0 + total_participants: int = 0 + lp_pi_locked: float = 0.0 + lp_tokens_locked: float = 0.0 + tge_token_price_pi: Optional[float] = None + participants: list = field(default_factory=list) + + +# ───────────────────────────────────────────── +# DATABASE LAYER +# ───────────────────────────────────────────── +class PiRC1Database: + """ + In-memory + JSON-backed database for PiRC1 ecosystem data. + """ + + def __init__(self, db_path: Path = DB_PATH): + self.db_path = db_path + self._data: dict = {} + self.load() + + # ── Persistence ────────────────────────── + def load(self): + if self.db_path.exists(): + with open(self.db_path, "r") as f: + self._data = json.load(f) + else: + self._data = { + "meta": {"name": "PiRC1 Database", "version": "1.0.0"}, + "pioneers": [], + "projects": [], + "launches": [], + "liquidity_pools": [], + "engagement_snapshots": [], + } + self.save() + + def save(self): + with open(self.db_path, "w") as f: + json.dump(self._data, f, indent=2, default=str) + + # ── Pioneers ───────────────────────────── + def add_pioneer(self, p: Pioneer) -> Pioneer: + rec = asdict(p) + # Remove computed properties before storing + rec.pop("lockup_pct", None) + rec.pop("lockup_years", None) + rec.pop("baseline_pipower_eligible", None) + self._data["pioneers"].append(rec) + self.save() + return p + + def get_pioneer(self, pioneer_id: str) -> Optional[dict]: + return next((p for p in self._data["pioneers"] if p["id"] == pioneer_id), None) + + def list_pioneers(self) -> list[dict]: + return self._data["pioneers"] + + def update_pioneer_pi(self, pioneer_id: str, mined_pi: float, locked_pi: float): + for p in self._data["pioneers"]: + if p["id"] == pioneer_id: + p["mined_pi"] = mined_pi + p["locked_pi"] = locked_pi + self.save() + return True + return False + + # ── Projects ───────────────────────────── + def add_project(self, proj: Project) -> Project: + self._data["projects"].append(asdict(proj)) + self.save() + return proj + + def get_project(self, project_id: str) -> Optional[dict]: + return next((p for p in self._data["projects"] if p["id"] == project_id), None) + + def list_projects(self) -> list[dict]: + return self._data["projects"] + + def update_project_status(self, project_id: str, status: str): + for p in self._data["projects"]: + if p["id"] == project_id: + p["status"] = status + self.save() + return True + return False + + # ── Launches ───────────────────────────── + def create_launch(self, project_id: str, allocation_design: str) -> dict: + launch = { + "id": f"launch_{uuid.uuid4().hex[:8]}", + "project_id": project_id, + "allocation_design": allocation_design, + "status": "upcoming", + "total_pi_committed": 0.0, + "total_participants": 0, + "lp_pi_locked": 0.0, + "lp_tokens_locked": 0.0, + "tge_token_price_pi": None, + "participants": [], + } + self._data["launches"].append(launch) + self.save() + return launch + + def get_launch(self, launch_id: str) -> Optional[dict]: + return next((l for l in self._data["launches"] if l["id"] == launch_id), None) + + def list_launches(self) -> list[dict]: + return self._data["launches"] + + +# ───────────────────────────────────────────── +# PIRC1 ENGINE – Business Logic +# ───────────────────────────────────────────── +class PiRC1Engine: + """ + Implements all PiRC1 specification logic: + - PiPower calculation + - Engagement scoring + - Design 1 & Design 2 allocation + - Liquidity pool formation + - TGE price discovery + """ + + def __init__(self, db: PiRC1Database): + self.db = db + + # ── PiPower Calculation ─────────────────── + def calculate_pipower( + self, + pioneer_id: str, + staked_pi: float, + total_staked_pi_network: float, + t_available: float, + platform_baseline: float = 100.0, + ) -> float: + """ + PiPower ∝ (staked_pi / total_staked_pi) × T_available + Long-Term Lockers who qualify get baseline PiPower auto-added. + """ + p = self.db.get_pioneer(pioneer_id) + if not p: + raise ValueError(f"Pioneer {pioneer_id} not found") + + proportional = (staked_pi / total_staked_pi_network) * t_available if total_staked_pi_network > 0 else 0.0 + + # Check baseline eligibility + lockup_pct = p["locked_pi"] / p["mined_pi"] if p["mined_pi"] > 0 else 0.0 + start = date.fromisoformat(p["lockup_start_date"]) + lockup_years = (date.today() - start).days / 365.25 + created = date.fromisoformat(p["account_created"]) + + baseline = 0.0 + if ( + lockup_pct >= BASELINE_LOCKUP_PCT + and lockup_years >= BASELINE_LOCKUP_YEARS + and created < BASELINE_CUTOFF_DATE + ): + baseline = platform_baseline + + return round(proportional + baseline, 6) + + # ── Engagement Scoring ─────────────────── + def score_engagement( + self, + registered: bool, + onboarded: bool, + features_used: int, + milestones_completed: int, + max_features: int = 10, + max_milestones: int = 5, + ) -> float: + """ + Returns engagement score 0.0 – 1.0 + """ + score = 0.0 + if registered: score += 0.20 + if onboarded: score += 0.20 + score += 0.30 * min(features_used / max_features, 1.0) + score += 0.30 * min(milestones_completed / max_milestones, 1.0) + return round(score, 4) + + # ── Design 1: Stability-Oriented ───────── + def allocate_design1( + self, + participants: list[dict], + t_available: float, + total_pi_committed: float, + project_liquidity_tokens: float, + ) -> dict: + """ + Design 1 – Equal token buckets for purchase and liquidity. + Moderate engagement-based discounts. + + Returns allocation result + LP formation data. + """ + purchase_bucket = t_available * 0.50 + liquidity_bucket = t_available * 0.50 + + total_pipower = sum(p["pipower"] for p in participants) or 1 + + allocations = [] + for p in participants: + share = p["pipower"] / total_pipower + tokens = round(share * purchase_bucket, 6) + + # Engagement bonus: up to 10% extra tokens (moderate) + bonus_pct = p["engagement_score"] * 0.10 + bonus_tokens = round(tokens * bonus_pct, 6) + + allocations.append({ + "pioneer_id": p["pioneer_id"], + "pipower": p["pipower"], + "engagement_score": p["engagement_score"], + "base_tokens": tokens, + "bonus_tokens": bonus_tokens, + "total_tokens": round(tokens + bonus_tokens, 6), + "pi_paid": p["pi_committed"], + "lock_up_months": 0, + "discount_pct": round(bonus_pct * 100, 2), + }) + + lp = self._form_lp(total_pi_committed, liquidity_bucket + project_liquidity_tokens) + return {"design": "design1", "allocations": allocations, "liquidity_pool": lp} + + # ── Design 2: Engagement-Weighted ──────── + def allocate_design2( + self, + participants: list[dict], + t_available: float, + total_pi_committed: float, + project_liquidity_tokens: float, + base_price_pi: float, + ) -> dict: + """ + Design 2 – Hybrid fixed-price + swap mechanism. + Large discounts for highly engaged users with proportional lock-ups. + """ + # Sort by engagement score (highest first) + sorted_p = sorted(participants, key=lambda x: x["engagement_score"], reverse=True) + n = len(sorted_p) + + allocations = [] + for rank, p in enumerate(sorted_p): + # Discount tiers: top 10% → 30% off, next 20% → 20% off, rest → 10% off + if rank < n * 0.10: + discount_pct = 0.30 + lock_up_months = 12 + elif rank < n * 0.30: + discount_pct = 0.20 + lock_up_months = 6 + else: + discount_pct = 0.10 + lock_up_months = 3 + + effective_price = base_price_pi * (1 - discount_pct) + tokens = round(p["pi_committed"] / effective_price, 6) if effective_price > 0 else 0 + + allocations.append({ + "pioneer_id": p["pioneer_id"], + "rank": rank + 1, + "engagement_score": p["engagement_score"], + "pipower": p["pipower"], + "pi_paid": p["pi_committed"], + "effective_price_pi": round(effective_price, 6), + "total_tokens": tokens, + "discount_pct": round(discount_pct * 100, 1), + "lock_up_months": lock_up_months, + }) + + lp = self._form_lp(total_pi_committed, project_liquidity_tokens) + return {"design": "design2", "allocations": allocations, "liquidity_pool": lp} + + # ── Liquidity Pool Formation ───────────── + def _form_lp(self, pi_locked: float, tokens_locked: float) -> dict: + """ + Forms and permanently locks the LP. + Initial price = pi_locked / tokens_locked (Pi per token). + """ + price = round(pi_locked / tokens_locked, 8) if tokens_locked > 0 else 0 + return { + "pi_locked": round(pi_locked, 6), + "tokens_locked": round(tokens_locked, 6), + "initial_price_pi": price, + "withdrawal_enabled": False, # PERMANENTLY DISABLED per PiRC1 spec + "formed_at": datetime.utcnow().isoformat(), + } + + # ── TGE Price Lower Bound ──────────────── + def tge_price_lower_bound(self, lp_pi: float, lp_tokens: float) -> float: + """ + Mathematical lower bound for token price relative to listing. + Per spec: price_lb = lp_pi / lp_tokens + """ + return round(lp_pi / lp_tokens, 8) if lp_tokens > 0 else 0.0 + + # ── Full Launch Simulation ──────────────── + def simulate_launch( + self, + project_id: str, + participant_data: list[dict], + allocation_design: str = "design1", + base_price_pi: float = 1.0, + ) -> dict: + """ + End-to-end launch simulation for a project. + participant_data: list of {pioneer_id, staked_pi, pi_committed, engagement_score} + """ + project = self.db.get_project(project_id) + if not project: + raise ValueError(f"Project {project_id} not found") + + t_available = project["tokens_for_launchpad"] + lp_tokens = project["tokens_for_liquidity"] + total_staked = sum(p["staked_pi"] for p in participant_data) or 1 + total_pi = sum(p["pi_committed"] for p in participant_data) + + # Enrich with PiPower + enriched = [] + for pd in participant_data: + pp = self.calculate_pipower(pd["pioneer_id"], pd["staked_pi"], total_staked, t_available) + enriched.append({**pd, "pipower": pp}) + + if allocation_design == "design1": + result = self.allocate_design1(enriched, t_available, total_pi, lp_tokens) + else: + result = self.allocate_design2(enriched, t_available, total_pi, lp_tokens, base_price_pi) + + result["project_id"] = project_id + result["project_name"] = project["name"] + result["token_symbol"] = project["token_symbol"] + result["total_pi_raised"] = total_pi + result["participant_count"] = len(enriched) + result["tge_price_lower_bound"] = self.tge_price_lower_bound( + result["liquidity_pool"]["pi_locked"], + result["liquidity_pool"]["tokens_locked"], + ) + return result + + # ── Reporting ──────────────────────────── + def report_pioneer(self, pioneer_id: str) -> dict: + p = self.db.get_pioneer(pioneer_id) + if not p: + return {"error": "Pioneer not found"} + lockup_pct = p["locked_pi"] / p["mined_pi"] if p["mined_pi"] > 0 else 0 + start = date.fromisoformat(p["lockup_start_date"]) + lockup_years = (date.today() - start).days / 365.25 + created = date.fromisoformat(p["account_created"]) + baseline_ok = ( + lockup_pct >= BASELINE_LOCKUP_PCT + and lockup_years >= BASELINE_LOCKUP_YEARS + and created < BASELINE_CUTOFF_DATE + ) + return { + **p, + "computed": { + "lockup_pct": round(lockup_pct * 100, 2), + "lockup_years": round(lockup_years, 2), + "baseline_pipower_eligible": baseline_ok, + "unlocked_pi": round(p["mined_pi"] - p["locked_pi"], 4), + } + } + + def report_project_summary(self, project_id: str) -> dict: + proj = self.db.get_project(project_id) + if not proj: + return {"error": "Project not found"} + community_alloc = proj["tokens_for_launchpad"] + proj["tokens_for_liquidity"] + community_pct = round(community_alloc / proj["total_supply"] * 100, 2) + return { + **proj, + "computed": { + "community_allocation_pct": community_pct, + "team_allocation_pct": round(proj["tokens_for_team"] / proj["total_supply"] * 100, 2), + "product_first_compliant": proj["has_working_product"], + } + } + + +# ───────────────────────────────────────────── +# DEMO / SEED +# ───────────────────────────────────────────── +def seed_demo(db: PiRC1Database, engine: PiRC1Engine): + print("\n=== PiRC1 Demo Seed ===\n") + + # Add pioneers + alice = Pioneer( + id="pioneer_alice", username="alice_pi", + mined_pi=5000, locked_pi=4800, + lockup_start_date="2022-01-15", account_created="2021-03-10", + kyc_verified=True, + ) + bob = Pioneer( + id="pioneer_bob", username="bob_pi", + mined_pi=300, locked_pi=200, + lockup_start_date="2023-06-01", account_created="2022-09-20", + kyc_verified=True, + ) + carol = Pioneer( + id="pioneer_carol", username="carol_pi", + mined_pi=12000, locked_pi=10800, + lockup_start_date="2021-05-10", account_created="2020-12-01", + kyc_verified=True, + ) + db.add_pioneer(alice) + db.add_pioneer(bob) + db.add_pioneer(carol) + print(f" ✓ Pioneers added: {alice.username}, {bob.username}, {carol.username}") + + # Add project + proj = Project( + id="proj_demoapp", name="Demo DeFi App", token_symbol="DDA", + has_working_product=True, total_supply=1_000_000_000, + tokens_for_launchpad=200_000_000, tokens_for_liquidity=100_000_000, + tokens_for_team=100_000_000, team_unlock_schedule_months=24, + allocation_design="design1", + escrow_wallet="G_ESCROW_DEMO_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", + category="DeFi", use_cases=["Payments", "Governance", "Staking"], + ) + db.add_project(proj) + print(f" ✓ Project added: {proj.name} ({proj.token_symbol})") + + # Engagement scores + alice_eng = engine.score_engagement(True, True, 8, 4) + bob_eng = engine.score_engagement(True, False, 2, 1) + carol_eng = engine.score_engagement(True, True, 10, 5) + print(f"\n Engagement scores:") + print(f" alice: {alice_eng}") + print(f" bob: {bob_eng}") + print(f" carol: {carol_eng}") + + # Pioneer reports + for pid in ["pioneer_alice", "pioneer_bob", "pioneer_carol"]: + r = engine.report_pioneer(pid) + c = r["computed"] + print(f"\n [{r['username']}]") + print(f" Lockup: {c['lockup_pct']}% for {c['lockup_years']}y") + print(f" Baseline PiPower eligible: {c['baseline_pipower_eligible']}") + print(f" Unlocked Pi: {c['unlocked_pi']}") + + # Simulate launch – Design 1 + participants = [ + {"pioneer_id": "pioneer_alice", "staked_pi": 500, "pi_committed": 450, "engagement_score": alice_eng}, + {"pioneer_id": "pioneer_bob", "staked_pi": 100, "pi_committed": 80, "engagement_score": bob_eng}, + {"pioneer_id": "pioneer_carol", "staked_pi": 900, "pi_committed": 850, "engagement_score": carol_eng}, + ] + + print("\n === Design 1 Launch Simulation ===") + result1 = engine.simulate_launch("proj_demoapp", participants, "design1") + print(f" Project: {result1['project_name']} ({result1['token_symbol']})") + print(f" Total Pi raised: {result1['total_pi_raised']}") + print(f" LP formed: {result1['liquidity_pool']['pi_locked']} Pi + {result1['liquidity_pool']['tokens_locked']} tokens") + print(f" TGE price lower bound: {result1['tge_price_lower_bound']} Pi") + print(f" Withdrawal enabled: {result1['liquidity_pool']['withdrawal_enabled']}") + for a in result1["allocations"]: + print(f" [{a['pioneer_id']}] tokens: {a['total_tokens']} | discount: {a['discount_pct']}%") + + print("\n === Design 2 Launch Simulation ===") + result2 = engine.simulate_launch("proj_demoapp", participants, "design2", base_price_pi=0.005) + for a in result2["allocations"]: + print(f" [{a['pioneer_id']}] rank #{a['rank']} | tokens: {a['total_tokens']} | discount: {a['discount_pct']}% | lock: {a['lock_up_months']}mo") + + # Project summary + summary = engine.report_project_summary("proj_demoapp") + c = summary["computed"] + print(f"\n Project Summary:") + print(f" Community allocation: {c['community_allocation_pct']}%") + print(f" Team allocation: {c['team_allocation_pct']}%") + print(f" Product-first compliant: {c['product_first_compliant']}") + print("\n ✓ All data saved to pirc1_database.json\n") + + +# ───────────────────────────────────────────── +# ENTRY POINT +# ───────────────────────────────────────────── +if __name__ == "__main__": + db = PiRC1Database(Path("pirc1_database.json")) + engine = PiRC1Engine(db) + seed_demo(db, engine) \ No newline at end of file