agent-first-pay 0.7.0

A payment tool for AI agents — send and receive across five networks through one interface, with spending limits you control.
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use crate::provider::PayError;
use crate::store::wallet::{self, WalletMetadata};
use crate::store::{MigrationLog, PayStore};
use crate::types::{HistoryRecord, Network};
use sqlx::PgPool;
use std::collections::BTreeMap;
use std::path::PathBuf;

const SCHEMA_SQL: &str = r#"
CREATE TABLE IF NOT EXISTS wallets (
    id TEXT PRIMARY KEY,
    network TEXT NOT NULL,
    metadata JSONB NOT NULL,
    created_at_epoch_s BIGINT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_wallets_network ON wallets(network);

CREATE TABLE IF NOT EXISTS transactions (
    sequence BIGSERIAL PRIMARY KEY,
    transaction_id TEXT NOT NULL UNIQUE,
    wallet TEXT NOT NULL,
    record JSONB NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_transactions_wallet ON transactions(wallet);

CREATE TABLE IF NOT EXISTS spend_rules (
    rule_id TEXT PRIMARY KEY,
    rule JSONB NOT NULL
);

CREATE TABLE IF NOT EXISTS spend_reservations (
    reservation_id BIGSERIAL PRIMARY KEY,
    op_id TEXT NOT NULL UNIQUE,
    reservation JSONB NOT NULL
);

CREATE TABLE IF NOT EXISTS spend_events (
    event_id BIGSERIAL PRIMARY KEY,
    reservation_id BIGINT NOT NULL,
    event JSONB NOT NULL
);

CREATE TABLE IF NOT EXISTS exchange_rate_cache (
    pair TEXT PRIMARY KEY,
    quote JSONB NOT NULL
);
"#;

/// Advisory lock key for spend operations (prevents concurrent spend check-then-write).
/// Hex of "afpay\0" = 0x616670617900.
pub const SPEND_ADVISORY_LOCK_KEY: i64 = 0x0061_6670_6179;

/// PostgreSQL-backed storage.
#[derive(Clone)]
pub struct PostgresStore {
    pool: PgPool,
    data_dir: String,
}

impl PostgresStore {
    /// Get a reference to the connection pool (used by spend module).
    pub fn pool(&self) -> &PgPool {
        &self.pool
    }

    pub async fn connect(database_url: &str, data_dir: &str) -> Result<Self, PayError> {
        let pool = PgPool::connect(database_url)
            .await
            .map_err(|e| PayError::InternalError(format!("postgres connect: {e}")))?;

        sqlx::raw_sql(SCHEMA_SQL)
            .execute(&pool)
            .await
            .map_err(|e| PayError::InternalError(format!("postgres schema init: {e}")))?;

        Ok(Self {
            pool,
            data_dir: data_dir.to_string(),
        })
    }
}

impl PayStore for PostgresStore {
    fn save_wallet_metadata(&self, meta: &WalletMetadata) -> Result<(), PayError> {
        let pool = self.pool.clone();
        let meta_json = serde_json::to_value(meta)
            .map_err(|e| PayError::InternalError(format!("serialize wallet metadata: {e}")))?;
        let network_str = meta.network.to_string();
        let id = meta.id.clone();
        let created = meta.created_at_epoch_s as i64;

        tokio::task::block_in_place(|| {
            tokio::runtime::Handle::current().block_on(async {
                sqlx::query(
                    "INSERT INTO wallets (id, network, metadata, created_at_epoch_s) \
                     VALUES ($1, $2, $3, $4) \
                     ON CONFLICT (id) DO UPDATE SET metadata = $3",
                )
                .bind(&id)
                .bind(&network_str)
                .bind(&meta_json)
                .bind(created)
                .execute(&pool)
                .await
                .map_err(|e| PayError::InternalError(format!("postgres save wallet: {e}")))?;
                Ok(())
            })
        })
    }

    fn load_wallet_metadata(&self, wallet_id: &str) -> Result<WalletMetadata, PayError> {
        let pool = self.pool.clone();
        let wallet_id = wallet_id.to_string();

        tokio::task::block_in_place(|| {
            tokio::runtime::Handle::current().block_on(async {
                let row: Option<(serde_json::Value,)> =
                    sqlx::query_as("SELECT metadata FROM wallets WHERE id = $1")
                        .bind(&wallet_id)
                        .fetch_optional(&pool)
                        .await
                        .map_err(|e| {
                            PayError::InternalError(format!("postgres load wallet: {e}"))
                        })?;

                match row {
                    Some((meta_json,)) => serde_json::from_value(meta_json).map_err(|e| {
                        PayError::InternalError(format!("postgres parse wallet metadata: {e}"))
                    }),
                    None => {
                        // Label fallback
                        if !wallet_id.starts_with("w_") {
                            let row: Option<(serde_json::Value,)> = sqlx::query_as(
                                "SELECT metadata FROM wallets WHERE metadata->>'label' = $1",
                            )
                            .bind(&wallet_id)
                            .fetch_optional(&pool)
                            .await
                            .map_err(|e| {
                                PayError::InternalError(format!(
                                    "postgres load wallet by label: {e}"
                                ))
                            })?;
                            if let Some((meta_json,)) = row {
                                return serde_json::from_value(meta_json).map_err(|e| {
                                    PayError::InternalError(format!(
                                        "postgres parse wallet metadata: {e}"
                                    ))
                                });
                            }
                        }
                        Err(PayError::WalletNotFound(format!(
                            "wallet {wallet_id} not found"
                        )))
                    }
                }
            })
        })
    }

    fn list_wallet_metadata(
        &self,
        network: Option<Network>,
    ) -> Result<Vec<WalletMetadata>, PayError> {
        let pool = self.pool.clone();

        tokio::task::block_in_place(|| {
            tokio::runtime::Handle::current().block_on(async {
                let rows: Vec<(serde_json::Value,)> = match network {
                    Some(n) => {
                        sqlx::query_as(
                            "SELECT metadata FROM wallets WHERE network = $1 ORDER BY id",
                        )
                        .bind(n.to_string())
                        .fetch_all(&pool)
                        .await
                    }
                    None => {
                        sqlx::query_as("SELECT metadata FROM wallets ORDER BY id")
                            .fetch_all(&pool)
                            .await
                    }
                }
                .map_err(|e| PayError::InternalError(format!("postgres list wallets: {e}")))?;

                rows.into_iter()
                    .map(|(meta_json,)| {
                        serde_json::from_value(meta_json).map_err(|e| {
                            PayError::InternalError(format!("postgres parse wallet metadata: {e}"))
                        })
                    })
                    .collect()
            })
        })
    }

    fn delete_wallet_metadata(&self, wallet_id: &str) -> Result<(), PayError> {
        let pool = self.pool.clone();
        let wallet_id = wallet_id.to_string();

        tokio::task::block_in_place(|| {
            tokio::runtime::Handle::current().block_on(async {
                let result = sqlx::query("DELETE FROM wallets WHERE id = $1")
                    .bind(&wallet_id)
                    .execute(&pool)
                    .await
                    .map_err(|e| PayError::InternalError(format!("postgres delete wallet: {e}")))?;

                if result.rows_affected() == 0 {
                    return Err(PayError::WalletNotFound(format!(
                        "wallet {wallet_id} not found"
                    )));
                }
                Ok(())
            })
        })
    }

    fn wallet_directory_path(&self, wallet_id: &str) -> Result<PathBuf, PayError> {
        wallet::wallet_directory_path(&self.data_dir, wallet_id)
    }

    fn wallet_data_directory_path(&self, wallet_id: &str) -> Result<PathBuf, PayError> {
        wallet::wallet_data_directory_path(&self.data_dir, wallet_id)
    }

    fn wallet_data_directory_path_for_meta(&self, meta: &WalletMetadata) -> PathBuf {
        wallet::wallet_data_directory_path_for_wallet_metadata(&self.data_dir, meta)
    }

    fn resolve_wallet_id(&self, id_or_label: &str) -> Result<String, PayError> {
        if id_or_label.starts_with("w_") {
            return Ok(id_or_label.to_string());
        }
        let all = self.list_wallet_metadata(None)?;
        let mut matches: Vec<&WalletMetadata> = all
            .iter()
            .filter(|w| w.label.as_deref() == Some(id_or_label))
            .collect();
        match matches.len() {
            0 => Err(PayError::WalletNotFound(format!(
                "no wallet found with ID or label '{id_or_label}'"
            ))),
            1 => Ok(matches.remove(0).id.clone()),
            n => Err(PayError::InvalidAmount(format!(
                "label '{id_or_label}' matches {n} wallets — use wallet ID instead"
            ))),
        }
    }

    fn append_transaction_record(&self, record: &HistoryRecord) -> Result<(), PayError> {
        let pool = self.pool.clone();
        let record_json = serde_json::to_value(record)
            .map_err(|e| PayError::InternalError(format!("serialize transaction record: {e}")))?;
        let tx_id = record.transaction_id.clone();
        let wallet = record.wallet.clone();

        tokio::task::block_in_place(|| {
            tokio::runtime::Handle::current().block_on(async {
                sqlx::query(
                    "INSERT INTO transactions (transaction_id, wallet, record) \
                     VALUES ($1, $2, $3) \
                     ON CONFLICT (transaction_id) DO NOTHING",
                )
                .bind(&tx_id)
                .bind(&wallet)
                .bind(&record_json)
                .execute(&pool)
                .await
                .map_err(|e| {
                    PayError::InternalError(format!("postgres append transaction: {e}"))
                })?;
                Ok(())
            })
        })
    }

    fn load_wallet_transaction_records(
        &self,
        wallet_id: &str,
    ) -> Result<Vec<HistoryRecord>, PayError> {
        let pool = self.pool.clone();
        let wallet_id = wallet_id.to_string();

        tokio::task::block_in_place(|| {
            tokio::runtime::Handle::current().block_on(async {
                let rows: Vec<(serde_json::Value,)> = sqlx::query_as(
                    "SELECT record FROM transactions WHERE wallet = $1 ORDER BY sequence",
                )
                .bind(&wallet_id)
                .fetch_all(&pool)
                .await
                .map_err(|e| PayError::InternalError(format!("postgres load transactions: {e}")))?;

                rows.into_iter()
                    .map(|(record_json,)| {
                        serde_json::from_value(record_json).map_err(|e| {
                            PayError::InternalError(format!(
                                "postgres parse transaction record: {e}"
                            ))
                        })
                    })
                    .collect()
            })
        })
    }

    fn find_transaction_record_by_id(
        &self,
        tx_id: &str,
    ) -> Result<Option<HistoryRecord>, PayError> {
        let pool = self.pool.clone();
        let tx_id = tx_id.to_string();

        tokio::task::block_in_place(|| {
            tokio::runtime::Handle::current().block_on(async {
                let row: Option<(serde_json::Value,)> =
                    sqlx::query_as("SELECT record FROM transactions WHERE transaction_id = $1")
                        .bind(&tx_id)
                        .fetch_optional(&pool)
                        .await
                        .map_err(|e| {
                            PayError::InternalError(format!("postgres find transaction: {e}"))
                        })?;

                match row {
                    Some((record_json,)) => {
                        let record: HistoryRecord =
                            serde_json::from_value(record_json).map_err(|e| {
                                PayError::InternalError(format!(
                                    "postgres parse transaction record: {e}"
                                ))
                            })?;
                        Ok(Some(record))
                    }
                    None => Ok(None),
                }
            })
        })
    }

    fn update_transaction_record_memo(
        &self,
        tx_id: &str,
        memo: Option<&BTreeMap<String, String>>,
    ) -> Result<(), PayError> {
        let pool = self.pool.clone();
        let tx_id = tx_id.to_string();
        let memo_json = serde_json::to_value(memo)
            .map_err(|e| PayError::InternalError(format!("serialize memo: {e}")))?;

        tokio::task::block_in_place(|| {
            tokio::runtime::Handle::current().block_on(async {
                // Read existing record, update memo, write back
                let row: Option<(serde_json::Value,)> =
                    sqlx::query_as("SELECT record FROM transactions WHERE transaction_id = $1")
                        .bind(&tx_id)
                        .fetch_optional(&pool)
                        .await
                        .map_err(|e| {
                            PayError::InternalError(format!("postgres read transaction: {e}"))
                        })?;

                let Some((record_json,)) = row else {
                    return Err(PayError::WalletNotFound(format!(
                        "transaction {tx_id} not found"
                    )));
                };

                let mut record: HistoryRecord = serde_json::from_value(record_json)
                    .map_err(|e| PayError::InternalError(format!("postgres parse record: {e}")))?;
                record.local_memo = serde_json::from_value(memo_json)
                    .map_err(|e| PayError::InternalError(format!("postgres parse memo: {e}")))?;
                let updated_json = serde_json::to_value(&record).map_err(|e| {
                    PayError::InternalError(format!("serialize updated record: {e}"))
                })?;

                sqlx::query("UPDATE transactions SET record = $1 WHERE transaction_id = $2")
                    .bind(&updated_json)
                    .bind(&tx_id)
                    .execute(&pool)
                    .await
                    .map_err(|e| {
                        PayError::InternalError(format!("postgres update transaction memo: {e}"))
                    })?;
                Ok(())
            })
        })
    }

    fn update_transaction_record_fee(
        &self,
        tx_id: &str,
        fee_value: u64,
        fee_unit: &str,
    ) -> Result<(), PayError> {
        let pool = self.pool.clone();
        let tx_id = tx_id.to_string();
        let fee_unit = fee_unit.to_string();

        tokio::task::block_in_place(|| {
            tokio::runtime::Handle::current().block_on(async {
                let row: Option<(serde_json::Value,)> =
                    sqlx::query_as("SELECT record FROM transactions WHERE transaction_id = $1")
                        .bind(&tx_id)
                        .fetch_optional(&pool)
                        .await
                        .map_err(|e| {
                            PayError::InternalError(format!("postgres read transaction: {e}"))
                        })?;

                let Some((record_json,)) = row else {
                    return Err(PayError::WalletNotFound(format!(
                        "transaction {tx_id} not found"
                    )));
                };

                let mut record: HistoryRecord = serde_json::from_value(record_json)
                    .map_err(|e| PayError::InternalError(format!("postgres parse record: {e}")))?;
                record.fee = Some(crate::types::Amount {
                    value: fee_value,
                    token: fee_unit,
                });
                let updated_json = serde_json::to_value(&record).map_err(|e| {
                    PayError::InternalError(format!("serialize updated record: {e}"))
                })?;

                sqlx::query("UPDATE transactions SET record = $1 WHERE transaction_id = $2")
                    .bind(&updated_json)
                    .bind(&tx_id)
                    .execute(&pool)
                    .await
                    .map_err(|e| {
                        PayError::InternalError(format!("postgres update transaction fee: {e}"))
                    })?;
                Ok(())
            })
        })
    }

    fn update_transaction_record_status(
        &self,
        tx_id: &str,
        status: crate::types::TxStatus,
        confirmed_at_epoch_s: Option<u64>,
    ) -> Result<(), PayError> {
        let pool = self.pool.clone();
        let tx_id = tx_id.to_string();

        tokio::task::block_in_place(|| {
            tokio::runtime::Handle::current().block_on(async {
                let row: Option<(serde_json::Value,)> =
                    sqlx::query_as("SELECT record FROM transactions WHERE transaction_id = $1")
                        .bind(&tx_id)
                        .fetch_optional(&pool)
                        .await
                        .map_err(|e| {
                            PayError::InternalError(format!("postgres read transaction: {e}"))
                        })?;

                let Some((record_json,)) = row else {
                    return Err(PayError::WalletNotFound(format!(
                        "transaction {tx_id} not found"
                    )));
                };

                let mut record: crate::types::HistoryRecord = serde_json::from_value(record_json)
                    .map_err(|e| {
                    PayError::InternalError(format!("postgres parse record: {e}"))
                })?;
                record.status = status;
                record.confirmed_at_epoch_s = confirmed_at_epoch_s;
                let updated_json = serde_json::to_value(&record).map_err(|e| {
                    PayError::InternalError(format!("serialize updated record: {e}"))
                })?;

                sqlx::query("UPDATE transactions SET record = $1 WHERE transaction_id = $2")
                    .bind(&updated_json)
                    .bind(&tx_id)
                    .execute(&pool)
                    .await
                    .map_err(|e| {
                        PayError::InternalError(format!("postgres update transaction status: {e}"))
                    })?;
                Ok(())
            })
        })
    }

    fn drain_migration_log(&self) -> Vec<MigrationLog> {
        Vec::new()
    }
}