pub struct PathData {
pub flow_vertices: Vec<Address>,
pub flow_edges: Vec<FlowEdge>,
pub streams: Vec<Stream>,
pub packed_coordinates: Vec<u8>,
pub source_coordinate: U256,
}Expand description
Simplified pathfinding result data structure
This struct contains the raw pathfinding results in a format that can be easily converted to contract-compatible types. It eliminates the need for manual field-by-field conversion between different type layers.
Fields§
§flow_vertices: Vec<Address>Sorted list of all addresses involved in the flow
flow_edges: Vec<FlowEdge>Flow edges as (stream_sink_id, amount) tuples
streams: Vec<Stream>Streams as (source_coordinate, flow_edge_ids, data) tuples
packed_coordinates: Vec<u8>Packed coordinates as raw bytes
source_coordinate: U256Source coordinate index
Implementations§
Source§impl PathData
impl PathData
Sourcepub fn from_transfers(
transfers: &[TransferStep],
from: Address,
to: Address,
target_flow: U192,
) -> Result<Self, PathfinderError>
pub fn from_transfers( transfers: &[TransferStep], from: Address, to: Address, target_flow: U192, ) -> Result<Self, PathfinderError>
Create PathData from transfer steps
This is the main constructor that takes the output from pathfinding and creates a PathData structure ready for contract conversion.
§Arguments
transfers- Vector of transfer steps from pathfindingfrom- Source addressto- Destination addresstarget_flow- Target flow amount
§Returns
A PathData structure ready for contract conversion
§Errors
Returns PathfinderError if flow matrix creation fails
Sourcepub fn to_flow_edges(&self) -> Vec<FlowEdge>
pub fn to_flow_edges(&self) -> Vec<FlowEdge>
Convert to standard Circles Hub FlowEdge types
Returns a vector of FlowEdge structs with the exact field names and types expected by the Circles Hub smart contract.
§Example
let edges = path_data.to_flow_edges();
assert_eq!(edges[0].streamSinkId, 1);
assert_eq!(edges[0].amount, U192::from(1000u64));Sourcepub fn to_streams(&self) -> Vec<Stream>
pub fn to_streams(&self) -> Vec<Stream>
Convert to standard Circles Hub Stream types
Returns a vector of Stream structs with the exact field names and types expected by the Circles Hub smart contract.
§Example
let streams = path_data.to_streams();
assert_eq!(streams[0].sourceCoordinate, 0);
assert_eq!(streams[0].flowEdgeIds, vec![1, 2]);Sourcepub fn to_contract_params(
&self,
) -> (Vec<Address>, Vec<FlowEdge>, Vec<Stream>, Bytes)
pub fn to_contract_params( &self, ) -> (Vec<Address>, Vec<FlowEdge>, Vec<Stream>, Bytes)
Get all contract call parameters in one tuple
This convenience method returns all the parameters needed for most Circles Hub contract calls in the correct order and format.
§Returns
A tuple of (flow_vertices, flow_edges, streams, packed_coordinates) ready to use in contract function calls.
§Example
let (vertices, edges, streams, coords) = path_data.to_contract_params();
// Ready for contract calls:
// contract.transferFlow(vertices, edges, streams, coords).send().await?;Examples found in repository?
7async fn main() -> Result<(), Box<dyn std::error::Error>> {
8 // Example addresses (replace with real addresses)
9 let sender = Address::from_str("0x52e14be00d5acff4424ad625662c6262b4fd1a58")?;
10 let receiver = Address::from_str("0xcf6dc192dc292d5f2789da2db02d6dd4f41f4214")?;
11 let amount = U192::from_str("1000000000000000000")?; // 1 CRC in wei
12 let rpc_url = "https://rpc.aboutcircles.com/";
13
14 println!("Finding path and preparing contract data...");
15 println!("From: {sender}");
16 println!("To: {receiver}");
17 println!("Amount: {amount} wei");
18
19 let params = FindPathParams {
20 from: sender,
21 to: receiver,
22 target_flow: U256::from(amount),
23 use_wrapped_balances: Some(true),
24 from_tokens: None,
25 to_tokens: None,
26 exclude_from_tokens: None,
27 exclude_to_tokens: None,
28 simulated_balances: None,
29 max_transfers: None,
30 };
31
32 // NEW API: One function call does everything
33 let path_data: PathData = prepare_flow_for_contract(rpc_url, params).await?;
34
35 println!("\nFlow matrix prepared for contract calls:");
36 println!("Flow vertices: {} addresses", path_data.flow_vertices.len());
37 println!("Flow edges: {} transfers", path_data.flow_edges.len());
38 println!("Streams: {} streams", path_data.streams.len());
39 println!(
40 "Packed coordinates: {} bytes",
41 path_data.packed_coordinates.len()
42 );
43 println!("Source coordinate: {}", path_data.source_coordinate);
44
45 // Demonstrate contract-ready data
46 println!("\nContract-ready data:");
47
48 // Flow vertices (already Address types)
49 println!(
50 "Vertices (first 3): {:?}",
51 &path_data.flow_vertices[..3.min(path_data.flow_vertices.len())]
52 );
53
54 // Flow edges (raw tuples - convert to contract types)
55 for (i, flow_edge) in path_data.flow_edges.iter().take(3).enumerate() {
56 println!(
57 "Edge {i}: stream_sink_id={}, amount={amount}",
58 flow_edge.streamSinkId
59 );
60 }
61
62 // Streams (raw tuples - convert to contract types)
63 for (i, stream) in path_data.streams.iter().enumerate() {
64 println!(
65 "Stream {i}: source_coordinate={}, flow_edge_ids={:?}",
66 stream.sourceCoordinate, stream.flowEdgeIds
67 );
68 }
69
70 // Packed coordinates (raw bytes - convert to Bytes for contracts)
71 println!(
72 "Packed coordinates length: {} bytes",
73 path_data.packed_coordinates.len()
74 );
75
76 // Example: How you would use this in a smart contract call
77 println!("\nExample smart contract usage:");
78 println!("```rust");
79 println!("let contract = MyContract::new(contract_address, provider);");
80 println!("let tx = contract");
81 println!(" .redeemPayment(");
82 println!(" module_address,");
83 println!(" subscription_id,");
84 println!(" path_data.flow_vertices, // Vec<Address>");
85 println!(" path_data.to_flow_edges(), // Vec<FlowEdge>");
86 println!(" path_data.to_streams(), // Vec<Stream>");
87 println!(" path_data.to_packed_coordinates() // Bytes");
88 println!(" )");
89 println!(" .send()");
90 println!(" .await?;");
91 println!("```");
92
93 // Demonstrate decomposition for tuple-based contract calls
94 let (vertices, edges, streams, packed_coords) = path_data.to_contract_params();
95 println!("\nFor tuple-based contract calls:");
96 println!("Decomposed into {} components ready for contract", 4);
97 println!("- {} vertices", vertices.len());
98 println!("- {} edges", edges.len());
99 println!("- {} streams", streams.len());
100 println!("- {} bytes of coordinates", packed_coords.len());
101
102 Ok(())
103}Sourcepub fn to_packed_coordinates(&self) -> Bytes
pub fn to_packed_coordinates(&self) -> Bytes
Get packed coordinates as Bytes
Convenience method to get the packed coordinates in the Bytes format expected by contract calls.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PathData
impl RefUnwindSafe for PathData
impl Send for PathData
impl Sync for PathData
impl Unpin for PathData
impl UnwindSafe for PathData
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more