@@ -3,8 +3,13 @@ package com.flipcash.libs.currency.math
33import com.flipcash.libs.currency.math.internal.DefaultMintMaxQuarkSupply
44import java.math.BigDecimal
55import java.math.BigInteger
6+ import java.math.RoundingMode
7+ import kotlin.math.abs
8+ import kotlin.math.roundToLong
69import kotlin.test.Test
710import kotlin.test.assertEquals
11+ import kotlin.test.assertTrue
12+ import kotlin.test.fail
813
914class EstimationTests {
1015
@@ -77,4 +82,59 @@ class EstimationTests {
7782
7883 assertEquals( expectedTotal, (received2 + fees2).toBigInteger())
7984 }
85+
86+ /* *
87+ * This test generates a CSV table to verify the symmetry and accuracy of the buy, sell, and
88+ * value exchange estimations across a wide range of values. It simulates a scenario where an
89+ * initial amount (`valueLocked`) is used to buy tokens from a zero supply. Then, for various
90+ * sub-amounts (`paymentValue`), it calculates the corresponding token amount (`paymentQuarks`)
91+ * and immediately sells them back.
92+ *
93+ * The core assertion is that the value received from selling the tokens (`sellValue`) should be
94+ * almost identical to the original `paymentValue`, accounting for potential minor rounding
95+ * discrepancies (hence the tolerance of `1.0`).
96+ *
97+ * The test iterates through logarithmically scaled values for `valueLocked` and `paymentValue`,
98+ * starting from 10,000 and going up to 1 quadrillion quarks, ensuring the formulas hold
99+ * for both small and extremely large numbers.
100+ *
101+ */
102+ @Test
103+ fun `estimate csv table` () {
104+ val startValue = 10_000L
105+ val endValue = 1_000_000_000_000_000L
106+
107+ println (" value locked,payment value,payment quarks,sell value" )
108+ var valueLocked = startValue
109+ while (valueLocked <= endValue) {
110+ val (circulatingSupply, _) = Estimator .buy(
111+ amountInQuarks = valueLocked,
112+ currentSupplyInQuarks = 0L ,
113+ mintDecimals = 6 ,
114+ feeBps = 0
115+ ).getOrThrow()
116+
117+ var paymentValue = startValue
118+ while (paymentValue <= valueLocked) {
119+ val paymentQuarks = Estimator .valueExchangeAsQuarks(
120+ valueInQuarks = paymentValue,
121+ currentSupplyInQuarks = circulatingSupply.setScale(0 , RoundingMode .HALF_UP ).toDouble().roundToLong(),
122+ mintDecimals = 6 ,
123+ ).getOrThrow()
124+
125+ val (sellValue, _) = Estimator .sell(
126+ amountInQuarks = paymentQuarks.toLong(),
127+ currentValueInQuarks = valueLocked,
128+ mintDecimals = 6 ,
129+ feeBps = 0
130+ ).getOrThrow()
131+
132+
133+ assertTrue { abs(paymentValue.toDouble() - sellValue.toDouble()) <= 1.0 }
134+ println (" $valueLocked ,$paymentValue ,${paymentQuarks.toBigInteger()} ,${sellValue.toBigInteger()} " )
135+ paymentValue * = 10L
136+ }
137+ valueLocked * = 10L
138+ }
139+ }
80140}
0 commit comments