Skip to content

Commit

Permalink
Add Order Instructions to APID (TBD54566975#93)
Browse files Browse the repository at this point in the history
* add orderinstructions

* add OrderInstructionsData field

* add order instructions

* update

* generate schemas

* update bound/kt

* lint

* bindings

* update pfi example

* bump to 21

* Update docs/API_DESIGN.md

Co-authored-by: Kendall Weihe <kendallweihe@gmail.com>

---------

Co-authored-by: Kendall Weihe <kendallweihe@gmail.com>
  • Loading branch information
nitro-neal and KendallWeihe authored Jul 30, 2024
1 parent adfa9f9 commit c35f7aa
Show file tree
Hide file tree
Showing 31 changed files with 928 additions and 117 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ Cargo.lock
build/
.gradle/
**/.idea/
.DS_Store
2 changes: 2 additions & 0 deletions bindings/tbdex_uniffi/src/http/exchanges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl GetExchangeResponseBody {
Message::Rfq(_) => MessageKind::Rfq,
Message::Quote(_) => MessageKind::Quote,
Message::Order(_) => MessageKind::Order,
Message::OrderInstructions(_) => MessageKind::OrderInstructions,
Message::Cancel(_) => MessageKind::Cancel,
Message::OrderStatus(_) => MessageKind::OrderStatus,
Message::Close(_) => MessageKind::Close,
Expand Down Expand Up @@ -197,6 +198,7 @@ impl ReplyToRequestBody {
let inner = InnerReplyToRequestBody::from_json_string(json)?;
let kind = match inner.message {
ReplyToMessage::Quote(_) => MessageKind::Quote,
ReplyToMessage::OrderInstructions(_) => MessageKind::OrderInstructions,
ReplyToMessage::OrderStatus(_) => MessageKind::OrderStatus,
ReplyToMessage::Close(_) => MessageKind::Close,
};
Expand Down
8 changes: 6 additions & 2 deletions bindings/tbdex_uniffi/src/http_client/exchanges.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
errors::Result,
messages::{
cancel::Cancel, close::Close, order::Order, order_status::OrderStatus, quote::Quote,
rfq::Rfq,
cancel::Cancel, close::Close, order::Order, order_instructions::OrderInstructions,
order_status::OrderStatus, quote::Quote, rfq::Rfq,
},
};
use std::sync::{Arc, RwLock};
Expand All @@ -13,6 +13,7 @@ pub struct Exchange {
pub rfq: Arc<Rfq>,
pub quote: Option<Arc<Quote>>,
pub order: Option<Arc<Order>>,
pub order_instructions: Option<Arc<OrderInstructions>>,
pub cancel: Option<Arc<Cancel>>,
pub order_statuses: Option<Vec<Arc<OrderStatus>>>,
pub close: Option<Arc<Close>>,
Expand All @@ -28,6 +29,9 @@ impl Exchange {
order: inner
.order
.map(|o| Arc::new(Order(Arc::new(RwLock::new((*o).clone()))))),
order_instructions: inner
.order_instructions
.map(|oi| Arc::new(OrderInstructions(Arc::new(RwLock::new((*oi).clone()))))),
cancel: inner
.cancel
.map(|c| Arc::new(Cancel(Arc::new(RwLock::new((*c).clone()))))),
Expand Down
11 changes: 7 additions & 4 deletions bindings/tbdex_uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::{
cancel::Cancel,
close::Close,
order::Order,
order_instructions::OrderInstructions,
order_status::OrderStatus,
quote::Quote,
rfq::{data::Rfq as RfqData, Rfq},
Expand All @@ -45,14 +46,16 @@ use tbdex::{
cancel::{Cancel as CancelData, CancelData as CancelDataData},
close::{Close as CloseData, CloseData as CloseDataData},
order::{Order as OrderData, OrderData as OrderDataData},
order_instructions::{
OrderInstructions as OrderInstructionsData,
OrderInstructionsData as OrderInstructionsDataData,
PaymentInstruction as PaymentInstructionData,
},
order_status::{
OrderStatus as OrderStatusData, OrderStatusData as OrderStatusDataData,
Status as OrderStatusStatus,
},
quote::{
PaymentInstruction as PaymentInstructionData, Quote as QuoteData,
QuoteData as QuoteDataData, QuoteDetails as QuoteDetailsData,
},
quote::{Quote as QuoteData, QuoteData as QuoteDataData, QuoteDetails as QuoteDetailsData},
MessageKind, MessageMetadata as MessageMetadataData,
},
resources::{
Expand Down
1 change: 1 addition & 0 deletions bindings/tbdex_uniffi/src/messages/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod cancel;
pub mod close;
pub mod order;
pub mod order_instructions;
pub mod order_status;
pub mod quote;
pub mod rfq;
69 changes: 69 additions & 0 deletions bindings/tbdex_uniffi/src/messages/order_instructions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::errors::{Result, TbdexSdkError};
use std::sync::{Arc, RwLock};
use tbdex::{
json::{FromJson, ToJson},
messages::order_instructions::{
OrderInstructions as InnerOrderInstructions, OrderInstructionsData,
},
};
use web5_uniffi_wrapper::dids::bearer_did::BearerDid;

pub struct OrderInstructions(pub Arc<RwLock<InnerOrderInstructions>>);

impl OrderInstructions {
pub fn create(
to: String,
from: String,
exchange_id: String,
data: OrderInstructionsData,
protocol: Option<String>,
external_id: Option<String>,
) -> Result<Self> {
let order_instructions =
InnerOrderInstructions::create(&to, &from, &exchange_id, &data, protocol, external_id)?;

Ok(Self(Arc::new(RwLock::new(order_instructions))))
}

pub fn sign(&self, bearer_did: Arc<BearerDid>) -> Result<()> {
let mut inner_order_instructions = self
.0
.write()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockWriteError"))?;
inner_order_instructions.sign(&bearer_did.0.clone())?;
Ok(())
}

pub fn from_json_string(json: &str) -> Result<Self> {
let inner_order_instructions = InnerOrderInstructions::from_json_string(json)?;

Ok(Self(Arc::new(RwLock::new(inner_order_instructions))))
}

pub fn to_json_string(&self) -> Result<String> {
let inner_order_instructions = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;

Ok(inner_order_instructions.to_json_string()?)
}

pub fn get_data(&self) -> Result<InnerOrderInstructions> {
let order_instructions = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;

Ok(order_instructions.clone())
}

pub fn verify(&self) -> Result<()> {
let order_instructions = self
.0
.read()
.map_err(|e| TbdexSdkError::from_poison_error(e, "RwLockReadError"))?;

Ok(order_instructions.verify()?)
}
}
39 changes: 33 additions & 6 deletions bindings/tbdex_uniffi/src/tbdex.udl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ enum MessageKind {
"Rfq",
"Quote",
"Order",
"OrderInstructions",
"Cancel",
"OrderStatus",
"Close"
Expand Down Expand Up @@ -163,12 +164,6 @@ dictionary QuoteDetailsData {
string subtotal;
string total;
string? fee;
PaymentInstructionData? payment_instruction;
};

dictionary PaymentInstructionData {
string? link;
string? instruction;
};

interface Order {
Expand Down Expand Up @@ -196,6 +191,37 @@ dictionary OrderDataData {

};

interface OrderInstructions {
[Name=create, Throws=TbdexSdkError]
constructor(string to, string from, string exchange_id, OrderInstructionsDataData data, string? protocol, string? external_id);
[Name=from_json_string, Throws=TbdexSdkError]
constructor([ByRef] string json);
[Throws=TbdexSdkError]
string to_json_string();
[Throws=TbdexSdkError]
OrderInstructionsData get_data();
[Throws=TbdexSdkError]
void verify();
[Throws=TbdexSdkError]
void sign(BearerDid bearer_did);
};

dictionary OrderInstructionsData {
MessageMetadataData metadata;
OrderInstructionsDataData data;
string signature;
};

dictionary OrderInstructionsDataData {
PaymentInstructionData payin;
PaymentInstructionData payout;
};

dictionary PaymentInstructionData {
string? link;
string? instruction;
};

interface Cancel {
[Name=create, Throws=TbdexSdkError]
constructor(string to, string from, string exchange_id, CancelDataData data, string? protocol, string? external_id);
Expand Down Expand Up @@ -293,6 +319,7 @@ dictionary ExchangeData {
Rfq rfq;
Quote? quote;
Order? order;
OrderInstructions? order_instructions;
Cancel? cancel;
sequence<OrderStatus>? order_statuses;
Close? close;
Expand Down
6 changes: 5 additions & 1 deletion bound/kt/src/main/kotlin/tbdex/sdk/http/Exchanges.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class GetExchangeResponseBody private constructor(
is Rfq -> Pair(MessageKind.RFQ, it.toJsonString())
is Quote -> Pair(MessageKind.QUOTE, it.toJsonString())
is Order -> Pair(MessageKind.ORDER, it.toJsonString())
is OrderInstructions -> Pair(MessageKind.ORDER_INSTRUCTIONS, it.toJsonString())
is Cancel -> Pair(MessageKind.CANCEL, it.toJsonString())
is OrderStatus -> Pair(MessageKind.ORDER_STATUS, it.toJsonString())
is Close -> Pair(MessageKind.CLOSE, it.toJsonString())
Expand All @@ -48,6 +49,7 @@ class GetExchangeResponseBody private constructor(
MessageKind.RFQ -> Rfq.fromJsonString(it.jsonSerialized)
MessageKind.QUOTE -> Quote.fromJsonString(it.jsonSerialized)
MessageKind.ORDER -> Order.fromJsonString(it.jsonSerialized)
MessageKind.ORDER_INSTRUCTIONS -> Order.fromJsonString(it.jsonSerialized)
MessageKind.CANCEL -> Cancel.fromJsonString(it.jsonSerialized)
MessageKind.ORDER_STATUS -> OrderStatus.fromJsonString(it.jsonSerialized)
MessageKind.CLOSE -> Close.fromJsonString(it.jsonSerialized)
Expand Down Expand Up @@ -176,6 +178,7 @@ class ReplyToRequestBody private constructor(
message,
when (message) {
is Quote -> RustCoreReplyToRequestBody(MessageKind.QUOTE, message.toJsonString())
is OrderInstructions -> RustCoreReplyToRequestBody(MessageKind.ORDER_INSTRUCTIONS, message.toJsonString())
is OrderStatus -> RustCoreReplyToRequestBody(MessageKind.ORDER_STATUS, message.toJsonString())
is Close -> RustCoreReplyToRequestBody(MessageKind.CLOSE, message.toJsonString())
else -> throw Exception("unknown type")
Expand All @@ -187,8 +190,9 @@ class ReplyToRequestBody private constructor(
val rustCoreReplyToRequestBody = RustCoreReplyToRequestBody.fromJsonString(json)
val data = rustCoreReplyToRequestBody.getData()

val message = when (data.kind) {
val message: ReplyToMessage = when (data.kind) {
MessageKind.QUOTE -> Quote.fromJsonString(data.jsonSerializedMessage)
MessageKind.ORDER_INSTRUCTIONS -> OrderInstructions.fromJsonString(data.jsonSerializedMessage)
MessageKind.ORDER_STATUS -> OrderStatus.fromJsonString(data.jsonSerializedMessage)
MessageKind.CLOSE -> Close.fromJsonString(data.jsonSerializedMessage)
else -> throw Exception("Unsupported message kind ${data.kind}")
Expand Down
2 changes: 2 additions & 0 deletions bound/kt/src/main/kotlin/tbdex/sdk/httpclient/Exchanges.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ data class Exchange(
val rfq: Rfq,
val quote: Quote? = null,
val order: Order? = null,
val orderInstructions: OrderInstructions? = null,
val cancel: Cancel? = null,
val orderStatuses: List<OrderStatus>? = null,
val close: Close? = null
Expand All @@ -28,6 +29,7 @@ data class Exchange(
Rfq.fromRustCoreRfq(rustCoreExchange.rfq),
rustCoreExchange.quote?.let { Quote.fromRustCoreQuote(it) },
rustCoreExchange.order?.let { Order.fromRustCoreOrder(it) },
rustCoreExchange.orderInstructions?.let { OrderInstructions.fromRustCoreOrderInstructions(it) },
rustCoreExchange.cancel?.let { Cancel.fromRustCoreCancel(it) },
rustCoreExchange.orderStatuses?.let { it -> it.map { OrderStatus.fromRustCoreOrderStatus(it) } },
rustCoreExchange.close?.let { Close.fromRustCoreClose(it) },
Expand Down
58 changes: 58 additions & 0 deletions bound/kt/src/main/kotlin/tbdex/sdk/messages/OrderInstructions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package tbdex.sdk.messages

import tbdex.sdk.http.ReplyToMessage
import tbdex.sdk.rust.SystemArchitecture
import tbdex.sdk.web5.BearerDid

import tbdex.sdk.rust.OrderInstructionsDataData as RustCoreOrderInstructionsData
typealias OrderInstructionsData = RustCoreOrderInstructionsData

class OrderInstructions private constructor(
val metadata: MessageMetadata,
val data: OrderInstructionsData,
val signature: String,
internal val rustCoreOrderInstructions: tbdex.sdk.rust.OrderInstructions
): Message, ReplyToMessage {
init {
SystemArchitecture.set() // ensure the sys arch is set for first-time loading
}

companion object {
fun create(
to: String,
from: String,
exchangeId: String,
data: OrderInstructionsData,
protocol: String? = null,
externalId: String? = null
): OrderInstructions {
val rustCoreOrderInstructions =
tbdex.sdk.rust.OrderInstructions.create(to, from, exchangeId, data, protocol, externalId)
val rustCoreData = rustCoreOrderInstructions.getData()
return OrderInstructions(rustCoreData.metadata, rustCoreData.data, rustCoreData.signature, rustCoreOrderInstructions)
}

fun fromJsonString(json: String): OrderInstructions {
val rustCoreOrderInstructions = tbdex.sdk.rust.OrderInstructions.fromJsonString(json)
val rustCoreData = rustCoreOrderInstructions.getData()
return OrderInstructions(rustCoreData.metadata, rustCoreData.data, rustCoreData.signature, rustCoreOrderInstructions)
}

internal fun fromRustCoreOrderInstructions(rustCoreOrderInstructions: tbdex.sdk.rust.OrderInstructions): OrderInstructions {
val rustCoreData = rustCoreOrderInstructions.getData()
return OrderInstructions(rustCoreData.metadata, rustCoreData.data, rustCoreData.signature, rustCoreOrderInstructions)
}
}

fun toJsonString(): String {
return this.rustCoreOrderInstructions.toJsonString()
}

fun sign(bearerDid: BearerDid) {
this.rustCoreOrderInstructions.sign(bearerDid.rustCoreBearerDid)
}

fun verify() {
this.rustCoreOrderInstructions.verify()
}
}
Loading

0 comments on commit c35f7aa

Please sign in to comment.