Skip to content
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

[ANCHOR-435]: Update event callback schema #1095

Merged
merged 10 commits into from
Sep 7, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
@AllArgsConstructor
@NoArgsConstructor
public class SendEventRequest {
/** The seconds since the Unix epoch, UTC. */
Long timestamp;
/** The ID of the event */
String id;
/** The ISO 8601 UTC datetime string */
String timestamp;
/** The type of event */
String type;
/** The event payload. */
AnchorEvent payload;
SendEventRequestPayload payload;

/**
* Creates a SendEventRequest from an AnchorEvent.
Expand All @@ -24,8 +28,10 @@ public class SendEventRequest {
*/
public static SendEventRequest from(AnchorEvent event) {
SendEventRequest request = new SendEventRequest();
request.setTimestamp(Instant.now().getEpochSecond());
request.setPayload(event);
request.setId(event.getId());
request.setType(event.getType().toString().toLowerCase());
request.setTimestamp(Instant.now().toString());
request.setPayload(SendEventRequestPayload.from(event));
return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.stellar.anchor.api.callback;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.stellar.anchor.api.event.AnchorEvent;
import org.stellar.anchor.api.platform.GetQuoteResponse;
import org.stellar.anchor.api.platform.GetTransactionResponse;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SendEventRequestPayload {
GetTransactionResponse transaction;
GetQuoteResponse quote;

/**
* Creates a SendEventRequestPayload from an AnchorEvent.
*
* @param event
* @return a SendEventRequestPayload
*/
public static SendEventRequestPayload from(AnchorEvent event) {
SendEventRequestPayload payload = new SendEventRequestPayload();
switch (event.getType()) {
case QUOTE_CREATED:
payload.setQuote(event.getQuote());
case TRANSACTION_CREATED:
payload.setTransaction(event.getTransaction());
case TRANSACTION_ERROR:
payload.setTransaction(event.getTransaction());
case TRANSACTION_STATUS_CHANGED:
payload.setTransaction(event.getTransaction());
}
return payload;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,33 @@ internal class KotlinReferenceServerIntegrationTest {
client.sendEvent(sendEventRequest1)
var latestEvent = client.getLatestEvent()
Assertions.assertNotNull(latestEvent)
JSONAssert.assertEquals(json(latestEvent), json(sendEventRequest1.payload), true)
JSONAssert.assertEquals(json(latestEvent), json(sendEventRequest1), true)
// send event2
client.sendEvent(sendEventRequest2)
latestEvent = client.getLatestEvent()
Assertions.assertNotNull(latestEvent)
JSONAssert.assertEquals(json(latestEvent), json(sendEventRequest2.payload), true)
JSONAssert.assertEquals(json(latestEvent), json(sendEventRequest2), true)
// check if there are totally two events recorded
assertEquals(client.getEvents().size, 2)
JSONAssert.assertEquals(json(client.getEvents()[0]), json(sendEventRequest1.payload), true)
JSONAssert.assertEquals(json(client.getEvents()[1]), json(sendEventRequest2.payload), true)
JSONAssert.assertEquals(json(client.getEvents()[0]), json(sendEventRequest1), true)
JSONAssert.assertEquals(json(client.getEvents()[1]), json(sendEventRequest2), true)
}
}

companion object {
val sendEventRequestJson =
"""
{
"timestamp": 1000000,
"timestamp": "2011-10-05T14:48:00.000Z",
"id": "2a419880-0dde-4821-90cb-f3bfcb671ea3",
"type": "transaction_created",
"payload": {
"type": "TRANSACTION_CREATED",
"id": 100,
"sep": 24,
"transaction": {
"amount_in": {
"amount": "10.0",
"asset": "USDC"
}
"transaction": {
"amount_in": {
"amount": "10.0",
"asset": "USDC"
}
}
}
}
"""
Expand All @@ -77,11 +76,10 @@ internal class KotlinReferenceServerIntegrationTest {
val sendEventRequestJson2 =
"""
{
"timestamp": 1000000,
"id": "2a419880-0dde-4821-90cb-f3bfcb671ea3",
"timestamp": "2011-10-05T14:48:00.000Z",
"type": "quote_created",
"payload": {
"type": "TRANSACTION_CREATED",
"id": 100,
"sep": 24,
"quote": {
"sell_amount": "10.0",
"buy_amount": "1"
Expand Down
Loading