use of io.tbd.tbdex.protocol.messages.Ask in project tbdex-protocol by TBD54566975.
the class HibernateStoreTest method hibernateThreadStoreTest.
@Test
@DisplayName("create DbMessage")
void hibernateThreadStoreTest() {
String threadToken = "thid";
Message message1 = new Message.Builder("mid", threadToken, "pfi", "alice").build(new Ask("USD", BigDecimal.valueOf(100), "USDC"));
threadStore.addMessageToThread(message1);
Assertions.assertEquals(threadStore.getThread(threadToken).getSize(), 1);
Message message2 = new Message.Builder("mid2", threadToken, "pfi2", "alice2").build(new Ask("USD", BigDecimal.valueOf(100), "USDC"));
threadStore.addMessageToThread(message2);
MessageThread messageThread = threadStore.getThread(threadToken);
Assertions.assertEquals(messageThread.getSize(), 2);
Assertions.assertEquals(messageThread.getLastMessage().id(), "mid2");
}
use of io.tbd.tbdex.protocol.messages.Ask in project tbdex-protocol by TBD54566975.
the class PaymentProcessorTest method happyPath.
@Test
@DisplayName("runs without error")
void happyPath() {
String threadToken = "thid";
Message message1 = new Message.Builder("mid", threadToken, "pfi", "alice").build(new Ask("USDC", BigDecimal.valueOf(100), "USDC"));
threadStore.addMessageToThread(message1);
PaymentProcessorRequest request = new PaymentProcessorRequest.Builder().account_number(TestHelper.PAYMENT_INSTRUMENT.account_number).routing_number(TestHelper.PAYMENT_INSTRUMENT.routing_number).bank_address(TestHelper.BANK_ADDRESS).billing_details(TestHelper.BILLING_DETAILS).wallet_address("123").email_address("123").build();
String body = JsonParser.getParser().toJson(request);
SettlementDetails settlementDetails = new SettlementDetails(JsonParser.getParser().toJsonTree(body));
paymentProcessor.process(settlementDetails, threadToken);
}
use of io.tbd.tbdex.protocol.messages.Ask in project tbdex-protocol by TBD54566975.
the class AskProcessorTest method testReturnsConditionalOffer.
@Test
@DisplayName("throws an exception response is not a conditional offer")
void testReturnsConditionalOffer() {
Message message = new Message.Builder("mid", "thid", "pfi", "alice").build(new Ask("USD", BigDecimal.valueOf(100), "USDC"));
Message response = processor.addMessage(message);
Assertions.assertSame(response.type(), MessageType.ConditionalOffer);
Assertions.assertSame(2, threadStore.getThread("thid").getSize());
}
use of io.tbd.tbdex.protocol.messages.Ask in project tbdex-protocol by TBD54566975.
the class PaymentProcessor method process.
public void process(SettlementDetails settlementDetails, String threadToken) {
// Get ASK from thread store
// TODO: change to get conditional offer and also add source and target amounts in offer
HibernateMessageThreadStore threadStore = new HibernateMessageThreadStore();
MessageThread messageThread = threadStore.getThread(threadToken);
Ask ask = messageThread.getAsk();
Preconditions.checkNotNull(ask);
String body = parser.toJson(settlementDetails.body);
PaymentProcessorRequest request = parser.fromJson(body, PaymentProcessorRequest.class);
// Register Bank Account with Circle.
// TODO: Do not store bank account in our database
BankAccount bankAccount = createBankAccount(request);
System.out.println(bankAccount);
Amount amount = new Amount.Builder().amount(ask.sourceAmount.toString()).currency(CurrencyCode.valueOf(ask.sourceCurrency)).build();
// On-Ramp
if (CurrencyCode.valueOf(ask.targetCurrency) == CurrencyCode.USDC) {
CreateWirePaymentRequest createWirePaymentRequest = new CreateWirePaymentRequest.Builder().amount(amount).trackingRef(bankAccount.trackingRef).build();
TransferRequest transferRequest = new TransferRequest.Builder().source(WALLET_SOURCE).destination(new Destination.Builder().type("blockchain").address(request.wallet_address).chain("ETH").build()).idempotencyKey(UUID.randomUUID().toString()).amount(amount).build();
try {
// Create a WIRE payment Request.
circleClient.createWirePayment(createWirePaymentRequest);
// Transfer USDC to external wallet address.
circleClient.transfer(transferRequest);
} catch (Exception e) {
System.out.println("wire payment failed");
}
// Off-Ramp
} else if (CurrencyCode.valueOf(ask.targetCurrency) == CurrencyCode.USD) {
try {
PayoutRequest payoutRequest = new PayoutRequest.Builder().source(WALLET_SOURCE).destination(new Destination.Builder().type("wire").id(bankAccount.id).build()).idempotencyKey(UUID.randomUUID().toString()).metadata(new Metadata.Builder().beneficiaryEmail(request.email_address).build()).amount(amount).build();
circleClient.payout(payoutRequest);
} catch (Exception e) {
System.out.println("payout failed");
}
}
}
use of io.tbd.tbdex.protocol.messages.Ask in project tbdex-protocol by TBD54566975.
the class AskProcessor method process.
@Override
public Message process(Message message) {
Ask ask = getBody(message);
ConditionalOffer conditionalOffer = new ConditionalOffer(ask.sourceCurrency, // Stand in for real offer.
ask.sourceAmount.multiply(BigDecimal.valueOf(0.98)), ask.targetCurrency);
String messageId = UUID.randomUUID().toString();
return new Message.Builder(messageId, message.threadID(), message.to(), message.from()).build(conditionalOffer);
}
Aggregations