| 1 | import 'package:cw_core/db/sqlite.dart'; |
| 2 | |
| 3 | class BridgeTransfer { |
| 4 | BridgeTransfer({ |
| 5 | required this.id, |
| 6 | required this.walletId, |
| 7 | required this.sourceChainId, |
| 8 | required this.destinationChainId, |
| 9 | required this.tokenSymbol, |
| 10 | required this.tokenContract, |
| 11 | required this.amount, |
| 12 | required this.recipientAddress, |
| 13 | required this.sourceTxHash, |
| 14 | required this.status, |
| 15 | required this.createdAt, |
| 16 | this.updatedAt, |
| 17 | this.confirmedAt, |
| 18 | this.amountRaw, |
| 19 | this.errorMessage, |
| 20 | this.statusMessage, |
| 21 | }); |
| 22 | |
| 23 | static const tableName = 'BridgeTransfer'; |
| 24 | |
| 25 | static Future<List<BridgeTransfer>> selectAll() async { |
| 26 | final database = db; |
| 27 | if (database == null) return []; |
| 28 | |
| 29 | final rows = await database.query( |
| 30 | tableName, |
| 31 | orderBy: 'created_at DESC', |
| 32 | ); |
| 33 | return rows.map(fromRow).toList(); |
| 34 | } |
| 35 | |
| 36 | static Future<void> insert(BridgeTransfer transfer) async { |
| 37 | final database = db; |
| 38 | if (database == null) return; |
| 39 | |
| 40 | await database.insert(tableName, transfer.toRow()); |
| 41 | } |
| 42 | |
| 43 | static Future<void> update(BridgeTransfer transfer) async { |
| 44 | final database = db; |
| 45 | if (database == null) return; |
| 46 | |
| 47 | await database.update( |
| 48 | tableName, |
| 49 | transfer.toRow(), |
| 50 | where: 'id = ?', |
| 51 | whereArgs: [transfer.id], |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | String id; |
| 56 | String walletId; |
| 57 | int sourceChainId; |
| 58 | int destinationChainId; |
| 59 | String tokenSymbol; |
| 60 | String tokenContract; |
| 61 | String amount; |
| 62 | String recipientAddress; |
| 63 | String sourceTxHash; |
| 64 | String status; |
| 65 | DateTime createdAt; |
| 66 | DateTime? updatedAt; |
| 67 | DateTime? confirmedAt; |
| 68 | String? amountRaw; |
| 69 | String? errorMessage; |
| 70 | String? statusMessage; |
| 71 | |
| 72 | bool get isActive => status == 'submitted' || status == 'confirming' || status == 'initiated'; |
| 73 | |
| 74 | Map<String, Object?> toRow() { |
| 75 | return { |
| 76 | 'id': id, |
| 77 | 'wallet_id': walletId, |
| 78 | 'source_chain_id': sourceChainId, |
| 79 | 'destination_chain_id': destinationChainId, |
| 80 | 'token_symbol': tokenSymbol, |
| 81 | 'token_contract': tokenContract, |
| 82 | 'amount': amount, |
| 83 | 'recipient_address': recipientAddress, |
| 84 | 'source_tx_hash': sourceTxHash, |
| 85 | 'status': status, |
| 86 | 'created_at': createdAt.millisecondsSinceEpoch, |
| 87 | 'updated_at': updatedAt?.millisecondsSinceEpoch, |
| 88 | 'confirmed_at': confirmedAt?.millisecondsSinceEpoch, |
| 89 | 'amount_raw': amountRaw, |
| 90 | 'error_message': errorMessage, |
| 91 | 'status_message': statusMessage, |
| 92 | }; |
| 93 | } |
| 94 | |
| 95 | static int? _nullableInt(Object? v) { |
| 96 | if (v == null) return null; |
| 97 | if (v is int) return v; |
| 98 | if (v is num) return v.toInt(); |
| 99 | return int.tryParse(v.toString()); |
| 100 | } |
| 101 | |
| 102 | static int _parseInt(Object? v) => _nullableInt(v) ?? 0; |
| 103 | static DateTime _parseDateTime(Object? v) => DateTime.fromMillisecondsSinceEpoch(_parseInt(v)); |
| 104 | |
| 105 | static BridgeTransfer fromRow(Map<String, Object?> m) { |
| 106 | String? asStr(Object? v) => v as String?; |
| 107 | |
| 108 | return BridgeTransfer( |
| 109 | id: m['id'] as String, |
| 110 | walletId: m['wallet_id'] as String, |
| 111 | sourceChainId: _parseInt(m['source_chain_id']), |
| 112 | destinationChainId: _parseInt(m['destination_chain_id']), |
| 113 | tokenSymbol: m['token_symbol'] as String, |
| 114 | tokenContract: m['token_contract'] as String, |
| 115 | amount: m['amount'] as String, |
| 116 | recipientAddress: m['recipient_address'] as String, |
| 117 | sourceTxHash: m['source_tx_hash'] as String, |
| 118 | status: m['status'] as String, |
| 119 | createdAt: _parseDateTime(m['created_at']), |
| 120 | updatedAt: _parseDateTime(m['updated_at']), |
| 121 | confirmedAt: _parseDateTime(m['confirmed_at']), |
| 122 | amountRaw: asStr(m['amount_raw']), |
| 123 | errorMessage: asStr(m['error_message']), |
| 124 | statusMessage: asStr(m['status_message']), |
| 125 | ); |
| 126 | } |
| 127 | } |