diff --git a/lib/services/ethereum/ethereum_api.dart b/lib/services/ethereum/ethereum_api.dart index cfc5c456c..a0f134614 100644 --- a/lib/services/ethereum/ethereum_api.dart +++ b/lib/services/ethereum/ethereum_api.dart @@ -465,89 +465,42 @@ abstract class EthereumAPI { } } - // static Future> getTokenAbi22( - // String contractAddress) async { - // try { - // final response = await get( - // Uri.parse( - // "https://api.etherscan.io/api?module=contract&action=getabi&address=$contractAddress&apikey=EG6J7RJIQVSTP2BS59D3TY2G55YHS5F2HP", - // ), - // ); - // - // if (response.statusCode == 200) { - // final json = jsonDecode(response.body) as Map; - // print("========================== 222222222222222 ================"); - // dev.log((jsonDecode(json["result"] as String)).toString()); - // print( - // "============================ 2222222222222222222 =============="); - // - // return EthereumResponse( - // json["result"] as String, - // null, - // ); - // } else { - // throw EthApiException( - // "getTokenAbi($contractAddress) failed with status code: " - // "${response.statusCode}", - // ); - // } - // } on EthApiException catch (e) { - // return EthereumResponse( - // null, - // e, - // ); - // } catch (e, s) { - // Logging.instance.log( - // "getTokenAbi(): $e\n$s", - // level: LogLevel.Error, - // ); - // return EthereumResponse( - // null, - // EthApiException(e.toString()), - // ); - // } - // } + /// Fetch the underlying contract address that a proxy contract points to + static Future> getProxyTokenImplementationAddress( + String contractAddress, + ) async { + try { + final response = await get(Uri.parse( + "$stackBaseServer/state?addrs=$contractAddress&parts=proxy")); + if (response.statusCode == 200) { + final json = jsonDecode(response.body); + final list = json["data"] as List; + final map = Map.from(list.first as Map); - // /// Fetch the underlying contract address that a proxy contract points to - // static Future> getProxyTokenImplementation( - // String contractAddress) async { - // try { - // final response = await get(Uri.parse( - // "$stackURI?module=contract&action=getsourcecode&address=$contractAddress")); - // // "$etherscanApi?module=contract&action=getsourcecode&address=$contractAddress&apikey=EG6J7RJIQVSTP2BS59D3TY2G55YHS5F2HP")); - // if (response.statusCode == 200) { - // final json = jsonDecode(response.body); - // if (json["message"] == "OK") { - // final list = json["result"] as List; - // final map = Map.from(list.first as Map); - // - // return EthereumResponse( - // map["Implementation"] as String, - // null, - // ); - // } else { - // throw EthApiException(json["message"] as String); - // } - // } else { - // throw EthApiException( - // "fetchProxyTokenImplementation($contractAddress) failed with status code: " - // "${response.statusCode}", - // ); - // } - // } on EthApiException catch (e) { - // return EthereumResponse( - // null, - // e, - // ); - // } catch (e, s) { - // Logging.instance.log( - // "fetchProxyTokenImplementation(): $e\n$s", - // level: LogLevel.Error, - // ); - // return EthereumResponse( - // null, - // EthApiException(e.toString()), - // ); - // } - // } + return EthereumResponse( + map["proxy"] as String, + null, + ); + } else { + throw EthApiException( + "getProxyTokenImplementationAddress($contractAddress) failed with" + " status code: ${response.statusCode}", + ); + } + } on EthApiException catch (e) { + return EthereumResponse( + null, + e, + ); + } catch (e, s) { + Logging.instance.log( + "getProxyTokenImplementationAddress($contractAddress) : $e\n$s", + level: LogLevel.Error, + ); + return EthereumResponse( + null, + EthApiException(e.toString()), + ); + } + } } diff --git a/lib/services/ethereum/ethereum_token_service.dart b/lib/services/ethereum/ethereum_token_service.dart index 6081d3d55..c57e668b2 100644 --- a/lib/services/ethereum/ethereum_token_service.dart +++ b/lib/services/ethereum/ethereum_token_service.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'dart:convert'; import 'package:decimal/decimal.dart'; import 'package:ethereum_addresses/ethereum_addresses.dart'; @@ -163,65 +162,66 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache { _sendFunction = _deployedContract.function('transfer'); } catch (_) { //==================================================================== - final list = List>.from( - jsonDecode(tokenContract.abi!) as List); - final functionNames = list.map((e) => e["name"] as String); - - if (!functionNames.contains("balanceOf")) { - list.add( - { - "encoding": "0x70a08231", - "inputs": [ - {"name": "account", "type": "address"} - ], - "name": "balanceOf", - "outputs": [ - {"name": "val_0", "type": "uint256"} - ], - "signature": "balanceOf(address)", - "type": "function" - }, - ); - } - - if (!functionNames.contains("transfer")) { - list.add( - { - "encoding": "0xa9059cbb", - "inputs": [ - {"name": "dst", "type": "address"}, - {"name": "rawAmount", "type": "uint256"} - ], - "name": "transfer", - "outputs": [ - {"name": "val_0", "type": "bool"} - ], - "signature": "transfer(address,uint256)", - "type": "function" - }, - ); - } + // final list = List>.from( + // jsonDecode(tokenContract.abi!) as List); + // final functionNames = list.map((e) => e["name"] as String); + // + // if (!functionNames.contains("balanceOf")) { + // list.add( + // { + // "encoding": "0x70a08231", + // "inputs": [ + // {"name": "account", "type": "address"} + // ], + // "name": "balanceOf", + // "outputs": [ + // {"name": "val_0", "type": "uint256"} + // ], + // "signature": "balanceOf(address)", + // "type": "function" + // }, + // ); + // } + // + // if (!functionNames.contains("transfer")) { + // list.add( + // { + // "encoding": "0xa9059cbb", + // "inputs": [ + // {"name": "dst", "type": "address"}, + // {"name": "rawAmount", "type": "uint256"} + // ], + // "name": "transfer", + // "outputs": [ + // {"name": "val_0", "type": "bool"} + // ], + // "signature": "transfer(address,uint256)", + // "type": "function" + // }, + // ); + // } //-------------------------------------------------------------------- //==================================================================== // function not found so likely a proxy so we need to fetch the impl //==================================================================== - final updatedToken = tokenContract.copyWith(abi: jsonEncode(list)); - // Store updated contract - final id = await MainDB.instance.putEthContract(updatedToken); - _tokenContract = updatedToken..id = id; + // final updatedToken = tokenContract.copyWith(abi: jsonEncode(list)); + // // Store updated contract + // final id = await MainDB.instance.putEthContract(updatedToken); + // _tokenContract = updatedToken..id = id; //-------------------------------------------------------------------- - // final contractAddressResponse = - // await EthereumAPI.getProxyTokenImplementation(contractAddress.hex); - // - // if (contractAddressResponse.value != null) { - // _tokenContract = await _updateTokenABI( - // forContract: tokenContract, - // usingContractAddress: contractAddressResponse.value!, - // ); - // } else { - // throw contractAddressResponse.exception!; - // } + final contractAddressResponse = + await EthereumAPI.getProxyTokenImplementationAddress( + contractAddress.hex); + + if (contractAddressResponse.value != null) { + _tokenContract = await _updateTokenABI( + forContract: tokenContract, + usingContractAddress: contractAddressResponse.value!, + ); + } else { + throw contractAddressResponse.exception!; + } //==================================================================== }