-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
173 additions
and
2 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
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
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
62 changes: 62 additions & 0 deletions
62
...rc/main/kotlin/com/rarible/protocol/order/listener/service/opensea/OpenseaEventHandler.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,62 @@ | ||
package com.rarible.protocol.order.listener.service.opensea | ||
|
||
import com.rarible.core.common.EventTimeMark | ||
import com.rarible.core.common.EventTimeMarks | ||
import com.rarible.core.kafka.RaribleKafkaEventHandler | ||
import com.rarible.core.logging.addToMdc | ||
import com.rarible.opensea.subscriber.model.OpenseaEvent | ||
import com.rarible.opensea.subscriber.model.OpenseaItemCancelled | ||
import com.rarible.protocol.order.core.misc.orderOffchainEventMarks | ||
import com.rarible.protocol.order.core.model.OrderStatus | ||
import com.rarible.protocol.order.core.repository.order.OrderRepository | ||
import com.rarible.protocol.order.core.service.OrderCancelService | ||
import com.rarible.protocol.order.listener.configuration.OpenseaEventProperties | ||
import com.rarible.protocol.order.listener.misc.ForeignOrderMetrics | ||
import io.daonomic.rpc.domain.Word | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.stereotype.Component | ||
|
||
|
||
@Component | ||
class OpenseaEventHandler( | ||
private val orderCancelService: OrderCancelService, | ||
private val properties: OpenseaEventProperties, | ||
private val orderRepository: OrderRepository, | ||
private val foreignOrderMetrics: ForeignOrderMetrics, | ||
) : RaribleKafkaEventHandler<OpenseaEvent> { | ||
|
||
private val logger: Logger = LoggerFactory.getLogger(javaClass) | ||
|
||
override suspend fun handle(event: OpenseaEvent) { | ||
val timeMarks = event.eventTimeMarks?.let { marks -> | ||
EventTimeMarks( | ||
source = marks.source, | ||
marks = marks.marks.map { EventTimeMark(it.name, it.date) } | ||
) | ||
} ?: orderOffchainEventMarks() | ||
when (val payload = event.payload) { | ||
is OpenseaItemCancelled -> handleCancelEvent(payload, timeMarks) | ||
} | ||
} | ||
|
||
private suspend fun handleCancelEvent(event: OpenseaItemCancelled, eventTimeMarks: EventTimeMarks) { | ||
val id = Word.apply(event.orderHash) | ||
val order = orderRepository.findById(id) ?: return | ||
if (order.status == OrderStatus.ACTIVE) { | ||
logger.warn( | ||
"Found canceled order but active in the database ${order.type}: $id. Will cancel: ${properties.cancelEnabled}" | ||
) | ||
foreignOrderMetrics.onOrderInconsistency(platform = order.platform, status = order.status.name) | ||
if (properties.cancelEnabled) { | ||
addToMdc("orderType" to order.type.name) { | ||
logger.info("Unexpected order cancellation: ${order.type}:${order.hash}") | ||
} | ||
orderCancelService.cancelOrder( | ||
id = id, | ||
eventTimeMarksDto = eventTimeMarks, | ||
) | ||
} | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...est/kotlin/com/rarible/protocol/order/listener/service/opensea/OpenseaEventHandlerTest.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,61 @@ | ||
package com.rarible.protocol.order.listener.service.opensea | ||
|
||
import com.rarible.core.kafka.KafkaMessage | ||
import com.rarible.core.kafka.RaribleKafkaProducer | ||
import com.rarible.core.kafka.json.JsonSerializer | ||
import com.rarible.core.test.wait.Wait | ||
import com.rarible.opensea.subscriber.OpenseaTopicProvider | ||
import com.rarible.opensea.subscriber.model.OpenseaEvent | ||
import com.rarible.opensea.subscriber.model.OpenseaEventType | ||
import com.rarible.opensea.subscriber.model.OpenseaItemCancelled | ||
import com.rarible.protocol.order.core.model.OrderStatus | ||
import com.rarible.protocol.order.listener.data.createOrder | ||
import com.rarible.protocol.order.listener.integration.AbstractIntegrationTest | ||
import com.rarible.protocol.order.listener.integration.IntegrationTest | ||
import kotlinx.coroutines.reactor.awaitSingle | ||
import kotlinx.coroutines.runBlocking | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import java.time.Instant | ||
import java.util.UUID | ||
|
||
@IntegrationTest | ||
class OpenseaEventHandlerTest : AbstractIntegrationTest() { | ||
|
||
@Test | ||
fun `check order cancellation`() = runBlocking { | ||
|
||
val order = createOrder().copy(status = OrderStatus.ACTIVE) | ||
orderVersionRepository.save(order.toOrderVersion()).awaitSingle() | ||
orderRepository.save(order) | ||
|
||
val producer = RaribleKafkaProducer( | ||
clientId = UUID.randomUUID().toString(), | ||
valueSerializerClass = JsonSerializer::class.java, | ||
valueClass = OpenseaEvent::class.java, | ||
defaultTopic = OpenseaTopicProvider.getEventTopic("e2e", "ethereum"), | ||
bootstrapServers = orderIndexerProperties.kafkaReplicaSet | ||
) | ||
producer.send( | ||
KafkaMessage( | ||
key = "", | ||
value = OpenseaEvent( | ||
eventId = "", | ||
event = OpenseaEventType.ITEM_CANCELLED, | ||
payload = OpenseaItemCancelled( | ||
orderHash = order.id.hash.prefixed(), | ||
eventTimestamp = Instant.now(), | ||
maker = null | ||
) | ||
), | ||
headers = mapOf(), | ||
id = "" | ||
) | ||
).ensureSuccess() | ||
|
||
Wait.waitAssert { | ||
val updatedOrder = orderRepository.findById(order.hash) | ||
assertThat(updatedOrder?.status).isEqualTo(OrderStatus.CANCELLED) | ||
} | ||
} | ||
} |
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
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