| 1 | import 'dart:convert'; |
| 2 | |
| 3 | import 'package:cw_core/utils/proxy_wrapper.dart'; |
| 4 | |
| 5 | class LayerZeroScanService { |
| 6 | static const String _baseUrl = 'https://scan.layerzero-api.com/v1'; |
| 7 | |
| 8 | static Future<LayerZeroMessageStatus?> getMessageStatus( |
| 9 | String sourceTxHash, |
| 10 | ) async { |
| 11 | try { |
| 12 | final uri = Uri.parse('$_baseUrl/messages/tx/$sourceTxHash'); |
| 13 | |
| 14 | final response = await ProxyWrapper().get(clearnetUri: uri); |
| 15 | |
| 16 | if (response.statusCode != 200) return null; |
| 17 | |
| 18 | final decoded = json.decode(response.body); |
| 19 | |
| 20 | Map<String, dynamic>? data; |
| 21 | if (decoded is List && decoded.isNotEmpty) { |
| 22 | data = decoded.first as Map<String, dynamic>?; |
| 23 | } else if (decoded is Map<String, dynamic>) { |
| 24 | if (decoded.containsKey('data') && decoded['data'] is List) { |
| 25 | final dataList = decoded['data'] as List; |
| 26 | if (dataList.isNotEmpty) { |
| 27 | data = dataList.first as Map<String, dynamic>?; |
| 28 | } |
| 29 | } else { |
| 30 | data = decoded; |
| 31 | } |
| 32 | } |
| 33 | if (data == null) return null; |
| 34 | return LayerZeroMessageStatus.fromJson(data); |
| 35 | } catch (_) { |
| 36 | return null; |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | class LayerZeroMessageStatus { |
| 42 | final String? guid; |
| 43 | final LayerZeroStatus? status; |
| 44 | final LayerZeroSource? source; |
| 45 | final LayerZeroDestination? destination; |
| 46 | final LayerZeroVerification? verification; |
| 47 | |
| 48 | LayerZeroMessageStatus({ |
| 49 | this.guid, |
| 50 | this.status, |
| 51 | this.source, |
| 52 | this.destination, |
| 53 | this.verification, |
| 54 | }); |
| 55 | |
| 56 | factory LayerZeroMessageStatus.fromJson(Map<String, dynamic> json) { |
| 57 | return LayerZeroMessageStatus( |
| 58 | guid: json['guid'] as String?, |
| 59 | status: json['status'] != null |
| 60 | ? LayerZeroStatus.fromJson( |
| 61 | json['status'] as Map<String, dynamic>, |
| 62 | ) |
| 63 | : null, |
| 64 | source: json['source'] != null |
| 65 | ? LayerZeroSource.fromJson( |
| 66 | json['source'] as Map<String, dynamic>, |
| 67 | ) |
| 68 | : null, |
| 69 | destination: json['destination'] != null |
| 70 | ? LayerZeroDestination.fromJson( |
| 71 | json['destination'] as Map<String, dynamic>, |
| 72 | ) |
| 73 | : null, |
| 74 | verification: json['verification'] != null |
| 75 | ? LayerZeroVerification.fromJson( |
| 76 | json['verification'] as Map<String, dynamic>, |
| 77 | ) |
| 78 | : null, |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | bool get isDelivered => |
| 83 | status?.name == 'DELIVERED' || |
| 84 | destination?.status == 'DELIVERED' || |
| 85 | destination?.status == 'SUCCEEDED'; |
| 86 | |
| 87 | bool get isFailed => status?.name == 'FAILED' || destination?.status == 'FAILED'; |
| 88 | |
| 89 | bool get isInflight => |
| 90 | status?.name == 'INFLIGHT' || |
| 91 | status?.name == 'CONFIRMING' || |
| 92 | destination?.status == 'WAITING' || |
| 93 | destination?.status == 'VALIDATING_TX' || |
| 94 | (destination?.status != 'SUCCEEDED' && |
| 95 | destination?.status != 'DELIVERED' && |
| 96 | destination?.status != 'FAILED' && |
| 97 | destination?.tx == null); |
| 98 | } |
| 99 | |
| 100 | class LayerZeroStatus { |
| 101 | final String? name; |
| 102 | final String? message; |
| 103 | |
| 104 | LayerZeroStatus({this.name, this.message}); |
| 105 | |
| 106 | factory LayerZeroStatus.fromJson(Map<String, dynamic> json) { |
| 107 | return LayerZeroStatus( |
| 108 | name: json['name'] as String?, |
| 109 | message: json['message'] as String?, |
| 110 | ); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | class LayerZeroSource { |
| 115 | final String? status; |
| 116 | final LayerZeroTransaction? tx; |
| 117 | |
| 118 | LayerZeroSource({ |
| 119 | this.status, |
| 120 | this.tx, |
| 121 | }); |
| 122 | |
| 123 | factory LayerZeroSource.fromJson(Map<String, dynamic> json) { |
| 124 | return LayerZeroSource( |
| 125 | status: json['status'] as String?, |
| 126 | tx: json['tx'] != null |
| 127 | ? LayerZeroTransaction.fromJson( |
| 128 | json['tx'] as Map<String, dynamic>, |
| 129 | ) |
| 130 | : null, |
| 131 | ); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | class LayerZeroDestination { |
| 136 | final String? status; |
| 137 | final LayerZeroTransaction? tx; |
| 138 | final LayerZeroNativeDrop? nativeDrop; |
| 139 | final LayerZeroLzCompose? lzCompose; |
| 140 | |
| 141 | LayerZeroDestination({ |
| 142 | this.status, |
| 143 | this.tx, |
| 144 | this.nativeDrop, |
| 145 | this.lzCompose, |
| 146 | }); |
| 147 | |
| 148 | factory LayerZeroDestination.fromJson(Map<String, dynamic> json) { |
| 149 | return LayerZeroDestination( |
| 150 | status: json['status'] as String?, |
| 151 | tx: json['tx'] != null |
| 152 | ? LayerZeroTransaction.fromJson( |
| 153 | json['tx'] as Map<String, dynamic>, |
| 154 | ) |
| 155 | : null, |
| 156 | nativeDrop: json['nativeDrop'] != null |
| 157 | ? LayerZeroNativeDrop.fromJson( |
| 158 | json['nativeDrop'] as Map<String, dynamic>, |
| 159 | ) |
| 160 | : null, |
| 161 | lzCompose: json['lzCompose'] != null |
| 162 | ? LayerZeroLzCompose.fromJson( |
| 163 | json['lzCompose'] as Map<String, dynamic>, |
| 164 | ) |
| 165 | : null, |
| 166 | ); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | class LayerZeroNativeDrop { |
| 171 | final String? status; |
| 172 | |
| 173 | LayerZeroNativeDrop({this.status}); |
| 174 | |
| 175 | factory LayerZeroNativeDrop.fromJson(Map<String, dynamic> json) { |
| 176 | return LayerZeroNativeDrop( |
| 177 | status: json['status'] as String?, |
| 178 | ); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | class LayerZeroLzCompose { |
| 183 | final String? status; |
| 184 | |
| 185 | LayerZeroLzCompose({this.status}); |
| 186 | |
| 187 | factory LayerZeroLzCompose.fromJson(Map<String, dynamic> json) { |
| 188 | return LayerZeroLzCompose( |
| 189 | status: json['status'] as String?, |
| 190 | ); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | class LayerZeroTransaction { |
| 195 | final String? txHash; |
| 196 | final String? blockHash; |
| 197 | final String? blockNumber; |
| 198 | final int? blockTimestamp; |
| 199 | final String? from; |
| 200 | |
| 201 | LayerZeroTransaction({ |
| 202 | this.txHash, |
| 203 | this.blockHash, |
| 204 | this.blockNumber, |
| 205 | this.blockTimestamp, |
| 206 | this.from, |
| 207 | }); |
| 208 | |
| 209 | factory LayerZeroTransaction.fromJson(Map<String, dynamic> json) { |
| 210 | final blockNumberValue = json['blockNumber']; |
| 211 | final blockNumberStr = blockNumberValue != null |
| 212 | ? (blockNumberValue is int ? blockNumberValue.toString() : blockNumberValue as String?) |
| 213 | : null; |
| 214 | return LayerZeroTransaction( |
| 215 | txHash: json['txHash'] as String?, |
| 216 | blockHash: json['blockHash'] as String?, |
| 217 | blockNumber: blockNumberStr, |
| 218 | blockTimestamp: json['blockTimestamp'] as int?, |
| 219 | from: json['from'] as String?, |
| 220 | ); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | class LayerZeroVerification { |
| 225 | final LayerZeroDvn? dvn; |
| 226 | final LayerZeroSealer? sealer; |
| 227 | |
| 228 | LayerZeroVerification({ |
| 229 | this.dvn, |
| 230 | this.sealer, |
| 231 | }); |
| 232 | |
| 233 | factory LayerZeroVerification.fromJson(Map<String, dynamic> json) { |
| 234 | return LayerZeroVerification( |
| 235 | dvn: json['dvn'] != null |
| 236 | ? LayerZeroDvn.fromJson( |
| 237 | json['dvn'] as Map<String, dynamic>, |
| 238 | ) |
| 239 | : null, |
| 240 | sealer: json['sealer'] != null |
| 241 | ? LayerZeroSealer.fromJson( |
| 242 | json['sealer'] as Map<String, dynamic>, |
| 243 | ) |
| 244 | : null, |
| 245 | ); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | class LayerZeroDvn { |
| 250 | final Map<String, LayerZeroDvnStatus>? dvns; |
| 251 | final String? status; |
| 252 | |
| 253 | LayerZeroDvn({ |
| 254 | this.dvns, |
| 255 | this.status, |
| 256 | }); |
| 257 | |
| 258 | factory LayerZeroDvn.fromJson(Map<String, dynamic> json) { |
| 259 | final dvnsMap = json['dvns'] as Map<String, dynamic>?; |
| 260 | final parsedDvns = dvnsMap?.map( |
| 261 | (key, value) => MapEntry( |
| 262 | key, |
| 263 | LayerZeroDvnStatus.fromJson(value as Map<String, dynamic>), |
| 264 | ), |
| 265 | ); |
| 266 | return LayerZeroDvn( |
| 267 | dvns: parsedDvns, |
| 268 | status: json['status'] as String?, |
| 269 | ); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | class LayerZeroDvnStatus { |
| 274 | final String? status; |
| 275 | final String? txHash; |
| 276 | final String? blockHash; |
| 277 | final int? blockNumber; |
| 278 | final int? blockTimestamp; |
| 279 | |
| 280 | LayerZeroDvnStatus({ |
| 281 | this.status, |
| 282 | this.txHash, |
| 283 | this.blockHash, |
| 284 | this.blockNumber, |
| 285 | this.blockTimestamp, |
| 286 | }); |
| 287 | |
| 288 | factory LayerZeroDvnStatus.fromJson(Map<String, dynamic> json) { |
| 289 | return LayerZeroDvnStatus( |
| 290 | status: json['status'] as String?, |
| 291 | txHash: json['txHash'] as String?, |
| 292 | blockHash: json['blockHash'] as String?, |
| 293 | blockNumber: json['blockNumber'] as int?, |
| 294 | blockTimestamp: json['blockTimestamp'] as int?, |
| 295 | ); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | class LayerZeroSealer { |
| 300 | final String? status; |
| 301 | final LayerZeroTransaction? tx; |
| 302 | |
| 303 | LayerZeroSealer({ |
| 304 | this.status, |
| 305 | this.tx, |
| 306 | }); |
| 307 | |
| 308 | factory LayerZeroSealer.fromJson(Map<String, dynamic> json) { |
| 309 | return LayerZeroSealer( |
| 310 | status: json['status'] as String?, |
| 311 | tx: json['tx'] != null |
| 312 | ? LayerZeroTransaction.fromJson( |
| 313 | json['tx'] as Map<String, dynamic>, |
| 314 | ) |
| 315 | : null, |
| 316 | ); |
| 317 | } |
| 318 | } |