Skip to content

Commit

Permalink
Simplify the EventService interface
Browse files Browse the repository at this point in the history
  • Loading branch information
lijamie98 committed Jun 28, 2023
1 parent a8bce9b commit 9c2d9d2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 40 deletions.
6 changes: 0 additions & 6 deletions core/src/main/java/org/stellar/anchor/event/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

import org.stellar.anchor.api.event.AnchorEvent;
import org.stellar.anchor.api.exception.EventPublishException;
import org.stellar.anchor.sep24.Sep24Transaction;
import org.stellar.anchor.sep31.Sep31Transaction;

public interface EventService {
void publish(Sep24Transaction txn, AnchorEvent.Type type);

void publish(Sep31Transaction txn, AnchorEvent.Type type);

void publish(AnchorEvent event) throws EventPublishException;
}
11 changes: 10 additions & 1 deletion core/src/main/java/org/stellar/anchor/sep24/Sep24Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.URISyntaxException;
import java.time.Instant;
import java.util.*;
import org.stellar.anchor.api.event.AnchorEvent;
import org.stellar.anchor.api.exception.*;
import org.stellar.anchor.api.sep.AssetInfo;
import org.stellar.anchor.api.sep.sep24.*;
Expand All @@ -30,6 +31,7 @@
import org.stellar.anchor.config.Sep24Config;
import org.stellar.anchor.event.EventService;
import org.stellar.anchor.util.GsonUtils;
import org.stellar.anchor.util.TransactionHelper;
import org.stellar.sdk.KeyPair;
import org.stellar.sdk.Memo;

Expand Down Expand Up @@ -180,7 +182,14 @@ public InteractiveTransactionResponse withdraw(

Sep24Transaction txn = builder.build();
txnStore.save(txn);
eventService.publish(txn, TRANSACTION_CREATED);

eventService.publish(
AnchorEvent.builder()
.id(UUID.randomUUID().toString())
.sep("24")
.type(TRANSACTION_CREATED)
.transaction(TransactionHelper.toGetTransactionResponse(txn, assetService))
.build());

infoF(
"Saved withdraw transaction. from={}, amountIn={}, amountOut={}.",
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/org/stellar/anchor/sep31/Sep31Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.Data;
import lombok.SneakyThrows;
import org.stellar.anchor.api.callback.*;
import org.stellar.anchor.api.event.AnchorEvent;
import org.stellar.anchor.api.exception.*;
import org.stellar.anchor.api.sep.AssetInfo;
import org.stellar.anchor.api.sep.AssetInfo.Sep12Operation;
Expand All @@ -36,6 +37,7 @@
import org.stellar.anchor.sep38.Sep38Quote;
import org.stellar.anchor.sep38.Sep38QuoteStore;
import org.stellar.anchor.util.Log;
import org.stellar.anchor.util.TransactionHelper;

public class Sep31Service {
private final AppConfig appConfig;
Expand Down Expand Up @@ -178,7 +180,13 @@ public Sep31PostTransactionResponse postTransaction(

updateDepositInfo();

eventService.publish(txn, TRANSACTION_CREATED);
eventService.publish(
AnchorEvent.builder()
.id(UUID.randomUUID().toString())
.sep("31")
.type(TRANSACTION_CREATED)
.transaction(TransactionHelper.toGetTransactionResponse(txn))
.build());

return Sep31PostTransactionResponse.builder()
.id(txn.getId())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package org.stellar.anchor.platform.event;

import static org.stellar.anchor.api.event.AnchorEvent.Type.TRANSACTION_CREATED;
import static org.stellar.anchor.util.Log.errorF;

import io.micrometer.core.instrument.Metrics;
import java.util.Map;
import java.util.UUID;
import lombok.SneakyThrows;
import org.stellar.anchor.api.event.AnchorEvent;
import org.stellar.anchor.api.exception.EventPublishException;
import org.stellar.anchor.asset.AssetService;
import org.stellar.anchor.config.event.EventConfig;
import org.stellar.anchor.event.EventPublisher;
import org.stellar.anchor.event.EventService;
import org.stellar.anchor.sep24.Sep24Transaction;
import org.stellar.anchor.sep31.Sep31Transaction;
import org.stellar.anchor.util.TransactionHelper;

public class DefaultEventService implements EventService {
private final EventConfig eventConfig;
Expand All @@ -30,32 +24,6 @@ public DefaultEventService(EventConfig eventConfig, AssetService assetService) {
this.assetService = assetService;
}

@Override
@SneakyThrows
public void publish(Sep24Transaction txn, AnchorEvent.Type type) {
AnchorEvent event =
AnchorEvent.builder()
.id(UUID.randomUUID().toString())
.sep("24")
.type(TRANSACTION_CREATED)
.transaction(TransactionHelper.toGetTransactionResponse(txn, assetService))
.build();
publish(event);
}

@Override
@SneakyThrows
public void publish(Sep31Transaction txn, AnchorEvent.Type type) {
AnchorEvent event =
AnchorEvent.builder()
.id(UUID.randomUUID().toString())
.sep("31")
.type(type)
.transaction(TransactionHelper.toGetTransactionResponse(txn))
.build();
publish(event);
}

@Override
public void publish(AnchorEvent event) throws EventPublishException {
if (eventConfig.isEnabled()) {
Expand Down

0 comments on commit 9c2d9d2

Please sign in to comment.