From 09b2c68cd5becb766a2f08b5406336b4aeb1eb8c Mon Sep 17 00:00:00 2001
From: julian <julian@cypherstack.com>
Date: Thu, 23 Mar 2023 13:48:38 -0600
Subject: [PATCH] get token contract info updates and fixes

---
 .../add_token_view/add_custom_token_view.dart |  9 +------
 .../add_wallet_view/add_wallet_view.dart      |  1 -
 lib/route_generator.dart                      | 19 +++++---------
 lib/services/ethereum/ethereum_api.dart       | 26 +++++++++----------
 4 files changed, 21 insertions(+), 34 deletions(-)

diff --git a/lib/pages/add_wallet_views/add_token_view/add_custom_token_view.dart b/lib/pages/add_wallet_views/add_token_view/add_custom_token_view.dart
index 26b417279..01f7b6f49 100644
--- a/lib/pages/add_wallet_views/add_token_view/add_custom_token_view.dart
+++ b/lib/pages/add_wallet_views/add_token_view/add_custom_token_view.dart
@@ -17,11 +17,8 @@ import 'package:stackwallet/widgets/stack_dialog.dart';
 class AddCustomTokenView extends ConsumerStatefulWidget {
   const AddCustomTokenView({
     Key? key,
-    required this.walletId,
   }) : super(key: key);
 
-  final String? walletId;
-
   static const routeName = "/addCustomToken";
 
   @override
@@ -86,7 +83,7 @@ class _AddCustomTokenViewState extends ConsumerState<AddCustomTokenView> {
                 label: "Search",
                 onPressed: () async {
                   final response = await showLoading(
-                    whileFuture: EthereumAPI.getTokenByContractAddress(
+                    whileFuture: EthereumAPI.getTokenContractInfoByAddress(
                         contractController.text),
                     context: context,
                     message: "Looking up contract",
@@ -96,10 +93,6 @@ class _AddCustomTokenViewState extends ConsumerState<AddCustomTokenView> {
                     nameController.text = currentToken!.name;
                     symbolController.text = currentToken!.symbol;
                     decimalsController.text = currentToken!.decimals.toString();
-                    if (widget.walletId != null) {
-                      // TODO: this needs to be changed?
-                      currentToken!.walletIds.add(widget.walletId!);
-                    }
                   } else {
                     nameController.text = "";
                     symbolController.text = "";
diff --git a/lib/pages/add_wallet_views/add_wallet_view/add_wallet_view.dart b/lib/pages/add_wallet_views/add_wallet_view/add_wallet_view.dart
index 6e74f9e81..9e9d8a6fc 100644
--- a/lib/pages/add_wallet_views/add_wallet_view/add_wallet_view.dart
+++ b/lib/pages/add_wallet_views/add_wallet_view/add_wallet_view.dart
@@ -82,7 +82,6 @@ class _AddWalletViewState extends ConsumerState<AddWalletView> {
   Future<void> _addToken() async {
     final token = await Navigator.of(context).pushNamed(
       AddCustomTokenView.routeName,
-      arguments: null, // no walletId as no wallet has been selected/created yet
     );
     if (token is EthContract) {
       await MainDB.instance.putEthContract(token);
diff --git a/lib/route_generator.dart b/lib/route_generator.dart
index c94867a00..7103d5f6b 100644
--- a/lib/route_generator.dart
+++ b/lib/route_generator.dart
@@ -223,18 +223,13 @@ class RouteGenerator {
         return _routeError("${settings.name} invalid args: ${args.toString()}");
 
       case AddCustomTokenView.routeName:
-        if (args is String?) {
-          return getRoute(
-            shouldUseMaterialRoute: useMaterialPageRoute,
-            builder: (_) => AddCustomTokenView(
-              walletId: args,
-            ),
-            settings: RouteSettings(
-              name: settings.name,
-            ),
-          );
-        }
-        return _routeError("${settings.name} invalid args: ${args.toString()}");
+        return getRoute(
+          shouldUseMaterialRoute: useMaterialPageRoute,
+          builder: (_) => const AddCustomTokenView(),
+          settings: RouteSettings(
+            name: settings.name,
+          ),
+        );
 
       case SingleFieldEditView.routeName:
         if (args is Tuple2<String, String>) {
diff --git a/lib/services/ethereum/ethereum_api.dart b/lib/services/ethereum/ethereum_api.dart
index ec8ebb7fe..0f9c42c6c 100644
--- a/lib/services/ethereum/ethereum_api.dart
+++ b/lib/services/ethereum/ethereum_api.dart
@@ -362,35 +362,35 @@ abstract class EthereumAPI {
         slow: feesSlow.toInt());
   }
 
-  static Future<EthereumResponse<EthContract>> getTokenByContractAddress(
+  static Future<EthereumResponse<EthContract>> getTokenContractInfoByAddress(
       String contractAddress) async {
     try {
-      final response = await get(Uri.parse(
-          "$etherscanApi?module=token&action=getToken&contractaddress=$contractAddress&apikey=EG6J7RJIQVSTP2BS59D3TY2G55YHS5F2HP"));
-      // "stackURI?module=token&action=getToken&contractaddress=$contractAddress"));
-      // "$blockScout?module=token&action=getToken&contractaddress=$contractAddress"));
+      final response = await get(
+        Uri.parse(
+          "$stackBaseServer/tokens?addrs=$contractAddress&parts=all",
+        ),
+      );
+
       if (response.statusCode == 200) {
         final json = jsonDecode(response.body);
         if (json["message"] == "OK") {
           final map = Map<String, dynamic>.from(json["result"] as Map);
           EthContract? token;
-          if (map["type"] == "ERC-20") {
+          if (map["isErc20"] == true) {
             token = EthContract(
-              address: map["contractAddress"] as String,
-              decimals: int.parse(map["decimals"] as String),
+              address: map["address"] as String,
+              decimals: map["decimals"] as int,
               name: map["name"] as String,
               symbol: map["symbol"] as String,
               type: EthContractType.erc20,
-              walletIds: [],
             );
-          } else if (map["type"] == "ERC-721") {
+          } else if (map["isErc721"] == true) {
             token = EthContract(
-              address: map["contractAddress"] as String,
-              decimals: int.parse(map["decimals"] as String),
+              address: map["address"] as String,
+              decimals: map["decimals"] as int,
               name: map["name"] as String,
               symbol: map["symbol"] as String,
               type: EthContractType.erc721,
-              walletIds: [],
             );
           } else {
             throw EthApiException(