@metamask/assets-controllers 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/LICENSE +20 -0
- package/README.md +30 -0
- package/dist/AccountTrackerController.d.ts +88 -0
- package/dist/AccountTrackerController.js +138 -0
- package/dist/AccountTrackerController.js.map +1 -0
- package/dist/AssetsContractController.d.ts +176 -0
- package/dist/AssetsContractController.js +314 -0
- package/dist/AssetsContractController.js.map +1 -0
- package/dist/CurrencyRateController.d.ts +98 -0
- package/dist/CurrencyRateController.js +193 -0
- package/dist/CurrencyRateController.js.map +1 -0
- package/dist/NftController.d.ts +409 -0
- package/dist/NftController.js +835 -0
- package/dist/NftController.js.map +1 -0
- package/dist/NftDetectionController.d.ts +179 -0
- package/dist/NftDetectionController.js +204 -0
- package/dist/NftDetectionController.js.map +1 -0
- package/dist/Standards/ERC20Standard.d.ts +42 -0
- package/dist/Standards/ERC20Standard.js +121 -0
- package/dist/Standards/ERC20Standard.js.map +1 -0
- package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.d.ts +78 -0
- package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.js +148 -0
- package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.js.map +1 -0
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.d.ts +88 -0
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.js +182 -0
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.js.map +1 -0
- package/dist/Standards/standards-types.d.ts +14 -0
- package/dist/Standards/standards-types.js +3 -0
- package/dist/Standards/standards-types.js.map +1 -0
- package/dist/TokenBalancesController.d.ts +69 -0
- package/dist/TokenBalancesController.js +94 -0
- package/dist/TokenBalancesController.js.map +1 -0
- package/dist/TokenDetectionController.d.ts +84 -0
- package/dist/TokenDetectionController.js +185 -0
- package/dist/TokenDetectionController.js.map +1 -0
- package/dist/TokenListController.d.ts +114 -0
- package/dist/TokenListController.js +256 -0
- package/dist/TokenListController.js.map +1 -0
- package/dist/TokenRatesController.d.ts +167 -0
- package/dist/TokenRatesController.js +284 -0
- package/dist/TokenRatesController.js.map +1 -0
- package/dist/TokensController.d.ts +238 -0
- package/dist/TokensController.js +530 -0
- package/dist/TokensController.js.map +1 -0
- package/dist/assetsUtil.d.ts +106 -0
- package/dist/assetsUtil.js +228 -0
- package/dist/assetsUtil.js.map +1 -0
- package/dist/crypto-compare.d.ts +12 -0
- package/dist/crypto-compare.js +67 -0
- package/dist/crypto-compare.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/token-service.d.ts +29 -0
- package/dist/token-service.js +134 -0
- package/dist/token-service.js.map +1 -0
- package/package.json +75 -0
|
@@ -0,0 +1,835 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.NftController = void 0;
|
|
13
|
+
const events_1 = require("events");
|
|
14
|
+
const ethereumjs_util_1 = require("ethereumjs-util");
|
|
15
|
+
const async_mutex_1 = require("async-mutex");
|
|
16
|
+
const base_controller_1 = require("@metamask/base-controller");
|
|
17
|
+
const controller_utils_1 = require("@metamask/controller-utils");
|
|
18
|
+
const assetsUtil_1 = require("./assetsUtil");
|
|
19
|
+
const ALL_NFTS_STATE_KEY = 'allNfts';
|
|
20
|
+
const ALL_NFTS_CONTRACTS_STATE_KEY = 'allNftContracts';
|
|
21
|
+
/**
|
|
22
|
+
* Controller that stores assets and exposes convenience methods
|
|
23
|
+
*/
|
|
24
|
+
class NftController extends base_controller_1.BaseController {
|
|
25
|
+
/**
|
|
26
|
+
* Creates an NftController instance.
|
|
27
|
+
*
|
|
28
|
+
* @param options - The controller options.
|
|
29
|
+
* @param options.onPreferencesStateChange - Allows subscribing to preference controller state changes.
|
|
30
|
+
* @param options.onNetworkStateChange - Allows subscribing to network controller state changes.
|
|
31
|
+
* @param options.getERC721AssetName - Gets the name of the asset at the given address.
|
|
32
|
+
* @param options.getERC721AssetSymbol - Gets the symbol of the asset at the given address.
|
|
33
|
+
* @param options.getERC721TokenURI - Gets the URI of the ERC721 token at the given address, with the given ID.
|
|
34
|
+
* @param options.getERC721OwnerOf - Get the owner of a ERC-721 NFT.
|
|
35
|
+
* @param options.getERC1155BalanceOf - Gets balance of a ERC-1155 NFT.
|
|
36
|
+
* @param options.getERC1155TokenURI - Gets the URI of the ERC1155 token at the given address, with the given ID.
|
|
37
|
+
* @param options.onNftAdded - Callback that is called when an NFT is added. Currently used pass data
|
|
38
|
+
* for tracking the NFT added event.
|
|
39
|
+
* @param config - Initial options used to configure this controller.
|
|
40
|
+
* @param state - Initial state to set on this controller.
|
|
41
|
+
*/
|
|
42
|
+
constructor({ onPreferencesStateChange, onNetworkStateChange, getERC721AssetName, getERC721AssetSymbol, getERC721TokenURI, getERC721OwnerOf, getERC1155BalanceOf, getERC1155TokenURI, onNftAdded, }, config, state) {
|
|
43
|
+
super(config, state);
|
|
44
|
+
this.mutex = new async_mutex_1.Mutex();
|
|
45
|
+
/**
|
|
46
|
+
* EventEmitter instance used to listen to specific EIP747 events
|
|
47
|
+
*/
|
|
48
|
+
this.hub = new events_1.EventEmitter();
|
|
49
|
+
/**
|
|
50
|
+
* Name of this controller used during composition
|
|
51
|
+
*/
|
|
52
|
+
this.name = 'NftController';
|
|
53
|
+
this.defaultConfig = {
|
|
54
|
+
networkType: controller_utils_1.MAINNET,
|
|
55
|
+
selectedAddress: '',
|
|
56
|
+
chainId: '',
|
|
57
|
+
ipfsGateway: controller_utils_1.IPFS_DEFAULT_GATEWAY_URL,
|
|
58
|
+
openSeaEnabled: false,
|
|
59
|
+
useIPFSSubdomains: true,
|
|
60
|
+
};
|
|
61
|
+
this.defaultState = {
|
|
62
|
+
allNftContracts: {},
|
|
63
|
+
allNfts: {},
|
|
64
|
+
ignoredNfts: [],
|
|
65
|
+
};
|
|
66
|
+
this.initialize();
|
|
67
|
+
this.getERC721AssetName = getERC721AssetName;
|
|
68
|
+
this.getERC721AssetSymbol = getERC721AssetSymbol;
|
|
69
|
+
this.getERC721TokenURI = getERC721TokenURI;
|
|
70
|
+
this.getERC721OwnerOf = getERC721OwnerOf;
|
|
71
|
+
this.getERC1155BalanceOf = getERC1155BalanceOf;
|
|
72
|
+
this.getERC1155TokenURI = getERC1155TokenURI;
|
|
73
|
+
this.onNftAdded = onNftAdded;
|
|
74
|
+
onPreferencesStateChange(({ selectedAddress, ipfsGateway, openSeaEnabled }) => {
|
|
75
|
+
this.configure({ selectedAddress, ipfsGateway, openSeaEnabled });
|
|
76
|
+
});
|
|
77
|
+
onNetworkStateChange(({ provider }) => {
|
|
78
|
+
const { chainId } = provider;
|
|
79
|
+
this.configure({ chainId });
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
getNftApi({ contractAddress, tokenId, useProxy, }) {
|
|
83
|
+
const { chainId } = this.config;
|
|
84
|
+
if (chainId === controller_utils_1.RINKEBY_CHAIN_ID) {
|
|
85
|
+
return `${controller_utils_1.OPENSEA_TEST_API_URL}/asset/${contractAddress}/${tokenId}`;
|
|
86
|
+
}
|
|
87
|
+
return useProxy
|
|
88
|
+
? `${controller_utils_1.OPENSEA_PROXY_URL}/asset/${contractAddress}/${tokenId}`
|
|
89
|
+
: `${controller_utils_1.OPENSEA_API_URL}/asset/${contractAddress}/${tokenId}`;
|
|
90
|
+
}
|
|
91
|
+
getNftContractInformationApi({ contractAddress, useProxy, }) {
|
|
92
|
+
const { chainId } = this.config;
|
|
93
|
+
if (chainId === controller_utils_1.RINKEBY_CHAIN_ID) {
|
|
94
|
+
return `${controller_utils_1.OPENSEA_TEST_API_URL}/asset_contract/${contractAddress}`;
|
|
95
|
+
}
|
|
96
|
+
return useProxy
|
|
97
|
+
? `${controller_utils_1.OPENSEA_PROXY_URL}/asset_contract/${contractAddress}`
|
|
98
|
+
: `${controller_utils_1.OPENSEA_API_URL}/asset_contract/${contractAddress}`;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Helper method to update nested state for allNfts and allNftContracts.
|
|
102
|
+
*
|
|
103
|
+
* @param newCollection - the modified piece of state to update in the controller's store
|
|
104
|
+
* @param baseStateKey - The root key in the store to update.
|
|
105
|
+
* @param passedConfig - An object containing the selectedAddress and chainId that are passed through the auto-detection flow.
|
|
106
|
+
* @param passedConfig.userAddress - the address passed through the NFT detection flow to ensure detected assets are stored to the correct account
|
|
107
|
+
* @param passedConfig.chainId - the chainId passed through the NFT detection flow to ensure detected assets are stored to the correct account
|
|
108
|
+
*/
|
|
109
|
+
updateNestedNftState(newCollection, baseStateKey, { userAddress, chainId } = {
|
|
110
|
+
userAddress: this.config.selectedAddress,
|
|
111
|
+
chainId: this.config.chainId,
|
|
112
|
+
}) {
|
|
113
|
+
const { [baseStateKey]: oldState } = this.state;
|
|
114
|
+
const addressState = oldState[userAddress];
|
|
115
|
+
const newAddressState = Object.assign(Object.assign({}, addressState), { [chainId]: newCollection });
|
|
116
|
+
const newState = Object.assign(Object.assign({}, oldState), { [userAddress]: newAddressState });
|
|
117
|
+
this.update({
|
|
118
|
+
[baseStateKey]: newState,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Request individual NFT information from OpenSea API.
|
|
123
|
+
*
|
|
124
|
+
* @param contractAddress - Hex address of the NFT contract.
|
|
125
|
+
* @param tokenId - The NFT identifier.
|
|
126
|
+
* @returns Promise resolving to the current NFT name and image.
|
|
127
|
+
*/
|
|
128
|
+
getNftInformationFromApi(contractAddress, tokenId) {
|
|
129
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
130
|
+
// Attempt to fetch the data with the proxy
|
|
131
|
+
let nftInformation = yield (0, controller_utils_1.fetchWithErrorHandling)({
|
|
132
|
+
url: this.getNftApi({
|
|
133
|
+
contractAddress,
|
|
134
|
+
tokenId,
|
|
135
|
+
useProxy: true,
|
|
136
|
+
}),
|
|
137
|
+
});
|
|
138
|
+
// if an openSeaApiKey is set we should attempt to refetch calling directly to OpenSea
|
|
139
|
+
if (!nftInformation && this.openSeaApiKey) {
|
|
140
|
+
nftInformation = yield (0, controller_utils_1.fetchWithErrorHandling)({
|
|
141
|
+
url: this.getNftApi({
|
|
142
|
+
contractAddress,
|
|
143
|
+
tokenId,
|
|
144
|
+
useProxy: false,
|
|
145
|
+
}),
|
|
146
|
+
options: {
|
|
147
|
+
headers: { 'X-API-KEY': this.openSeaApiKey },
|
|
148
|
+
},
|
|
149
|
+
// catch 403 errors (in case API key is down we don't want to blow up)
|
|
150
|
+
errorCodesToCatch: [403],
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
// if we were still unable to fetch the data we return out the default/null of `NftMetadata`
|
|
154
|
+
if (!nftInformation) {
|
|
155
|
+
return {
|
|
156
|
+
name: null,
|
|
157
|
+
description: null,
|
|
158
|
+
image: null,
|
|
159
|
+
standard: null,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
// if we've reached this point, we have successfully fetched some data for nftInformation
|
|
163
|
+
// now we reconfigure the data to conform to the `NftMetadata` type for storage.
|
|
164
|
+
const { num_sales, background_color, image_url, image_preview_url, image_thumbnail_url, image_original_url, animation_url, animation_original_url, name, description, external_link, creator, last_sale, asset_contract: { schema_name }, } = nftInformation;
|
|
165
|
+
/* istanbul ignore next */
|
|
166
|
+
const nftMetadata = Object.assign({}, { name: name || null }, { description: description || null }, { image: image_url || null }, creator && { creator }, num_sales && { numberOfSales: num_sales }, background_color && { backgroundColor: background_color }, image_preview_url && { imagePreview: image_preview_url }, image_thumbnail_url && { imageThumbnail: image_thumbnail_url }, image_original_url && { imageOriginal: image_original_url }, animation_url && { animation: animation_url }, animation_original_url && {
|
|
167
|
+
animationOriginal: animation_original_url,
|
|
168
|
+
}, external_link && { externalLink: external_link }, last_sale && { lastSale: last_sale }, schema_name && { standard: schema_name });
|
|
169
|
+
return nftMetadata;
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Request individual NFT information from contracts that follows Metadata Interface.
|
|
174
|
+
*
|
|
175
|
+
* @param contractAddress - Hex address of the NFT contract.
|
|
176
|
+
* @param tokenId - The NFT identifier.
|
|
177
|
+
* @returns Promise resolving to the current NFT name and image.
|
|
178
|
+
*/
|
|
179
|
+
getNftInformationFromTokenURI(contractAddress, tokenId) {
|
|
180
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
181
|
+
const { ipfsGateway, useIPFSSubdomains } = this.config;
|
|
182
|
+
const result = yield this.getNftURIAndStandard(contractAddress, tokenId);
|
|
183
|
+
let tokenURI = result[0];
|
|
184
|
+
const standard = result[1];
|
|
185
|
+
if (tokenURI.startsWith('ipfs://')) {
|
|
186
|
+
tokenURI = (0, assetsUtil_1.getFormattedIpfsUrl)(ipfsGateway, tokenURI, useIPFSSubdomains);
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
const object = yield (0, controller_utils_1.handleFetch)(tokenURI);
|
|
190
|
+
// TODO: Check image_url existence. This is not part of EIP721 nor EIP1155
|
|
191
|
+
const image = Object.prototype.hasOwnProperty.call(object, 'image')
|
|
192
|
+
? 'image'
|
|
193
|
+
: /* istanbul ignore next */ 'image_url';
|
|
194
|
+
return {
|
|
195
|
+
image: object[image],
|
|
196
|
+
name: object.name,
|
|
197
|
+
description: object.description,
|
|
198
|
+
standard,
|
|
199
|
+
favorite: false,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
catch (_a) {
|
|
203
|
+
return {
|
|
204
|
+
image: null,
|
|
205
|
+
name: null,
|
|
206
|
+
description: null,
|
|
207
|
+
standard: standard || null,
|
|
208
|
+
favorite: false,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Retrieve NFT uri with metadata. TODO Update method to use IPFS.
|
|
215
|
+
*
|
|
216
|
+
* @param contractAddress - NFT contract address.
|
|
217
|
+
* @param tokenId - NFT token id.
|
|
218
|
+
* @returns Promise resolving NFT uri and token standard.
|
|
219
|
+
*/
|
|
220
|
+
getNftURIAndStandard(contractAddress, tokenId) {
|
|
221
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
222
|
+
// try ERC721 uri
|
|
223
|
+
try {
|
|
224
|
+
const uri = yield this.getERC721TokenURI(contractAddress, tokenId);
|
|
225
|
+
return [uri, controller_utils_1.ERC721];
|
|
226
|
+
}
|
|
227
|
+
catch (_a) {
|
|
228
|
+
// Ignore error
|
|
229
|
+
}
|
|
230
|
+
// try ERC1155 uri
|
|
231
|
+
try {
|
|
232
|
+
const tokenURI = yield this.getERC1155TokenURI(contractAddress, tokenId);
|
|
233
|
+
/**
|
|
234
|
+
* According to EIP1155 the URI value allows for ID substitution
|
|
235
|
+
* in case the string `{id}` exists.
|
|
236
|
+
* https://eips.ethereum.org/EIPS/eip-1155#metadata
|
|
237
|
+
*/
|
|
238
|
+
if (!tokenURI.includes('{id}')) {
|
|
239
|
+
return [tokenURI, controller_utils_1.ERC1155];
|
|
240
|
+
}
|
|
241
|
+
const hexTokenId = (0, ethereumjs_util_1.stripHexPrefix)((0, controller_utils_1.BNToHex)(new ethereumjs_util_1.BN(tokenId)))
|
|
242
|
+
.padStart(64, '0')
|
|
243
|
+
.toLowerCase();
|
|
244
|
+
return [tokenURI.replace('{id}', hexTokenId), controller_utils_1.ERC1155];
|
|
245
|
+
}
|
|
246
|
+
catch (_b) {
|
|
247
|
+
// Ignore error
|
|
248
|
+
}
|
|
249
|
+
return ['', ''];
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Request individual NFT information (name, image url and description).
|
|
254
|
+
*
|
|
255
|
+
* @param contractAddress - Hex address of the NFT contract.
|
|
256
|
+
* @param tokenId - The NFT identifier.
|
|
257
|
+
* @returns Promise resolving to the current NFT name and image.
|
|
258
|
+
*/
|
|
259
|
+
getNftInformation(contractAddress, tokenId) {
|
|
260
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
261
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
262
|
+
const blockchainMetadata = yield (0, controller_utils_1.safelyExecute)(() => __awaiter(this, void 0, void 0, function* () {
|
|
263
|
+
return yield this.getNftInformationFromTokenURI(contractAddress, tokenId);
|
|
264
|
+
}));
|
|
265
|
+
let openSeaMetadata;
|
|
266
|
+
if (this.config.openSeaEnabled) {
|
|
267
|
+
openSeaMetadata = yield (0, controller_utils_1.safelyExecute)(() => __awaiter(this, void 0, void 0, function* () {
|
|
268
|
+
return yield this.getNftInformationFromApi(contractAddress, tokenId);
|
|
269
|
+
}));
|
|
270
|
+
}
|
|
271
|
+
return Object.assign(Object.assign({}, openSeaMetadata), { name: (_b = (_a = blockchainMetadata.name) !== null && _a !== void 0 ? _a : openSeaMetadata === null || openSeaMetadata === void 0 ? void 0 : openSeaMetadata.name) !== null && _b !== void 0 ? _b : null, description: (_d = (_c = blockchainMetadata.description) !== null && _c !== void 0 ? _c : openSeaMetadata === null || openSeaMetadata === void 0 ? void 0 : openSeaMetadata.description) !== null && _d !== void 0 ? _d : null, image: (_f = (_e = blockchainMetadata.image) !== null && _e !== void 0 ? _e : openSeaMetadata === null || openSeaMetadata === void 0 ? void 0 : openSeaMetadata.image) !== null && _f !== void 0 ? _f : null, standard: (_h = (_g = blockchainMetadata.standard) !== null && _g !== void 0 ? _g : openSeaMetadata === null || openSeaMetadata === void 0 ? void 0 : openSeaMetadata.standard) !== null && _h !== void 0 ? _h : null });
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Request NFT contract information from OpenSea API.
|
|
276
|
+
*
|
|
277
|
+
* @param contractAddress - Hex address of the NFT contract.
|
|
278
|
+
* @returns Promise resolving to the current NFT name and image.
|
|
279
|
+
*/
|
|
280
|
+
getNftContractInformationFromApi(contractAddress) {
|
|
281
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
282
|
+
/* istanbul ignore if */
|
|
283
|
+
let apiNftContractObject = yield (0, controller_utils_1.fetchWithErrorHandling)({
|
|
284
|
+
url: this.getNftContractInformationApi({
|
|
285
|
+
contractAddress,
|
|
286
|
+
useProxy: true,
|
|
287
|
+
}),
|
|
288
|
+
});
|
|
289
|
+
// if we successfully fetched return the fetched data immediately
|
|
290
|
+
if (apiNftContractObject) {
|
|
291
|
+
return apiNftContractObject;
|
|
292
|
+
}
|
|
293
|
+
// if we were unsuccessful in fetching from the API and an OpenSea API key is present
|
|
294
|
+
// attempt to refetch directly against the OpenSea API and if successful return the data immediately
|
|
295
|
+
if (this.openSeaApiKey) {
|
|
296
|
+
apiNftContractObject = yield (0, controller_utils_1.fetchWithErrorHandling)({
|
|
297
|
+
url: this.getNftContractInformationApi({
|
|
298
|
+
contractAddress,
|
|
299
|
+
useProxy: false,
|
|
300
|
+
}),
|
|
301
|
+
options: {
|
|
302
|
+
headers: { 'X-API-KEY': this.openSeaApiKey },
|
|
303
|
+
},
|
|
304
|
+
// catch 403 errors (in case API key is down we don't want to blow up)
|
|
305
|
+
errorCodesToCatch: [403],
|
|
306
|
+
});
|
|
307
|
+
if (apiNftContractObject) {
|
|
308
|
+
return apiNftContractObject;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
// If we've reached this point we were unable to fetch data from either the proxy or opensea so we return
|
|
312
|
+
// the default/null of ApiNftContract
|
|
313
|
+
return {
|
|
314
|
+
address: contractAddress,
|
|
315
|
+
asset_contract_type: null,
|
|
316
|
+
created_date: null,
|
|
317
|
+
schema_name: null,
|
|
318
|
+
symbol: null,
|
|
319
|
+
total_supply: null,
|
|
320
|
+
description: null,
|
|
321
|
+
external_link: null,
|
|
322
|
+
collection: {
|
|
323
|
+
name: null,
|
|
324
|
+
image_url: null,
|
|
325
|
+
},
|
|
326
|
+
};
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Request NFT contract information from the contract itself.
|
|
331
|
+
*
|
|
332
|
+
* @param contractAddress - Hex address of the NFT contract.
|
|
333
|
+
* @returns Promise resolving to the current NFT name and image.
|
|
334
|
+
*/
|
|
335
|
+
getNftContractInformationFromContract(contractAddress) {
|
|
336
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
337
|
+
const name = yield this.getERC721AssetName(contractAddress);
|
|
338
|
+
const symbol = yield this.getERC721AssetSymbol(contractAddress);
|
|
339
|
+
return {
|
|
340
|
+
collection: { name },
|
|
341
|
+
symbol,
|
|
342
|
+
address: contractAddress,
|
|
343
|
+
};
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Request NFT contract information from OpenSea API.
|
|
348
|
+
*
|
|
349
|
+
* @param contractAddress - Hex address of the NFT contract.
|
|
350
|
+
* @returns Promise resolving to the NFT contract name, image and description.
|
|
351
|
+
*/
|
|
352
|
+
getNftContractInformation(contractAddress) {
|
|
353
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
354
|
+
const blockchainContractData = yield (0, controller_utils_1.safelyExecute)(() => __awaiter(this, void 0, void 0, function* () {
|
|
355
|
+
return yield this.getNftContractInformationFromContract(contractAddress);
|
|
356
|
+
}));
|
|
357
|
+
let openSeaContractData;
|
|
358
|
+
if (this.config.openSeaEnabled) {
|
|
359
|
+
openSeaContractData = yield (0, controller_utils_1.safelyExecute)(() => __awaiter(this, void 0, void 0, function* () {
|
|
360
|
+
return yield this.getNftContractInformationFromApi(contractAddress);
|
|
361
|
+
}));
|
|
362
|
+
}
|
|
363
|
+
if (blockchainContractData || openSeaContractData) {
|
|
364
|
+
return Object.assign(Object.assign(Object.assign({}, openSeaContractData), blockchainContractData), { collection: Object.assign(Object.assign({ image_url: null }, openSeaContractData === null || openSeaContractData === void 0 ? void 0 : openSeaContractData.collection), blockchainContractData === null || blockchainContractData === void 0 ? void 0 : blockchainContractData.collection) });
|
|
365
|
+
}
|
|
366
|
+
/* istanbul ignore next */
|
|
367
|
+
return {
|
|
368
|
+
address: contractAddress,
|
|
369
|
+
asset_contract_type: null,
|
|
370
|
+
created_date: null,
|
|
371
|
+
schema_name: null,
|
|
372
|
+
symbol: null,
|
|
373
|
+
total_supply: null,
|
|
374
|
+
description: null,
|
|
375
|
+
external_link: null,
|
|
376
|
+
collection: { name: null, image_url: null },
|
|
377
|
+
};
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Adds an individual NFT to the stored NFT list.
|
|
382
|
+
*
|
|
383
|
+
* @param address - Hex address of the NFT contract.
|
|
384
|
+
* @param tokenId - The NFT identifier.
|
|
385
|
+
* @param nftMetadata - NFT optional information (name, image and description).
|
|
386
|
+
* @param nftContract - An object containing contract data of the NFT being added.
|
|
387
|
+
* @param detection - The chain ID and address of the currently selected network and account at the moment the NFT was detected.
|
|
388
|
+
* @returns Promise resolving to the current NFT list.
|
|
389
|
+
*/
|
|
390
|
+
addIndividualNft(address, tokenId, nftMetadata, nftContract, detection) {
|
|
391
|
+
var _a;
|
|
392
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
393
|
+
// TODO: Remove unused return
|
|
394
|
+
const releaseLock = yield this.mutex.acquire();
|
|
395
|
+
try {
|
|
396
|
+
address = (0, controller_utils_1.toChecksumHexAddress)(address);
|
|
397
|
+
const { allNfts } = this.state;
|
|
398
|
+
let chainId, selectedAddress;
|
|
399
|
+
if (detection) {
|
|
400
|
+
chainId = detection.chainId;
|
|
401
|
+
selectedAddress = detection.userAddress;
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
chainId = this.config.chainId;
|
|
405
|
+
selectedAddress = this.config.selectedAddress;
|
|
406
|
+
}
|
|
407
|
+
const nfts = ((_a = allNfts[selectedAddress]) === null || _a === void 0 ? void 0 : _a[chainId]) || [];
|
|
408
|
+
const existingEntry = nfts.find((nft) => nft.address.toLowerCase() === address.toLowerCase() &&
|
|
409
|
+
nft.tokenId === tokenId);
|
|
410
|
+
if (existingEntry) {
|
|
411
|
+
const differentMetadata = (0, assetsUtil_1.compareNftMetadata)(nftMetadata, existingEntry);
|
|
412
|
+
if (differentMetadata) {
|
|
413
|
+
// TODO: Switch to indexToUpdate
|
|
414
|
+
const indexToRemove = nfts.findIndex((nft) => nft.address.toLowerCase() === address.toLowerCase() &&
|
|
415
|
+
nft.tokenId === tokenId);
|
|
416
|
+
/* istanbul ignore next */
|
|
417
|
+
if (indexToRemove !== -1) {
|
|
418
|
+
nfts.splice(indexToRemove, 1);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
else {
|
|
422
|
+
return nfts;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
const newEntry = Object.assign({ address,
|
|
426
|
+
tokenId, favorite: (existingEntry === null || existingEntry === void 0 ? void 0 : existingEntry.favorite) || false, isCurrentlyOwned: true }, nftMetadata);
|
|
427
|
+
const newNfts = [...nfts, newEntry];
|
|
428
|
+
this.updateNestedNftState(newNfts, ALL_NFTS_STATE_KEY, {
|
|
429
|
+
chainId,
|
|
430
|
+
userAddress: selectedAddress,
|
|
431
|
+
});
|
|
432
|
+
if (this.onNftAdded) {
|
|
433
|
+
this.onNftAdded({
|
|
434
|
+
address,
|
|
435
|
+
symbol: nftContract.symbol,
|
|
436
|
+
tokenId: tokenId.toString(),
|
|
437
|
+
standard: nftMetadata.standard,
|
|
438
|
+
source: detection ? 'detected' : 'custom',
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
return newNfts;
|
|
442
|
+
}
|
|
443
|
+
finally {
|
|
444
|
+
releaseLock();
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Adds an NFT contract to the stored NFT contracts list.
|
|
450
|
+
*
|
|
451
|
+
* @param address - Hex address of the NFT contract.
|
|
452
|
+
* @param detection - The chain ID and address of the currently selected network and account at the moment the NFT was detected.
|
|
453
|
+
* @returns Promise resolving to the current NFT contracts list.
|
|
454
|
+
*/
|
|
455
|
+
addNftContract(address, detection) {
|
|
456
|
+
var _a;
|
|
457
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
458
|
+
const releaseLock = yield this.mutex.acquire();
|
|
459
|
+
try {
|
|
460
|
+
address = (0, controller_utils_1.toChecksumHexAddress)(address);
|
|
461
|
+
const { allNftContracts } = this.state;
|
|
462
|
+
let chainId, selectedAddress;
|
|
463
|
+
if (detection) {
|
|
464
|
+
chainId = detection.chainId;
|
|
465
|
+
selectedAddress = detection.userAddress;
|
|
466
|
+
}
|
|
467
|
+
else {
|
|
468
|
+
chainId = this.config.chainId;
|
|
469
|
+
selectedAddress = this.config.selectedAddress;
|
|
470
|
+
}
|
|
471
|
+
const nftContracts = ((_a = allNftContracts[selectedAddress]) === null || _a === void 0 ? void 0 : _a[chainId]) || [];
|
|
472
|
+
const existingEntry = nftContracts.find((nftContract) => nftContract.address.toLowerCase() === address.toLowerCase());
|
|
473
|
+
if (existingEntry) {
|
|
474
|
+
return nftContracts;
|
|
475
|
+
}
|
|
476
|
+
const contractInformation = yield this.getNftContractInformation(address);
|
|
477
|
+
const { asset_contract_type, created_date, schema_name, symbol, total_supply, description, external_link, collection: { name, image_url }, } = contractInformation;
|
|
478
|
+
// If being auto-detected opensea information is expected
|
|
479
|
+
// Otherwise at least name from the contract is needed
|
|
480
|
+
if ((detection && !name) ||
|
|
481
|
+
Object.keys(contractInformation).length === 0) {
|
|
482
|
+
return nftContracts;
|
|
483
|
+
}
|
|
484
|
+
/* istanbul ignore next */
|
|
485
|
+
const newEntry = Object.assign({}, { address }, description && { description }, name && { name }, image_url && { logo: image_url }, symbol && { symbol }, total_supply !== null &&
|
|
486
|
+
typeof total_supply !== 'undefined' && { totalSupply: total_supply }, asset_contract_type && { assetContractType: asset_contract_type }, created_date && { createdDate: created_date }, schema_name && { schemaName: schema_name }, external_link && { externalLink: external_link });
|
|
487
|
+
const newNftContracts = [...nftContracts, newEntry];
|
|
488
|
+
this.updateNestedNftState(newNftContracts, ALL_NFTS_CONTRACTS_STATE_KEY, {
|
|
489
|
+
chainId,
|
|
490
|
+
userAddress: selectedAddress,
|
|
491
|
+
});
|
|
492
|
+
return newNftContracts;
|
|
493
|
+
}
|
|
494
|
+
finally {
|
|
495
|
+
releaseLock();
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Removes an individual NFT from the stored token list and saves it in ignored NFTs list.
|
|
501
|
+
*
|
|
502
|
+
* @param address - Hex address of the NFT contract.
|
|
503
|
+
* @param tokenId - Token identifier of the NFT.
|
|
504
|
+
*/
|
|
505
|
+
removeAndIgnoreIndividualNft(address, tokenId) {
|
|
506
|
+
var _a;
|
|
507
|
+
address = (0, controller_utils_1.toChecksumHexAddress)(address);
|
|
508
|
+
const { allNfts, ignoredNfts } = this.state;
|
|
509
|
+
const { chainId, selectedAddress } = this.config;
|
|
510
|
+
const newIgnoredNfts = [...ignoredNfts];
|
|
511
|
+
const nfts = ((_a = allNfts[selectedAddress]) === null || _a === void 0 ? void 0 : _a[chainId]) || [];
|
|
512
|
+
const newNfts = nfts.filter((nft) => {
|
|
513
|
+
if (nft.address.toLowerCase() === address.toLowerCase() &&
|
|
514
|
+
nft.tokenId === tokenId) {
|
|
515
|
+
const alreadyIgnored = newIgnoredNfts.find((c) => c.address === address && c.tokenId === tokenId);
|
|
516
|
+
!alreadyIgnored && newIgnoredNfts.push(nft);
|
|
517
|
+
return false;
|
|
518
|
+
}
|
|
519
|
+
return true;
|
|
520
|
+
});
|
|
521
|
+
this.updateNestedNftState(newNfts, ALL_NFTS_STATE_KEY);
|
|
522
|
+
this.update({
|
|
523
|
+
ignoredNfts: newIgnoredNfts,
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Removes an individual NFT from the stored token list.
|
|
528
|
+
*
|
|
529
|
+
* @param address - Hex address of the NFT contract.
|
|
530
|
+
* @param tokenId - Token identifier of the NFT.
|
|
531
|
+
*/
|
|
532
|
+
removeIndividualNft(address, tokenId) {
|
|
533
|
+
var _a;
|
|
534
|
+
address = (0, controller_utils_1.toChecksumHexAddress)(address);
|
|
535
|
+
const { allNfts } = this.state;
|
|
536
|
+
const { chainId, selectedAddress } = this.config;
|
|
537
|
+
const nfts = ((_a = allNfts[selectedAddress]) === null || _a === void 0 ? void 0 : _a[chainId]) || [];
|
|
538
|
+
const newNfts = nfts.filter((nft) => !(nft.address.toLowerCase() === address.toLowerCase() &&
|
|
539
|
+
nft.tokenId === tokenId));
|
|
540
|
+
this.updateNestedNftState(newNfts, ALL_NFTS_STATE_KEY);
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Removes an NFT contract to the stored NFT contracts list.
|
|
544
|
+
*
|
|
545
|
+
* @param address - Hex address of the NFT contract.
|
|
546
|
+
* @returns Promise resolving to the current NFT contracts list.
|
|
547
|
+
*/
|
|
548
|
+
removeNftContract(address) {
|
|
549
|
+
var _a;
|
|
550
|
+
address = (0, controller_utils_1.toChecksumHexAddress)(address);
|
|
551
|
+
const { allNftContracts } = this.state;
|
|
552
|
+
const { chainId, selectedAddress } = this.config;
|
|
553
|
+
const nftContracts = ((_a = allNftContracts[selectedAddress]) === null || _a === void 0 ? void 0 : _a[chainId]) || [];
|
|
554
|
+
const newNftContracts = nftContracts.filter((nftContract) => !(nftContract.address.toLowerCase() === address.toLowerCase()));
|
|
555
|
+
this.updateNestedNftState(newNftContracts, ALL_NFTS_CONTRACTS_STATE_KEY);
|
|
556
|
+
return newNftContracts;
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Sets an OpenSea API key to retrieve NFT information.
|
|
560
|
+
*
|
|
561
|
+
* @param openSeaApiKey - OpenSea API key.
|
|
562
|
+
*/
|
|
563
|
+
setApiKey(openSeaApiKey) {
|
|
564
|
+
this.openSeaApiKey = openSeaApiKey;
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Checks the ownership of a ERC-721 or ERC-1155 NFT for a given address.
|
|
568
|
+
*
|
|
569
|
+
* @param ownerAddress - User public address.
|
|
570
|
+
* @param nftAddress - NFT contract address.
|
|
571
|
+
* @param nftId - NFT token ID.
|
|
572
|
+
* @returns Promise resolving the NFT ownership.
|
|
573
|
+
*/
|
|
574
|
+
isNftOwner(ownerAddress, nftAddress, nftId) {
|
|
575
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
576
|
+
// Checks the ownership for ERC-721.
|
|
577
|
+
try {
|
|
578
|
+
const owner = yield this.getERC721OwnerOf(nftAddress, nftId);
|
|
579
|
+
return ownerAddress.toLowerCase() === owner.toLowerCase();
|
|
580
|
+
// eslint-disable-next-line no-empty
|
|
581
|
+
}
|
|
582
|
+
catch (_a) {
|
|
583
|
+
// Ignore ERC-721 contract error
|
|
584
|
+
}
|
|
585
|
+
// Checks the ownership for ERC-1155.
|
|
586
|
+
try {
|
|
587
|
+
const balance = yield this.getERC1155BalanceOf(ownerAddress, nftAddress, nftId);
|
|
588
|
+
return !balance.isZero();
|
|
589
|
+
// eslint-disable-next-line no-empty
|
|
590
|
+
}
|
|
591
|
+
catch (_b) {
|
|
592
|
+
// Ignore ERC-1155 contract error
|
|
593
|
+
}
|
|
594
|
+
throw new Error('Unable to verify ownership. Probably because the standard is not supported or the chain is incorrect.');
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Verifies currently selected address owns entered NFT address/tokenId combo and
|
|
599
|
+
* adds the NFT and respective NFT contract to the stored NFT and NFT contracts lists.
|
|
600
|
+
*
|
|
601
|
+
* @param address - Hex address of the NFT contract.
|
|
602
|
+
* @param tokenId - The NFT identifier.
|
|
603
|
+
*/
|
|
604
|
+
addNftVerifyOwnership(address, tokenId) {
|
|
605
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
606
|
+
const { selectedAddress } = this.config;
|
|
607
|
+
if (!(yield this.isNftOwner(selectedAddress, address, tokenId))) {
|
|
608
|
+
throw new Error('This NFT is not owned by the user');
|
|
609
|
+
}
|
|
610
|
+
yield this.addNft(address, tokenId);
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Adds an NFT and respective NFT contract to the stored NFT and NFT contracts lists.
|
|
615
|
+
*
|
|
616
|
+
* @param address - Hex address of the NFT contract.
|
|
617
|
+
* @param tokenId - The NFT identifier.
|
|
618
|
+
* @param nftMetadata - NFT optional metadata.
|
|
619
|
+
* @param detection - The chain ID and address of the currently selected network and account at the moment the NFT was detected.
|
|
620
|
+
* @returns Promise resolving to the current NFT list.
|
|
621
|
+
*/
|
|
622
|
+
addNft(address, tokenId, nftMetadata, detection) {
|
|
623
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
624
|
+
address = (0, controller_utils_1.toChecksumHexAddress)(address);
|
|
625
|
+
const newNftContracts = yield this.addNftContract(address, detection);
|
|
626
|
+
nftMetadata =
|
|
627
|
+
nftMetadata || (yield this.getNftInformation(address, tokenId));
|
|
628
|
+
// If NFT contract was not added, do not add individual NFT
|
|
629
|
+
const nftContract = newNftContracts.find((contract) => contract.address.toLowerCase() === address.toLowerCase());
|
|
630
|
+
// If NFT contract information, add individual NFT
|
|
631
|
+
if (nftContract) {
|
|
632
|
+
yield this.addIndividualNft(address, tokenId, nftMetadata, nftContract, detection);
|
|
633
|
+
}
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Removes an NFT from the stored token list.
|
|
638
|
+
*
|
|
639
|
+
* @param address - Hex address of the NFT contract.
|
|
640
|
+
* @param tokenId - Token identifier of the NFT.
|
|
641
|
+
*/
|
|
642
|
+
removeNft(address, tokenId) {
|
|
643
|
+
var _a;
|
|
644
|
+
address = (0, controller_utils_1.toChecksumHexAddress)(address);
|
|
645
|
+
this.removeIndividualNft(address, tokenId);
|
|
646
|
+
const { allNfts } = this.state;
|
|
647
|
+
const { chainId, selectedAddress } = this.config;
|
|
648
|
+
const nfts = ((_a = allNfts[selectedAddress]) === null || _a === void 0 ? void 0 : _a[chainId]) || [];
|
|
649
|
+
const remainingNft = nfts.find((nft) => nft.address.toLowerCase() === address.toLowerCase());
|
|
650
|
+
if (!remainingNft) {
|
|
651
|
+
this.removeNftContract(address);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
/**
|
|
655
|
+
* Removes an NFT from the stored token list and saves it in ignored NFTs list.
|
|
656
|
+
*
|
|
657
|
+
* @param address - Hex address of the NFT contract.
|
|
658
|
+
* @param tokenId - Token identifier of the NFT.
|
|
659
|
+
*/
|
|
660
|
+
removeAndIgnoreNft(address, tokenId) {
|
|
661
|
+
var _a;
|
|
662
|
+
address = (0, controller_utils_1.toChecksumHexAddress)(address);
|
|
663
|
+
this.removeAndIgnoreIndividualNft(address, tokenId);
|
|
664
|
+
const { allNfts } = this.state;
|
|
665
|
+
const { chainId, selectedAddress } = this.config;
|
|
666
|
+
const nfts = ((_a = allNfts[selectedAddress]) === null || _a === void 0 ? void 0 : _a[chainId]) || [];
|
|
667
|
+
const remainingNft = nfts.find((nft) => nft.address.toLowerCase() === address.toLowerCase());
|
|
668
|
+
if (!remainingNft) {
|
|
669
|
+
this.removeNftContract(address);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Removes all NFTs from the ignored list.
|
|
674
|
+
*/
|
|
675
|
+
clearIgnoredNfts() {
|
|
676
|
+
this.update({ ignoredNfts: [] });
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* Checks whether input NFT is still owned by the user
|
|
680
|
+
* And updates the isCurrentlyOwned value on the NFT object accordingly.
|
|
681
|
+
*
|
|
682
|
+
* @param nft - The NFT object to check and update.
|
|
683
|
+
* @param batch - A boolean indicating whether this method is being called as part of a batch or single update.
|
|
684
|
+
* @param accountParams - The userAddress and chainId to check ownership against
|
|
685
|
+
* @param accountParams.userAddress - the address passed through the confirmed transaction flow to ensure detected assets are stored to the correct account
|
|
686
|
+
* @param accountParams.chainId - the chainId passed through the confirmed transaction flow to ensure detected assets are stored to the correct account
|
|
687
|
+
* @returns the NFT with the updated isCurrentlyOwned value
|
|
688
|
+
*/
|
|
689
|
+
checkAndUpdateSingleNftOwnershipStatus(nft, batch, { userAddress, chainId } = {
|
|
690
|
+
userAddress: this.config.selectedAddress,
|
|
691
|
+
chainId: this.config.chainId,
|
|
692
|
+
}) {
|
|
693
|
+
var _a;
|
|
694
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
695
|
+
const { address, tokenId } = nft;
|
|
696
|
+
let isOwned = nft.isCurrentlyOwned;
|
|
697
|
+
try {
|
|
698
|
+
isOwned = yield this.isNftOwner(userAddress, address, tokenId);
|
|
699
|
+
}
|
|
700
|
+
catch (error) {
|
|
701
|
+
if (!(error instanceof Error &&
|
|
702
|
+
error.message.includes('Unable to verify ownership'))) {
|
|
703
|
+
throw error;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
nft.isCurrentlyOwned = isOwned;
|
|
707
|
+
if (batch === true) {
|
|
708
|
+
return nft;
|
|
709
|
+
}
|
|
710
|
+
// if this is not part of a batched update we update this one NFT in state
|
|
711
|
+
const { allNfts } = this.state;
|
|
712
|
+
const nfts = ((_a = allNfts[userAddress]) === null || _a === void 0 ? void 0 : _a[chainId]) || [];
|
|
713
|
+
const nftToUpdate = nfts.find((item) => item.tokenId === tokenId &&
|
|
714
|
+
item.address.toLowerCase() === address.toLowerCase());
|
|
715
|
+
if (nftToUpdate) {
|
|
716
|
+
nftToUpdate.isCurrentlyOwned = isOwned;
|
|
717
|
+
this.updateNestedNftState(nfts, ALL_NFTS_STATE_KEY, {
|
|
718
|
+
userAddress,
|
|
719
|
+
chainId,
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
return nft;
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
/**
|
|
726
|
+
* Checks whether NFTs associated with current selectedAddress/chainId combination are still owned by the user
|
|
727
|
+
* And updates the isCurrentlyOwned value on each accordingly.
|
|
728
|
+
*/
|
|
729
|
+
checkAndUpdateAllNftsOwnershipStatus() {
|
|
730
|
+
var _a;
|
|
731
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
732
|
+
const { allNfts } = this.state;
|
|
733
|
+
const { chainId, selectedAddress } = this.config;
|
|
734
|
+
const nfts = ((_a = allNfts[selectedAddress]) === null || _a === void 0 ? void 0 : _a[chainId]) || [];
|
|
735
|
+
const updatedNfts = yield Promise.all(nfts.map((nft) => __awaiter(this, void 0, void 0, function* () {
|
|
736
|
+
var _b;
|
|
737
|
+
return ((_b = (yield this.checkAndUpdateSingleNftOwnershipStatus(nft, true))) !== null && _b !== void 0 ? _b : nft);
|
|
738
|
+
})));
|
|
739
|
+
this.updateNestedNftState(updatedNfts, ALL_NFTS_STATE_KEY);
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* Update NFT favorite status.
|
|
744
|
+
*
|
|
745
|
+
* @param address - Hex address of the NFT contract.
|
|
746
|
+
* @param tokenId - Hex address of the NFT contract.
|
|
747
|
+
* @param favorite - NFT new favorite status.
|
|
748
|
+
*/
|
|
749
|
+
updateNftFavoriteStatus(address, tokenId, favorite) {
|
|
750
|
+
var _a;
|
|
751
|
+
const { allNfts } = this.state;
|
|
752
|
+
const { chainId, selectedAddress } = this.config;
|
|
753
|
+
const nfts = ((_a = allNfts[selectedAddress]) === null || _a === void 0 ? void 0 : _a[chainId]) || [];
|
|
754
|
+
const index = nfts.findIndex((nft) => nft.address === address && nft.tokenId === tokenId);
|
|
755
|
+
if (index === -1) {
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
const updatedNft = Object.assign(Object.assign({}, nfts[index]), { favorite });
|
|
759
|
+
// Update Nfts array
|
|
760
|
+
nfts[index] = updatedNft;
|
|
761
|
+
this.updateNestedNftState(nfts, ALL_NFTS_STATE_KEY);
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* Returns an NFT by the address and token id.
|
|
765
|
+
*
|
|
766
|
+
* @param address - Hex address of the NFT contract.
|
|
767
|
+
* @param tokenId - Number that represents the id of the token.
|
|
768
|
+
* @param selectedAddress - Hex address of the user account.
|
|
769
|
+
* @param chainId - Id of the current network.
|
|
770
|
+
* @returns Object containing the NFT and its position in the array
|
|
771
|
+
*/
|
|
772
|
+
findNftByAddressAndTokenId(address, tokenId, selectedAddress, chainId) {
|
|
773
|
+
var _a;
|
|
774
|
+
const { allNfts } = this.state;
|
|
775
|
+
const nfts = ((_a = allNfts[selectedAddress]) === null || _a === void 0 ? void 0 : _a[chainId]) || [];
|
|
776
|
+
const index = nfts.findIndex((nft) => nft.address.toLowerCase() === address.toLowerCase() &&
|
|
777
|
+
nft.tokenId === tokenId);
|
|
778
|
+
if (index === -1) {
|
|
779
|
+
return null;
|
|
780
|
+
}
|
|
781
|
+
return { nft: nfts[index], index };
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* Update NFT data.
|
|
785
|
+
*
|
|
786
|
+
* @param nft - NFT object to find the right NFT to updates.
|
|
787
|
+
* @param updates - NFT partial object to update properties of the NFT.
|
|
788
|
+
* @param selectedAddress - Hex address of the user account.
|
|
789
|
+
* @param chainId - Id of the current network.
|
|
790
|
+
*/
|
|
791
|
+
updateNft(nft, updates, selectedAddress, chainId) {
|
|
792
|
+
var _a;
|
|
793
|
+
const { allNfts } = this.state;
|
|
794
|
+
const nfts = ((_a = allNfts[selectedAddress]) === null || _a === void 0 ? void 0 : _a[chainId]) || [];
|
|
795
|
+
const nftInfo = this.findNftByAddressAndTokenId(nft.address, nft.tokenId, selectedAddress, chainId);
|
|
796
|
+
if (!nftInfo) {
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
const updatedNft = Object.assign(Object.assign({}, nft), updates);
|
|
800
|
+
const newNfts = [
|
|
801
|
+
...nfts.slice(0, nftInfo.index),
|
|
802
|
+
updatedNft,
|
|
803
|
+
...nfts.slice(nftInfo.index + 1),
|
|
804
|
+
];
|
|
805
|
+
this.updateNestedNftState(newNfts, ALL_NFTS_STATE_KEY);
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* Resets the transaction status of an NFT.
|
|
809
|
+
*
|
|
810
|
+
* @param transactionId - NFT transaction id.
|
|
811
|
+
* @param selectedAddress - Hex address of the user account.
|
|
812
|
+
* @param chainId - Id of the current network.
|
|
813
|
+
* @returns a boolean indicating if the reset was well succeded or not
|
|
814
|
+
*/
|
|
815
|
+
resetNftTransactionStatusByTransactionId(transactionId, selectedAddress, chainId) {
|
|
816
|
+
var _a;
|
|
817
|
+
const { allNfts } = this.state;
|
|
818
|
+
const nfts = ((_a = allNfts[selectedAddress]) === null || _a === void 0 ? void 0 : _a[chainId]) || [];
|
|
819
|
+
const index = nfts.findIndex((nft) => nft.transactionId === transactionId);
|
|
820
|
+
if (index === -1) {
|
|
821
|
+
return false;
|
|
822
|
+
}
|
|
823
|
+
const updatedNft = Object.assign(Object.assign({}, nfts[index]), { transactionId: undefined });
|
|
824
|
+
const newNfts = [
|
|
825
|
+
...nfts.slice(0, index),
|
|
826
|
+
updatedNft,
|
|
827
|
+
...nfts.slice(index + 1),
|
|
828
|
+
];
|
|
829
|
+
this.updateNestedNftState(newNfts, ALL_NFTS_STATE_KEY);
|
|
830
|
+
return true;
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
exports.NftController = NftController;
|
|
834
|
+
exports.default = NftController;
|
|
835
|
+
//# sourceMappingURL=NftController.js.map
|