-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
De-dupe fills and trades from WebSocket. (#413)
Co-authored-by: mobile-build-bot-git <[email protected]>
- Loading branch information
1 parent
896b7d2
commit ef33a43
Showing
7 changed files
with
395 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/commonMain/kotlin/exchange.dydx.abacus/processor/base/MergeWithIds.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package exchange.dydx.abacus.processor.base | ||
|
||
/** | ||
* Merge two lists of payloads, dropping older items if a new item with the same ID exists. | ||
*/ | ||
fun mergeWithIds( | ||
new: List<Any>, | ||
existing: List<Any>, | ||
id: (Any) -> String?, | ||
): List<Any> { | ||
val ids = mutableSetOf<String>() | ||
val merged = mutableListOf<Any>() | ||
new.forEach { item -> | ||
id(item)?.let { itemId -> | ||
ids.add(itemId) | ||
merged.add(item) | ||
} | ||
} | ||
existing.forEach { item -> | ||
id(item)?.let { itemId -> | ||
if (!ids.contains(itemId)) { | ||
ids.add(itemId) | ||
merged.add(item) | ||
} | ||
} | ||
} | ||
|
||
return merged | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 6 additions & 11 deletions
17
src/commonMain/kotlin/exchange.dydx.abacus/processor/wallet/account/FillsProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,19 @@ | ||
package exchange.dydx.abacus.processor.wallet.account | ||
|
||
import exchange.dydx.abacus.processor.base.BaseProcessor | ||
import exchange.dydx.abacus.processor.base.mergeWithIds | ||
import exchange.dydx.abacus.protocols.ParserProtocol | ||
|
||
internal class FillsProcessor(parser: ParserProtocol) : BaseProcessor(parser) { | ||
private val itemProcessor = FillProcessor(parser = parser) | ||
|
||
override fun received(existing: List<Any>?, payload: List<Any>): List<Any>? { | ||
val output = mutableListOf<Any>() | ||
val newItems = payload.mapNotNull { | ||
parser.asNativeMap(it)?.let { map -> | ||
itemProcessor.received(null, map) | ||
} | ||
val new = payload.mapNotNull { eachPayload -> | ||
parser.asNativeMap(eachPayload)?.let { eachPayloadData -> itemProcessor.received(null, eachPayloadData) } | ||
} | ||
if (newItems != null) { | ||
output.addAll(newItems) | ||
existing?.let { | ||
return mergeWithIds(new, existing) { data -> parser.asNativeMap(data)?.let { parser.asString(it["id"]) } } | ||
} | ||
if (existing != null) { | ||
output.addAll(existing) | ||
} | ||
return output | ||
return new | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/commonTest/kotlin/exchange.dydx.abacus/payload/v4/V4DuplicateWebsocketMessageTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package exchange.dydx.abacus.payload.v4 | ||
|
||
import exchange.dydx.abacus.tests.extensions.loadv4TradesChanged | ||
import kotlin.test.Test | ||
|
||
class V4DuplicateWebsocketMessageTests : V4BaseTests() { | ||
|
||
@Test | ||
fun testDuplicateFills() { | ||
setup() | ||
|
||
repeat(2) { | ||
test( | ||
{ | ||
perp.socket( | ||
testWsUrl, | ||
mock.batchedSubaccountsChannel.channel_batch_data_order_filled_1, | ||
0, | ||
null, | ||
) | ||
}, | ||
""" | ||
{ | ||
"wallet": { | ||
"account": { | ||
"tradingRewards": { | ||
"total": 2800.8 | ||
}, | ||
"subaccounts": { | ||
"0": { | ||
"equity": { | ||
}, | ||
"freeCollateral": { | ||
}, | ||
"quoteBalance": { | ||
"current": 1599696.37 | ||
}, | ||
"orders": { | ||
}, | ||
"fills":[ | ||
{ | ||
"id":"a74830f8-d506-54b3-bf3b-1de791b8fe4e", | ||
"fee":"-0.067364", | ||
"side":"BUY", | ||
"size":"82", | ||
"type":"LIMIT", | ||
"price":"9.128", | ||
"orderId":"f7c9cd24-57cd-5240-a98d-3c9c3c11767d", | ||
"createdAt":"2024-05-06T18:41:20.606Z", | ||
"liquidity":"MAKER", | ||
"clientMetadata":"0", | ||
"marketId":"APT-USD" | ||
}, | ||
{ | ||
"id":"0d473eec-93b0-5c49-94ca-b8017454d769", | ||
"fee":"-0.001643", | ||
"side":"BUY", | ||
"size":"2", | ||
"type":"LIMIT", | ||
"price":"9.128", | ||
"orderId":"f7c9cd24-57cd-5240-a98d-3c9c3c11767d", | ||
"createdAt":"2024-05-06T18:41:20.606Z", | ||
"liquidity":"MAKER", | ||
"clientMetadata":"0", | ||
"marketId":"APT-USD" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun testDuplicateTrades() { | ||
setup() | ||
|
||
repeat(2) { | ||
test( | ||
{ | ||
perp.loadv4TradesChanged(mock, testWsUrl) | ||
}, | ||
""" | ||
{ | ||
"markets":{ | ||
"markets":{ | ||
"ETH-USD":{ | ||
"trades": [ | ||
{ | ||
"id": "8ee6d90d-272d-5edd-bf0f-2e4d6ae3d3b7", | ||
"side": "BUY", | ||
"size": 1.593707, | ||
"price": 1255.949, | ||
"createdAt": "2022-12-12T02:28:14.859Z", | ||
"resources": { | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
""".trimIndent(), | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.