Repository navigation
feat(perps): show net realized pnl and store profit share #6728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,7 @@ import kotlin.math.min | |
| PerpsFavorite::class, | ||
| PerpsMarketCategoryRelation::class, | ||
| ], | ||
| version = 9, | ||
| version = 10, | ||
| ) | ||
| @DaoReturnTypeConverters(PagingSourceDaoReturnTypeConverter::class) | ||
| abstract class PerpsDatabase : RoomDatabase() { | ||
|
|
@@ -124,6 +124,14 @@ abstract class PerpsDatabase : RoomDatabase() { | |
| connection.execSQL("ALTER TABLE `markets` ADD COLUMN `trade_volume_score_1d` INTEGER NOT NULL DEFAULT 0") | ||
| } | ||
| } | ||
| val MIGRATION_9_10 = | ||
| object : Migration(9, 10) { | ||
| override suspend fun migrate(connection: SQLiteConnection) { | ||
| connection.execSQL("ALTER TABLE `perps_orders` ADD COLUMN `net_realized_pnl` TEXT NOT NULL DEFAULT ''") | ||
| connection.execSQL("ALTER TABLE `perps_orders` ADD COLUMN `net_roe` TEXT NOT NULL DEFAULT ''") | ||
| connection.execSQL("ALTER TABLE `perps_orders` ADD COLUMN `profit_share_amount` TEXT NOT NULL DEFAULT ''") | ||
|
Comment on lines
+130
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On a 9→10 upgrade, every cached order receives empty net fields, but Useful? React with 👍 / 👎. |
||
| } | ||
| } | ||
|
|
||
| @Suppress("DEPRECATION") | ||
| fun getDatabase( | ||
|
|
@@ -143,15 +151,15 @@ abstract class PerpsDatabase : RoomDatabase() { | |
| context, | ||
| PerpsDatabase::class.java, | ||
| File(dir, Constants.DataBase.PERPS_DB_NAME).absolutePath, | ||
| ).setDriver(ReportingAndroidSQLiteDriver("Perps", 9)) | ||
| ).setDriver(ReportingAndroidSQLiteDriver("Perps", 10)) | ||
| .addCallback( | ||
| object : Callback() { | ||
| override suspend fun onOpen(connection: SQLiteConnection) { | ||
| super.onOpen(connection) | ||
| connection.execSQL("PRAGMA synchronous = NORMAL") | ||
| } | ||
| }, | ||
| ).addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8, MIGRATION_8_9) | ||
| ).addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8, MIGRATION_8_9, MIGRATION_9_10) | ||
| .fallbackToDestructiveMigration() | ||
| .enableMultiInstanceInvalidation() | ||
| .setQueryCoroutineContext( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,10 +85,10 @@ interface PerpsOrderDao : BaseDao<PerpsOrder> { | |
| @Query("SELECT MAX(updated_at) FROM perps_orders WHERE status IN ('filled', 'rejected', 'closed')") | ||
| suspend fun getLatestUpdatedAt(): String? | ||
|
|
||
| @Query("SELECT SUM(CAST(realized_pnl AS REAL)) FROM perps_orders WHERE order_type = 'close'") | ||
| @Query("SELECT SUM(CAST(COALESCE(NULLIF(net_realized_pnl, ''), realized_pnl) AS REAL)) FROM perps_orders WHERE order_type = 'close'") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| suspend fun getTotalRealizedPnl(): Double? | ||
|
|
||
| @Query("SELECT COALESCE(SUM(CAST(realized_pnl AS REAL)), 0) FROM perps_orders WHERE order_type = 'close'") | ||
| @Query("SELECT COALESCE(SUM(CAST(COALESCE(NULLIF(net_realized_pnl, ''), realized_pnl) AS REAL)), 0) FROM perps_orders WHERE order_type = 'close'") | ||
| fun observeTotalRealizedPnl(): Flow<Double> | ||
|
|
||
| @Query("SELECT SUM(CAST(entry_price AS REAL) * ABS(CAST(quantity AS REAL))) FROM perps_orders WHERE order_type = 'close'") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an orders response omits any of these new properties—or explicitly returns
nullfor an older order—Gson bypasses Kotlin constructor defaults, so the corresponding non-null property is actuallynull.upsertSyncedOrders()then inserts it into aTEXT NOT NULLcolumn and the order refresh fails instead of falling back to the legacy PnL. Make the wire fields nullable and normalize them before persistence, or provide a deserializer that supplies the empty-string defaults.Useful? React with 👍 / 👎.