Skip to content

Commit 1f02dc5

Browse files
committed
feat(currency/math): switch to discrete bonding curve
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent f7b2156 commit 1f02dc5

123 files changed

Lines changed: 2086 additions & 791 deletions

File tree

Some content is hidden

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

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: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
outputs.upToDateWhen {
32+
// Check for -PforceCurveTables on command line OR task property
33+
val force = project.hasProperty("forceCurveTables") || forceRegenerate.get()
34+
35+
if (force) {
36+
false
37+
} else if (rustTableUrl.isPresent && !rustTableFile.isPresent) {
38+
val outDir = outputDir.get().asFile
39+
val pricingExists = outDir.resolve("discrete_pricing_table.bin").exists()
40+
val cumulativeExists = outDir.resolve("discrete_cumulative_table.bin").exists()
41+
pricingExists && cumulativeExists
42+
} else {
43+
true
44+
}
45+
}
46+
}
47+
48+
@TaskAction
49+
fun generate() {
50+
val content = when {
51+
rustTableUrl.isPresent -> {
52+
val url = rustTableUrl.get()
53+
logger.lifecycle("Downloading table from: $url")
54+
URI(url).toURL().readText()
55+
}
56+
rustTableFile.isPresent -> {
57+
logger.lifecycle("Reading table from: ${rustTableFile.get().asFile.path}")
58+
rustTableFile.get().asFile.readText()
59+
}
60+
else -> throw IllegalArgumentException("Must specify either rustTableUrl or rustTableFile")
61+
}
62+
63+
val pricing = extractTable(content, "DISCRETE_PRICING_TABLE")
64+
val cumulative = extractTable(content, "DISCRETE_CUMULATIVE_VALUE_TABLE")
65+
66+
val outDir = outputDir.get().asFile
67+
outDir.mkdirs()
68+
69+
writeBinaryTable(outDir.resolve("discrete_pricing_table.bin"), pricing)
70+
writeBinaryTable(outDir.resolve("discrete_cumulative_table.bin"), cumulative)
71+
72+
logger.lifecycle("Generated curve tables:")
73+
logger.lifecycle(" Pricing: ${pricing.size} entries")
74+
logger.lifecycle(" Cumulative: ${cumulative.size} entries")
75+
}
76+
77+
private fun extractTable(content: String, tableName: String): List<BigInteger> {
78+
val pattern = """pub static $tableName: &\[u128\] = &\[([\s\S]*?)\];""".toRegex()
79+
val match = pattern.find(content)
80+
?: throw IllegalArgumentException("Could not find table: $tableName")
81+
82+
val tableContent = match.groupValues[1]
83+
84+
return tableContent.lines()
85+
.map { it.replace(Regex("//.*$"), "") }
86+
.flatMap { Regex("""\d+""").findAll(it).map { m -> m.value } }
87+
.map { BigInteger(it) }
88+
}
89+
90+
private fun writeBinaryTable(file: java.io.File, values: List<BigInteger>) {
91+
val mask64 = BigInteger("FFFFFFFFFFFFFFFF", 16)
92+
93+
file.outputStream().buffered().use { out ->
94+
val buffer = ByteBuffer.allocate(16).order(ByteOrder.LITTLE_ENDIAN)
95+
96+
for (value in values) {
97+
val low = (value and mask64).toLong()
98+
val high = (value shr 64).toLong()
99+
100+
buffer.clear()
101+
buffer.putLong(low)
102+
buffer.putLong(high)
103+
out.write(buffer.array())
104+
}
105+
}
106+
}
107+
}

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)