Skip to content

Commit a28375a

Browse files
committed
feat(currency/math): switch to discrete bonding curve
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent f7b2156 commit a28375a

File tree

123 files changed

+2085
-791
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+2085
-791
lines changed

apps/flipcash/app/src/main/kotlin/com/flipcash/app/FlipcashApp.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.bugsnag.android.Bugsnag
1515
import com.flipcash.android.app.BuildConfig
1616
import com.flipcash.app.auth.AuthManager
1717
import com.flipcash.app.currency.PreferredCurrencyController
18+
import com.flipcash.libs.currency.math.Curves
1819
import com.getcode.crypt.MnemonicCache
1920
import com.getcode.opencode.repositories.EventRepository
2021
import com.getcode.utils.ErrorUtils
@@ -90,6 +91,7 @@ class FlipcashApp : Application(), Configuration.Provider, SingletonImageLoader.
9091
Firebase.initialize(this)
9192
Firebase.crashlytics.isCrashlyticsCollectionEnabled = BuildConfig.NOTIFY_ERRORS || !BuildConfig.DEBUG
9293
MnemonicCache.init(this)
94+
Curves.initialize(this)
9395
authManager.init()
9496

9597
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import org.gradle.api.DefaultTask
2+
import org.gradle.api.file.DirectoryProperty
3+
import org.gradle.api.file.RegularFileProperty
4+
import org.gradle.api.provider.Property
5+
import org.gradle.api.tasks.*
6+
import java.net.URI
7+
import java.nio.ByteBuffer
8+
import java.nio.ByteOrder
9+
import java.math.BigInteger
10+
11+
abstract class GenerateCurveTables : DefaultTask() {
12+
13+
@get:Input
14+
@get:Optional
15+
abstract val rustTableUrl: Property<String>
16+
17+
@get:InputFile
18+
@get:Optional
19+
abstract val rustTableFile: RegularFileProperty
20+
21+
@get:OutputDirectory
22+
abstract val outputDir: DirectoryProperty
23+
24+
@get:Input
25+
abstract val forceRegenerate: Property<Boolean>
26+
27+
init {
28+
// Default to false
29+
forceRegenerate.convention(false)
30+
31+
onlyIf {
32+
val force = project.hasProperty("forceCurveTables") || forceRegenerate.get()
33+
34+
if (force) {
35+
true
36+
} else if (rustTableUrl.isPresent && !rustTableFile.isPresent) {
37+
val outDir = outputDir.get().asFile
38+
val pricingExists = outDir.resolve("discrete_pricing_table.bin").exists()
39+
val cumulativeExists = outDir.resolve("discrete_cumulative_table.bin").exists()
40+
!(pricingExists && cumulativeExists) // run only if files are MISSING
41+
} else {
42+
true
43+
}
44+
}
45+
}
46+
47+
@TaskAction
48+
fun generate() {
49+
val content = when {
50+
rustTableUrl.isPresent -> {
51+
val url = rustTableUrl.get()
52+
logger.lifecycle("Downloading table from: $url")
53+
URI(url).toURL().readText()
54+
}
55+
rustTableFile.isPresent -> {
56+
logger.lifecycle("Reading table from: ${rustTableFile.get().asFile.path}")
57+
rustTableFile.get().asFile.readText()
58+
}
59+
else -> throw IllegalArgumentException("Must specify either rustTableUrl or rustTableFile")
60+
}
61+
62+
val pricing = extractTable(content, "DISCRETE_PRICING_TABLE")
63+
val cumulative = extractTable(content, "DISCRETE_CUMULATIVE_VALUE_TABLE")
64+
65+
val outDir = outputDir.get().asFile
66+
outDir.mkdirs()
67+
68+
writeBinaryTable(outDir.resolve("discrete_pricing_table.bin"), pricing)
69+
writeBinaryTable(outDir.resolve("discrete_cumulative_table.bin"), cumulative)
70+
71+
logger.lifecycle("Generated curve tables:")
72+
logger.lifecycle(" Pricing: ${pricing.size} entries")
73+
logger.lifecycle(" Cumulative: ${cumulative.size} entries")
74+
}
75+
76+
private fun extractTable(content: String, tableName: String): List<BigInteger> {
77+
val pattern = """pub static $tableName: &\[u128\] = &\[([\s\S]*?)\];""".toRegex()
78+
val match = pattern.find(content)
79+
?: throw IllegalArgumentException("Could not find table: $tableName")
80+
81+
val tableContent = match.groupValues[1]
82+
83+
return tableContent.lines()
84+
.map { it.replace(Regex("//.*$"), "") }
85+
.flatMap { Regex("""\d+""").findAll(it).map { m -> m.value } }
86+
.map { BigInteger(it) }
87+
}
88+
89+
private fun writeBinaryTable(file: java.io.File, values: List<BigInteger>) {
90+
val mask64 = BigInteger("FFFFFFFFFFFFFFFF", 16)
91+
92+
file.outputStream().buffered().use { out ->
93+
val buffer = ByteBuffer.allocate(16).order(ByteOrder.LITTLE_ENDIAN)
94+
95+
for (value in values) {
96+
val low = (value and mask64).toLong()
97+
val high = (value shr 64).toLong()
98+
99+
buffer.clear()
100+
buffer.putLong(low)
101+
buffer.putLong(high)
102+
out.write(buffer.array())
103+
}
104+
}
105+
}
106+
}

libs/currency-math-test/build/intermediates/annotation_processor_list/debug/javaPreCompileDebug/annotationProcessors.json

Lines changed: 0 additions & 1 deletion
This file was deleted.
Binary file not shown.

libs/currency-math-test/build/intermediates/compile_symbol_list/debug/generateDebugRFile/R.txt

Lines changed: 0 additions & 210 deletions
This file was deleted.
Binary file not shown.

libs/currency-math-test/build/intermediates/incremental/debug/packageDebugResources/compile-file-map.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

libs/currency-math-test/build/intermediates/incremental/debug/packageDebugResources/merger.xml

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)