aori-rs 0.3.3

A rust sdk for interacting with the aori api.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# Aori Rust SDK

![aori-rs banner](https://raw.githubusercontent.com/aori-io/.github/refs/heads/main/assets/aori-rs.png)

## Getting Started

#### Installation

Add this to your `Cargo.toml`:

```toml
[dependencies]
aori-rs = "0.3.0"
tokio = { version = "1.0", features = ["full"] }
```

#### Authorization

Interacting with the Aori API currently requires an API key. Inquire at https://aori.io/contact

When you have your API key, you can include it in any API request by passing it as an additional parameter to any of the SDK functions:

```rust
use aori_rs::{get_quote, submit_swap, QuoteRequest, SwapRequest, AORI_API};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load API key from your preferred method
    let api_key = std::env::var("AORI_API_KEY").ok();

    // Use it with any API call
    let quote = get_quote(&quote_request, Some(AORI_API), api_key.as_deref()).await?;

    // Then submit a swap with the same key
    let swap = submit_swap(&swap_request, Some(AORI_API), api_key.as_deref()).await?;

    Ok(())
}
```

You can also use API keys with WebSocket connections:

```rust
use aori_rs::{AoriWebSocket, AORI_WS_API};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize websocket with API key
    let api_key = std::env::var("AORI_API_KEY").ok();
    let ws = AoriWebSocket::new(Some(AORI_WS_API), api_key);

    ws.connect(|event| {
        println!("Received event: {:?}", event);
    }).await?;

    Ok(())
}
```

## API Reference

| Method | Endpoint                   | Description                      | Request Body     |
| ------ | -------------------------- | -------------------------------- | ---------------- |
| `GET`  | `/chains`                  | Get a list of supported chains   | -                |
| `POST` | `/quote`                   | Get a quote                      | `<QuoteRequest>` |
| `POST` | `/swap`                    | Execute Swap                     | `<SwapRequest>`  |
| `GET`  | `/data`                    | Query Historical Orders Database | -                |
| `GET`  | `/data/status/{orderHash}` | Get Swap Details/Status          | -                |
| `WS`   | `/stream`                  | Open a Websocket Connection      | -                |

### `/quote`

The quote endpoint acts as the primary endpoint for users to request quotes.

#### Example QuoteRequest

```bash
curl -X POST https://v3development.api.aori.io/quote \
-H "Content-Type: application/json" \
-H "x-api-key: your_api_key_here" \
-d '{
    "offerer": "0x0000000000000000000000000000000000000000",
    "recipient": "0x0000000000000000000000000000000000000000",
    "inputToken": "0x4200000000000000000000000000000000000006",
    "outputToken": "0xaf88d065e77c8cc2239327c5edb3a432268e5831",
    "inputAmount": "1000000000000000000",
    "inputChain": "base",
    "outputChain": "arbitrum"
}'
```

#### Example QuoteResponse

```json
{
  "orderHash": "0x9a3af...",
  "signingHash": "0xas23f...",
  "offerer": "0x...",
  "recipient": "0x...",
  "inputToken": "0x...",
  "outputToken": "0x...",
  "inputAmount": "1000000000000000000",
  "outputAmount": "1000000000000000000",
  "inputChain": "base",
  "outputChain": "arbitrum",
  "startTime": "1700000000",
  "endTime": "1700000010",
  "estimatedTime": 3000
}
```

### `/swap`

The swap endpoint acts as the primary endpoint for users to post signed orders for execution.

#### Example SwapRequest

```bash
curl -X POST https://api.aori.io/swap \
-H "Content-Type: application/json" \
-d '{
    "orderHash": "0x...",
    "signature": "0x..."
}'
```

#### Example SwapResponse

```json
{
  "orderHash": "0x9a3af...",
  "offerer": "0x...",
  "recipient": "0x...",
  "inputToken": "0x...",
  "outputToken": "0x...",
  "inputAmount": "1000000000000000000",
  "outputAmount": "1000000000000000000",
  "inputChain": "base",
  "outputChain": "arbitrum",
  "startTime": "1700000000",
  "endTime": "1700000010",
  "status": "pending",
  "createdAt": "1700000000"
}
```

### `/data`

The data endpoint acts as the primary endpoint for users to query historical orders.

#### Parameters

| Parameter     | Type           | Description                                                 |
| ------------- | -------------- | ----------------------------------------------------------- |
| order_hash    | String         | Hash of the order                                           |
| offerer       | String         | Address of the order creator                                |
| recipient     | String         | Address of the order recipient                              |
| input_token   | String         | Address of the token being sent                             |
| input_amount  | String         | Amount of input token                                       |
| output_token  | String         | Address of the token being received                         |
| output_amount | String         | Amount of output token                                      |
| input_chain   | String         | Chain key for the input token (e.g., "arbitrum")            |
| output_chain  | String         | Chain key for the output token (e.g., "base")               |
| src_tx        | Option<String> | Source chain transaction hash                               |
| dst_tx        | Option<String> | Destination chain transaction hash                          |
| status        | String         | Order status (Pending, Received, Completed, Failed)        |
| min_time      | i64            | Unix timestamp, start of filter range by created_at         |
| max_time      | i64            | Unix timestamp, end of filter range by created_at           |
| page          | i64            | Page number (1-x)                                           |
| limit         | i64            | Results per page (1-100)                                    |

## Chains

| Chain    | chainKey   | chainId | eid   | address                                      | vm  |
| -------- | ---------- | ------- | ----- | -------------------------------------------- | --- |
| Ethereum | `ethereum` | 1       | 30101 | `0x98AD96Ef787ba5180814055039F8E37d98ADea63` | EVM |
| Base     | `base`     | 8453    | 30184 | `0xFfe691A6dDb5D2645321e0a920C2e7Bdd00dD3D8` | EVM |
| Arbitrum | `arbitrum` | 42161   | 30110 | `0xFfe691A6dDb5D2645321e0a920C2e7Bdd00dD3D8` | EVM |
| Optimism | `optimism` | 10      | 30111 | `0xFfe691A6dDb5D2645321e0a920C2e7Bdd00dD3D8` | EVM |

## SDK Functions

| Function            | Description                                                    | Parameters                                                                                  | Return Type                                       |
| ------------------- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------- | ------------------------------------------------- |
| `get_quote`         | Requests a quote for a token swap                              | `request: &QuoteRequest, base_url: Option<&str>, api_key: Option<&str>`                     | `Result<QuoteResponse>`                           |
| `sign_order`        | Signs an order using the provided private key                  | `quote_response: &QuoteResponse, private_key: &str`                                         | `Result<String>`                                  |
| `sign_readable_order` | Signs an order using EIP-712 typed data (for frontends)      | `quote_response: &QuoteResponse, private_key: &str, user_address: &str, base_url: Option<&str>` | `Result<(String, String)>`                       |
| `submit_swap`       | Submits a signed swap order to the Aori API.                   | `request: &SwapRequest, base_url: Option<&str>, api_key: Option<&str>`                      | `Result<SwapResponse>`                            |
| `get_order_status`  | Gets the current status of an order.                           | `order_hash: &str, base_url: Option<&str>, api_key: Option<&str>`                           | `Result<OrderStatus>`                             |
| `get_chains`        | Fetches the list of supported chains and their configurations. | `base_url: Option<&str>, api_key: Option<&str>`                                             | `Result<Vec<ChainInfo>>`                          |
| `poll_order_status` | Polls the status of an order until completion or timeout.      | `order_hash: &str, base_url: Option<&str>, api_key: Option<&str>, options: PollOrderStatusOptions` | `Result<OrderStatus>`                             |
| `get_order_details` | Fetches detailed information about an order.                   | `order_hash: &str, base_url: Option<&str>, api_key: Option<&str>`                           | `Result<OrderDetails>`                            |
| `query_orders`      | Queries orders with filtering criteria.                        | `base_url: &str, params: &QueryOrdersParams, api_key: Option<&str>`                        | `Result<QueryOrdersResponse>`                     |

# Examples

### Using API Keys with Environment Variables

This example demonstrates how to use API keys from environment variables:

```rust
use aori_rs::{
    get_quote, 
    submit_swap, 
    get_order_status,
    sign_order,
    QuoteRequest,
    SwapRequest,
    AORI_API
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Get API key from environment
    let api_key = std::env::var("AORI_API_KEY").ok();
    
    // Create a quote request
    let quote_request = QuoteRequest {
        offerer: "0x...".to_string(),
        recipient: "0x...".to_string(),
        input_token: "0x...".to_string(),
        output_token: "0x...".to_string(),
        input_amount: "1000000000000000000".to_string(), // 1 token with 18 decimals
        input_chain: "arbitrum".to_string(),
        output_chain: "base".to_string(),
    };
    
    // Include API key in all requests
    let quote = get_quote(&quote_request, Some(AORI_API), api_key.as_deref()).await?;
    println!("Quote received: {:?}", quote);
    
    // Sign the quote
    let private_key = "your_private_key_here";
    let signature = sign_order(&quote, private_key)?;
    
    // Submit the swap with API key
    let swap_response = submit_swap(&SwapRequest {
        order_hash: quote.order_hash,
        signature,
    }, Some(AORI_API), api_key.as_deref()).await?;
    
    // Check order status with API key
    let status = get_order_status(&swap_response.order_hash, Some(AORI_API), api_key.as_deref()).await?;
    println!("Order status: {:?}", status);

    Ok(())
}
```

### Executing an Order with Private Key Signing

This example demonstrates how to use the SDK with private key signing for backend applications:

```rust
use aori_rs::{
    get_quote,
    sign_order,
    submit_swap,
    get_order_status,
    poll_order_status,
    PollOrderStatusOptions,
    QuoteRequest,
    SwapRequest,
    AORI_API
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Get API key from environment variables or a secured source
    let api_key = std::env::var("AORI_API_KEY").ok();
    let private_key = std::env::var("PRIVATE_KEY")?;

    // 1. Get the quote first
    let quote_request = QuoteRequest {
        offerer: "0x1234567890123456789012345678901234567890".to_string(),
        recipient: "0x1234567890123456789012345678901234567890".to_string(),
        input_token: "0x4200000000000000000000000000000000000006".to_string(), // WETH on Base
        output_token: "0xaf88d065e77c8cc2239327c5edb3a432268e5831".to_string(), // USDC on Arbitrum
        input_amount: "1000000000000000000".to_string(), // 1 WETH
        input_chain: "base".to_string(),
        output_chain: "arbitrum".to_string(),
    };

    let quote = get_quote(&quote_request, Some(AORI_API), api_key.as_deref()).await?;
    println!("Quote received: {:?}", quote);

    // 2. Sign the order using private key
    let signature = sign_order(&quote, &private_key)?;

    // 3. Submit the swap with signature (including API key)
    let swap_request = SwapRequest {
        order_hash: quote.order_hash.clone(),
        signature,
    };

    let swap_response = submit_swap(&swap_request, Some(AORI_API), api_key.as_deref()).await?;
    println!("Swap submitted successfully: {:?}", swap_response);

    // 4. Poll for order status with callbacks
    let options = PollOrderStatusOptions {
        on_status_change: Some(|status| {
            println!("Status changed: {:?}", status);
        }),
        on_complete: Some(|status| {
            println!("Order completed: {:?}", status);
        }),
        on_error: Some(|error| {
            eprintln!("Polling error: {:?}", error);
        }),
        interval: Some(5000), // 5 seconds
        timeout: Some(300000), // 5 minutes
    };

    let final_status = poll_order_status(
        &swap_response.order_hash,
        Some(AORI_API),
        api_key.as_deref(),
        options
    ).await?;

    println!("Final order status: {:?}", final_status);

    Ok(())
}
```

### Real-time Order Monitoring with WebSocket

This example demonstrates WebSocket usage for real-time order monitoring:

```rust
use aori_rs::{AoriWebSocket, WSEventType, AORI_WS_API};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let api_key = std::env::var("AORI_API_KEY").ok();
    let ws = AoriWebSocket::new(Some(AORI_WS_API), api_key);
    
    ws.connect(|event| {
        println!("Received WebSocket event: {:?}", event);
        
        match event.event_type {
            WSEventType::Created => {
                println!("🆕 New order created: {}", event.order.order_hash);
            },
            WSEventType::Received => {
                println!("📥 Order received: {} - Tx: {}", 
                    event.order.order_hash, 
                    event.order.src_tx
                );
            },
            WSEventType::Completed => {
                println!("✅ Order completed: {} - Tx: {}", 
                    event.order.order_hash, 
                    event.order.dst_tx
                );
            },
            WSEventType::Failed => {
                println!("❌ Order failed: {}", event.order.order_hash);
            },
        }
    }).await?;

    Ok(())
}
```

### Querying Order History

This example demonstrates how to query historical orders with filtering:

```rust
use aori_rs::{query_orders, QueryOrdersParams, AORI_API};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let api_key = std::env::var("AORI_API_KEY").ok();
    
    let params = QueryOrdersParams {
        offerer: Some("0x1234567890123456789012345678901234567890".to_string()),
        input_chain: Some("ethereum".to_string()),
        output_chain: Some("base".to_string()),
        status: Some("completed".to_string()),
        page: Some(1),
        limit: Some(10),
        ..Default::default()
    };

    let results = query_orders(AORI_API, &params, api_key.as_deref()).await?;
    
    println!("Found {} orders", results.orders.len());
    println!("Page {} of {} (Total: {} orders)", 
        results.pagination.current_page,
        results.pagination.total_pages,
        results.pagination.total_records
    );
    
    for order in results.orders {
        println!("Order: {} - Status: {} - Amount: {} {}", 
            order.order_hash, 
            order.status,
            order.input_amount,
            order.input_chain
        );
    }

    Ok(())
}
```

## License

This project is released under the [MIT License](LICENSE).