@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,121 @@
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.ERC20Standard = void 0;
13
+ const contracts_1 = require("@ethersproject/contracts");
14
+ const metamask_eth_abis_1 = require("@metamask/metamask-eth-abis");
15
+ const ethereumjs_util_1 = require("ethereumjs-util");
16
+ const abi_1 = require("@ethersproject/abi");
17
+ const controller_utils_1 = require("@metamask/controller-utils");
18
+ const assetsUtil_1 = require("../assetsUtil");
19
+ class ERC20Standard {
20
+ constructor(provider) {
21
+ this.provider = provider;
22
+ }
23
+ /**
24
+ * Get balance or count for current account on specific asset contract.
25
+ *
26
+ * @param address - Asset ERC20 contract address.
27
+ * @param selectedAddress - Current account public address.
28
+ * @returns Promise resolving to BN object containing balance for current account on specific asset contract.
29
+ */
30
+ getBalanceOf(address, selectedAddress) {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ const contract = new contracts_1.Contract(address, metamask_eth_abis_1.abiERC20, this.provider);
33
+ const balance = yield contract.balanceOf(selectedAddress);
34
+ return (0, assetsUtil_1.ethersBigNumberToBN)(balance);
35
+ });
36
+ }
37
+ /**
38
+ * Query for the decimals for a given ERC20 asset.
39
+ *
40
+ * @param address - ERC20 asset contract string.
41
+ * @returns Promise resolving to the 'decimals'.
42
+ */
43
+ getTokenDecimals(address) {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ const contract = new contracts_1.Contract(address, metamask_eth_abis_1.abiERC20, this.provider);
46
+ try {
47
+ const decimals = yield contract.decimals();
48
+ return decimals.toString();
49
+ }
50
+ catch (err) {
51
+ // Mirror previous implementation
52
+ if (err.message.includes('call revert exception')) {
53
+ throw new Error('Failed to parse token decimals');
54
+ }
55
+ throw err;
56
+ }
57
+ });
58
+ }
59
+ /**
60
+ * Query for symbol for a given ERC20 asset.
61
+ *
62
+ * @param address - ERC20 asset contract address.
63
+ * @returns Promise resolving to the 'symbol'.
64
+ */
65
+ getTokenSymbol(address) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ // Signature for calling `symbol()`
68
+ const payload = { to: address, data: '0x95d89b41' };
69
+ const result = yield this.provider.call(payload);
70
+ const abiCoder = new abi_1.AbiCoder();
71
+ // Parse as string - treat empty string as failure
72
+ try {
73
+ const decoded = abiCoder.decode(['string'], result)[0];
74
+ if ((decoded === null || decoded === void 0 ? void 0 : decoded.length) > 0) {
75
+ return decoded;
76
+ }
77
+ }
78
+ catch (_a) {
79
+ // Ignore error
80
+ }
81
+ // Parse as bytes - treat empty string as failure
82
+ try {
83
+ const utf8 = (0, ethereumjs_util_1.toUtf8)(result);
84
+ if (utf8.length > 0) {
85
+ return utf8;
86
+ }
87
+ }
88
+ catch (_b) {
89
+ // Ignore error
90
+ }
91
+ throw new Error('Failed to parse token symbol');
92
+ });
93
+ }
94
+ /**
95
+ * Query if a contract implements an interface.
96
+ *
97
+ * @param address - Asset contract address.
98
+ * @param userAddress - The public address for the currently active user's account.
99
+ * @returns Promise resolving an object containing the standard, decimals, symbol and balance of the given contract/userAddress pair.
100
+ */
101
+ getDetails(address, userAddress) {
102
+ return __awaiter(this, void 0, void 0, function* () {
103
+ const [decimals, symbol] = yield Promise.all([
104
+ this.getTokenDecimals(address),
105
+ this.getTokenSymbol(address),
106
+ ]);
107
+ let balance;
108
+ if (userAddress) {
109
+ balance = yield this.getBalanceOf(address, userAddress);
110
+ }
111
+ return {
112
+ decimals,
113
+ symbol,
114
+ balance,
115
+ standard: controller_utils_1.ERC20,
116
+ };
117
+ });
118
+ }
119
+ }
120
+ exports.ERC20Standard = ERC20Standard;
121
+ //# sourceMappingURL=ERC20Standard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ERC20Standard.js","sourceRoot":"","sources":["../../src/Standards/ERC20Standard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,wDAAoD;AACpD,mEAAuD;AACvD,qDAA6C;AAC7C,4CAA8C;AAE9C,iEAAmD;AACnD,8CAAoD;AAEpD,MAAa,aAAa;IAGxB,YAAY,QAAsB;QAChC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED;;;;;;OAMG;IACG,YAAY,CAAC,OAAe,EAAE,eAAuB;;YACzD,MAAM,QAAQ,GAAG,IAAI,oBAAQ,CAAC,OAAO,EAAE,4BAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;YAChE,MAAM,OAAO,GAAG,MAAM,QAAQ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AAM,CAAC,CAAC;gBAC5B,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;oBACnB,OAAO,IAAI,CAAC;iBACb;aACF;YAAC,WAAM;gBACN,eAAe;aAChB;YAED,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;QAClD,CAAC;KAAA;IAED;;;;;;OAMG;IACG,UAAU,CACd,OAAe,EACf,WAAoB;;YAOpB,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;gBAC3C,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC;gBAC9B,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC;aAC7B,CAAC,CAAC;YACH,IAAI,OAAO,CAAC;YACZ,IAAI,WAAW,EAAE;gBACf,OAAO,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;aACzD;YACD,OAAO;gBACL,QAAQ;gBACR,MAAM;gBACN,OAAO;gBACP,QAAQ,EAAE,wBAAK;aAChB,CAAC;QACJ,CAAC;KAAA;CACF;AA3GD,sCA2GC","sourcesContent":["import { Contract } from '@ethersproject/contracts';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport { BN, toUtf8 } from 'ethereumjs-util';\nimport { AbiCoder } from '@ethersproject/abi';\nimport { Web3Provider } from '@ethersproject/providers';\nimport { ERC20 } from '@metamask/controller-utils';\nimport { ethersBigNumberToBN } from '../assetsUtil';\n\nexport class ERC20Standard {\n private provider: Web3Provider;\n\n constructor(provider: Web3Provider) {\n this.provider = provider;\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 getBalanceOf(address: string, selectedAddress: string): Promise<BN> {\n const contract = new Contract(address, abiERC20, this.provider);\n const balance = await contract.balanceOf(selectedAddress);\n return ethersBigNumberToBN(balance);\n }\n\n /**\n * Query for the decimals for a given ERC20 asset.\n *\n * @param address - ERC20 asset contract string.\n * @returns Promise resolving to the 'decimals'.\n */\n async getTokenDecimals(address: string): Promise<string> {\n const contract = new Contract(address, abiERC20, this.provider);\n try {\n const decimals = await contract.decimals();\n return decimals.toString();\n } catch (err: any) {\n // Mirror previous implementation\n if (err.message.includes('call revert exception')) {\n throw new Error('Failed to parse token decimals');\n }\n throw err;\n }\n }\n\n /**\n * Query for symbol for a given ERC20 asset.\n *\n * @param address - ERC20 asset contract address.\n * @returns Promise resolving to the 'symbol'.\n */\n async getTokenSymbol(address: string): Promise<string> {\n // Signature for calling `symbol()`\n const payload = { to: address, data: '0x95d89b41' };\n const result = await this.provider.call(payload);\n\n const abiCoder = new AbiCoder();\n\n // Parse as string - treat empty string as failure\n try {\n const decoded = abiCoder.decode(['string'], result)[0];\n if (decoded?.length > 0) {\n return decoded;\n }\n } catch {\n // Ignore error\n }\n\n // Parse as bytes - treat empty string as failure\n try {\n const utf8 = toUtf8(result);\n if (utf8.length > 0) {\n return utf8;\n }\n } catch {\n // Ignore error\n }\n\n throw new Error('Failed to parse token symbol');\n }\n\n /**\n * Query if a contract implements an interface.\n *\n * @param address - Asset contract address.\n * @param userAddress - The public address for the currently active user's account.\n * @returns Promise resolving an object containing the standard, decimals, symbol and balance of the given contract/userAddress pair.\n */\n async getDetails(\n address: string,\n userAddress?: string,\n ): Promise<{\n standard: string;\n symbol: string | undefined;\n decimals: string | undefined;\n balance: BN | undefined;\n }> {\n const [decimals, symbol] = await Promise.all([\n this.getTokenDecimals(address),\n this.getTokenSymbol(address),\n ]);\n let balance;\n if (userAddress) {\n balance = await this.getBalanceOf(address, userAddress);\n }\n return {\n decimals,\n symbol,\n balance,\n standard: ERC20,\n };\n }\n}\n"]}
@@ -0,0 +1,78 @@
1
+ import { BN } from 'ethereumjs-util';
2
+ import { Web3Provider } from '@ethersproject/providers';
3
+ export declare class ERC1155Standard {
4
+ private provider;
5
+ constructor(provider: Web3Provider);
6
+ /**
7
+ * Query if contract implements ERC1155 URI Metadata interface.
8
+ *
9
+ * @param address - ERC1155 asset contract address.
10
+ * @returns Promise resolving to whether the contract implements ERC1155 URI Metadata interface.
11
+ */
12
+ contractSupportsURIMetadataInterface: (address: string) => Promise<boolean>;
13
+ /**
14
+ * Query if contract implements ERC1155 Token Receiver interface.
15
+ *
16
+ * @param address - ERC1155 asset contract address.
17
+ * @returns Promise resolving to whether the contract implements ERC1155 Token Receiver interface.
18
+ */
19
+ contractSupportsTokenReceiverInterface: (address: string) => Promise<boolean>;
20
+ /**
21
+ * Query if contract implements ERC1155 interface.
22
+ *
23
+ * @param address - ERC1155 asset contract address.
24
+ * @returns Promise resolving to whether the contract implements the base ERC1155 interface.
25
+ */
26
+ contractSupportsBase1155Interface: (address: string) => Promise<boolean>;
27
+ /**
28
+ * Query for tokenURI for a given asset.
29
+ *
30
+ * @param address - ERC1155 asset contract address.
31
+ * @param tokenId - ERC1155 asset identifier.
32
+ * @returns Promise resolving to the 'tokenURI'.
33
+ */
34
+ getTokenURI: (address: string, tokenId: string) => Promise<string>;
35
+ /**
36
+ * Query for balance of a given ERC1155 token.
37
+ *
38
+ * @param contractAddress - ERC1155 asset contract address.
39
+ * @param address - Wallet public address.
40
+ * @param tokenId - ERC1155 asset identifier.
41
+ * @returns Promise resolving to the 'balanceOf'.
42
+ */
43
+ getBalanceOf: (contractAddress: string, address: string, tokenId: string) => Promise<BN>;
44
+ /**
45
+ * Transfer single ERC1155 token.
46
+ * When minting/creating tokens, the from arg MUST be set to 0x0 (i.e. zero address).
47
+ * When burning/destroying tokens, the to arg MUST be set to 0x0 (i.e. zero address).
48
+ *
49
+ * @param operator - ERC1155 token address.
50
+ * @param from - ERC1155 token holder.
51
+ * @param to - ERC1155 token recipient.
52
+ * @param id - ERC1155 token id.
53
+ * @param value - Number of tokens to be sent.
54
+ * @returns Promise resolving to the 'transferSingle'.
55
+ */
56
+ transferSingle: (operator: string, from: string, to: string, id: string, value: string) => Promise<void>;
57
+ /**
58
+ * Query if a contract implements an interface.
59
+ *
60
+ * @param address - ERC1155 asset contract address.
61
+ * @param interfaceId - Interface identifier.
62
+ * @returns Promise resolving to whether the contract implements `interfaceID`.
63
+ */
64
+ private contractSupportsInterface;
65
+ /**
66
+ * Query if a contract implements an interface.
67
+ *
68
+ * @param address - Asset contract address.
69
+ * @param ipfsGateway - The user's preferred IPFS gateway.
70
+ * @param tokenId - tokenId of a given token in the contract.
71
+ * @returns Promise resolving an object containing the standard, tokenURI, symbol and name of the given contract/tokenId pair.
72
+ */
73
+ getDetails: (address: string, ipfsGateway: string, tokenId?: string | undefined) => Promise<{
74
+ standard: string;
75
+ tokenURI: string | undefined;
76
+ image: string | undefined;
77
+ }>;
78
+ }
@@ -0,0 +1,148 @@
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.ERC1155Standard = void 0;
13
+ const metamask_eth_abis_1 = require("@metamask/metamask-eth-abis");
14
+ const contracts_1 = require("@ethersproject/contracts");
15
+ const controller_utils_1 = require("@metamask/controller-utils");
16
+ const assetsUtil_1 = require("../../../assetsUtil");
17
+ class ERC1155Standard {
18
+ constructor(provider) {
19
+ /**
20
+ * Query if contract implements ERC1155 URI Metadata interface.
21
+ *
22
+ * @param address - ERC1155 asset contract address.
23
+ * @returns Promise resolving to whether the contract implements ERC1155 URI Metadata interface.
24
+ */
25
+ this.contractSupportsURIMetadataInterface = (address) => __awaiter(this, void 0, void 0, function* () {
26
+ return this.contractSupportsInterface(address, controller_utils_1.ERC1155_METADATA_URI_INTERFACE_ID);
27
+ });
28
+ /**
29
+ * Query if contract implements ERC1155 Token Receiver interface.
30
+ *
31
+ * @param address - ERC1155 asset contract address.
32
+ * @returns Promise resolving to whether the contract implements ERC1155 Token Receiver interface.
33
+ */
34
+ this.contractSupportsTokenReceiverInterface = (address) => __awaiter(this, void 0, void 0, function* () {
35
+ return this.contractSupportsInterface(address, controller_utils_1.ERC1155_TOKEN_RECEIVER_INTERFACE_ID);
36
+ });
37
+ /**
38
+ * Query if contract implements ERC1155 interface.
39
+ *
40
+ * @param address - ERC1155 asset contract address.
41
+ * @returns Promise resolving to whether the contract implements the base ERC1155 interface.
42
+ */
43
+ this.contractSupportsBase1155Interface = (address) => __awaiter(this, void 0, void 0, function* () {
44
+ return this.contractSupportsInterface(address, controller_utils_1.ERC1155_INTERFACE_ID);
45
+ });
46
+ /**
47
+ * Query for tokenURI for a given asset.
48
+ *
49
+ * @param address - ERC1155 asset contract address.
50
+ * @param tokenId - ERC1155 asset identifier.
51
+ * @returns Promise resolving to the 'tokenURI'.
52
+ */
53
+ this.getTokenURI = (address, tokenId) => __awaiter(this, void 0, void 0, function* () {
54
+ const contract = new contracts_1.Contract(address, metamask_eth_abis_1.abiERC1155, this.provider);
55
+ return contract.uri(tokenId);
56
+ });
57
+ /**
58
+ * Query for balance of a given ERC1155 token.
59
+ *
60
+ * @param contractAddress - ERC1155 asset contract address.
61
+ * @param address - Wallet public address.
62
+ * @param tokenId - ERC1155 asset identifier.
63
+ * @returns Promise resolving to the 'balanceOf'.
64
+ */
65
+ this.getBalanceOf = (contractAddress, address, tokenId) => __awaiter(this, void 0, void 0, function* () {
66
+ const contract = new contracts_1.Contract(contractAddress, metamask_eth_abis_1.abiERC1155, this.provider);
67
+ const balance = yield contract.balanceOf(address, tokenId);
68
+ return (0, assetsUtil_1.ethersBigNumberToBN)(balance);
69
+ });
70
+ /**
71
+ * Transfer single ERC1155 token.
72
+ * When minting/creating tokens, the from arg MUST be set to 0x0 (i.e. zero address).
73
+ * When burning/destroying tokens, the to arg MUST be set to 0x0 (i.e. zero address).
74
+ *
75
+ * @param operator - ERC1155 token address.
76
+ * @param from - ERC1155 token holder.
77
+ * @param to - ERC1155 token recipient.
78
+ * @param id - ERC1155 token id.
79
+ * @param value - Number of tokens to be sent.
80
+ * @returns Promise resolving to the 'transferSingle'.
81
+ */
82
+ this.transferSingle = (operator, from, to, id, value) => __awaiter(this, void 0, void 0, function* () {
83
+ const contract = new contracts_1.Contract(operator, metamask_eth_abis_1.abiERC1155, this.provider);
84
+ return new Promise((resolve, reject) => {
85
+ contract.transferSingle(operator, from, to, id, value, (error, result) => {
86
+ /* istanbul ignore if */
87
+ if (error) {
88
+ reject(error);
89
+ return;
90
+ }
91
+ resolve(result);
92
+ });
93
+ });
94
+ });
95
+ /**
96
+ * Query if a contract implements an interface.
97
+ *
98
+ * @param address - ERC1155 asset contract address.
99
+ * @param interfaceId - Interface identifier.
100
+ * @returns Promise resolving to whether the contract implements `interfaceID`.
101
+ */
102
+ this.contractSupportsInterface = (address, interfaceId) => __awaiter(this, void 0, void 0, function* () {
103
+ const contract = new contracts_1.Contract(address, metamask_eth_abis_1.abiERC1155, this.provider);
104
+ return contract.supportsInterface(interfaceId);
105
+ });
106
+ /**
107
+ * Query if a contract implements an interface.
108
+ *
109
+ * @param address - Asset contract address.
110
+ * @param ipfsGateway - The user's preferred IPFS gateway.
111
+ * @param tokenId - tokenId of a given token in the contract.
112
+ * @returns Promise resolving an object containing the standard, tokenURI, symbol and name of the given contract/tokenId pair.
113
+ */
114
+ this.getDetails = (address, ipfsGateway, tokenId) => __awaiter(this, void 0, void 0, function* () {
115
+ const isERC1155 = yield this.contractSupportsBase1155Interface(address);
116
+ if (!isERC1155) {
117
+ throw new Error("This isn't a valid ERC1155 contract");
118
+ }
119
+ let tokenURI, image;
120
+ if (tokenId) {
121
+ tokenURI = yield this.getTokenURI(address, tokenId);
122
+ if (tokenURI.startsWith('ipfs://')) {
123
+ tokenURI = (0, assetsUtil_1.getFormattedIpfsUrl)(ipfsGateway, tokenURI, true);
124
+ }
125
+ try {
126
+ const response = yield (0, controller_utils_1.timeoutFetch)(tokenURI);
127
+ const object = yield response.json();
128
+ image = object === null || object === void 0 ? void 0 : object.image;
129
+ if (image === null || image === void 0 ? void 0 : image.startsWith('ipfs://')) {
130
+ image = (0, assetsUtil_1.getFormattedIpfsUrl)(ipfsGateway, image, true);
131
+ }
132
+ }
133
+ catch (_a) {
134
+ // ignore
135
+ }
136
+ }
137
+ // TODO consider querying to the metadata to get name.
138
+ return {
139
+ standard: controller_utils_1.ERC1155,
140
+ tokenURI,
141
+ image,
142
+ };
143
+ });
144
+ this.provider = provider;
145
+ }
146
+ }
147
+ exports.ERC1155Standard = ERC1155Standard;
148
+ //# sourceMappingURL=ERC1155Standard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ERC1155Standard.js","sourceRoot":"","sources":["../../../../src/Standards/NftStandards/ERC1155/ERC1155Standard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mEAAyD;AACzD,wDAAoD;AAGpD,iEAMoC;AACpC,oDAA+E;AAE/E,MAAa,eAAe;IAG1B,YAAY,QAAsB;QAIlC;;;;;WAKG;QACH,yCAAoC,GAAG,CACrC,OAAe,EACG,EAAE;YACpB,OAAO,IAAI,CAAC,yBAAyB,CACnC,OAAO,EACP,oDAAiC,CAClC,CAAC;QACJ,CAAC,CAAA,CAAC;QAEF;;;;;WAKG;QACH,2CAAsC,GAAG,CACvC,OAAe,EACG,EAAE;YACpB,OAAO,IAAI,CAAC,yBAAyB,CACnC,OAAO,EACP,sDAAmC,CACpC,CAAC;QACJ,CAAC,CAAA,CAAC;QAEF;;;;;WAKG;QACH,sCAAiC,GAAG,CAClC,OAAe,EACG,EAAE;YACpB,OAAO,IAAI,CAAC,yBAAyB,CAAC,OAAO,EAAE,uCAAoB,CAAC,CAAC;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D;YACtD,OAAO;gBACL,QAAQ,EAAE,0BAAO;gBACjB,QAAQ;gBACR,KAAK;aACN,CAAC;QACJ,CAAC,CAAA,CAAC;QAjLA,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;CAiLF;AAtLD,0CAsLC","sourcesContent":["import { abiERC1155 } from '@metamask/metamask-eth-abis';\nimport { Contract } from '@ethersproject/contracts';\nimport { BN } from 'ethereumjs-util';\nimport { Web3Provider } from '@ethersproject/providers';\nimport {\n ERC1155,\n ERC1155_INTERFACE_ID,\n ERC1155_METADATA_URI_INTERFACE_ID,\n ERC1155_TOKEN_RECEIVER_INTERFACE_ID,\n timeoutFetch,\n} from '@metamask/controller-utils';\nimport { getFormattedIpfsUrl, ethersBigNumberToBN } from '../../../assetsUtil';\n\nexport class ERC1155Standard {\n private provider: Web3Provider;\n\n constructor(provider: Web3Provider) {\n this.provider = provider;\n }\n\n /**\n * Query if contract implements ERC1155 URI Metadata interface.\n *\n * @param address - ERC1155 asset contract address.\n * @returns Promise resolving to whether the contract implements ERC1155 URI Metadata interface.\n */\n contractSupportsURIMetadataInterface = async (\n address: string,\n ): Promise<boolean> => {\n return this.contractSupportsInterface(\n address,\n ERC1155_METADATA_URI_INTERFACE_ID,\n );\n };\n\n /**\n * Query if contract implements ERC1155 Token Receiver interface.\n *\n * @param address - ERC1155 asset contract address.\n * @returns Promise resolving to whether the contract implements ERC1155 Token Receiver interface.\n */\n contractSupportsTokenReceiverInterface = async (\n address: string,\n ): Promise<boolean> => {\n return this.contractSupportsInterface(\n address,\n ERC1155_TOKEN_RECEIVER_INTERFACE_ID,\n );\n };\n\n /**\n * Query if contract implements ERC1155 interface.\n *\n * @param address - ERC1155 asset contract address.\n * @returns Promise resolving to whether the contract implements the base ERC1155 interface.\n */\n contractSupportsBase1155Interface = async (\n address: string,\n ): Promise<boolean> => {\n return this.contractSupportsInterface(address, ERC1155_INTERFACE_ID);\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 getTokenURI = async (address: string, tokenId: string): Promise<string> => {\n const contract = new Contract(address, abiERC1155, this.provider);\n return contract.uri(tokenId);\n };\n\n /**\n * Query for balance of a given ERC1155 token.\n *\n * @param contractAddress - ERC1155 asset contract address.\n * @param address - Wallet public address.\n * @param tokenId - ERC1155 asset identifier.\n * @returns Promise resolving to the 'balanceOf'.\n */\n getBalanceOf = async (\n contractAddress: string,\n address: string,\n tokenId: string,\n ): Promise<BN> => {\n const contract = new Contract(contractAddress, abiERC1155, this.provider);\n const balance = await contract.balanceOf(address, tokenId);\n return ethersBigNumberToBN(balance);\n };\n\n /**\n * Transfer single ERC1155 token.\n * When minting/creating tokens, the from arg MUST be set to 0x0 (i.e. zero address).\n * When burning/destroying tokens, the to arg MUST be set to 0x0 (i.e. zero address).\n *\n * @param operator - ERC1155 token address.\n * @param from - ERC1155 token holder.\n * @param to - ERC1155 token recipient.\n * @param id - ERC1155 token id.\n * @param value - Number of tokens to be sent.\n * @returns Promise resolving to the 'transferSingle'.\n */\n transferSingle = async (\n operator: string,\n from: string,\n to: string,\n id: string,\n value: string,\n ): Promise<void> => {\n const contract = new Contract(operator, abiERC1155, this.provider);\n return new Promise<void>((resolve, reject) => {\n contract.transferSingle(\n operator,\n from,\n to,\n id,\n value,\n (error: Error, result: void) => {\n /* istanbul ignore if */\n if (error) {\n reject(error);\n return;\n }\n resolve(result);\n },\n );\n });\n };\n\n /**\n * Query if a contract implements an interface.\n *\n * @param address - ERC1155 asset contract address.\n * @param interfaceId - Interface identifier.\n * @returns Promise resolving to whether the contract implements `interfaceID`.\n */\n private contractSupportsInterface = async (\n address: string,\n interfaceId: string,\n ): Promise<boolean> => {\n const contract = new Contract(address, abiERC1155, this.provider);\n return contract.supportsInterface(interfaceId);\n };\n\n /**\n * Query if a contract implements an interface.\n *\n * @param address - Asset contract address.\n * @param ipfsGateway - The user's preferred IPFS gateway.\n * @param tokenId - tokenId of a given token in the contract.\n * @returns Promise resolving an object containing the standard, tokenURI, symbol and name of the given contract/tokenId pair.\n */\n getDetails = async (\n address: string,\n ipfsGateway: string,\n tokenId?: string,\n ): Promise<{\n standard: string;\n tokenURI: string | undefined;\n image: string | undefined;\n }> => {\n const isERC1155 = await this.contractSupportsBase1155Interface(address);\n\n if (!isERC1155) {\n throw new Error(\"This isn't a valid ERC1155 contract\");\n }\n let tokenURI, image;\n\n if (tokenId) {\n tokenURI = await this.getTokenURI(address, tokenId);\n if (tokenURI.startsWith('ipfs://')) {\n tokenURI = getFormattedIpfsUrl(ipfsGateway, tokenURI, true);\n }\n\n try {\n const response = await timeoutFetch(tokenURI);\n const object = await response.json();\n image = object?.image;\n if (image?.startsWith('ipfs://')) {\n image = getFormattedIpfsUrl(ipfsGateway, image, true);\n }\n } catch {\n // ignore\n }\n }\n\n // TODO consider querying to the metadata to get name.\n return {\n standard: ERC1155,\n tokenURI,\n image,\n };\n };\n}\n"]}
@@ -0,0 +1,88 @@
1
+ import { Web3Provider } from '@ethersproject/providers';
2
+ export declare class ERC721Standard {
3
+ private provider;
4
+ constructor(provider: Web3Provider);
5
+ /**
6
+ * Query if contract implements ERC721Metadata interface.
7
+ *
8
+ * @param address - ERC721 asset contract address.
9
+ * @returns Promise resolving to whether the contract implements ERC721Metadata interface.
10
+ */
11
+ contractSupportsMetadataInterface: (address: string) => Promise<boolean>;
12
+ /**
13
+ * Query if contract implements ERC721Enumerable interface.
14
+ *
15
+ * @param address - ERC721 asset contract address.
16
+ * @returns Promise resolving to whether the contract implements ERC721Enumerable interface.
17
+ */
18
+ contractSupportsEnumerableInterface: (address: string) => Promise<boolean>;
19
+ /**
20
+ * Query if contract implements ERC721 interface.
21
+ *
22
+ * @param address - ERC721 asset contract address.
23
+ * @returns Promise resolving to whether the contract implements ERC721 interface.
24
+ */
25
+ contractSupportsBase721Interface: (address: string) => Promise<boolean>;
26
+ /**
27
+ * Enumerate assets assigned to an owner.
28
+ *
29
+ * @param address - ERC721 asset contract address.
30
+ * @param selectedAddress - Current account public address.
31
+ * @param index - An NFT counter less than `balanceOf(selectedAddress)`.
32
+ * @returns Promise resolving to token identifier for the 'index'th asset assigned to 'selectedAddress'.
33
+ */
34
+ getNftTokenId: (address: string, selectedAddress: string, index: number) => Promise<string>;
35
+ /**
36
+ * Query for tokenURI for a given asset.
37
+ *
38
+ * @param address - ERC721 asset contract address.
39
+ * @param tokenId - ERC721 asset identifier.
40
+ * @returns Promise resolving to the 'tokenURI'.
41
+ */
42
+ getTokenURI: (address: string, tokenId: string) => Promise<string>;
43
+ /**
44
+ * Query for name for a given asset.
45
+ *
46
+ * @param address - ERC721 asset contract address.
47
+ * @returns Promise resolving to the 'name'.
48
+ */
49
+ getAssetName: (address: string) => Promise<string>;
50
+ /**
51
+ * Query for symbol for a given asset.
52
+ *
53
+ * @param address - ERC721 asset contract address.
54
+ * @returns Promise resolving to the 'symbol'.
55
+ */
56
+ getAssetSymbol: (address: string) => Promise<string>;
57
+ /**
58
+ * Query for owner for a given ERC721 asset.
59
+ *
60
+ * @param address - ERC721 asset contract address.
61
+ * @param tokenId - ERC721 asset identifier.
62
+ * @returns Promise resolving to the owner address.
63
+ */
64
+ getOwnerOf(address: string, tokenId: string): Promise<string>;
65
+ /**
66
+ * Query if a contract implements an interface.
67
+ *
68
+ * @param address - Asset contract address.
69
+ * @param interfaceId - Interface identifier.
70
+ * @returns Promise resolving to whether the contract implements `interfaceID`.
71
+ */
72
+ private contractSupportsInterface;
73
+ /**
74
+ * Query if a contract implements an interface.
75
+ *
76
+ * @param address - Asset contract address.
77
+ * @param ipfsGateway - The user's preferred IPFS gateway.
78
+ * @param tokenId - tokenId of a given token in the contract.
79
+ * @returns Promise resolving an object containing the standard, tokenURI, symbol and name of the given contract/tokenId pair.
80
+ */
81
+ getDetails: (address: string, ipfsGateway: string, tokenId?: string | undefined) => Promise<{
82
+ standard: string;
83
+ tokenURI: string | undefined;
84
+ symbol: string | undefined;
85
+ name: string | undefined;
86
+ image: string | undefined;
87
+ }>;
88
+ }
@@ -0,0 +1,182 @@
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.ERC721Standard = void 0;
13
+ const metamask_eth_abis_1 = require("@metamask/metamask-eth-abis");
14
+ const contracts_1 = require("@ethersproject/contracts");
15
+ const controller_utils_1 = require("@metamask/controller-utils");
16
+ const assetsUtil_1 = require("../../../assetsUtil");
17
+ class ERC721Standard {
18
+ constructor(provider) {
19
+ /**
20
+ * Query if contract implements ERC721Metadata interface.
21
+ *
22
+ * @param address - ERC721 asset contract address.
23
+ * @returns Promise resolving to whether the contract implements ERC721Metadata interface.
24
+ */
25
+ this.contractSupportsMetadataInterface = (address) => __awaiter(this, void 0, void 0, function* () {
26
+ return this.contractSupportsInterface(address, controller_utils_1.ERC721_METADATA_INTERFACE_ID);
27
+ });
28
+ /**
29
+ * Query if contract implements ERC721Enumerable interface.
30
+ *
31
+ * @param address - ERC721 asset contract address.
32
+ * @returns Promise resolving to whether the contract implements ERC721Enumerable interface.
33
+ */
34
+ this.contractSupportsEnumerableInterface = (address) => __awaiter(this, void 0, void 0, function* () {
35
+ return this.contractSupportsInterface(address, controller_utils_1.ERC721_ENUMERABLE_INTERFACE_ID);
36
+ });
37
+ /**
38
+ * Query if contract implements ERC721 interface.
39
+ *
40
+ * @param address - ERC721 asset contract address.
41
+ * @returns Promise resolving to whether the contract implements ERC721 interface.
42
+ */
43
+ this.contractSupportsBase721Interface = (address) => __awaiter(this, void 0, void 0, function* () {
44
+ return this.contractSupportsInterface(address, controller_utils_1.ERC721_INTERFACE_ID);
45
+ });
46
+ /**
47
+ * Enumerate assets assigned to an owner.
48
+ *
49
+ * @param address - ERC721 asset contract address.
50
+ * @param selectedAddress - Current account public address.
51
+ * @param index - An NFT counter less than `balanceOf(selectedAddress)`.
52
+ * @returns Promise resolving to token identifier for the 'index'th asset assigned to 'selectedAddress'.
53
+ */
54
+ this.getNftTokenId = (address, selectedAddress, index) => __awaiter(this, void 0, void 0, function* () {
55
+ const contract = new contracts_1.Contract(address, metamask_eth_abis_1.abiERC721, this.provider);
56
+ return contract.tokenOfOwnerByIndex(selectedAddress, index);
57
+ });
58
+ /**
59
+ * Query for tokenURI for a given asset.
60
+ *
61
+ * @param address - ERC721 asset contract address.
62
+ * @param tokenId - ERC721 asset identifier.
63
+ * @returns Promise resolving to the 'tokenURI'.
64
+ */
65
+ this.getTokenURI = (address, tokenId) => __awaiter(this, void 0, void 0, function* () {
66
+ const contract = new contracts_1.Contract(address, metamask_eth_abis_1.abiERC721, this.provider);
67
+ const supportsMetadata = yield this.contractSupportsMetadataInterface(address);
68
+ if (!supportsMetadata) {
69
+ throw new Error('Contract does not support ERC721 metadata interface.');
70
+ }
71
+ return contract.tokenURI(tokenId);
72
+ });
73
+ /**
74
+ * Query for name for a given asset.
75
+ *
76
+ * @param address - ERC721 asset contract address.
77
+ * @returns Promise resolving to the 'name'.
78
+ */
79
+ this.getAssetName = (address) => __awaiter(this, void 0, void 0, function* () {
80
+ const contract = new contracts_1.Contract(address, metamask_eth_abis_1.abiERC721, this.provider);
81
+ return contract.name();
82
+ });
83
+ /**
84
+ * Query for symbol for a given asset.
85
+ *
86
+ * @param address - ERC721 asset contract address.
87
+ * @returns Promise resolving to the 'symbol'.
88
+ */
89
+ this.getAssetSymbol = (address) => __awaiter(this, void 0, void 0, function* () {
90
+ const contract = new contracts_1.Contract(address, metamask_eth_abis_1.abiERC721, this.provider);
91
+ return contract.symbol();
92
+ });
93
+ /**
94
+ * Query if a contract implements an interface.
95
+ *
96
+ * @param address - Asset contract address.
97
+ * @param interfaceId - Interface identifier.
98
+ * @returns Promise resolving to whether the contract implements `interfaceID`.
99
+ */
100
+ this.contractSupportsInterface = (address, interfaceId) => __awaiter(this, void 0, void 0, function* () {
101
+ const contract = new contracts_1.Contract(address, metamask_eth_abis_1.abiERC721, this.provider);
102
+ try {
103
+ return yield contract.supportsInterface(interfaceId);
104
+ }
105
+ catch (err) {
106
+ // Mirror previous implementation
107
+ if (err.message.includes('call revert exception')) {
108
+ return false;
109
+ }
110
+ throw err;
111
+ }
112
+ });
113
+ /**
114
+ * Query if a contract implements an interface.
115
+ *
116
+ * @param address - Asset contract address.
117
+ * @param ipfsGateway - The user's preferred IPFS gateway.
118
+ * @param tokenId - tokenId of a given token in the contract.
119
+ * @returns Promise resolving an object containing the standard, tokenURI, symbol and name of the given contract/tokenId pair.
120
+ */
121
+ this.getDetails = (address, ipfsGateway, tokenId) => __awaiter(this, void 0, void 0, function* () {
122
+ const isERC721 = yield this.contractSupportsBase721Interface(address);
123
+ if (!isERC721) {
124
+ throw new Error("This isn't a valid ERC721 contract");
125
+ }
126
+ let tokenURI, image, symbol, name;
127
+ // TODO upgrade to use Promise.allSettled for name/symbol when we can refactor to use es2020 in tsconfig
128
+ try {
129
+ symbol = yield this.getAssetSymbol(address);
130
+ }
131
+ catch (_a) {
132
+ // ignore
133
+ }
134
+ try {
135
+ name = yield this.getAssetName(address);
136
+ }
137
+ catch (_b) {
138
+ // ignore
139
+ }
140
+ if (tokenId) {
141
+ try {
142
+ tokenURI = yield this.getTokenURI(address, tokenId);
143
+ if (tokenURI.startsWith('ipfs://')) {
144
+ tokenURI = (0, assetsUtil_1.getFormattedIpfsUrl)(ipfsGateway, tokenURI, true);
145
+ }
146
+ const response = yield (0, controller_utils_1.timeoutFetch)(tokenURI);
147
+ const object = yield response.json();
148
+ image = object === null || object === void 0 ? void 0 : object.image;
149
+ if (image === null || image === void 0 ? void 0 : image.startsWith('ipfs://')) {
150
+ image = (0, assetsUtil_1.getFormattedIpfsUrl)(ipfsGateway, image, true);
151
+ }
152
+ }
153
+ catch (_c) {
154
+ // ignore
155
+ }
156
+ }
157
+ return {
158
+ standard: controller_utils_1.ERC721,
159
+ tokenURI,
160
+ symbol,
161
+ name,
162
+ image,
163
+ };
164
+ });
165
+ this.provider = provider;
166
+ }
167
+ /**
168
+ * Query for owner for a given ERC721 asset.
169
+ *
170
+ * @param address - ERC721 asset contract address.
171
+ * @param tokenId - ERC721 asset identifier.
172
+ * @returns Promise resolving to the owner address.
173
+ */
174
+ getOwnerOf(address, tokenId) {
175
+ return __awaiter(this, void 0, void 0, function* () {
176
+ const contract = new contracts_1.Contract(address, metamask_eth_abis_1.abiERC721, this.provider);
177
+ return contract.ownerOf(tokenId);
178
+ });
179
+ }
180
+ }
181
+ exports.ERC721Standard = ERC721Standard;
182
+ //# sourceMappingURL=ERC721Standard.js.map