@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.
Files changed (58) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/LICENSE +20 -0
  3. package/README.md +30 -0
  4. package/dist/AccountTrackerController.d.ts +88 -0
  5. package/dist/AccountTrackerController.js +138 -0
  6. package/dist/AccountTrackerController.js.map +1 -0
  7. package/dist/AssetsContractController.d.ts +176 -0
  8. package/dist/AssetsContractController.js +314 -0
  9. package/dist/AssetsContractController.js.map +1 -0
  10. package/dist/CurrencyRateController.d.ts +98 -0
  11. package/dist/CurrencyRateController.js +193 -0
  12. package/dist/CurrencyRateController.js.map +1 -0
  13. package/dist/NftController.d.ts +409 -0
  14. package/dist/NftController.js +835 -0
  15. package/dist/NftController.js.map +1 -0
  16. package/dist/NftDetectionController.d.ts +179 -0
  17. package/dist/NftDetectionController.js +204 -0
  18. package/dist/NftDetectionController.js.map +1 -0
  19. package/dist/Standards/ERC20Standard.d.ts +42 -0
  20. package/dist/Standards/ERC20Standard.js +121 -0
  21. package/dist/Standards/ERC20Standard.js.map +1 -0
  22. package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.d.ts +78 -0
  23. package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.js +148 -0
  24. package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.js.map +1 -0
  25. package/dist/Standards/NftStandards/ERC721/ERC721Standard.d.ts +88 -0
  26. package/dist/Standards/NftStandards/ERC721/ERC721Standard.js +182 -0
  27. package/dist/Standards/NftStandards/ERC721/ERC721Standard.js.map +1 -0
  28. package/dist/Standards/standards-types.d.ts +14 -0
  29. package/dist/Standards/standards-types.js +3 -0
  30. package/dist/Standards/standards-types.js.map +1 -0
  31. package/dist/TokenBalancesController.d.ts +69 -0
  32. package/dist/TokenBalancesController.js +94 -0
  33. package/dist/TokenBalancesController.js.map +1 -0
  34. package/dist/TokenDetectionController.d.ts +84 -0
  35. package/dist/TokenDetectionController.js +185 -0
  36. package/dist/TokenDetectionController.js.map +1 -0
  37. package/dist/TokenListController.d.ts +114 -0
  38. package/dist/TokenListController.js +256 -0
  39. package/dist/TokenListController.js.map +1 -0
  40. package/dist/TokenRatesController.d.ts +167 -0
  41. package/dist/TokenRatesController.js +284 -0
  42. package/dist/TokenRatesController.js.map +1 -0
  43. package/dist/TokensController.d.ts +238 -0
  44. package/dist/TokensController.js +530 -0
  45. package/dist/TokensController.js.map +1 -0
  46. package/dist/assetsUtil.d.ts +106 -0
  47. package/dist/assetsUtil.js +228 -0
  48. package/dist/assetsUtil.js.map +1 -0
  49. package/dist/crypto-compare.d.ts +12 -0
  50. package/dist/crypto-compare.js +67 -0
  51. package/dist/crypto-compare.js.map +1 -0
  52. package/dist/index.d.ts +11 -0
  53. package/dist/index.js +31 -0
  54. package/dist/index.js.map +1 -0
  55. package/dist/token-service.d.ts +29 -0
  56. package/dist/token-service.js +134 -0
  57. package/dist/token-service.js.map +1 -0
  58. package/package.json +75 -0
@@ -0,0 +1,314 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.AssetsContractController = exports.MISSING_PROVIDER_ERROR = exports.SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID = void 0;
16
+ const single_call_balance_checker_abi_1 = __importDefault(require("single-call-balance-checker-abi"));
17
+ const contracts_1 = require("@ethersproject/contracts");
18
+ const providers_1 = require("@ethersproject/providers");
19
+ const base_controller_1 = require("@metamask/base-controller");
20
+ const controller_utils_1 = require("@metamask/controller-utils");
21
+ const assetsUtil_1 = require("./assetsUtil");
22
+ const ERC721Standard_1 = require("./Standards/NftStandards/ERC721/ERC721Standard");
23
+ const ERC1155Standard_1 = require("./Standards/NftStandards/ERC1155/ERC1155Standard");
24
+ const ERC20Standard_1 = require("./Standards/ERC20Standard");
25
+ /**
26
+ * Check if token detection is enabled for certain networks
27
+ *
28
+ * @param chainId - ChainID of network
29
+ * @returns Whether the current network supports token detection
30
+ */
31
+ exports.SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID = {
32
+ [assetsUtil_1.SupportedTokenDetectionNetworks.mainnet]: '0xb1f8e55c7f64d203c1400b9d8555d050f94adf39',
33
+ [assetsUtil_1.SupportedTokenDetectionNetworks.bsc]: '0x2352c63A83f9Fd126af8676146721Fa00924d7e4',
34
+ [assetsUtil_1.SupportedTokenDetectionNetworks.polygon]: '0x2352c63A83f9Fd126af8676146721Fa00924d7e4',
35
+ [assetsUtil_1.SupportedTokenDetectionNetworks.avax]: '0xD023D153a0DFa485130ECFdE2FAA7e612EF94818',
36
+ };
37
+ exports.MISSING_PROVIDER_ERROR = 'AssetsContractController failed to set the provider correctly. A provider must be set for this method to be available';
38
+ /**
39
+ * Controller that interacts with contracts on mainnet through web3
40
+ */
41
+ class AssetsContractController extends base_controller_1.BaseController {
42
+ /**
43
+ * Creates a AssetsContractController instance.
44
+ *
45
+ * @param options - The controller options.
46
+ * @param options.onPreferencesStateChange - Allows subscribing to preference controller state changes.
47
+ * @param options.onNetworkStateChange - Allows subscribing to network controller state changes.
48
+ * @param config - Initial options used to configure this controller.
49
+ * @param state - Initial state to set on this controller.
50
+ */
51
+ constructor({ onPreferencesStateChange, onNetworkStateChange, }, config, state) {
52
+ super(config, state);
53
+ /**
54
+ * Name of this controller used during composition
55
+ */
56
+ this.name = 'AssetsContractController';
57
+ this.defaultConfig = {
58
+ provider: undefined,
59
+ ipfsGateway: controller_utils_1.IPFS_DEFAULT_GATEWAY_URL,
60
+ chainId: assetsUtil_1.SupportedTokenDetectionNetworks.mainnet,
61
+ };
62
+ this.initialize();
63
+ onPreferencesStateChange(({ ipfsGateway }) => {
64
+ this.configure({ ipfsGateway });
65
+ });
66
+ onNetworkStateChange((networkState) => {
67
+ if (this.config.chainId !== networkState.provider.chainId) {
68
+ this.configure({
69
+ chainId: networkState.provider.chainId,
70
+ });
71
+ }
72
+ });
73
+ }
74
+ /**
75
+ * Sets a new provider.
76
+ *
77
+ * TODO: Replace this wth a method.
78
+ *
79
+ * @property provider - Provider used to create a new underlying Web3 instance
80
+ */
81
+ set provider(provider) {
82
+ this._provider = new providers_1.Web3Provider(provider);
83
+ this.erc721Standard = new ERC721Standard_1.ERC721Standard(this._provider);
84
+ this.erc1155Standard = new ERC1155Standard_1.ERC1155Standard(this._provider);
85
+ this.erc20Standard = new ERC20Standard_1.ERC20Standard(this._provider);
86
+ }
87
+ get provider() {
88
+ throw new Error('Property only used for setting');
89
+ }
90
+ /**
91
+ * Get balance or count for current account on specific asset contract.
92
+ *
93
+ * @param address - Asset ERC20 contract address.
94
+ * @param selectedAddress - Current account public address.
95
+ * @returns Promise resolving to BN object containing balance for current account on specific asset contract.
96
+ */
97
+ getERC20BalanceOf(address, selectedAddress) {
98
+ return __awaiter(this, void 0, void 0, function* () {
99
+ if (!this.erc20Standard) {
100
+ throw new Error(exports.MISSING_PROVIDER_ERROR);
101
+ }
102
+ return this.erc20Standard.getBalanceOf(address, selectedAddress);
103
+ });
104
+ }
105
+ /**
106
+ * Query for the decimals for a given ERC20 asset.
107
+ *
108
+ * @param address - ERC20 asset contract address.
109
+ * @returns Promise resolving to the 'decimals'.
110
+ */
111
+ getERC20TokenDecimals(address) {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ if (this.erc20Standard === undefined) {
114
+ throw new Error(exports.MISSING_PROVIDER_ERROR);
115
+ }
116
+ return yield this.erc20Standard.getTokenDecimals(address);
117
+ });
118
+ }
119
+ /**
120
+ * Enumerate assets assigned to an owner.
121
+ *
122
+ * @param address - ERC721 asset contract address.
123
+ * @param selectedAddress - Current account public address.
124
+ * @param index - An NFT counter less than `balanceOf(selectedAddress)`.
125
+ * @returns Promise resolving to token identifier for the 'index'th asset assigned to 'selectedAddress'.
126
+ */
127
+ getERC721NftTokenId(address, selectedAddress, index) {
128
+ if (this.erc721Standard === undefined) {
129
+ throw new Error(exports.MISSING_PROVIDER_ERROR);
130
+ }
131
+ return this.erc721Standard.getNftTokenId(address, selectedAddress, index);
132
+ }
133
+ /**
134
+ * Enumerate assets assigned to an owner.
135
+ *
136
+ * @param tokenAddress - ERC721 asset contract address.
137
+ * @param userAddress - Current account public address.
138
+ * @param tokenId - ERC721 asset identifier.
139
+ * @returns Promise resolving to an object containing the token standard and a set of details which depend on which standard the token supports.
140
+ */
141
+ getTokenStandardAndDetails(tokenAddress, userAddress, tokenId) {
142
+ return __awaiter(this, void 0, void 0, function* () {
143
+ if (this.erc721Standard === undefined ||
144
+ this.erc1155Standard === undefined ||
145
+ this.erc20Standard === undefined) {
146
+ throw new Error(exports.MISSING_PROVIDER_ERROR);
147
+ }
148
+ const { ipfsGateway } = this.config;
149
+ // ERC721
150
+ try {
151
+ return Object.assign({}, (yield this.erc721Standard.getDetails(tokenAddress, ipfsGateway, tokenId)));
152
+ }
153
+ catch (_a) {
154
+ // Ignore
155
+ }
156
+ // ERC1155
157
+ try {
158
+ return Object.assign({}, (yield this.erc1155Standard.getDetails(tokenAddress, ipfsGateway, tokenId)));
159
+ }
160
+ catch (_b) {
161
+ // Ignore
162
+ }
163
+ // ERC20
164
+ try {
165
+ return Object.assign({}, (yield this.erc20Standard.getDetails(tokenAddress, userAddress)));
166
+ }
167
+ catch (_c) {
168
+ // Ignore
169
+ }
170
+ throw new Error('Unable to determine contract standard');
171
+ });
172
+ }
173
+ /**
174
+ * Query for tokenURI for a given ERC721 asset.
175
+ *
176
+ * @param address - ERC721 asset contract address.
177
+ * @param tokenId - ERC721 asset identifier.
178
+ * @returns Promise resolving to the 'tokenURI'.
179
+ */
180
+ getERC721TokenURI(address, tokenId) {
181
+ return __awaiter(this, void 0, void 0, function* () {
182
+ if (this.erc721Standard === undefined) {
183
+ throw new Error(exports.MISSING_PROVIDER_ERROR);
184
+ }
185
+ return this.erc721Standard.getTokenURI(address, tokenId);
186
+ });
187
+ }
188
+ /**
189
+ * Query for name for a given asset.
190
+ *
191
+ * @param address - ERC721 or ERC20 asset contract address.
192
+ * @returns Promise resolving to the 'name'.
193
+ */
194
+ getERC721AssetName(address) {
195
+ return __awaiter(this, void 0, void 0, function* () {
196
+ if (this.erc721Standard === undefined) {
197
+ throw new Error(exports.MISSING_PROVIDER_ERROR);
198
+ }
199
+ return this.erc721Standard.getAssetName(address);
200
+ });
201
+ }
202
+ /**
203
+ * Query for symbol for a given asset.
204
+ *
205
+ * @param address - ERC721 or ERC20 asset contract address.
206
+ * @returns Promise resolving to the 'symbol'.
207
+ */
208
+ getERC721AssetSymbol(address) {
209
+ return __awaiter(this, void 0, void 0, function* () {
210
+ if (this.erc721Standard === undefined) {
211
+ throw new Error(exports.MISSING_PROVIDER_ERROR);
212
+ }
213
+ return this.erc721Standard.getAssetSymbol(address);
214
+ });
215
+ }
216
+ /**
217
+ * Query for owner for a given ERC721 asset.
218
+ *
219
+ * @param address - ERC721 asset contract address.
220
+ * @param tokenId - ERC721 asset identifier.
221
+ * @returns Promise resolving to the owner address.
222
+ */
223
+ getERC721OwnerOf(address, tokenId) {
224
+ return __awaiter(this, void 0, void 0, function* () {
225
+ if (this.erc721Standard === undefined) {
226
+ throw new Error(exports.MISSING_PROVIDER_ERROR);
227
+ }
228
+ return this.erc721Standard.getOwnerOf(address, tokenId);
229
+ });
230
+ }
231
+ /**
232
+ * Query for tokenURI for a given asset.
233
+ *
234
+ * @param address - ERC1155 asset contract address.
235
+ * @param tokenId - ERC1155 asset identifier.
236
+ * @returns Promise resolving to the 'tokenURI'.
237
+ */
238
+ getERC1155TokenURI(address, tokenId) {
239
+ return __awaiter(this, void 0, void 0, function* () {
240
+ if (this.erc1155Standard === undefined) {
241
+ throw new Error(exports.MISSING_PROVIDER_ERROR);
242
+ }
243
+ return this.erc1155Standard.getTokenURI(address, tokenId);
244
+ });
245
+ }
246
+ /**
247
+ * Query for balance of a given ERC 1155 token.
248
+ *
249
+ * @param userAddress - Wallet public address.
250
+ * @param nftAddress - ERC1155 asset contract address.
251
+ * @param nftId - ERC1155 asset identifier.
252
+ * @returns Promise resolving to the 'balanceOf'.
253
+ */
254
+ getERC1155BalanceOf(userAddress, nftAddress, nftId) {
255
+ return __awaiter(this, void 0, void 0, function* () {
256
+ if (this.erc1155Standard === undefined) {
257
+ throw new Error(exports.MISSING_PROVIDER_ERROR);
258
+ }
259
+ return yield this.erc1155Standard.getBalanceOf(nftAddress, userAddress, nftId);
260
+ });
261
+ }
262
+ /**
263
+ * Transfer single ERC1155 token.
264
+ *
265
+ * @param nftAddress - ERC1155 token address.
266
+ * @param senderAddress - ERC1155 token sender.
267
+ * @param recipientAddress - ERC1155 token recipient.
268
+ * @param nftId - ERC1155 token id.
269
+ * @param qty - Quantity of tokens to be sent.
270
+ * @returns Promise resolving to the 'transferSingle' ERC1155 token.
271
+ */
272
+ transferSingleERC1155(nftAddress, senderAddress, recipientAddress, nftId, qty) {
273
+ return __awaiter(this, void 0, void 0, function* () {
274
+ if (this.erc1155Standard === undefined) {
275
+ throw new Error(exports.MISSING_PROVIDER_ERROR);
276
+ }
277
+ return yield this.erc1155Standard.transferSingle(nftAddress, senderAddress, recipientAddress, nftId, qty);
278
+ });
279
+ }
280
+ /**
281
+ * Get the token balance for a list of token addresses in a single call. Only non-zero balances
282
+ * are returned.
283
+ *
284
+ * @param selectedAddress - The address to check token balances for.
285
+ * @param tokensToDetect - The token addresses to detect balances for.
286
+ * @returns The list of non-zero token balances.
287
+ */
288
+ getBalancesInSingleCall(selectedAddress, tokensToDetect) {
289
+ return __awaiter(this, void 0, void 0, function* () {
290
+ if (!(this.config.chainId in exports.SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID)) {
291
+ // Only fetch balance if contract address exists
292
+ return {};
293
+ }
294
+ const contractAddress = exports.SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID[this.config.chainId];
295
+ const contract = new contracts_1.Contract(contractAddress, single_call_balance_checker_abi_1.default, this._provider);
296
+ const result = yield contract.balances([selectedAddress], tokensToDetect);
297
+ const nonZeroBalances = {};
298
+ /* istanbul ignore else */
299
+ if (result.length > 0) {
300
+ tokensToDetect.forEach((tokenAddress, index) => {
301
+ const balance = result[index];
302
+ /* istanbul ignore else */
303
+ if (String(balance) !== '0') {
304
+ nonZeroBalances[tokenAddress] = balance;
305
+ }
306
+ });
307
+ }
308
+ return nonZeroBalances;
309
+ });
310
+ }
311
+ }
312
+ exports.AssetsContractController = AssetsContractController;
313
+ exports.default = AssetsContractController;
314
+ //# sourceMappingURL=AssetsContractController.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AssetsContractController.js","sourceRoot":"","sources":["../src/AssetsContractController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,sGAA4E;AAC5E,wDAAoD;AACpD,wDAAwD;AACxD,+DAImC;AAEnC,iEAAsE;AAEtE,6CAA+D;AAC/D,mFAAgF;AAChF,sFAAmF;AACnF,6DAA0D;AAE1D;;;;;GAKG;AACU,QAAA,uCAAuC,GAA2B;IAC7E,CAAC,4CAA+B,CAAC,OAAO,CAAC,EACvC,4CAA4C;IAC9C,CAAC,4CAA+B,CAAC,GAAG,CAAC,EACnC,4CAA4C;IAC9C,CAAC,4CAA+B,CAAC,OAAO,CAAC,EACvC,4CAA4C;IAC9C,CAAC,4CAA+B,CAAC,IAAI,CAAC,EACpC,4CAA4C;CAC/C,CAAC;AAEW,QAAA,sBAAsB,GACjC,uHAAuH,CAAC;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C;aACX;YACD,MAAM,eAAe,GACnB,+CAAuC,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YAE/D,MAAM,QAAQ,GAAG,IAAI,oBAAQ,CAC3B,eAAe,EACf,yCAA6B,EAC7B,IAAI,CAAC,SAAS,CACf,CAAC;YACF,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,QAAQ,CAAC,CAAC,eAAe,CAAC,EAAE,cAAc,CAAC,CAAC;YAC1E,MAAM,eAAe,GAAe,EAAE,CAAC;YACvC,0BAA0B;YAC1B,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;gBACrB,cAAc,CAAC,OAAO,CAAC,CAAC,YAAY,EAAE,KAAK,EAAE,EAAE;oBAC7C,MAAM,OAAO,GAAO,MAAM,CAAC,KAAK,CAAC,CAAC;oBAClC,0BAA0B;oBAC1B,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,GAAG,EAAE;wBAC3B,eAAe,CAAC,YAAY,CAAC,GAAG,OAAO,CAAC;qBACzC;gBACH,CAAC,CAAC,CAAC;aACJ;YACD,OAAO,eAAe,CAAC;QACzB,CAAC;KAAA;CACF;AAnWD,4DAmWC;AAED,kBAAe,wBAAwB,CAAC","sourcesContent":["import { BN } from 'ethereumjs-util';\nimport abiSingleCallBalancesContract from 'single-call-balance-checker-abi';\nimport { Contract } from '@ethersproject/contracts';\nimport { Web3Provider } from '@ethersproject/providers';\nimport {\n BaseController,\n BaseConfig,\n BaseState,\n} from '@metamask/base-controller';\nimport type { PreferencesState } from '@metamask/preferences-controller';\nimport { IPFS_DEFAULT_GATEWAY_URL } from '@metamask/controller-utils';\nimport { NetworkState } from '@metamask/network-controller';\nimport { SupportedTokenDetectionNetworks } from './assetsUtil';\nimport { ERC721Standard } from './Standards/NftStandards/ERC721/ERC721Standard';\nimport { ERC1155Standard } from './Standards/NftStandards/ERC1155/ERC1155Standard';\nimport { ERC20Standard } from './Standards/ERC20Standard';\n\n/**\n * Check if token detection is enabled for certain networks\n *\n * @param chainId - ChainID of network\n * @returns Whether the current network supports token detection\n */\nexport const SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID: Record<string, string> = {\n [SupportedTokenDetectionNetworks.mainnet]:\n '0xb1f8e55c7f64d203c1400b9d8555d050f94adf39',\n [SupportedTokenDetectionNetworks.bsc]:\n '0x2352c63A83f9Fd126af8676146721Fa00924d7e4',\n [SupportedTokenDetectionNetworks.polygon]:\n '0x2352c63A83f9Fd126af8676146721Fa00924d7e4',\n [SupportedTokenDetectionNetworks.avax]:\n '0xD023D153a0DFa485130ECFdE2FAA7e612EF94818',\n};\n\nexport const MISSING_PROVIDER_ERROR =\n 'AssetsContractController failed to set the provider correctly. A provider must be set for this method to be available';\n\n/**\n * @type AssetsContractConfig\n *\n * Assets Contract controller configuration\n * @property provider - Provider used to create a new web3 instance\n */\nexport interface AssetsContractConfig extends BaseConfig {\n provider: any;\n ipfsGateway: string;\n chainId: string;\n}\n\n/**\n * @type BalanceMap\n *\n * Key value object containing the balance for each tokenAddress\n * @property [tokenAddress] - Address of the token\n */\nexport interface BalanceMap {\n [tokenAddress: string]: BN;\n}\n\n/**\n * Controller that interacts with contracts on mainnet through web3\n */\nexport class AssetsContractController extends BaseController<\n AssetsContractConfig,\n BaseState\n> {\n private _provider?: Web3Provider;\n\n private erc721Standard?: ERC721Standard;\n\n private erc1155Standard?: ERC1155Standard;\n\n private erc20Standard?: ERC20Standard;\n\n /**\n * Name of this controller used during composition\n */\n override name = 'AssetsContractController';\n\n /**\n * Creates a AssetsContractController instance.\n *\n * @param options - The controller options.\n * @param options.onPreferencesStateChange - Allows subscribing to preference controller state changes.\n * @param options.onNetworkStateChange - Allows subscribing to network controller state changes.\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 onPreferencesStateChange,\n onNetworkStateChange,\n }: {\n onPreferencesStateChange: (\n listener: (preferencesState: PreferencesState) => void,\n ) => void;\n onNetworkStateChange: (\n listener: (networkState: NetworkState) => void,\n ) => void;\n },\n config?: Partial<AssetsContractConfig>,\n state?: Partial<BaseState>,\n ) {\n super(config, state);\n this.defaultConfig = {\n provider: undefined,\n ipfsGateway: IPFS_DEFAULT_GATEWAY_URL,\n chainId: SupportedTokenDetectionNetworks.mainnet,\n };\n this.initialize();\n\n onPreferencesStateChange(({ ipfsGateway }) => {\n this.configure({ ipfsGateway });\n });\n\n onNetworkStateChange((networkState) => {\n if (this.config.chainId !== networkState.provider.chainId) {\n this.configure({\n chainId: networkState.provider.chainId,\n });\n }\n });\n }\n\n /**\n * Sets a new provider.\n *\n * TODO: Replace this wth a method.\n *\n * @property provider - Provider used to create a new underlying Web3 instance\n */\n set provider(provider: any) {\n this._provider = new Web3Provider(provider);\n this.erc721Standard = new ERC721Standard(this._provider);\n this.erc1155Standard = new ERC1155Standard(this._provider);\n this.erc20Standard = new ERC20Standard(this._provider);\n }\n\n get provider() {\n throw new Error('Property only used for setting');\n }\n\n /**\n * Get balance or count for current account on specific asset contract.\n *\n * @param address - Asset ERC20 contract address.\n * @param selectedAddress - Current account public address.\n * @returns Promise resolving to BN object containing balance for current account on specific asset contract.\n */\n async getERC20BalanceOf(\n address: string,\n selectedAddress: string,\n ): Promise<BN> {\n if (!this.erc20Standard) {\n throw new Error(MISSING_PROVIDER_ERROR);\n }\n return this.erc20Standard.getBalanceOf(address, selectedAddress);\n }\n\n /**\n * Query for the decimals for a given ERC20 asset.\n *\n * @param address - ERC20 asset contract address.\n * @returns Promise resolving to the 'decimals'.\n */\n async getERC20TokenDecimals(address: string): Promise<string> {\n if (this.erc20Standard === undefined) {\n throw new Error(MISSING_PROVIDER_ERROR);\n }\n return await this.erc20Standard.getTokenDecimals(address);\n }\n\n /**\n * Enumerate assets assigned to an owner.\n *\n * @param address - ERC721 asset contract address.\n * @param selectedAddress - Current account public address.\n * @param index - An NFT counter less than `balanceOf(selectedAddress)`.\n * @returns Promise resolving to token identifier for the 'index'th asset assigned to 'selectedAddress'.\n */\n getERC721NftTokenId(\n address: string,\n selectedAddress: string,\n index: number,\n ): Promise<string> {\n if (this.erc721Standard === undefined) {\n throw new Error(MISSING_PROVIDER_ERROR);\n }\n return this.erc721Standard.getNftTokenId(address, selectedAddress, index);\n }\n\n /**\n * Enumerate assets assigned to an owner.\n *\n * @param tokenAddress - ERC721 asset contract address.\n * @param userAddress - Current account public address.\n * @param tokenId - ERC721 asset identifier.\n * @returns Promise resolving to an object containing the token standard and a set of details which depend on which standard the token supports.\n */\n async getTokenStandardAndDetails(\n tokenAddress: string,\n userAddress?: string,\n tokenId?: string,\n ): Promise<{\n standard: string;\n tokenURI?: string | undefined;\n symbol?: string | undefined;\n name?: string | undefined;\n decimals?: string | undefined;\n balance?: BN | undefined;\n }> {\n if (\n this.erc721Standard === undefined ||\n this.erc1155Standard === undefined ||\n this.erc20Standard === undefined\n ) {\n throw new Error(MISSING_PROVIDER_ERROR);\n }\n\n const { ipfsGateway } = this.config;\n\n // ERC721\n try {\n return {\n ...(await this.erc721Standard.getDetails(\n tokenAddress,\n ipfsGateway,\n tokenId,\n )),\n };\n } catch {\n // Ignore\n }\n\n // ERC1155\n try {\n return {\n ...(await this.erc1155Standard.getDetails(\n tokenAddress,\n ipfsGateway,\n tokenId,\n )),\n };\n } catch {\n // Ignore\n }\n\n // ERC20\n try {\n return {\n ...(await this.erc20Standard.getDetails(tokenAddress, userAddress)),\n };\n } catch {\n // Ignore\n }\n\n throw new Error('Unable to determine contract standard');\n }\n\n /**\n * Query for tokenURI for a given ERC721 asset.\n *\n * @param address - ERC721 asset contract address.\n * @param tokenId - ERC721 asset identifier.\n * @returns Promise resolving to the 'tokenURI'.\n */\n async getERC721TokenURI(address: string, tokenId: string): Promise<string> {\n if (this.erc721Standard === undefined) {\n throw new Error(MISSING_PROVIDER_ERROR);\n }\n return this.erc721Standard.getTokenURI(address, tokenId);\n }\n\n /**\n * Query for name for a given asset.\n *\n * @param address - ERC721 or ERC20 asset contract address.\n * @returns Promise resolving to the 'name'.\n */\n async getERC721AssetName(address: string): Promise<string> {\n if (this.erc721Standard === undefined) {\n throw new Error(MISSING_PROVIDER_ERROR);\n }\n return this.erc721Standard.getAssetName(address);\n }\n\n /**\n * Query for symbol for a given asset.\n *\n * @param address - ERC721 or ERC20 asset contract address.\n * @returns Promise resolving to the 'symbol'.\n */\n async getERC721AssetSymbol(address: string): Promise<string> {\n if (this.erc721Standard === undefined) {\n throw new Error(MISSING_PROVIDER_ERROR);\n }\n return this.erc721Standard.getAssetSymbol(address);\n }\n\n /**\n * Query for owner for a given ERC721 asset.\n *\n * @param address - ERC721 asset contract address.\n * @param tokenId - ERC721 asset identifier.\n * @returns Promise resolving to the owner address.\n */\n async getERC721OwnerOf(address: string, tokenId: string): Promise<string> {\n if (this.erc721Standard === undefined) {\n throw new Error(MISSING_PROVIDER_ERROR);\n }\n return this.erc721Standard.getOwnerOf(address, tokenId);\n }\n\n /**\n * Query for tokenURI for a given asset.\n *\n * @param address - ERC1155 asset contract address.\n * @param tokenId - ERC1155 asset identifier.\n * @returns Promise resolving to the 'tokenURI'.\n */\n async getERC1155TokenURI(address: string, tokenId: string): Promise<string> {\n if (this.erc1155Standard === undefined) {\n throw new Error(MISSING_PROVIDER_ERROR);\n }\n return this.erc1155Standard.getTokenURI(address, tokenId);\n }\n\n /**\n * Query for balance of a given ERC 1155 token.\n *\n * @param userAddress - Wallet public address.\n * @param nftAddress - ERC1155 asset contract address.\n * @param nftId - ERC1155 asset identifier.\n * @returns Promise resolving to the 'balanceOf'.\n */\n async getERC1155BalanceOf(\n userAddress: string,\n nftAddress: string,\n nftId: string,\n ): Promise<BN> {\n if (this.erc1155Standard === undefined) {\n throw new Error(MISSING_PROVIDER_ERROR);\n }\n return await this.erc1155Standard.getBalanceOf(\n nftAddress,\n userAddress,\n nftId,\n );\n }\n\n /**\n * Transfer single ERC1155 token.\n *\n * @param nftAddress - ERC1155 token address.\n * @param senderAddress - ERC1155 token sender.\n * @param recipientAddress - ERC1155 token recipient.\n * @param nftId - ERC1155 token id.\n * @param qty - Quantity of tokens to be sent.\n * @returns Promise resolving to the 'transferSingle' ERC1155 token.\n */\n async transferSingleERC1155(\n nftAddress: string,\n senderAddress: string,\n recipientAddress: string,\n nftId: string,\n qty: string,\n ): Promise<void> {\n if (this.erc1155Standard === undefined) {\n throw new Error(MISSING_PROVIDER_ERROR);\n }\n return await this.erc1155Standard.transferSingle(\n nftAddress,\n senderAddress,\n recipientAddress,\n nftId,\n qty,\n );\n }\n\n /**\n * Get the token balance for a list of token addresses in a single call. Only non-zero balances\n * are returned.\n *\n * @param selectedAddress - The address to check token balances for.\n * @param tokensToDetect - The token addresses to detect balances for.\n * @returns The list of non-zero token balances.\n */\n async getBalancesInSingleCall(\n selectedAddress: string,\n tokensToDetect: string[],\n ) {\n if (!(this.config.chainId in SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID)) {\n // Only fetch balance if contract address exists\n return {};\n }\n const contractAddress =\n SINGLE_CALL_BALANCES_ADDRESS_BY_CHAINID[this.config.chainId];\n\n const contract = new Contract(\n contractAddress,\n abiSingleCallBalancesContract,\n this._provider,\n );\n const result = await contract.balances([selectedAddress], tokensToDetect);\n const nonZeroBalances: BalanceMap = {};\n /* istanbul ignore else */\n if (result.length > 0) {\n tokensToDetect.forEach((tokenAddress, index) => {\n const balance: BN = result[index];\n /* istanbul ignore else */\n if (String(balance) !== '0') {\n nonZeroBalances[tokenAddress] = balance;\n }\n });\n }\n return nonZeroBalances;\n }\n}\n\nexport default AssetsContractController;\n"]}
@@ -0,0 +1,98 @@
1
+ import type { Patch } from 'immer';
2
+ import { BaseControllerV2, RestrictedControllerMessenger } from '@metamask/base-controller';
3
+ import { fetchExchangeRate as defaultFetchExchangeRate } from './crypto-compare';
4
+ /**
5
+ * @type CurrencyRateState
6
+ * @property conversionDate - Timestamp of conversion rate expressed in ms since UNIX epoch
7
+ * @property conversionRate - Conversion rate from current base asset to the current currency
8
+ * @property currentCurrency - Currently-active ISO 4217 currency code
9
+ * @property nativeCurrency - Symbol for the base asset used for conversion
10
+ * @property pendingCurrentCurrency - The currency being switched to
11
+ * @property pendingNativeCurrency - The base asset currency being switched to
12
+ * @property usdConversionRate - Conversion rate from usd to the current currency
13
+ */
14
+ export declare type CurrencyRateState = {
15
+ conversionDate: number | null;
16
+ conversionRate: number | null;
17
+ currentCurrency: string;
18
+ nativeCurrency: string;
19
+ pendingCurrentCurrency: string | null;
20
+ pendingNativeCurrency: string | null;
21
+ usdConversionRate: number | null;
22
+ };
23
+ declare const name = "CurrencyRateController";
24
+ export declare type CurrencyRateStateChange = {
25
+ type: `${typeof name}:stateChange`;
26
+ payload: [CurrencyRateState, Patch[]];
27
+ };
28
+ export declare type GetCurrencyRateState = {
29
+ type: `${typeof name}:getState`;
30
+ handler: () => CurrencyRateState;
31
+ };
32
+ declare type CurrencyRateMessenger = RestrictedControllerMessenger<typeof name, GetCurrencyRateState, CurrencyRateStateChange, never, never>;
33
+ /**
34
+ * Controller that passively polls on a set interval for an exchange rate from the current network
35
+ * asset to the user's preferred currency.
36
+ */
37
+ export declare class CurrencyRateController extends BaseControllerV2<typeof name, CurrencyRateState, CurrencyRateMessenger> {
38
+ private mutex;
39
+ private intervalId?;
40
+ private intervalDelay;
41
+ private fetchExchangeRate;
42
+ private includeUsdRate;
43
+ /**
44
+ * Creates a CurrencyRateController instance.
45
+ *
46
+ * @param options - Constructor options.
47
+ * @param options.includeUsdRate - Keep track of the USD rate in addition to the current currency rate.
48
+ * @param options.interval - The polling interval, in milliseconds.
49
+ * @param options.messenger - A reference to the messaging system.
50
+ * @param options.state - Initial state to set on this controller.
51
+ * @param options.fetchExchangeRate - Fetches the exchange rate from an external API. This option is primarily meant for use in unit tests.
52
+ */
53
+ constructor({ includeUsdRate, interval, messenger, state, fetchExchangeRate, }: {
54
+ includeUsdRate?: boolean;
55
+ interval?: number;
56
+ messenger: CurrencyRateMessenger;
57
+ state?: Partial<CurrencyRateState>;
58
+ fetchExchangeRate?: typeof defaultFetchExchangeRate;
59
+ });
60
+ /**
61
+ * Start polling for the currency rate.
62
+ */
63
+ start(): Promise<void>;
64
+ /**
65
+ * Stop polling for the currency rate.
66
+ */
67
+ stop(): void;
68
+ /**
69
+ * Prepare to discard this controller.
70
+ *
71
+ * This stops any active polling.
72
+ */
73
+ destroy(): void;
74
+ /**
75
+ * Sets a currency to track.
76
+ *
77
+ * @param currentCurrency - ISO 4217 currency code.
78
+ */
79
+ setCurrentCurrency(currentCurrency: string): Promise<void>;
80
+ /**
81
+ * Sets a new native currency.
82
+ *
83
+ * @param symbol - Symbol for the base asset.
84
+ */
85
+ setNativeCurrency(symbol: string): Promise<void>;
86
+ private stopPolling;
87
+ /**
88
+ * Starts a new polling interval.
89
+ */
90
+ private startPolling;
91
+ /**
92
+ * Updates exchange rate for the current currency.
93
+ *
94
+ * @returns The controller state.
95
+ */
96
+ updateExchangeRate(): Promise<CurrencyRateState | void>;
97
+ }
98
+ export default CurrencyRateController;
@@ -0,0 +1,193 @@
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.CurrencyRateController = void 0;
13
+ const async_mutex_1 = require("async-mutex");
14
+ const base_controller_1 = require("@metamask/base-controller");
15
+ const controller_utils_1 = require("@metamask/controller-utils");
16
+ const crypto_compare_1 = require("./crypto-compare");
17
+ const name = 'CurrencyRateController';
18
+ const metadata = {
19
+ conversionDate: { persist: true, anonymous: true },
20
+ conversionRate: { persist: true, anonymous: true },
21
+ currentCurrency: { persist: true, anonymous: true },
22
+ nativeCurrency: { persist: true, anonymous: true },
23
+ pendingCurrentCurrency: { persist: false, anonymous: true },
24
+ pendingNativeCurrency: { persist: false, anonymous: true },
25
+ usdConversionRate: { persist: true, anonymous: true },
26
+ };
27
+ const defaultState = {
28
+ conversionDate: 0,
29
+ conversionRate: 0,
30
+ currentCurrency: 'usd',
31
+ nativeCurrency: 'ETH',
32
+ pendingCurrentCurrency: null,
33
+ pendingNativeCurrency: null,
34
+ usdConversionRate: null,
35
+ };
36
+ /**
37
+ * Controller that passively polls on a set interval for an exchange rate from the current network
38
+ * asset to the user's preferred currency.
39
+ */
40
+ class CurrencyRateController extends base_controller_1.BaseControllerV2 {
41
+ /**
42
+ * Creates a CurrencyRateController instance.
43
+ *
44
+ * @param options - Constructor options.
45
+ * @param options.includeUsdRate - Keep track of the USD rate in addition to the current currency rate.
46
+ * @param options.interval - The polling interval, in milliseconds.
47
+ * @param options.messenger - A reference to the messaging system.
48
+ * @param options.state - Initial state to set on this controller.
49
+ * @param options.fetchExchangeRate - Fetches the exchange rate from an external API. This option is primarily meant for use in unit tests.
50
+ */
51
+ constructor({ includeUsdRate = false, interval = 180000, messenger, state, fetchExchangeRate = crypto_compare_1.fetchExchangeRate, }) {
52
+ super({
53
+ name,
54
+ metadata,
55
+ messenger,
56
+ state: Object.assign(Object.assign({}, defaultState), state),
57
+ });
58
+ this.mutex = new async_mutex_1.Mutex();
59
+ this.includeUsdRate = includeUsdRate;
60
+ this.intervalDelay = interval;
61
+ this.fetchExchangeRate = fetchExchangeRate;
62
+ }
63
+ /**
64
+ * Start polling for the currency rate.
65
+ */
66
+ start() {
67
+ return __awaiter(this, void 0, void 0, function* () {
68
+ yield this.startPolling();
69
+ });
70
+ }
71
+ /**
72
+ * Stop polling for the currency rate.
73
+ */
74
+ stop() {
75
+ this.stopPolling();
76
+ }
77
+ /**
78
+ * Prepare to discard this controller.
79
+ *
80
+ * This stops any active polling.
81
+ */
82
+ destroy() {
83
+ super.destroy();
84
+ this.stopPolling();
85
+ }
86
+ /**
87
+ * Sets a currency to track.
88
+ *
89
+ * @param currentCurrency - ISO 4217 currency code.
90
+ */
91
+ setCurrentCurrency(currentCurrency) {
92
+ return __awaiter(this, void 0, void 0, function* () {
93
+ this.update((state) => {
94
+ state.pendingCurrentCurrency = currentCurrency;
95
+ });
96
+ yield this.updateExchangeRate();
97
+ });
98
+ }
99
+ /**
100
+ * Sets a new native currency.
101
+ *
102
+ * @param symbol - Symbol for the base asset.
103
+ */
104
+ setNativeCurrency(symbol) {
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ this.update((state) => {
107
+ state.pendingNativeCurrency = symbol;
108
+ });
109
+ yield this.updateExchangeRate();
110
+ });
111
+ }
112
+ stopPolling() {
113
+ if (this.intervalId) {
114
+ clearInterval(this.intervalId);
115
+ }
116
+ }
117
+ /**
118
+ * Starts a new polling interval.
119
+ */
120
+ startPolling() {
121
+ return __awaiter(this, void 0, void 0, function* () {
122
+ this.stopPolling();
123
+ // TODO: Expose polling currency rate update errors
124
+ yield (0, controller_utils_1.safelyExecute)(() => this.updateExchangeRate());
125
+ this.intervalId = setInterval(() => __awaiter(this, void 0, void 0, function* () {
126
+ yield (0, controller_utils_1.safelyExecute)(() => this.updateExchangeRate());
127
+ }), this.intervalDelay);
128
+ });
129
+ }
130
+ /**
131
+ * Updates exchange rate for the current currency.
132
+ *
133
+ * @returns The controller state.
134
+ */
135
+ updateExchangeRate() {
136
+ return __awaiter(this, void 0, void 0, function* () {
137
+ const releaseLock = yield this.mutex.acquire();
138
+ const { currentCurrency: stateCurrentCurrency, nativeCurrency: stateNativeCurrency, pendingCurrentCurrency, pendingNativeCurrency, } = this.state;
139
+ let conversionDate = null;
140
+ let conversionRate = null;
141
+ let usdConversionRate = null;
142
+ const currentCurrency = pendingCurrentCurrency !== null && pendingCurrentCurrency !== void 0 ? pendingCurrentCurrency : stateCurrentCurrency;
143
+ const nativeCurrency = pendingNativeCurrency !== null && pendingNativeCurrency !== void 0 ? pendingNativeCurrency : stateNativeCurrency;
144
+ // For preloaded testnets (Rinkeby, Ropsten, Goerli, Kovan) we want to fetch exchange rate for real ETH.
145
+ const nativeCurrencyForExchangeRate = Object.values(controller_utils_1.TESTNET_TICKER_SYMBOLS).includes(nativeCurrency)
146
+ ? controller_utils_1.FALL_BACK_VS_CURRENCY // ETH
147
+ : nativeCurrency;
148
+ try {
149
+ if (currentCurrency &&
150
+ nativeCurrency &&
151
+ // if either currency is an empty string we can skip the comparison
152
+ // because it will result in an error from the api and ultimately
153
+ // a null conversionRate either way.
154
+ currentCurrency !== '' &&
155
+ nativeCurrency !== '') {
156
+ ({ conversionRate, usdConversionRate } = yield this.fetchExchangeRate(currentCurrency, nativeCurrencyForExchangeRate, this.includeUsdRate));
157
+ conversionDate = Date.now() / 1000;
158
+ }
159
+ }
160
+ catch (error) {
161
+ if (!(error instanceof Error &&
162
+ error.message.includes('market does not exist for this coin pair'))) {
163
+ throw error;
164
+ }
165
+ }
166
+ finally {
167
+ try {
168
+ this.update(() => {
169
+ return {
170
+ conversionDate,
171
+ conversionRate,
172
+ // we currently allow and handle an empty string as a valid nativeCurrency
173
+ // in cases where a user has not entered a native ticker symbol for a custom network
174
+ // currentCurrency is not from user input but this protects us from unexpected changes.
175
+ nativeCurrency,
176
+ currentCurrency,
177
+ pendingCurrentCurrency: null,
178
+ pendingNativeCurrency: null,
179
+ usdConversionRate,
180
+ };
181
+ });
182
+ }
183
+ finally {
184
+ releaseLock();
185
+ }
186
+ }
187
+ return this.state;
188
+ });
189
+ }
190
+ }
191
+ exports.CurrencyRateController = CurrencyRateController;
192
+ exports.default = CurrencyRateController;
193
+ //# sourceMappingURL=CurrencyRateController.js.map