mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-11-26 11:14:11 +01:00
feat: Enable NFTs for Solana wallet (#2082)
* feat: Enable NFTs for Solana wallet * Add localization for texts * Adjust file naming * Adjust file naming
This commit is contained in:
@@ -2,8 +2,10 @@ import 'dart:convert';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:cake_wallet/core/wallet_connect/wc_bottom_sheet_service.dart';
|
||||
import 'package:cake_wallet/entities/solana_nft_asset_model.dart';
|
||||
import 'package:cake_wallet/reactions/wallet_connect.dart';
|
||||
import 'package:cake_wallet/src/screens/wallet_connect/widgets/message_display_widget.dart';
|
||||
import 'package:cw_core/wallet_type.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
||||
@@ -19,7 +21,8 @@ abstract class NFTViewModelBase with Store {
|
||||
NFTViewModelBase(this.appStore, this.bottomSheetService)
|
||||
: isLoading = false,
|
||||
isImportNFTLoading = false,
|
||||
nftAssetByWalletModels = ObservableList() {
|
||||
nftAssetByWalletModels = ObservableList(),
|
||||
solanaNftAssetModels = ObservableList() {
|
||||
getNFTAssetByWallet();
|
||||
|
||||
reaction((_) => appStore.wallet, (_) => getNFTAssetByWallet());
|
||||
@@ -36,31 +39,44 @@ abstract class NFTViewModelBase with Store {
|
||||
|
||||
ObservableList<NFTAssetModel> nftAssetByWalletModels;
|
||||
|
||||
ObservableList<SolanaNFTAssetModel> solanaNftAssetModels;
|
||||
|
||||
@action
|
||||
Future<void> getNFTAssetByWallet() async {
|
||||
if (!isEVMCompatibleChain(appStore.wallet!.type)) return;
|
||||
final walletType = appStore.wallet!.type;
|
||||
|
||||
if (!isNFTACtivatedChain(walletType)) return;
|
||||
|
||||
final walletAddress = appStore.wallet!.walletInfo.address;
|
||||
log('Fetching wallet NFTs for $walletAddress');
|
||||
|
||||
final chainName = getChainNameBasedOnWalletType(appStore.wallet!.type);
|
||||
final chainName = getChainNameBasedOnWalletType(walletType);
|
||||
// the [chain] refers to the chain network that the nft is on
|
||||
// the [format] refers to the number format type of the responses
|
||||
// the [normalizedMetadata] field is a boolean that determines if
|
||||
// the response would include a json string of the NFT Metadata that can be decoded
|
||||
// and used within the wallet
|
||||
// the [excludeSpam] field is a boolean that determines if spam nfts be excluded from the response.
|
||||
final uri = Uri.https(
|
||||
'deep-index.moralis.io',
|
||||
'/api/v2.2/$walletAddress/nft',
|
||||
{
|
||||
"chain": chainName,
|
||||
"format": "decimal",
|
||||
"media_items": "false",
|
||||
"exclude_spam": "true",
|
||||
"normalizeMetadata": "true",
|
||||
},
|
||||
);
|
||||
|
||||
Uri uri;
|
||||
if (walletType == WalletType.solana) {
|
||||
uri = Uri.https(
|
||||
'solana-gateway.moralis.io',
|
||||
'/account/$chainName/$walletAddress/nft',
|
||||
);
|
||||
} else {
|
||||
uri = Uri.https(
|
||||
'deep-index.moralis.io',
|
||||
'/api/v2.2/$walletAddress/nft',
|
||||
{
|
||||
"chain": chainName,
|
||||
"format": "decimal",
|
||||
"media_items": "false",
|
||||
"exclude_spam": "true",
|
||||
"normalizeMetadata": "true",
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
isLoading = true;
|
||||
@@ -73,13 +89,30 @@ abstract class NFTViewModelBase with Store {
|
||||
},
|
||||
);
|
||||
|
||||
final decodedResponse = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
final decodedResponse = jsonDecode(response.body);
|
||||
|
||||
final result = WalletNFTsResponseModel.fromJson(decodedResponse).result ?? [];
|
||||
if (walletType == WalletType.solana) {
|
||||
final results = await Future.wait(
|
||||
(decodedResponse as List<dynamic>).map(
|
||||
(x) {
|
||||
final data = x as Map<String, dynamic>;
|
||||
final mint = data['mint'] as String? ?? '';
|
||||
return getSolanaNFTDetails(mint, chainName);
|
||||
},
|
||||
).toList(),
|
||||
);
|
||||
|
||||
nftAssetByWalletModels.clear();
|
||||
solanaNftAssetModels.clear();
|
||||
|
||||
nftAssetByWalletModels.addAll(result);
|
||||
solanaNftAssetModels.addAll(results);
|
||||
} else {
|
||||
final result =
|
||||
WalletNFTsResponseModel.fromJson(decodedResponse as Map<String, dynamic>).result ?? [];
|
||||
|
||||
nftAssetByWalletModels.clear();
|
||||
|
||||
nftAssetByWalletModels.addAll(result);
|
||||
}
|
||||
|
||||
isLoading = false;
|
||||
} catch (e) {
|
||||
@@ -94,51 +127,76 @@ abstract class NFTViewModelBase with Store {
|
||||
}
|
||||
}
|
||||
|
||||
Future<SolanaNFTAssetModel> getSolanaNFTDetails(String address, String chainName) async {
|
||||
final uri = Uri.https(
|
||||
'solana-gateway.moralis.io',
|
||||
'/nft/$chainName/$address/metadata',
|
||||
);
|
||||
|
||||
final response = await http.get(
|
||||
uri,
|
||||
headers: {
|
||||
"Accept": "application/json",
|
||||
"X-API-Key": secrets.moralisApiKey,
|
||||
},
|
||||
);
|
||||
|
||||
final decodedResponse = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
|
||||
return SolanaNFTAssetModel.fromJson(decodedResponse);
|
||||
}
|
||||
|
||||
@action
|
||||
Future<void> importNFT(String tokenAddress, String tokenId) async {
|
||||
Future<void> importNFT(String tokenAddress, String? tokenId) async {
|
||||
final chainName = getChainNameBasedOnWalletType(appStore.wallet!.type);
|
||||
// the [chain] refers to the chain network that the nft is on
|
||||
// the [format] refers to the number format type of the responses
|
||||
// the [normalizedMetadata] field is a boolean that determines if
|
||||
// the response would include a json string of the NFT Metadata that can be decoded
|
||||
// and used within the wallet
|
||||
final uri = Uri.https(
|
||||
'deep-index.moralis.io',
|
||||
'/api/v2.2/nft/$tokenAddress/$tokenId',
|
||||
{
|
||||
"chain": chainName,
|
||||
"format": "decimal",
|
||||
"media_items": "false",
|
||||
"normalizeMetadata": "true",
|
||||
},
|
||||
);
|
||||
|
||||
try {
|
||||
isImportNFTLoading = true;
|
||||
|
||||
final response = await http.get(
|
||||
uri,
|
||||
headers: {
|
||||
"Accept": "application/json",
|
||||
"X-API-Key": secrets.moralisApiKey,
|
||||
},
|
||||
);
|
||||
if (appStore.wallet!.type == WalletType.solana) {
|
||||
final result = await getSolanaNFTDetails(tokenAddress, chainName);
|
||||
|
||||
final decodedResponse = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
solanaNftAssetModels.add(result);
|
||||
} else {
|
||||
final uri = Uri.https(
|
||||
'deep-index.moralis.io',
|
||||
'/api/v2.2/nft/$tokenAddress/$tokenId',
|
||||
{
|
||||
"chain": chainName,
|
||||
"format": "decimal",
|
||||
"media_items": "false",
|
||||
"normalizeMetadata": "true",
|
||||
},
|
||||
);
|
||||
|
||||
final nftAsset = NFTAssetModel.fromJson(decodedResponse);
|
||||
final response = await http.get(
|
||||
uri,
|
||||
headers: {
|
||||
"Accept": "application/json",
|
||||
"X-API-Key": secrets.moralisApiKey,
|
||||
},
|
||||
);
|
||||
|
||||
nftAssetByWalletModels.add(nftAsset);
|
||||
final decodedResponse = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
|
||||
isImportNFTLoading = false;
|
||||
final nftAsset = NFTAssetModel.fromJson(decodedResponse);
|
||||
|
||||
nftAssetByWalletModels.add(nftAsset);
|
||||
}
|
||||
} catch (e) {
|
||||
isImportNFTLoading = false;
|
||||
bottomSheetService.queueBottomSheet(
|
||||
isModalDismissible: true,
|
||||
widget: BottomSheetMessageDisplayWidget(
|
||||
message: e.toString(),
|
||||
),
|
||||
);
|
||||
} finally {
|
||||
isImportNFTLoading = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user