Repository: farcaster-project/farcaster-node
Pull request #817 (2022-12-12 16:36:17)

Grpc: Added decoded OfferInfo field to returned offers
This adds richer information to returned offers, allowing the user of the API to use offers directly without going through the OfferInfo endpoint.


Patch:
diff --git a/src/grpcd/proto/farcaster.proto b/src/grpcd/proto/farcaster.proto
index 8d9e3570..b391744e 100644
--- a/src/grpcd/proto/farcaster.proto
+++ b/src/grpcd/proto/farcaster.proto
@@ -79,7 +79,7 @@ message SwapInfoResponse {
     string connection = 2;
     uint64 uptime = 3;
     uint64 since= 4;
-    string public_offer = 5;
+    OfferInfo offer = 5;
     bool connected = 6;
     TradeRole trade_role = 7;
     SwapRole swap_role = 8;
@@ -94,18 +94,23 @@ message OfferInfoRequest {
 
 message OfferInfoResponse {
     uint32 id = 1;
-    uint64 arbitrating_amount = 2;
-    uint64 accordant_amount = 3;
-    uint32 cancel_timelock = 4;
-    uint32 punish_timelock = 5;
-    string fee_strategy = 6;
-    SwapRole maker_role = 7;
-    string uuid = 8;
-    Network network = 9;
-    Blockchain arbitrating_blockchain = 10;
-    Blockchain accordant_blockchain = 11;
-    string node_id = 12;
-    string peer_address = 13;
+    OfferInfo offer_info = 2;
+}
+
+message OfferInfo {
+    uint64 arbitrating_amount = 1;
+    uint64 accordant_amount = 2;
+    uint32 cancel_timelock = 3;
+    uint32 punish_timelock = 4;
+    string fee_strategy = 5;
+    SwapRole maker_role = 6;
+    string uuid = 7;
+    Network network = 8;
+    Blockchain arbitrating_blockchain = 9;
+    Blockchain accordant_blockchain = 10;
+    string node_id = 11;
+    string peer_address = 12;
+    string encoded_offer = 13;
 }
 
 message PeersRequest {
@@ -131,7 +136,7 @@ enum OfferSelector {
 
 message ListOffersResponse {
     uint32 id = 1;
-    repeated string public_offers = 2;
+    repeated OfferInfo offers = 2;
 }
 
 message CheckpointsRequest {
@@ -151,7 +156,7 @@ enum CheckpointSelector {
 
 message CheckpointEntry {
     string swap_id = 1;
-    string public_offer = 2;
+    OfferInfo offer = 2;
     TradeRole trade_role = 3;
 }
 
@@ -206,7 +211,7 @@ message MakeRequest {
  
 message MakeResponse {
     uint32 id = 1;
-    string offer = 2;
+    OfferInfo offer = 2;
 }
 
 message TakeRequest {
diff --git a/src/grpcd/runtime.rs b/src/grpcd/runtime.rs
index e6a0892b..d9fff9b4 100644
--- a/src/grpcd/runtime.rs
+++ b/src/grpcd/runtime.rs
@@ -147,6 +147,32 @@ impl From<Outcome> for farcaster::Outcome {
     }
 }
 
+impl From<PublicOffer> for OfferInfo {
+    fn from(public_offer: PublicOffer) -> OfferInfo {
+        OfferInfo {
+            arbitrating_amount: public_offer.offer.arbitrating_amount.as_sat(),
+            accordant_amount: public_offer.offer.accordant_amount.as_pico(),
+            cancel_timelock: public_offer.offer.cancel_timelock.as_u32(),
+            punish_timelock: public_offer.offer.punish_timelock.as_u32(),
+            fee_strategy: public_offer.offer.fee_strategy.to_string(),
+            maker_role: farcaster::SwapRole::from(public_offer.offer.maker_role).into(),
+            uuid: public_offer.offer.uuid.to_string(),
+            network: farcaster::Network::from(public_offer.offer.network).into(),
+            arbitrating_blockchain: farcaster::Blockchain::from(
+                public_offer.offer.arbitrating_blockchain,
+            )
+            .into(),
+            accordant_blockchain: farcaster::Blockchain::from(
+                public_offer.offer.accordant_blockchain,
+            )
+            .into(),
+            node_id: public_offer.node_id.to_string(),
+            peer_address: public_offer.peer_address.to_string(),
+            encoded_offer: public_offer.to_string(),
+        }
+    }
+}
+
 impl From<StateReport> for farcaster::State {
     fn from(state_report: StateReport) -> farcaster::State {
         match state_report.clone() {
@@ -456,7 +482,7 @@ impl Farcaster for FarcasterService {
                     connected,
                     uptime: uptime.as_secs(),
                     since: since,
-                    public_offer: public_offer.to_string(),
+                    offer: Some(public_offer.into()),
                     trade_role: farcaster::TradeRole::from(local_trade_role).into(),
                     swap_role: farcaster::SwapRole::from(local_swap_role).into(),
                     connected_counterparty_node_id: connected_counterparty_node_id
@@ -492,14 +518,17 @@ impl Farcaster for FarcasterService {
             Ok(BusMsg::Info(InfoMsg::OfferList(mut offers))) => {
                 let reply = ListOffersResponse {
                     id,
-                    public_offers: offers.drain(..).map(|o| o.offer).collect(),
+                    offers: offers
+                        .drain(..)
+                        .map(|o| OfferInfo::from(o.details))
+                        .collect(),
                 };
                 Ok(GrpcResponse::new(reply))
             }
             Ok(BusMsg::Info(InfoMsg::OfferStatusList(mut offers))) => {
                 let reply = ListOffersResponse {
                     id,
-                    public_offers: offers.drain(..).map(|o| o.offer.to_string()).collect(),
+                    offers: offers.drain(..).map(|o| OfferInfo::from(o.offer)).collect(),
                 };
                 Ok(GrpcResponse::new(reply))
             }
@@ -523,24 +552,7 @@ impl Farcaster for FarcasterService {
 
         let reply = OfferInfoResponse {
             id,
-            arbitrating_amount: public_offer.offer.arbitrating_amount.as_sat(),
-            accordant_amount: public_offer.offer.accordant_amount.as_pico(),
-            cancel_timelock: public_offer.offer.cancel_timelock.as_u32(),
-            punish_timelock: public_offer.offer.punish_timelock.as_u32(),
-            fee_strategy: public_offer.offer.fee_strategy.to_string(),
-            maker_role: farcaster::SwapRole::from(public_offer.offer.maker_role).into(),
-            uuid: public_offer.offer.uuid.to_string(),
-            network: farcaster::Network::from(public_offer.offer.network).into(),
-            arbitrating_blockchain: farcaster::Blockchain::from(
-                public_offer.offer.arbitrating_blockchain,
-            )
-            .into(),
-            accordant_blockchain: farcaster::Blockchain::from(
-                public_offer.offer.accordant_blockchain,
-            )
-            .into(),
-            node_id: public_offer.node_id.to_string(),
-            peer_address: public_offer.peer_address.to_string(),
+            offer_info: Some(public_offer.into()),
         };
         Ok(GrpcResponse::new(reply))
     }
@@ -600,7 +612,7 @@ impl Farcaster for FarcasterService {
                         .iter()
                         .map(|entry| farcaster::CheckpointEntry {
                             swap_id: format!("{:#x}", entry.swap_id),
-                            public_offer: format!("{}", entry.public_offer),
+                            offer: Some(entry.public_offer.clone().into()),
                             trade_role: farcaster::TradeRole::from(entry.trade_role) as i32,
                         })
                         .collect(),
@@ -810,7 +822,7 @@ impl Farcaster for FarcasterService {
             Ok(BusMsg::Info(InfoMsg::MadeOffer(made_offer))) => {
                 let reply = farcaster::MakeResponse {
                     id,
-                    offer: made_offer.offer_info.offer,
+                    offer: Some(made_offer.offer_info.details.into()),
                 };
                 Ok(GrpcResponse::new(reply))
             }


Test patch:
diff --git a/tests/grpc.rs b/tests/grpc.rs
index 0fda2c6d..1b9d0520 100644
--- a/tests/grpc.rs
+++ b/tests/grpc.rs
@@ -95,7 +95,7 @@ async fn grpc_server_functional_test() {
     // Test revoke offer
     let request = tonic::Request::new(RevokeOfferRequest {
         id: 4,
-        public_offer: offer.to_string(),
+        public_offer: offer.unwrap().encoded_offer,
     });
     let response = farcaster_client_1.revoke_offer(request).await;
     assert_eq!(response.unwrap().into_inner().id, 4);
@@ -113,7 +113,7 @@ async fn grpc_server_functional_test() {
     // Test Offer info
     let offer_info_request = tonic::Request::new(OfferInfoRequest {
         id: 21,
-        public_offer: offer.clone().to_string(),
+        public_offer: offer.clone().unwrap().encoded_offer,
     });
     let response = farcaster_client_2.offer_info(offer_info_request).await;
     assert_eq!(response.unwrap().into_inner().id, 21);
@@ -129,7 +129,7 @@ async fn grpc_server_functional_test() {
     // Test take offer
     let take_request = TakeRequest {
         id: 5,
-        public_offer: offer.to_string(),
+        public_offer: offer.unwrap().encoded_offer,
         bitcoin_address: btc_address.to_string(),
         monero_address: xmr_address.to_string(),
     };
@@ -185,7 +185,7 @@ async fn grpc_server_functional_test() {
     let btc_address = bitcoin_rpc.get_new_address(None, None).unwrap();
     let take_request = TakeRequest {
         id: 5,
-        public_offer: offer.to_string(),
+        public_offer: offer.unwrap().encoded_offer,
         bitcoin_address: btc_address.to_string(),
         monero_address: xmr_address.to_string(),
     };