@metamask/assets-controllers 30.0.0 → 31.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 +81 -1
- package/dist/AccountTrackerController.js +2 -2
- package/dist/AccountTrackerController.mjs +1 -1
- package/dist/AssetsContractController.js +2 -2
- package/dist/AssetsContractController.mjs +1 -1
- package/dist/NftController.js +2 -2
- package/dist/NftController.mjs +3 -3
- package/dist/NftDetectionController.js +2 -2
- package/dist/NftDetectionController.mjs +1 -1
- package/dist/TokenBalancesController.js +2 -2
- package/dist/TokenBalancesController.mjs +1 -1
- package/dist/TokenListController.js +3 -3
- package/dist/TokenListController.mjs +2 -2
- package/dist/TokenRatesController.js +2 -2
- package/dist/TokenRatesController.mjs +1 -1
- package/dist/TokensController.js +3 -3
- package/dist/TokensController.mjs +2 -2
- package/dist/chunk-4AC3X2U5.js +771 -0
- package/dist/chunk-4AC3X2U5.js.map +1 -0
- package/dist/chunk-543CC74T.mjs +253 -0
- package/dist/chunk-543CC74T.mjs.map +1 -0
- package/dist/{chunk-Z3OQU4XW.mjs → chunk-5W5OO2Q5.mjs} +2 -2
- package/dist/chunk-5W5OO2Q5.mjs.map +1 -0
- package/dist/chunk-62T7RKU3.mjs +1265 -0
- package/dist/chunk-62T7RKU3.mjs.map +1 -0
- package/dist/{chunk-PAJTKWEC.mjs → chunk-65PB33TE.mjs} +1 -1
- package/dist/chunk-65PB33TE.mjs.map +1 -0
- package/dist/{chunk-7MMEHAKG.mjs → chunk-73F3SN5O.mjs} +332 -265
- package/dist/chunk-73F3SN5O.mjs.map +1 -0
- package/dist/{chunk-XHMM35YT.mjs → chunk-A2DDWXMS.mjs} +2 -2
- package/dist/chunk-CGLUTXI7.js +1265 -0
- package/dist/chunk-CGLUTXI7.js.map +1 -0
- package/dist/{chunk-X4FFNQHE.mjs → chunk-E4ECCGJV.mjs} +1 -1
- package/dist/chunk-E4ECCGJV.mjs.map +1 -0
- package/dist/{chunk-V4ZO3F2S.js → chunk-GU53EI7A.js} +1 -1
- package/dist/chunk-GU53EI7A.js.map +1 -0
- package/dist/{chunk-MHN7CJCZ.mjs → chunk-HLCGZGPA.mjs} +1 -1
- package/dist/chunk-HLCGZGPA.mjs.map +1 -0
- package/dist/{chunk-LD4GC7OR.js → chunk-IBK6AXPP.js} +8 -9
- package/dist/chunk-IBK6AXPP.js.map +1 -0
- package/dist/{chunk-5F5EQAX5.js → chunk-JUI3XNEF.js} +3 -3
- package/dist/{chunk-3R43XIIX.js → chunk-PYMUBJQX.js} +2 -2
- package/dist/chunk-PYMUBJQX.js.map +1 -0
- package/dist/chunk-QFDTOEYR.js +253 -0
- package/dist/chunk-QFDTOEYR.js.map +1 -0
- package/dist/{chunk-4ODKGWYQ.js → chunk-QHWKLZUC.js} +1 -1
- package/dist/chunk-QHWKLZUC.js.map +1 -0
- package/dist/{chunk-6W5EQ3JQ.js → chunk-UEDNQBJN.js} +1 -1
- package/dist/chunk-UEDNQBJN.js.map +1 -0
- package/dist/{chunk-4LY47RPI.mjs → chunk-WCCLSUOI.mjs} +8 -9
- package/dist/{chunk-4LY47RPI.mjs.map → chunk-WCCLSUOI.mjs.map} +1 -1
- package/dist/index.js +10 -12
- package/dist/index.mjs +11 -13
- package/dist/token-service.js +2 -2
- package/dist/token-service.mjs +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/AccountTrackerController.d.ts +1 -1
- package/dist/types/AccountTrackerController.d.ts.map +1 -1
- package/dist/types/AssetsContractController.d.ts +1 -1
- package/dist/types/AssetsContractController.d.ts.map +1 -1
- package/dist/types/NftController.d.ts +65 -184
- package/dist/types/NftController.d.ts.map +1 -1
- package/dist/types/NftDetectionController.d.ts +34 -75
- package/dist/types/NftDetectionController.d.ts.map +1 -1
- package/dist/types/TokenRatesController.d.ts +6 -6
- package/dist/types/TokenRatesController.d.ts.map +1 -1
- package/dist/types/TokensController.d.ts +27 -131
- package/dist/types/TokensController.d.ts.map +1 -1
- package/dist/types/index.d.ts +5 -3
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +12 -12
- package/dist/chunk-3R43XIIX.js.map +0 -1
- package/dist/chunk-46UZDIXW.mjs +0 -1205
- package/dist/chunk-46UZDIXW.mjs.map +0 -1
- package/dist/chunk-4ODKGWYQ.js.map +0 -1
- package/dist/chunk-6W5EQ3JQ.js.map +0 -1
- package/dist/chunk-7MMEHAKG.mjs.map +0 -1
- package/dist/chunk-FMZML3V5.js +0 -237
- package/dist/chunk-FMZML3V5.js.map +0 -1
- package/dist/chunk-KOKB6U4Z.mjs +0 -237
- package/dist/chunk-KOKB6U4Z.mjs.map +0 -1
- package/dist/chunk-LAU6ZDZR.js +0 -1205
- package/dist/chunk-LAU6ZDZR.js.map +0 -1
- package/dist/chunk-LD4GC7OR.js.map +0 -1
- package/dist/chunk-MBCN3MNX.js +0 -704
- package/dist/chunk-MBCN3MNX.js.map +0 -1
- package/dist/chunk-MHN7CJCZ.mjs.map +0 -1
- package/dist/chunk-PAJTKWEC.mjs.map +0 -1
- package/dist/chunk-V4ZO3F2S.js.map +0 -1
- package/dist/chunk-X4FFNQHE.mjs.map +0 -1
- package/dist/chunk-Z3OQU4XW.mjs.map +0 -1
- /package/dist/{chunk-XHMM35YT.mjs.map → chunk-A2DDWXMS.mjs.map} +0 -0
- /package/dist/{chunk-5F5EQAX5.js.map → chunk-JUI3XNEF.js.map} +0 -0
package/dist/chunk-FMZML3V5.js
DELETED
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/NftDetectionController.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var _controllerutils = require('@metamask/controller-utils');
|
|
8
|
-
var _pollingcontroller = require('@metamask/polling-controller');
|
|
9
|
-
var DEFAULT_INTERVAL = 18e4;
|
|
10
|
-
var BlockaidResultType = /* @__PURE__ */ ((BlockaidResultType2) => {
|
|
11
|
-
BlockaidResultType2["Benign"] = "Benign";
|
|
12
|
-
BlockaidResultType2["Spam"] = "Spam";
|
|
13
|
-
BlockaidResultType2["Warning"] = "Warning";
|
|
14
|
-
BlockaidResultType2["Malicious"] = "Malicious";
|
|
15
|
-
return BlockaidResultType2;
|
|
16
|
-
})(BlockaidResultType || {});
|
|
17
|
-
var NftDetectionController = class extends _pollingcontroller.StaticIntervalPollingControllerV1 {
|
|
18
|
-
/**
|
|
19
|
-
* Creates an NftDetectionController instance.
|
|
20
|
-
*
|
|
21
|
-
* @param options - The controller options.
|
|
22
|
-
* @param options.chainId - The chain ID of the current network.
|
|
23
|
-
* @param options.onNftsStateChange - Allows subscribing to assets controller state changes.
|
|
24
|
-
* @param options.onPreferencesStateChange - Allows subscribing to preferences controller state changes.
|
|
25
|
-
* @param options.onNetworkStateChange - Allows subscribing to network controller state changes.
|
|
26
|
-
* @param options.getOpenSeaApiKey - Gets the OpenSea API key, if one is set.
|
|
27
|
-
* @param options.addNft - Add an NFT.
|
|
28
|
-
* @param options.getNftApi - Gets the URL to fetch an NFT from OpenSea.
|
|
29
|
-
* @param options.getNftState - Gets the current state of the Assets controller.
|
|
30
|
-
* @param options.disabled - Represents previous value of useNftDetection. Used to detect changes of useNftDetection. Default value is true.
|
|
31
|
-
* @param options.selectedAddress - Represents current selected address.
|
|
32
|
-
* @param options.getNetworkClientById - Gets the network client by ID, from the NetworkController.
|
|
33
|
-
* @param config - Initial options used to configure this controller.
|
|
34
|
-
* @param state - Initial state to set on this controller.
|
|
35
|
-
*/
|
|
36
|
-
constructor({
|
|
37
|
-
chainId: initialChainId,
|
|
38
|
-
getNetworkClientById,
|
|
39
|
-
onPreferencesStateChange,
|
|
40
|
-
onNetworkStateChange,
|
|
41
|
-
getOpenSeaApiKey,
|
|
42
|
-
addNft,
|
|
43
|
-
getNftApi,
|
|
44
|
-
getNftState,
|
|
45
|
-
disabled: initialDisabled,
|
|
46
|
-
selectedAddress: initialSelectedAddress
|
|
47
|
-
}, config, state) {
|
|
48
|
-
super(config, state);
|
|
49
|
-
/**
|
|
50
|
-
* Name of this controller used during composition
|
|
51
|
-
*/
|
|
52
|
-
this.name = "NftDetectionController";
|
|
53
|
-
/**
|
|
54
|
-
* Checks whether network is mainnet or not.
|
|
55
|
-
*
|
|
56
|
-
* @returns Whether current network is mainnet.
|
|
57
|
-
*/
|
|
58
|
-
this.isMainnet = () => this.config.chainId === _controllerutils.ChainId.mainnet;
|
|
59
|
-
this.isMainnetByNetworkClientId = (networkClient) => {
|
|
60
|
-
return networkClient.configuration.chainId === _controllerutils.ChainId.mainnet;
|
|
61
|
-
};
|
|
62
|
-
this.defaultConfig = {
|
|
63
|
-
interval: DEFAULT_INTERVAL,
|
|
64
|
-
chainId: initialChainId,
|
|
65
|
-
selectedAddress: initialSelectedAddress,
|
|
66
|
-
disabled: initialDisabled
|
|
67
|
-
};
|
|
68
|
-
this.initialize();
|
|
69
|
-
this.getNftState = getNftState;
|
|
70
|
-
this.getNetworkClientById = getNetworkClientById;
|
|
71
|
-
onPreferencesStateChange(({ selectedAddress, useNftDetection }) => {
|
|
72
|
-
const { selectedAddress: previouslySelectedAddress, disabled } = this.config;
|
|
73
|
-
if (selectedAddress !== previouslySelectedAddress || !useNftDetection !== disabled) {
|
|
74
|
-
this.configure({ selectedAddress, disabled: !useNftDetection });
|
|
75
|
-
if (useNftDetection) {
|
|
76
|
-
this.start();
|
|
77
|
-
} else {
|
|
78
|
-
this.stop();
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
onNetworkStateChange(({ selectedNetworkClientId }) => {
|
|
83
|
-
const selectedNetworkClient = getNetworkClientById(
|
|
84
|
-
selectedNetworkClientId
|
|
85
|
-
);
|
|
86
|
-
const { chainId } = selectedNetworkClient.configuration;
|
|
87
|
-
this.configure({ chainId });
|
|
88
|
-
});
|
|
89
|
-
this.getOpenSeaApiKey = getOpenSeaApiKey;
|
|
90
|
-
this.addNft = addNft;
|
|
91
|
-
this.getNftApi = getNftApi;
|
|
92
|
-
this.setIntervalLength(this.config.interval);
|
|
93
|
-
}
|
|
94
|
-
getOwnerNftApi({
|
|
95
|
-
address,
|
|
96
|
-
next
|
|
97
|
-
}) {
|
|
98
|
-
return `${_controllerutils.NFT_API_BASE_URL}/users/${address}/tokens?chainIds=1&limit=50&includeTopBid=true&continuation=${next ?? ""}`;
|
|
99
|
-
}
|
|
100
|
-
async getOwnerNfts(address) {
|
|
101
|
-
let nftApiResponse;
|
|
102
|
-
let nfts = [];
|
|
103
|
-
let next;
|
|
104
|
-
do {
|
|
105
|
-
nftApiResponse = await _controllerutils.fetchWithErrorHandling.call(void 0, {
|
|
106
|
-
url: this.getOwnerNftApi({ address, next }),
|
|
107
|
-
options: {
|
|
108
|
-
headers: {
|
|
109
|
-
Version: "1"
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
|
-
timeout: 15e3
|
|
113
|
-
});
|
|
114
|
-
if (!nftApiResponse) {
|
|
115
|
-
return nfts;
|
|
116
|
-
}
|
|
117
|
-
const newNfts = nftApiResponse.tokens.filter(
|
|
118
|
-
(elm) => elm.token.isSpam === false && (elm.blockaidResult?.result_type ? elm.blockaidResult?.result_type === "Benign" /* Benign */ : true)
|
|
119
|
-
);
|
|
120
|
-
nfts = [...nfts, ...newNfts];
|
|
121
|
-
} while (next = nftApiResponse.continuation);
|
|
122
|
-
return nfts;
|
|
123
|
-
}
|
|
124
|
-
async _executePoll(networkClientId, options) {
|
|
125
|
-
await this.detectNfts({ networkClientId, userAddress: options.address });
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Start polling for the currency rate.
|
|
129
|
-
*/
|
|
130
|
-
async start() {
|
|
131
|
-
if (!this.isMainnet() || this.disabled) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
await this.startPolling();
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Stop polling for the currency rate.
|
|
138
|
-
*/
|
|
139
|
-
stop() {
|
|
140
|
-
this.stopPolling();
|
|
141
|
-
}
|
|
142
|
-
stopPolling() {
|
|
143
|
-
if (this.intervalId) {
|
|
144
|
-
clearInterval(this.intervalId);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Starts a new polling interval.
|
|
149
|
-
*
|
|
150
|
-
* @param interval - An interval on which to poll.
|
|
151
|
-
*/
|
|
152
|
-
async startPolling(interval) {
|
|
153
|
-
interval && this.configure({ interval }, false, false);
|
|
154
|
-
this.stopPolling();
|
|
155
|
-
await this.detectNfts();
|
|
156
|
-
this.intervalId = setInterval(async () => {
|
|
157
|
-
await this.detectNfts();
|
|
158
|
-
}, this.config.interval);
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Triggers asset ERC721 token auto detection on mainnet. Any newly detected NFTs are
|
|
162
|
-
* added.
|
|
163
|
-
*
|
|
164
|
-
* @param options - Options bag.
|
|
165
|
-
* @param options.networkClientId - The network client ID to detect NFTs on.
|
|
166
|
-
* @param options.userAddress - The address to detect NFTs for.
|
|
167
|
-
*/
|
|
168
|
-
async detectNfts({
|
|
169
|
-
networkClientId,
|
|
170
|
-
userAddress
|
|
171
|
-
} = { userAddress: this.config.selectedAddress }) {
|
|
172
|
-
if (!this.isMainnet() || this.disabled) {
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
if (!userAddress) {
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
const apiNfts = await this.getOwnerNfts(userAddress);
|
|
179
|
-
const addNftPromises = apiNfts.map(async (nft) => {
|
|
180
|
-
const {
|
|
181
|
-
tokenId: token_id,
|
|
182
|
-
contract,
|
|
183
|
-
kind,
|
|
184
|
-
image: image_url,
|
|
185
|
-
imageSmall: image_thumbnail_url,
|
|
186
|
-
metadata: { imageOriginal: image_original_url } = {},
|
|
187
|
-
name,
|
|
188
|
-
description,
|
|
189
|
-
attributes,
|
|
190
|
-
topBid,
|
|
191
|
-
lastSale,
|
|
192
|
-
rarityRank,
|
|
193
|
-
rarityScore,
|
|
194
|
-
collection
|
|
195
|
-
} = nft.token;
|
|
196
|
-
let ignored;
|
|
197
|
-
const { ignoredNfts } = this.getNftState();
|
|
198
|
-
if (ignoredNfts.length) {
|
|
199
|
-
ignored = ignoredNfts.find((c) => {
|
|
200
|
-
return c.address === _controllerutils.toChecksumHexAddress.call(void 0, contract) && c.tokenId === token_id;
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
if (!ignored) {
|
|
204
|
-
const nftMetadata = Object.assign(
|
|
205
|
-
{},
|
|
206
|
-
{ name },
|
|
207
|
-
description && { description },
|
|
208
|
-
image_url && { image: image_url },
|
|
209
|
-
image_thumbnail_url && { imageThumbnail: image_thumbnail_url },
|
|
210
|
-
image_original_url && { imageOriginal: image_original_url },
|
|
211
|
-
kind && { standard: kind.toUpperCase() },
|
|
212
|
-
lastSale && { lastSale },
|
|
213
|
-
attributes && { attributes },
|
|
214
|
-
topBid && { topBid },
|
|
215
|
-
rarityRank && { rarityRank },
|
|
216
|
-
rarityScore && { rarityScore },
|
|
217
|
-
collection && { collection }
|
|
218
|
-
);
|
|
219
|
-
await this.addNft(contract, token_id, {
|
|
220
|
-
nftMetadata,
|
|
221
|
-
userAddress,
|
|
222
|
-
source: "detected" /* Detected */,
|
|
223
|
-
networkClientId
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
|
-
await Promise.all(addNftPromises);
|
|
228
|
-
}
|
|
229
|
-
};
|
|
230
|
-
var NftDetectionController_default = NftDetectionController;
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
exports.BlockaidResultType = BlockaidResultType; exports.NftDetectionController = NftDetectionController; exports.NftDetectionController_default = NftDetectionController_default;
|
|
237
|
-
//# sourceMappingURL=chunk-FMZML3V5.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/NftDetectionController.ts"],"names":["Blockaid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AAY,qBAAqB,QAAQ,KAC3C,EAAE,YAAY;AAAA,QAElB,CAAC;AAAA,MACH;AAGA,UAAI,CAAC,SAAS;AAEZ,cAAM,cAA2B,OAAO;AAAA,UACtC,CAAC;AAAA,UACD,EAAE,KAAK;AAAA,UACP,eAAe,EAAE,YAAY;AAAA,UAC7B,aAAa,EAAE,OAAO,UAAU;AAAA,UAChC,uBAAuB,EAAE,gBAAgB,oBAAoB;AAAA,UAC7D,sBAAsB,EAAE,eAAe,mBAAmB;AAAA,UAC1D,QAAQ,EAAE,UAAU,KAAK,YAAY,EAAE;AAAA,UACvC,YAAY,EAAE,SAAS;AAAA,UACvB,cAAc,EAAE,WAAW;AAAA,UAC3B,UAAU,EAAE,OAAO;AAAA,UACnB,cAAc,EAAE,WAAW;AAAA,UAC3B,eAAe,EAAE,YAAY;AAAA,UAC7B,cAAc,EAAE,WAAW;AAAA,QAC7B;AAEA,cAAM,KAAK,OAAO,UAAU,UAAU;AAAA,UACpC;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AACD,UAAM,QAAQ,IAAI,cAAc;AAAA,EAClC;AACF;AAEA,IAAO,iCAAQ","sourcesContent":["import type { BaseConfig, BaseState } from '@metamask/base-controller';\nimport {\n fetchWithErrorHandling,\n toChecksumHexAddress,\n ChainId,\n NFT_API_BASE_URL,\n} from '@metamask/controller-utils';\nimport type {\n NetworkClientId,\n NetworkController,\n NetworkState,\n NetworkClient,\n} from '@metamask/network-controller';\nimport { StaticIntervalPollingControllerV1 } from '@metamask/polling-controller';\nimport type { PreferencesState } from '@metamask/preferences-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport { Source } from './constants';\nimport {\n type NftController,\n type NftState,\n type NftMetadata,\n} from './NftController';\n\nconst DEFAULT_INTERVAL = 180000;\n\n/**\n * @type ApiNft\n *\n * NFT object coming from OpenSea api\n * @property token_id - The NFT identifier\n * @property num_sales - Number of sales\n * @property background_color - The background color to be displayed with the item\n * @property image_url - URI of an image associated with this NFT\n * @property image_preview_url - URI of a smaller image associated with this NFT\n * @property image_thumbnail_url - URI of a thumbnail image associated with this NFT\n * @property image_original_url - URI of the original image associated with this NFT\n * @property animation_url - URI of a animation associated with this NFT\n * @property animation_original_url - URI of the original animation associated with this NFT\n * @property name - The NFT name\n * @property description - The NFT description\n * @property external_link - External link containing additional information\n * @property assetContract - The NFT contract information object\n * @property creator - The NFT owner information object\n * @property lastSale - When this item was last sold\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNft {\n token_id: string;\n num_sales: number | null;\n background_color: string | null;\n image_url: string | null;\n image_preview_url: string | null;\n image_thumbnail_url: string | null;\n image_original_url: string | null;\n animation_url: string | null;\n animation_original_url: string | null;\n name: string | null;\n description: string | null;\n external_link: string | null;\n asset_contract: ApiNftContract;\n creator: ApiNftCreator;\n last_sale: ApiNftLastSale | null;\n}\n\n/**\n * @type ApiNftContract\n *\n * NFT contract object coming from OpenSea api\n * @property address - Address of the NFT contract\n * @property asset_contract_type - The NFT type, it could be `semi-fungible` or `non-fungible`\n * @property created_date - Creation date\n * @property collection - Object containing the contract name and URI of an image associated\n * @property schema_name - The schema followed by the contract, it could be `ERC721` or `ERC1155`\n * @property symbol - The NFT contract symbol\n * @property total_supply - Total supply of NFTs\n * @property description - The NFT contract description\n * @property external_link - External link containing additional information\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNftContract {\n address: string;\n asset_contract_type: string | null;\n created_date: string | null;\n schema_name: string | null;\n symbol: string | null;\n total_supply: string | null;\n description: string | null;\n external_link: string | null;\n collection: {\n name: string | null;\n image_url?: string | null;\n tokenCount?: string | null;\n };\n}\n\n/**\n * @type ApiNftLastSale\n *\n * NFT sale object coming from OpenSea api\n * @property event_timestamp - Object containing a `username`\n * @property total_price - URI of NFT image associated with this owner\n * @property transaction - Object containing transaction_hash and block_hash\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNftLastSale {\n event_timestamp: string;\n total_price: string;\n transaction: { transaction_hash: string; block_hash: string };\n}\n\n/**\n * @type ApiNftCreator\n *\n * NFT creator object coming from OpenSea api\n * @property user - Object containing a `username`\n * @property profile_img_url - URI of NFT image associated with this owner\n * @property address - The owner address\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNftCreator {\n user: { username: string };\n profile_img_url: string;\n address: string;\n}\n\n/**\n * @type NftDetectionConfig\n *\n * NftDetection configuration\n * @property interval - Polling interval used to fetch new token rates\n * @property chainId - Current chain ID\n * @property selectedAddress - Vault selected address\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface NftDetectionConfig extends BaseConfig {\n interval: number;\n chainId: Hex;\n selectedAddress: string;\n}\n\nexport type ReservoirResponse = {\n tokens: TokensResponse[];\n continuation?: string;\n};\n\nexport type TokensResponse = {\n token: TokenResponse;\n ownership: Ownership;\n market?: Market;\n blockaidResult?: Blockaid;\n};\n\nexport enum BlockaidResultType {\n Benign = 'Benign',\n Spam = 'Spam',\n Warning = 'Warning',\n Malicious = 'Malicious',\n}\n\nexport type Blockaid = {\n contract: string;\n chainId: number;\n result_type: BlockaidResultType;\n malicious_score: string;\n attack_types: object;\n};\n\nexport type Market = {\n floorAsk?: FloorAsk;\n topBid?: TopBid;\n};\n\nexport type TokenResponse = {\n chainId: number;\n contract: string;\n tokenId: string;\n kind?: string;\n name?: string;\n image?: string;\n imageSmall?: string;\n imageLarge?: string;\n metadata?: Metadata;\n description?: string;\n supply?: number;\n remainingSupply?: number;\n rarityScore?: number;\n rarity?: number;\n rarityRank?: number;\n media?: string;\n isFlagged?: boolean;\n isSpam?: boolean;\n isNsfw?: boolean;\n metadataDisabled?: boolean;\n lastFlagUpdate?: string;\n lastFlagChange?: string;\n collection?: Collection;\n lastSale?: LastSale;\n topBid?: TopBid;\n lastAppraisalValue?: number;\n attributes?: Attributes[];\n};\n\nexport type TopBid = {\n id?: string;\n price?: Price;\n source?: {\n id?: string;\n domain?: string;\n name?: string;\n icon?: string;\n url?: string;\n };\n};\n\nexport type LastSale = {\n saleId?: string;\n token?: {\n contract?: string;\n tokenId?: string;\n name?: string;\n image?: string;\n collection?: {\n id?: string;\n name?: string;\n };\n };\n orderSource?: string;\n orderSide?: 'ask' | 'bid';\n orderKind?: string;\n orderId?: string;\n from?: string;\n to?: string;\n amount?: string;\n fillSource?: string;\n block?: number;\n txHash?: string;\n logIndex?: number;\n batchIndex?: number;\n timestamp?: number;\n price?: Price;\n washTradingScore?: number;\n royaltyFeeBps?: number;\n marketplaceFeeBps?: number;\n paidFullRoyalty?: boolean;\n feeBreakdown?: FeeBreakdown[];\n isDeleted?: boolean;\n createdAt?: string;\n updatedAt?: string;\n};\n\nexport type FeeBreakdown = {\n kind?: string;\n bps?: number;\n recipient?: string;\n source?: string;\n rawAmount?: string;\n};\n\nexport type Attributes = {\n key?: string;\n kind?: string;\n value: string;\n tokenCount?: number;\n onSaleCount?: number;\n floorAskPrice?: Price | null;\n topBidValue?: number | null;\n createdAt?: string;\n};\n\nexport type Collection = {\n id?: string;\n name?: string;\n slug?: string;\n symbol?: string;\n imageUrl?: string;\n image?: string;\n isSpam?: boolean;\n isNsfw?: boolean;\n creator?: string;\n tokenCount?: string;\n metadataDisabled?: boolean;\n openseaVerificationStatus?: string;\n floorAskPrice?: Price;\n royaltiesBps?: number;\n royalties?: Royalties[];\n};\n\nexport type Royalties = {\n bps?: number;\n recipient?: string;\n};\n\nexport type Ownership = {\n tokenCount?: string;\n onSaleCount?: string;\n floorAsk?: FloorAsk;\n acquiredAt?: string;\n};\n\nexport type FloorAsk = {\n id?: string;\n price?: Price;\n maker?: string;\n kind?: string;\n validFrom?: number;\n validUntil?: number;\n source?: Source;\n rawData?: Metadata;\n isNativeOffChainCancellable?: boolean;\n};\n\nexport type Price = {\n currency?: {\n contract?: string;\n name?: string;\n symbol?: string;\n decimals?: number;\n chainId?: number;\n };\n amount?: {\n raw?: string;\n decimal?: number;\n usd?: number;\n native?: number;\n };\n netAmount?: {\n raw?: string;\n decimal?: number;\n usd?: number;\n native?: number;\n };\n};\n\nexport type Metadata = {\n imageOriginal?: string;\n tokenURI?: string;\n};\n\n/**\n * Controller that passively polls on a set interval for NFT auto detection\n */\nexport class NftDetectionController extends StaticIntervalPollingControllerV1<\n NftDetectionConfig,\n BaseState\n> {\n private intervalId?: ReturnType<typeof setTimeout>;\n\n private getOwnerNftApi({\n address,\n next,\n }: {\n address: string;\n next?: string;\n }) {\n return `${NFT_API_BASE_URL}/users/${address}/tokens?chainIds=1&limit=50&includeTopBid=true&continuation=${\n next ?? ''\n }`;\n }\n\n private async getOwnerNfts(address: string) {\n let nftApiResponse: ReservoirResponse;\n let nfts: TokensResponse[] = [];\n let next;\n\n do {\n nftApiResponse = await fetchWithErrorHandling({\n url: this.getOwnerNftApi({ address, next }),\n options: {\n headers: {\n Version: '1',\n },\n },\n timeout: 15000,\n });\n\n if (!nftApiResponse) {\n return nfts;\n }\n\n const newNfts = nftApiResponse.tokens.filter(\n (elm) =>\n elm.token.isSpam === false &&\n (elm.blockaidResult?.result_type\n ? elm.blockaidResult?.result_type === BlockaidResultType.Benign\n : true),\n );\n\n nfts = [...nfts, ...newNfts];\n } while ((next = nftApiResponse.continuation));\n\n return nfts;\n }\n\n /**\n * Name of this controller used during composition\n */\n override name = 'NftDetectionController';\n\n private readonly getOpenSeaApiKey: () => string | undefined;\n\n private readonly addNft: NftController['addNft'];\n\n private readonly getNftApi: NftController['getNftApi'];\n\n private readonly getNftState: () => NftState;\n\n private readonly getNetworkClientById: NetworkController['getNetworkClientById'];\n\n /**\n * Creates an NftDetectionController instance.\n *\n * @param options - The controller options.\n * @param options.chainId - The chain ID of the current network.\n * @param options.onNftsStateChange - Allows subscribing to assets controller state changes.\n * @param options.onPreferencesStateChange - Allows subscribing to preferences controller state changes.\n * @param options.onNetworkStateChange - Allows subscribing to network controller state changes.\n * @param options.getOpenSeaApiKey - Gets the OpenSea API key, if one is set.\n * @param options.addNft - Add an NFT.\n * @param options.getNftApi - Gets the URL to fetch an NFT from OpenSea.\n * @param options.getNftState - Gets the current state of the Assets controller.\n * @param options.disabled - Represents previous value of useNftDetection. Used to detect changes of useNftDetection. Default value is true.\n * @param options.selectedAddress - Represents current selected address.\n * @param options.getNetworkClientById - Gets the network client by ID, from the NetworkController.\n * @param config - Initial options used to configure this controller.\n * @param state - Initial state to set on this controller.\n */\n constructor(\n {\n chainId: initialChainId,\n getNetworkClientById,\n onPreferencesStateChange,\n onNetworkStateChange,\n getOpenSeaApiKey,\n addNft,\n getNftApi,\n getNftState,\n disabled: initialDisabled,\n selectedAddress: initialSelectedAddress,\n }: {\n chainId: Hex;\n getNetworkClientById: NetworkController['getNetworkClientById'];\n onNftsStateChange: (listener: (nftsState: NftState) => void) => void;\n onPreferencesStateChange: (\n listener: (preferencesState: PreferencesState) => void,\n ) => void;\n onNetworkStateChange: (\n listener: (networkState: NetworkState) => void,\n ) => void;\n getOpenSeaApiKey: () => string | undefined;\n addNft: NftController['addNft'];\n getNftApi: NftController['getNftApi'];\n getNftState: () => NftState;\n disabled: boolean;\n selectedAddress: string;\n },\n config?: Partial<NftDetectionConfig>,\n state?: Partial<BaseState>,\n ) {\n super(config, state);\n this.defaultConfig = {\n interval: DEFAULT_INTERVAL,\n chainId: initialChainId,\n selectedAddress: initialSelectedAddress,\n disabled: initialDisabled,\n };\n this.initialize();\n this.getNftState = getNftState;\n this.getNetworkClientById = getNetworkClientById;\n onPreferencesStateChange(({ selectedAddress, useNftDetection }) => {\n const { selectedAddress: previouslySelectedAddress, disabled } =\n this.config;\n\n if (\n selectedAddress !== previouslySelectedAddress ||\n !useNftDetection !== disabled\n ) {\n this.configure({ selectedAddress, disabled: !useNftDetection });\n if (useNftDetection) {\n this.start();\n } else {\n this.stop();\n }\n }\n });\n\n onNetworkStateChange(({ selectedNetworkClientId }) => {\n const selectedNetworkClient = getNetworkClientById(\n selectedNetworkClientId,\n );\n const { chainId } = selectedNetworkClient.configuration;\n\n this.configure({ chainId });\n });\n this.getOpenSeaApiKey = getOpenSeaApiKey;\n this.addNft = addNft;\n this.getNftApi = getNftApi;\n this.setIntervalLength(this.config.interval);\n }\n\n async _executePoll(\n networkClientId: string,\n options: { address: string },\n ): Promise<void> {\n await this.detectNfts({ networkClientId, userAddress: options.address });\n }\n\n /**\n * Start polling for the currency rate.\n */\n async start() {\n if (!this.isMainnet() || this.disabled) {\n return;\n }\n\n await this.startPolling();\n }\n\n /**\n * Stop polling for the currency rate.\n */\n stop() {\n this.stopPolling();\n }\n\n private stopPolling() {\n if (this.intervalId) {\n clearInterval(this.intervalId);\n }\n }\n\n /**\n * Starts a new polling interval.\n *\n * @param interval - An interval on which to poll.\n */\n private async startPolling(interval?: number): Promise<void> {\n interval && this.configure({ interval }, false, false);\n this.stopPolling();\n await this.detectNfts();\n this.intervalId = setInterval(async () => {\n await this.detectNfts();\n }, this.config.interval);\n }\n\n /**\n * Checks whether network is mainnet or not.\n *\n * @returns Whether current network is mainnet.\n */\n isMainnet = (): boolean => this.config.chainId === ChainId.mainnet;\n\n isMainnetByNetworkClientId = (networkClient: NetworkClient): boolean => {\n return networkClient.configuration.chainId === ChainId.mainnet;\n };\n\n /**\n * Triggers asset ERC721 token auto detection on mainnet. Any newly detected NFTs are\n * added.\n *\n * @param options - Options bag.\n * @param options.networkClientId - The network client ID to detect NFTs on.\n * @param options.userAddress - The address to detect NFTs for.\n */\n async detectNfts(\n {\n networkClientId,\n userAddress,\n }: {\n networkClientId?: NetworkClientId;\n userAddress: string;\n } = { userAddress: this.config.selectedAddress },\n ) {\n /* istanbul ignore if */\n if (!this.isMainnet() || this.disabled) {\n return;\n }\n /* istanbul ignore else */\n if (!userAddress) {\n return;\n }\n\n const apiNfts = await this.getOwnerNfts(userAddress);\n const addNftPromises = apiNfts.map(async (nft) => {\n const {\n tokenId: token_id,\n contract,\n kind,\n image: image_url,\n imageSmall: image_thumbnail_url,\n metadata: { imageOriginal: image_original_url } = {},\n name,\n description,\n attributes,\n topBid,\n lastSale,\n rarityRank,\n rarityScore,\n collection,\n } = nft.token;\n\n let ignored;\n /* istanbul ignore else */\n const { ignoredNfts } = this.getNftState();\n if (ignoredNfts.length) {\n ignored = ignoredNfts.find((c) => {\n /* istanbul ignore next */\n return (\n c.address === toChecksumHexAddress(contract) &&\n c.tokenId === token_id\n );\n });\n }\n\n /* istanbul ignore else */\n if (!ignored) {\n /* istanbul ignore next */\n const nftMetadata: NftMetadata = Object.assign(\n {},\n { name },\n description && { description },\n image_url && { image: image_url },\n image_thumbnail_url && { imageThumbnail: image_thumbnail_url },\n image_original_url && { imageOriginal: image_original_url },\n kind && { standard: kind.toUpperCase() },\n lastSale && { lastSale },\n attributes && { attributes },\n topBid && { topBid },\n rarityRank && { rarityRank },\n rarityScore && { rarityScore },\n collection && { collection },\n );\n\n await this.addNft(contract, token_id, {\n nftMetadata,\n userAddress,\n source: Source.Detected,\n networkClientId,\n });\n }\n });\n await Promise.all(addNftPromises);\n }\n}\n\nexport default NftDetectionController;\n"]}
|
package/dist/chunk-KOKB6U4Z.mjs
DELETED
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
// src/NftDetectionController.ts
|
|
2
|
-
import {
|
|
3
|
-
fetchWithErrorHandling,
|
|
4
|
-
toChecksumHexAddress,
|
|
5
|
-
ChainId,
|
|
6
|
-
NFT_API_BASE_URL
|
|
7
|
-
} from "@metamask/controller-utils";
|
|
8
|
-
import { StaticIntervalPollingControllerV1 } from "@metamask/polling-controller";
|
|
9
|
-
var DEFAULT_INTERVAL = 18e4;
|
|
10
|
-
var BlockaidResultType = /* @__PURE__ */ ((BlockaidResultType2) => {
|
|
11
|
-
BlockaidResultType2["Benign"] = "Benign";
|
|
12
|
-
BlockaidResultType2["Spam"] = "Spam";
|
|
13
|
-
BlockaidResultType2["Warning"] = "Warning";
|
|
14
|
-
BlockaidResultType2["Malicious"] = "Malicious";
|
|
15
|
-
return BlockaidResultType2;
|
|
16
|
-
})(BlockaidResultType || {});
|
|
17
|
-
var NftDetectionController = class extends StaticIntervalPollingControllerV1 {
|
|
18
|
-
/**
|
|
19
|
-
* Creates an NftDetectionController instance.
|
|
20
|
-
*
|
|
21
|
-
* @param options - The controller options.
|
|
22
|
-
* @param options.chainId - The chain ID of the current network.
|
|
23
|
-
* @param options.onNftsStateChange - Allows subscribing to assets controller state changes.
|
|
24
|
-
* @param options.onPreferencesStateChange - Allows subscribing to preferences controller state changes.
|
|
25
|
-
* @param options.onNetworkStateChange - Allows subscribing to network controller state changes.
|
|
26
|
-
* @param options.getOpenSeaApiKey - Gets the OpenSea API key, if one is set.
|
|
27
|
-
* @param options.addNft - Add an NFT.
|
|
28
|
-
* @param options.getNftApi - Gets the URL to fetch an NFT from OpenSea.
|
|
29
|
-
* @param options.getNftState - Gets the current state of the Assets controller.
|
|
30
|
-
* @param options.disabled - Represents previous value of useNftDetection. Used to detect changes of useNftDetection. Default value is true.
|
|
31
|
-
* @param options.selectedAddress - Represents current selected address.
|
|
32
|
-
* @param options.getNetworkClientById - Gets the network client by ID, from the NetworkController.
|
|
33
|
-
* @param config - Initial options used to configure this controller.
|
|
34
|
-
* @param state - Initial state to set on this controller.
|
|
35
|
-
*/
|
|
36
|
-
constructor({
|
|
37
|
-
chainId: initialChainId,
|
|
38
|
-
getNetworkClientById,
|
|
39
|
-
onPreferencesStateChange,
|
|
40
|
-
onNetworkStateChange,
|
|
41
|
-
getOpenSeaApiKey,
|
|
42
|
-
addNft,
|
|
43
|
-
getNftApi,
|
|
44
|
-
getNftState,
|
|
45
|
-
disabled: initialDisabled,
|
|
46
|
-
selectedAddress: initialSelectedAddress
|
|
47
|
-
}, config, state) {
|
|
48
|
-
super(config, state);
|
|
49
|
-
/**
|
|
50
|
-
* Name of this controller used during composition
|
|
51
|
-
*/
|
|
52
|
-
this.name = "NftDetectionController";
|
|
53
|
-
/**
|
|
54
|
-
* Checks whether network is mainnet or not.
|
|
55
|
-
*
|
|
56
|
-
* @returns Whether current network is mainnet.
|
|
57
|
-
*/
|
|
58
|
-
this.isMainnet = () => this.config.chainId === ChainId.mainnet;
|
|
59
|
-
this.isMainnetByNetworkClientId = (networkClient) => {
|
|
60
|
-
return networkClient.configuration.chainId === ChainId.mainnet;
|
|
61
|
-
};
|
|
62
|
-
this.defaultConfig = {
|
|
63
|
-
interval: DEFAULT_INTERVAL,
|
|
64
|
-
chainId: initialChainId,
|
|
65
|
-
selectedAddress: initialSelectedAddress,
|
|
66
|
-
disabled: initialDisabled
|
|
67
|
-
};
|
|
68
|
-
this.initialize();
|
|
69
|
-
this.getNftState = getNftState;
|
|
70
|
-
this.getNetworkClientById = getNetworkClientById;
|
|
71
|
-
onPreferencesStateChange(({ selectedAddress, useNftDetection }) => {
|
|
72
|
-
const { selectedAddress: previouslySelectedAddress, disabled } = this.config;
|
|
73
|
-
if (selectedAddress !== previouslySelectedAddress || !useNftDetection !== disabled) {
|
|
74
|
-
this.configure({ selectedAddress, disabled: !useNftDetection });
|
|
75
|
-
if (useNftDetection) {
|
|
76
|
-
this.start();
|
|
77
|
-
} else {
|
|
78
|
-
this.stop();
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
onNetworkStateChange(({ selectedNetworkClientId }) => {
|
|
83
|
-
const selectedNetworkClient = getNetworkClientById(
|
|
84
|
-
selectedNetworkClientId
|
|
85
|
-
);
|
|
86
|
-
const { chainId } = selectedNetworkClient.configuration;
|
|
87
|
-
this.configure({ chainId });
|
|
88
|
-
});
|
|
89
|
-
this.getOpenSeaApiKey = getOpenSeaApiKey;
|
|
90
|
-
this.addNft = addNft;
|
|
91
|
-
this.getNftApi = getNftApi;
|
|
92
|
-
this.setIntervalLength(this.config.interval);
|
|
93
|
-
}
|
|
94
|
-
getOwnerNftApi({
|
|
95
|
-
address,
|
|
96
|
-
next
|
|
97
|
-
}) {
|
|
98
|
-
return `${NFT_API_BASE_URL}/users/${address}/tokens?chainIds=1&limit=50&includeTopBid=true&continuation=${next ?? ""}`;
|
|
99
|
-
}
|
|
100
|
-
async getOwnerNfts(address) {
|
|
101
|
-
let nftApiResponse;
|
|
102
|
-
let nfts = [];
|
|
103
|
-
let next;
|
|
104
|
-
do {
|
|
105
|
-
nftApiResponse = await fetchWithErrorHandling({
|
|
106
|
-
url: this.getOwnerNftApi({ address, next }),
|
|
107
|
-
options: {
|
|
108
|
-
headers: {
|
|
109
|
-
Version: "1"
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
|
-
timeout: 15e3
|
|
113
|
-
});
|
|
114
|
-
if (!nftApiResponse) {
|
|
115
|
-
return nfts;
|
|
116
|
-
}
|
|
117
|
-
const newNfts = nftApiResponse.tokens.filter(
|
|
118
|
-
(elm) => elm.token.isSpam === false && (elm.blockaidResult?.result_type ? elm.blockaidResult?.result_type === "Benign" /* Benign */ : true)
|
|
119
|
-
);
|
|
120
|
-
nfts = [...nfts, ...newNfts];
|
|
121
|
-
} while (next = nftApiResponse.continuation);
|
|
122
|
-
return nfts;
|
|
123
|
-
}
|
|
124
|
-
async _executePoll(networkClientId, options) {
|
|
125
|
-
await this.detectNfts({ networkClientId, userAddress: options.address });
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Start polling for the currency rate.
|
|
129
|
-
*/
|
|
130
|
-
async start() {
|
|
131
|
-
if (!this.isMainnet() || this.disabled) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
await this.startPolling();
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Stop polling for the currency rate.
|
|
138
|
-
*/
|
|
139
|
-
stop() {
|
|
140
|
-
this.stopPolling();
|
|
141
|
-
}
|
|
142
|
-
stopPolling() {
|
|
143
|
-
if (this.intervalId) {
|
|
144
|
-
clearInterval(this.intervalId);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Starts a new polling interval.
|
|
149
|
-
*
|
|
150
|
-
* @param interval - An interval on which to poll.
|
|
151
|
-
*/
|
|
152
|
-
async startPolling(interval) {
|
|
153
|
-
interval && this.configure({ interval }, false, false);
|
|
154
|
-
this.stopPolling();
|
|
155
|
-
await this.detectNfts();
|
|
156
|
-
this.intervalId = setInterval(async () => {
|
|
157
|
-
await this.detectNfts();
|
|
158
|
-
}, this.config.interval);
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Triggers asset ERC721 token auto detection on mainnet. Any newly detected NFTs are
|
|
162
|
-
* added.
|
|
163
|
-
*
|
|
164
|
-
* @param options - Options bag.
|
|
165
|
-
* @param options.networkClientId - The network client ID to detect NFTs on.
|
|
166
|
-
* @param options.userAddress - The address to detect NFTs for.
|
|
167
|
-
*/
|
|
168
|
-
async detectNfts({
|
|
169
|
-
networkClientId,
|
|
170
|
-
userAddress
|
|
171
|
-
} = { userAddress: this.config.selectedAddress }) {
|
|
172
|
-
if (!this.isMainnet() || this.disabled) {
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
if (!userAddress) {
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
const apiNfts = await this.getOwnerNfts(userAddress);
|
|
179
|
-
const addNftPromises = apiNfts.map(async (nft) => {
|
|
180
|
-
const {
|
|
181
|
-
tokenId: token_id,
|
|
182
|
-
contract,
|
|
183
|
-
kind,
|
|
184
|
-
image: image_url,
|
|
185
|
-
imageSmall: image_thumbnail_url,
|
|
186
|
-
metadata: { imageOriginal: image_original_url } = {},
|
|
187
|
-
name,
|
|
188
|
-
description,
|
|
189
|
-
attributes,
|
|
190
|
-
topBid,
|
|
191
|
-
lastSale,
|
|
192
|
-
rarityRank,
|
|
193
|
-
rarityScore,
|
|
194
|
-
collection
|
|
195
|
-
} = nft.token;
|
|
196
|
-
let ignored;
|
|
197
|
-
const { ignoredNfts } = this.getNftState();
|
|
198
|
-
if (ignoredNfts.length) {
|
|
199
|
-
ignored = ignoredNfts.find((c) => {
|
|
200
|
-
return c.address === toChecksumHexAddress(contract) && c.tokenId === token_id;
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
if (!ignored) {
|
|
204
|
-
const nftMetadata = Object.assign(
|
|
205
|
-
{},
|
|
206
|
-
{ name },
|
|
207
|
-
description && { description },
|
|
208
|
-
image_url && { image: image_url },
|
|
209
|
-
image_thumbnail_url && { imageThumbnail: image_thumbnail_url },
|
|
210
|
-
image_original_url && { imageOriginal: image_original_url },
|
|
211
|
-
kind && { standard: kind.toUpperCase() },
|
|
212
|
-
lastSale && { lastSale },
|
|
213
|
-
attributes && { attributes },
|
|
214
|
-
topBid && { topBid },
|
|
215
|
-
rarityRank && { rarityRank },
|
|
216
|
-
rarityScore && { rarityScore },
|
|
217
|
-
collection && { collection }
|
|
218
|
-
);
|
|
219
|
-
await this.addNft(contract, token_id, {
|
|
220
|
-
nftMetadata,
|
|
221
|
-
userAddress,
|
|
222
|
-
source: "detected" /* Detected */,
|
|
223
|
-
networkClientId
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
|
-
await Promise.all(addNftPromises);
|
|
228
|
-
}
|
|
229
|
-
};
|
|
230
|
-
var NftDetectionController_default = NftDetectionController;
|
|
231
|
-
|
|
232
|
-
export {
|
|
233
|
-
BlockaidResultType,
|
|
234
|
-
NftDetectionController,
|
|
235
|
-
NftDetectionController_default
|
|
236
|
-
};
|
|
237
|
-
//# sourceMappingURL=chunk-KOKB6U4Z.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/NftDetectionController.ts"],"sourcesContent":["import type { BaseConfig, BaseState } from '@metamask/base-controller';\nimport {\n fetchWithErrorHandling,\n toChecksumHexAddress,\n ChainId,\n NFT_API_BASE_URL,\n} from '@metamask/controller-utils';\nimport type {\n NetworkClientId,\n NetworkController,\n NetworkState,\n NetworkClient,\n} from '@metamask/network-controller';\nimport { StaticIntervalPollingControllerV1 } from '@metamask/polling-controller';\nimport type { PreferencesState } from '@metamask/preferences-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport { Source } from './constants';\nimport {\n type NftController,\n type NftState,\n type NftMetadata,\n} from './NftController';\n\nconst DEFAULT_INTERVAL = 180000;\n\n/**\n * @type ApiNft\n *\n * NFT object coming from OpenSea api\n * @property token_id - The NFT identifier\n * @property num_sales - Number of sales\n * @property background_color - The background color to be displayed with the item\n * @property image_url - URI of an image associated with this NFT\n * @property image_preview_url - URI of a smaller image associated with this NFT\n * @property image_thumbnail_url - URI of a thumbnail image associated with this NFT\n * @property image_original_url - URI of the original image associated with this NFT\n * @property animation_url - URI of a animation associated with this NFT\n * @property animation_original_url - URI of the original animation associated with this NFT\n * @property name - The NFT name\n * @property description - The NFT description\n * @property external_link - External link containing additional information\n * @property assetContract - The NFT contract information object\n * @property creator - The NFT owner information object\n * @property lastSale - When this item was last sold\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNft {\n token_id: string;\n num_sales: number | null;\n background_color: string | null;\n image_url: string | null;\n image_preview_url: string | null;\n image_thumbnail_url: string | null;\n image_original_url: string | null;\n animation_url: string | null;\n animation_original_url: string | null;\n name: string | null;\n description: string | null;\n external_link: string | null;\n asset_contract: ApiNftContract;\n creator: ApiNftCreator;\n last_sale: ApiNftLastSale | null;\n}\n\n/**\n * @type ApiNftContract\n *\n * NFT contract object coming from OpenSea api\n * @property address - Address of the NFT contract\n * @property asset_contract_type - The NFT type, it could be `semi-fungible` or `non-fungible`\n * @property created_date - Creation date\n * @property collection - Object containing the contract name and URI of an image associated\n * @property schema_name - The schema followed by the contract, it could be `ERC721` or `ERC1155`\n * @property symbol - The NFT contract symbol\n * @property total_supply - Total supply of NFTs\n * @property description - The NFT contract description\n * @property external_link - External link containing additional information\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNftContract {\n address: string;\n asset_contract_type: string | null;\n created_date: string | null;\n schema_name: string | null;\n symbol: string | null;\n total_supply: string | null;\n description: string | null;\n external_link: string | null;\n collection: {\n name: string | null;\n image_url?: string | null;\n tokenCount?: string | null;\n };\n}\n\n/**\n * @type ApiNftLastSale\n *\n * NFT sale object coming from OpenSea api\n * @property event_timestamp - Object containing a `username`\n * @property total_price - URI of NFT image associated with this owner\n * @property transaction - Object containing transaction_hash and block_hash\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNftLastSale {\n event_timestamp: string;\n total_price: string;\n transaction: { transaction_hash: string; block_hash: string };\n}\n\n/**\n * @type ApiNftCreator\n *\n * NFT creator object coming from OpenSea api\n * @property user - Object containing a `username`\n * @property profile_img_url - URI of NFT image associated with this owner\n * @property address - The owner address\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface ApiNftCreator {\n user: { username: string };\n profile_img_url: string;\n address: string;\n}\n\n/**\n * @type NftDetectionConfig\n *\n * NftDetection configuration\n * @property interval - Polling interval used to fetch new token rates\n * @property chainId - Current chain ID\n * @property selectedAddress - Vault selected address\n */\n// This interface was created before this ESLint rule was added.\n// Convert to a `type` in a future major version.\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface NftDetectionConfig extends BaseConfig {\n interval: number;\n chainId: Hex;\n selectedAddress: string;\n}\n\nexport type ReservoirResponse = {\n tokens: TokensResponse[];\n continuation?: string;\n};\n\nexport type TokensResponse = {\n token: TokenResponse;\n ownership: Ownership;\n market?: Market;\n blockaidResult?: Blockaid;\n};\n\nexport enum BlockaidResultType {\n Benign = 'Benign',\n Spam = 'Spam',\n Warning = 'Warning',\n Malicious = 'Malicious',\n}\n\nexport type Blockaid = {\n contract: string;\n chainId: number;\n result_type: BlockaidResultType;\n malicious_score: string;\n attack_types: object;\n};\n\nexport type Market = {\n floorAsk?: FloorAsk;\n topBid?: TopBid;\n};\n\nexport type TokenResponse = {\n chainId: number;\n contract: string;\n tokenId: string;\n kind?: string;\n name?: string;\n image?: string;\n imageSmall?: string;\n imageLarge?: string;\n metadata?: Metadata;\n description?: string;\n supply?: number;\n remainingSupply?: number;\n rarityScore?: number;\n rarity?: number;\n rarityRank?: number;\n media?: string;\n isFlagged?: boolean;\n isSpam?: boolean;\n isNsfw?: boolean;\n metadataDisabled?: boolean;\n lastFlagUpdate?: string;\n lastFlagChange?: string;\n collection?: Collection;\n lastSale?: LastSale;\n topBid?: TopBid;\n lastAppraisalValue?: number;\n attributes?: Attributes[];\n};\n\nexport type TopBid = {\n id?: string;\n price?: Price;\n source?: {\n id?: string;\n domain?: string;\n name?: string;\n icon?: string;\n url?: string;\n };\n};\n\nexport type LastSale = {\n saleId?: string;\n token?: {\n contract?: string;\n tokenId?: string;\n name?: string;\n image?: string;\n collection?: {\n id?: string;\n name?: string;\n };\n };\n orderSource?: string;\n orderSide?: 'ask' | 'bid';\n orderKind?: string;\n orderId?: string;\n from?: string;\n to?: string;\n amount?: string;\n fillSource?: string;\n block?: number;\n txHash?: string;\n logIndex?: number;\n batchIndex?: number;\n timestamp?: number;\n price?: Price;\n washTradingScore?: number;\n royaltyFeeBps?: number;\n marketplaceFeeBps?: number;\n paidFullRoyalty?: boolean;\n feeBreakdown?: FeeBreakdown[];\n isDeleted?: boolean;\n createdAt?: string;\n updatedAt?: string;\n};\n\nexport type FeeBreakdown = {\n kind?: string;\n bps?: number;\n recipient?: string;\n source?: string;\n rawAmount?: string;\n};\n\nexport type Attributes = {\n key?: string;\n kind?: string;\n value: string;\n tokenCount?: number;\n onSaleCount?: number;\n floorAskPrice?: Price | null;\n topBidValue?: number | null;\n createdAt?: string;\n};\n\nexport type Collection = {\n id?: string;\n name?: string;\n slug?: string;\n symbol?: string;\n imageUrl?: string;\n image?: string;\n isSpam?: boolean;\n isNsfw?: boolean;\n creator?: string;\n tokenCount?: string;\n metadataDisabled?: boolean;\n openseaVerificationStatus?: string;\n floorAskPrice?: Price;\n royaltiesBps?: number;\n royalties?: Royalties[];\n};\n\nexport type Royalties = {\n bps?: number;\n recipient?: string;\n};\n\nexport type Ownership = {\n tokenCount?: string;\n onSaleCount?: string;\n floorAsk?: FloorAsk;\n acquiredAt?: string;\n};\n\nexport type FloorAsk = {\n id?: string;\n price?: Price;\n maker?: string;\n kind?: string;\n validFrom?: number;\n validUntil?: number;\n source?: Source;\n rawData?: Metadata;\n isNativeOffChainCancellable?: boolean;\n};\n\nexport type Price = {\n currency?: {\n contract?: string;\n name?: string;\n symbol?: string;\n decimals?: number;\n chainId?: number;\n };\n amount?: {\n raw?: string;\n decimal?: number;\n usd?: number;\n native?: number;\n };\n netAmount?: {\n raw?: string;\n decimal?: number;\n usd?: number;\n native?: number;\n };\n};\n\nexport type Metadata = {\n imageOriginal?: string;\n tokenURI?: string;\n};\n\n/**\n * Controller that passively polls on a set interval for NFT auto detection\n */\nexport class NftDetectionController extends StaticIntervalPollingControllerV1<\n NftDetectionConfig,\n BaseState\n> {\n private intervalId?: ReturnType<typeof setTimeout>;\n\n private getOwnerNftApi({\n address,\n next,\n }: {\n address: string;\n next?: string;\n }) {\n return `${NFT_API_BASE_URL}/users/${address}/tokens?chainIds=1&limit=50&includeTopBid=true&continuation=${\n next ?? ''\n }`;\n }\n\n private async getOwnerNfts(address: string) {\n let nftApiResponse: ReservoirResponse;\n let nfts: TokensResponse[] = [];\n let next;\n\n do {\n nftApiResponse = await fetchWithErrorHandling({\n url: this.getOwnerNftApi({ address, next }),\n options: {\n headers: {\n Version: '1',\n },\n },\n timeout: 15000,\n });\n\n if (!nftApiResponse) {\n return nfts;\n }\n\n const newNfts = nftApiResponse.tokens.filter(\n (elm) =>\n elm.token.isSpam === false &&\n (elm.blockaidResult?.result_type\n ? elm.blockaidResult?.result_type === BlockaidResultType.Benign\n : true),\n );\n\n nfts = [...nfts, ...newNfts];\n } while ((next = nftApiResponse.continuation));\n\n return nfts;\n }\n\n /**\n * Name of this controller used during composition\n */\n override name = 'NftDetectionController';\n\n private readonly getOpenSeaApiKey: () => string | undefined;\n\n private readonly addNft: NftController['addNft'];\n\n private readonly getNftApi: NftController['getNftApi'];\n\n private readonly getNftState: () => NftState;\n\n private readonly getNetworkClientById: NetworkController['getNetworkClientById'];\n\n /**\n * Creates an NftDetectionController instance.\n *\n * @param options - The controller options.\n * @param options.chainId - The chain ID of the current network.\n * @param options.onNftsStateChange - Allows subscribing to assets controller state changes.\n * @param options.onPreferencesStateChange - Allows subscribing to preferences controller state changes.\n * @param options.onNetworkStateChange - Allows subscribing to network controller state changes.\n * @param options.getOpenSeaApiKey - Gets the OpenSea API key, if one is set.\n * @param options.addNft - Add an NFT.\n * @param options.getNftApi - Gets the URL to fetch an NFT from OpenSea.\n * @param options.getNftState - Gets the current state of the Assets controller.\n * @param options.disabled - Represents previous value of useNftDetection. Used to detect changes of useNftDetection. Default value is true.\n * @param options.selectedAddress - Represents current selected address.\n * @param options.getNetworkClientById - Gets the network client by ID, from the NetworkController.\n * @param config - Initial options used to configure this controller.\n * @param state - Initial state to set on this controller.\n */\n constructor(\n {\n chainId: initialChainId,\n getNetworkClientById,\n onPreferencesStateChange,\n onNetworkStateChange,\n getOpenSeaApiKey,\n addNft,\n getNftApi,\n getNftState,\n disabled: initialDisabled,\n selectedAddress: initialSelectedAddress,\n }: {\n chainId: Hex;\n getNetworkClientById: NetworkController['getNetworkClientById'];\n onNftsStateChange: (listener: (nftsState: NftState) => void) => void;\n onPreferencesStateChange: (\n listener: (preferencesState: PreferencesState) => void,\n ) => void;\n onNetworkStateChange: (\n listener: (networkState: NetworkState) => void,\n ) => void;\n getOpenSeaApiKey: () => string | undefined;\n addNft: NftController['addNft'];\n getNftApi: NftController['getNftApi'];\n getNftState: () => NftState;\n disabled: boolean;\n selectedAddress: string;\n },\n config?: Partial<NftDetectionConfig>,\n state?: Partial<BaseState>,\n ) {\n super(config, state);\n this.defaultConfig = {\n interval: DEFAULT_INTERVAL,\n chainId: initialChainId,\n selectedAddress: initialSelectedAddress,\n disabled: initialDisabled,\n };\n this.initialize();\n this.getNftState = getNftState;\n this.getNetworkClientById = getNetworkClientById;\n onPreferencesStateChange(({ selectedAddress, useNftDetection }) => {\n const { selectedAddress: previouslySelectedAddress, disabled } =\n this.config;\n\n if (\n selectedAddress !== previouslySelectedAddress ||\n !useNftDetection !== disabled\n ) {\n this.configure({ selectedAddress, disabled: !useNftDetection });\n if (useNftDetection) {\n this.start();\n } else {\n this.stop();\n }\n }\n });\n\n onNetworkStateChange(({ selectedNetworkClientId }) => {\n const selectedNetworkClient = getNetworkClientById(\n selectedNetworkClientId,\n );\n const { chainId } = selectedNetworkClient.configuration;\n\n this.configure({ chainId });\n });\n this.getOpenSeaApiKey = getOpenSeaApiKey;\n this.addNft = addNft;\n this.getNftApi = getNftApi;\n this.setIntervalLength(this.config.interval);\n }\n\n async _executePoll(\n networkClientId: string,\n options: { address: string },\n ): Promise<void> {\n await this.detectNfts({ networkClientId, userAddress: options.address });\n }\n\n /**\n * Start polling for the currency rate.\n */\n async start() {\n if (!this.isMainnet() || this.disabled) {\n return;\n }\n\n await this.startPolling();\n }\n\n /**\n * Stop polling for the currency rate.\n */\n stop() {\n this.stopPolling();\n }\n\n private stopPolling() {\n if (this.intervalId) {\n clearInterval(this.intervalId);\n }\n }\n\n /**\n * Starts a new polling interval.\n *\n * @param interval - An interval on which to poll.\n */\n private async startPolling(interval?: number): Promise<void> {\n interval && this.configure({ interval }, false, false);\n this.stopPolling();\n await this.detectNfts();\n this.intervalId = setInterval(async () => {\n await this.detectNfts();\n }, this.config.interval);\n }\n\n /**\n * Checks whether network is mainnet or not.\n *\n * @returns Whether current network is mainnet.\n */\n isMainnet = (): boolean => this.config.chainId === ChainId.mainnet;\n\n isMainnetByNetworkClientId = (networkClient: NetworkClient): boolean => {\n return networkClient.configuration.chainId === ChainId.mainnet;\n };\n\n /**\n * Triggers asset ERC721 token auto detection on mainnet. Any newly detected NFTs are\n * added.\n *\n * @param options - Options bag.\n * @param options.networkClientId - The network client ID to detect NFTs on.\n * @param options.userAddress - The address to detect NFTs for.\n */\n async detectNfts(\n {\n networkClientId,\n userAddress,\n }: {\n networkClientId?: NetworkClientId;\n userAddress: string;\n } = { userAddress: this.config.selectedAddress },\n ) {\n /* istanbul ignore if */\n if (!this.isMainnet() || this.disabled) {\n return;\n }\n /* istanbul ignore else */\n if (!userAddress) {\n return;\n }\n\n const apiNfts = await this.getOwnerNfts(userAddress);\n const addNftPromises = apiNfts.map(async (nft) => {\n const {\n tokenId: token_id,\n contract,\n kind,\n image: image_url,\n imageSmall: image_thumbnail_url,\n metadata: { imageOriginal: image_original_url } = {},\n name,\n description,\n attributes,\n topBid,\n lastSale,\n rarityRank,\n rarityScore,\n collection,\n } = nft.token;\n\n let ignored;\n /* istanbul ignore else */\n const { ignoredNfts } = this.getNftState();\n if (ignoredNfts.length) {\n ignored = ignoredNfts.find((c) => {\n /* istanbul ignore next */\n return (\n c.address === toChecksumHexAddress(contract) &&\n c.tokenId === token_id\n );\n });\n }\n\n /* istanbul ignore else */\n if (!ignored) {\n /* istanbul ignore next */\n const nftMetadata: NftMetadata = Object.assign(\n {},\n { name },\n description && { description },\n image_url && { image: image_url },\n image_thumbnail_url && { imageThumbnail: image_thumbnail_url },\n image_original_url && { imageOriginal: image_original_url },\n kind && { standard: kind.toUpperCase() },\n lastSale && { lastSale },\n attributes && { attributes },\n topBid && { topBid },\n rarityRank && { rarityRank },\n rarityScore && { rarityScore },\n collection && { collection },\n );\n\n await this.addNft(contract, token_id, {\n nftMetadata,\n userAddress,\n source: Source.Detected,\n networkClientId,\n });\n }\n });\n await Promise.all(addNftPromises);\n }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