diff --git a/lib/services/coins/ethereum/ethereum_wallet.dart b/lib/services/coins/ethereum/ethereum_wallet.dart index ff37014d5..9adf47246 100644 --- a/lib/services/coins/ethereum/ethereum_wallet.dart +++ b/lib/services/coins/ethereum/ethereum_wallet.dart @@ -741,45 +741,7 @@ class EthereumWallet extends CoinServiceAPI { @override Future updateSentCachedTxData(Map txData) async { - final priceData = - await _priceAPI.getPricesAnd24hChange(baseCurrency: _prefs.currency); - Decimal currentPrice = priceData[coin]?.item1 ?? Decimal.zero; - final locale = await Devicelocale.currentLocale; - final String worthNow = Format.localizedStringAsFixed( - value: - ((currentPrice * Decimal.fromInt(txData["recipientAmt"] as int)) / - Decimal.fromInt(Constants.satsPerCoin(coin))) - .toDecimal(scaleOnInfinitePrecision: 2), - decimalPlaces: 2, - locale: locale!); - - final tx = models.Transaction( - txid: txData["txid"] as String, - confirmedStatus: false, - timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000, - txType: "Sent", - amount: txData["recipientAmt"] as int, - worthNow: worthNow, - worthAtBlockTimestamp: worthNow, - fees: txData["fee"] as int, - inputSize: 0, - outputSize: 0, - inputs: [], - outputs: [], - address: txData["address"] as String, - height: -1, - confirmations: 0, - ); - - if (cachedTxData == null) { - final data = await _fetchTransactionData(); - _transactionData = Future(() => data); - } else { - final transactions = cachedTxData!.getAllTransactions(); - transactions[tx.txid] = tx; - cachedTxData = models.TransactionData.fromMap(transactions); - _transactionData = Future(() => cachedTxData!); - } + //Only used for Electrumx coins } @override diff --git a/lib/services/tokens/ethereum/ethereum_token.dart b/lib/services/tokens/ethereum/ethereum_token.dart index c3ee0f3cc..c19ac7bbb 100644 --- a/lib/services/tokens/ethereum/ethereum_token.dart +++ b/lib/services/tokens/ethereum/ethereum_token.dart @@ -1,6 +1,5 @@ import 'dart:convert'; import 'dart:math'; -import 'package:devicelocale/devicelocale.dart'; import 'package:http/http.dart'; import 'package:decimal/decimal.dart'; import 'package:stackwallet/utilities/eth_commons.dart'; @@ -42,6 +41,26 @@ class AbiRequestResponse { } } +class TokenData { + final String message; + final Map result; + final String status; + + const TokenData({ + required this.message, + required this.result, + required this.status, + }); + + factory TokenData.fromJson(Map json) { + return TokenData( + message: json['message'] as String, + result: json['result'] as Map, + status: json['status'] as String, + ); + } +} + const int MINIMUM_CONFIRMATIONS = 3; class EthereumToken extends TokenServiceAPI { @@ -81,7 +100,7 @@ class EthereumToken extends TokenServiceAPI { return AbiRequestResponse.fromJson( json.decode(response.body) as Map); } else { - throw Exception('Failed to load token abi'); + throw Exception("ERROR GETTING TOKENABI ${response.reasonPhrase}"); } } @@ -185,6 +204,8 @@ class EthereumToken extends TokenServiceAPI { _balanceFunction = _contract.function('balanceOf'); _sendFunction = _contract.function('transfer'); _client = await getEthClient(); + + // print(_credentials.p) } @override @@ -294,47 +315,6 @@ class EthereumToken extends TokenServiceAPI { _transactionData ??= _fetchTransactionData(); Future? _transactionData; - @override - Future updateSentCachedTxData(Map txData) async { - Decimal currentPrice = Decimal.zero; - final locale = await Devicelocale.currentLocale; - final String worthNow = Format.localizedStringAsFixed( - value: - ((currentPrice * Decimal.fromInt(txData["recipientAmt"] as int)) / - Decimal.fromInt(Constants.satsPerCoin(coin))) - .toDecimal(scaleOnInfinitePrecision: 2), - decimalPlaces: 2, - locale: locale!); - - final tx = models.Transaction( - txid: txData["txid"] as String, - confirmedStatus: false, - timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000, - txType: "Sent", - amount: txData["recipientAmt"] as int, - worthNow: worthNow, - worthAtBlockTimestamp: worthNow, - fees: txData["fee"] as int, - inputSize: 0, - outputSize: 0, - inputs: [], - outputs: [], - address: txData["address"] as String, - height: -1, - confirmations: 0, - ); - - if (cachedTxData == null) { - final data = await _fetchTransactionData(); - _transactionData = Future(() => data); - } else { - final transactions = cachedTxData!.getAllTransactions(); - transactions[tx.txid] = tx; - cachedTxData = models.TransactionData.fromMap(transactions); - _transactionData = Future(() => cachedTxData!); - } - } - Future _fetchTransactionData() async { String thisAddress = await currentReceivingAddress; // final cachedTransactions = {} as TransactionData?; @@ -440,9 +420,6 @@ class EthereumToken extends TokenServiceAPI { } } - // final transactionsMap = {} as Map; - // transactionsMap - // .addAll(TransactionData.fromJson(result).getAllTransactions()); final txModel = TransactionData.fromMap( TransactionData.fromJson(result).getAllTransactions()); @@ -455,6 +432,34 @@ class EthereumToken extends TokenServiceAPI { return isValidEthereumAddress(address); } + //Validate that a custom token is valid and is ERC-20, a token will be valid + @override + Future getTokenByContractAddress(String contractAddress) async { + final response = await get(Uri.parse( + "$blockExplorer?module=token&action=getToken&contractaddress=$contractAddress")); + if (response.statusCode == 200) { + return TokenData.fromJson( + json.decode(response.body) as Map); + } else { + throw Exception("ERROR GETTING TOKEN ${response.reasonPhrase}"); + } + } + + //Validate that a custom token is valid and is ERC-20 + @override + Future isValidToken(String contractAddress) async { + TokenData tokenData = await getTokenByContractAddress(contractAddress); + + if (tokenData.message == "OK") { + final result = tokenData.result; + if (result["type"] == "ERC-20") { + return true; + } + return false; + } + return false; + } + Future getCurrentNode() async { return NodeService(secureStorageInterface: _secureStore) .getPrimaryNodeFor(coin: coin) ?? diff --git a/lib/services/tokens/token_service.dart b/lib/services/tokens/token_service.dart index b2cf4c2aa..f243a769c 100644 --- a/lib/services/tokens/token_service.dart +++ b/lib/services/tokens/token_service.dart @@ -41,7 +41,6 @@ abstract class TokenServiceAPI { Future get maxFee; Future get currentReceivingAddress; - // Future get currentLegacyReceivingAddress; Future get availableBalance; Future get totalBalance; @@ -52,9 +51,6 @@ abstract class TokenServiceAPI { Future refresh(); - // String get walletName; - // String get walletId; - bool validateAddress(String address); Future initializeNew(); @@ -62,6 +58,5 @@ abstract class TokenServiceAPI { Future estimateFeeFor(int satoshiAmount, int feeRate); - // used for electrumx coins - Future updateSentCachedTxData(Map txData); + Future isValidToken(String contractAddress); } diff --git a/lib/utilities/eth_commons.dart b/lib/utilities/eth_commons.dart index 627c8f96b..d31fbbccb 100644 --- a/lib/utilities/eth_commons.dart +++ b/lib/utilities/eth_commons.dart @@ -58,14 +58,18 @@ const _gasTrackerUrl = Future fetchAddressTransactions( String address, String action) async { - final response = await get(Uri.parse( - "$blockExplorer?module=account&action=$action&address=$address")); - - if (response.statusCode == 200) { - return AddressTransaction.fromJson( - json.decode(response.body) as Map); - } else { - throw Exception('Failed to load transactions'); + try { + final response = await get(Uri.parse( + "$blockExplorer?module=account&action=$action&address=$address")); + if (response.statusCode == 200) { + return AddressTransaction.fromJson( + json.decode(response.body) as Map); + } else { + throw Exception( + 'ERROR GETTING TRANSACTIONS WITH STATUS ${response.statusCode}'); + } + } catch (e, s) { + throw Exception('ERROR GETTING TRANSACTIONS ${e.toString()}'); } }