|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:flutter/foundation.dart'; |
| 4 | +import 'package:http/http.dart' as http; |
| 5 | + |
| 6 | +import 'package:fula_files/core/models/billing/supported_chain.dart'; |
| 7 | +import 'package:fula_files/core/services/nft_contract_service.dart'; |
| 8 | + |
| 9 | +/// Relay client for gasless meta-tx claims on Base. |
| 10 | +/// |
| 11 | +/// The creator deposits ETH when creating a claim offer. The relay submits |
| 12 | +/// the meta-tx on behalf of the claimer and is reimbursed from the deposit. |
| 13 | +class MetaTxRelayService { |
| 14 | + MetaTxRelayService._(); |
| 15 | + static final MetaTxRelayService instance = MetaTxRelayService._(); |
| 16 | + |
| 17 | + static const String _relayUrl = 'https://cloud.fx.land/api/v1/nft/relay'; |
| 18 | + static const Duration _timeout = Duration(seconds: 30); |
| 19 | + |
| 20 | + /// Check if a claim link has a gas deposit (indicating gasless is available). |
| 21 | + Future<BigInt> getGasDeposit({ |
| 22 | + required int chainId, |
| 23 | + required String linkHash, |
| 24 | + }) async { |
| 25 | + final contract = NftContractService.instance; |
| 26 | + // claimGasDeposits(bytes32) — public mapping getter |
| 27 | + // selector: keccak256("claimGasDeposits(bytes32)")[:4] |
| 28 | + const selector = '6e3e2fa3'; |
| 29 | + final hashHex = linkHash.replaceFirst('0x', '').padLeft(64, '0'); |
| 30 | + final data = '0x$selector$hashHex'; |
| 31 | + |
| 32 | + final chain = SupportedChain.byChainId(chainId); |
| 33 | + if (chain?.nftContractAddress == null) return BigInt.zero; |
| 34 | + |
| 35 | + try { |
| 36 | + final result = await contract.ethCall( |
| 37 | + chainId: chainId, |
| 38 | + contractAddress: chain!.nftContractAddress!, |
| 39 | + data: data, |
| 40 | + ); |
| 41 | + final hex = result.replaceFirst('0x', ''); |
| 42 | + if (hex.isEmpty || hex.length < 64) return BigInt.zero; |
| 43 | + return BigInt.parse(hex.substring(0, 64), radix: 16); |
| 44 | + } catch (e) { |
| 45 | + debugPrint('MetaTxRelayService: getGasDeposit error: $e'); |
| 46 | + return BigInt.zero; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /// Get the meta-tx nonce for a signer address from the contract. |
| 51 | + Future<int> getMetaNonce({ |
| 52 | + required int chainId, |
| 53 | + required String address, |
| 54 | + }) async { |
| 55 | + final contract = NftContractService.instance; |
| 56 | + // metaNonces(address) — public mapping getter |
| 57 | + const selector = 'a3c573eb'; |
| 58 | + final addrHex = address.toLowerCase().replaceFirst('0x', '').padLeft(64, '0'); |
| 59 | + final data = '0x$selector$addrHex'; |
| 60 | + |
| 61 | + final chain = SupportedChain.byChainId(chainId); |
| 62 | + if (chain?.nftContractAddress == null) return 0; |
| 63 | + |
| 64 | + try { |
| 65 | + final result = await contract.ethCall( |
| 66 | + chainId: chainId, |
| 67 | + contractAddress: chain!.nftContractAddress!, |
| 68 | + data: data, |
| 69 | + ); |
| 70 | + return contract.decodeUint256(result) ?? 0; |
| 71 | + } catch (e) { |
| 72 | + debugPrint('MetaTxRelayService: getMetaNonce error: $e'); |
| 73 | + return 0; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /// Submit a meta-transaction via the relay. |
| 78 | + /// For claim actions: pass [secret] (the raw preimage). The relay passes it to claimNFTMeta. |
| 79 | + /// For burn/transferBack: pass [claimKey] (the public identifier). |
| 80 | + /// Returns the transaction hash. |
| 81 | + Future<String> relay({ |
| 82 | + required String action, |
| 83 | + required int chainId, |
| 84 | + String? secret, |
| 85 | + String? claimKey, |
| 86 | + required String signer, |
| 87 | + required int deadline, |
| 88 | + required int nonce, |
| 89 | + required String signature, |
| 90 | + int? tokenId, |
| 91 | + int? amount, |
| 92 | + }) async { |
| 93 | + final body = <String, dynamic>{ |
| 94 | + 'action': action, |
| 95 | + 'chainId': chainId, |
| 96 | + 'signer': signer, |
| 97 | + 'deadline': deadline, |
| 98 | + 'nonce': nonce, |
| 99 | + 'signature': signature, |
| 100 | + }; |
| 101 | + // For claim: send secret (relay needs it for claimNFTMeta) and claimKey (for gas deposit lookup) |
| 102 | + if (secret != null) body['secret'] = secret; |
| 103 | + if (claimKey != null) body['claimKey'] = claimKey; |
| 104 | + if (tokenId != null) body['tokenId'] = tokenId; |
| 105 | + if (amount != null) body['amount'] = amount; |
| 106 | + |
| 107 | + final response = await http.post( |
| 108 | + Uri.parse(_relayUrl), |
| 109 | + headers: {'Content-Type': 'application/json'}, |
| 110 | + body: jsonEncode(body), |
| 111 | + ).timeout(_timeout); |
| 112 | + |
| 113 | + final result = jsonDecode(response.body) as Map<String, dynamic>; |
| 114 | + |
| 115 | + if (response.statusCode != 200 || result['success'] != true) { |
| 116 | + final error = result['error'] ?? 'Relay failed (${response.statusCode})'; |
| 117 | + throw Exception(error); |
| 118 | + } |
| 119 | + |
| 120 | + return result['txHash'] as String; |
| 121 | + } |
| 122 | +} |
0 commit comments