@metamask-previews/assets-controllers 11.0.1-preview.3dbf14f
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 +235 -0
- package/LICENSE +20 -0
- package/README.md +30 -0
- package/dist/AccountTrackerController.d.ts +106 -0
- package/dist/AccountTrackerController.d.ts.map +1 -0
- package/dist/AccountTrackerController.js +163 -0
- package/dist/AccountTrackerController.js.map +1 -0
- package/dist/AssetsContractController.d.ts +188 -0
- package/dist/AssetsContractController.d.ts.map +1 -0
- package/dist/AssetsContractController.js +330 -0
- package/dist/AssetsContractController.js.map +1 -0
- package/dist/CurrencyRateController.d.ts +101 -0
- package/dist/CurrencyRateController.d.ts.map +1 -0
- package/dist/CurrencyRateController.js +219 -0
- package/dist/CurrencyRateController.js.map +1 -0
- package/dist/NftController.d.ts +466 -0
- package/dist/NftController.d.ts.map +1 -0
- package/dist/NftController.js +943 -0
- package/dist/NftController.js.map +1 -0
- package/dist/NftDetectionController.d.ts +182 -0
- package/dist/NftDetectionController.d.ts.map +1 -0
- package/dist/NftDetectionController.js +188 -0
- package/dist/NftDetectionController.js.map +1 -0
- package/dist/Standards/ERC20Standard.d.ts +50 -0
- package/dist/Standards/ERC20Standard.d.ts.map +1 -0
- package/dist/Standards/ERC20Standard.js +144 -0
- package/dist/Standards/ERC20Standard.js.map +1 -0
- package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.d.ts +79 -0
- package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.d.ts.map +1 -0
- package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.js +148 -0
- package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.js.map +1 -0
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.d.ts +89 -0
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.d.ts.map +1 -0
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.js +182 -0
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.js.map +1 -0
- package/dist/Standards/standards-types.d.ts +15 -0
- package/dist/Standards/standards-types.d.ts.map +1 -0
- package/dist/Standards/standards-types.js +3 -0
- package/dist/Standards/standards-types.js.map +1 -0
- package/dist/TokenBalancesController.d.ts +71 -0
- package/dist/TokenBalancesController.d.ts.map +1 -0
- package/dist/TokenBalancesController.js +94 -0
- package/dist/TokenBalancesController.js.map +1 -0
- package/dist/TokenDetectionController.d.ts +87 -0
- package/dist/TokenDetectionController.d.ts.map +1 -0
- package/dist/TokenDetectionController.js +189 -0
- package/dist/TokenDetectionController.js.map +1 -0
- package/dist/TokenListController.d.ts +117 -0
- package/dist/TokenListController.d.ts.map +1 -0
- package/dist/TokenListController.js +248 -0
- package/dist/TokenListController.js.map +1 -0
- package/dist/TokenRatesController.d.ts +172 -0
- package/dist/TokenRatesController.d.ts.map +1 -0
- package/dist/TokenRatesController.js +327 -0
- package/dist/TokenRatesController.js.map +1 -0
- package/dist/TokensController.d.ts +250 -0
- package/dist/TokensController.d.ts.map +1 -0
- package/dist/TokensController.js +550 -0
- package/dist/TokensController.js.map +1 -0
- package/dist/assetsUtil.d.ts +109 -0
- package/dist/assetsUtil.d.ts.map +1 -0
- package/dist/assetsUtil.js +227 -0
- package/dist/assetsUtil.js.map +1 -0
- package/dist/constants.d.ts +6 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +10 -0
- package/dist/constants.js.map +1 -0
- package/dist/crypto-compare.d.ts +13 -0
- package/dist/crypto-compare.d.ts.map +1 -0
- package/dist/crypto-compare.js +67 -0
- package/dist/crypto-compare.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/token-service.d.ts +31 -0
- package/dist/token-service.d.ts.map +1 -0
- package/dist/token-service.js +134 -0
- package/dist/token-service.js.map +1 -0
- package/package.json +82 -0
|
@@ -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 contracts_1 = require("@ethersproject/contracts");
|
|
14
|
+
const controller_utils_1 = require("@metamask/controller-utils");
|
|
15
|
+
const metamask_eth_abis_1 = require("@metamask/metamask-eth-abis");
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ERC721Standard.js","sourceRoot":"","sources":["../../../../src/Standards/NftStandards/ERC721/ERC721Standard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,wDAAoD;AAEpD,iEAMoC;AACpC,mEAAwD;AAExD,oDAA0D;AAE1D,MAAa,cAAc;IAGzB,YAAY,QAAsB;QAIlC;;;;;WAKG;QACH,sCAAiC,GAAG,CAClC,OAAe,EACG,EAAE;YACpB,OAAO,IAAI,CAAC,yBAAyB,CACnC,OAAO,EACP,+CAA4B,CAC7B,CAAC;QACJ,CAAC,CAAA,CAAC;QAEF;;;;;WAKG;QACH,wCAAmC,GAAG,CACpC,OAAe,EACG,EAAE;YACpB,OAAO,IAAI,CAAC,yBAAyB,CACnC,OAAO,EACP,iDAA8B,CAC/B,CAAC;QACJ,CAAC,CAAA,CAAC;QAEF;;;;;WAKG;QACH,qCAAgC,GAAG,CACjC,OAA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AA,gCAAmB,EAAC,WAAW,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;qBAC7D;oBAED,MAAM,QAAQ,GAAG,MAAM,IAAA,+BAAY,EAAC,QAAQ,CAAC,CAAC;oBAC9C,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;oBACrC,KAAK,GAAG,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,KAAK,CAAC;oBACtB,IAAI,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,UAAU,CAAC,SAAS,CAAC,EAAE;wBAChC,KAAK,GAAG,IAAA,gCAAmB,EAAC,WAAW,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;qBACvD;iBACF;gBAAC,WAAM;oBACN,SAAS;iBACV;aACF;YAED,OAAO;gBACL,QAAQ,EAAE,yBAAM;gBAChB,QAAQ;gBACR,MAAM;gBACN,IAAI;gBACJ,KAAK;aACN,CAAC;QACJ,CAAC,CAAA,CAAC;QAzMA,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAqGD;;;;;;OAMG;IACG,UAAU,CAAC,OAAe,EAAE,OAAe;;YAC/C,MAAM,QAAQ,GAAG,IAAI,oBAAQ,CAAC,OAAO,EAAE,6BAAS,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;YACjE,OAAO,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QACnC,CAAC;KAAA;CA0FF;AA9MD,wCA8MC","sourcesContent":["import { Contract } from '@ethersproject/contracts';\nimport type { Web3Provider } from '@ethersproject/providers';\nimport {\n timeoutFetch,\n ERC721_INTERFACE_ID,\n ERC721_METADATA_INTERFACE_ID,\n ERC721_ENUMERABLE_INTERFACE_ID,\n ERC721,\n} from '@metamask/controller-utils';\nimport { abiERC721 } from '@metamask/metamask-eth-abis';\n\nimport { getFormattedIpfsUrl } from '../../../assetsUtil';\n\nexport class ERC721Standard {\n private readonly provider: Web3Provider;\n\n constructor(provider: Web3Provider) {\n this.provider = provider;\n }\n\n /**\n * Query if contract implements ERC721Metadata interface.\n *\n * @param address - ERC721 asset contract address.\n * @returns Promise resolving to whether the contract implements ERC721Metadata interface.\n */\n contractSupportsMetadataInterface = async (\n address: string,\n ): Promise<boolean> => {\n return this.contractSupportsInterface(\n address,\n ERC721_METADATA_INTERFACE_ID,\n );\n };\n\n /**\n * Query if contract implements ERC721Enumerable interface.\n *\n * @param address - ERC721 asset contract address.\n * @returns Promise resolving to whether the contract implements ERC721Enumerable interface.\n */\n contractSupportsEnumerableInterface = async (\n address: string,\n ): Promise<boolean> => {\n return this.contractSupportsInterface(\n address,\n ERC721_ENUMERABLE_INTERFACE_ID,\n );\n };\n\n /**\n * Query if contract implements ERC721 interface.\n *\n * @param address - ERC721 asset contract address.\n * @returns Promise resolving to whether the contract implements ERC721 interface.\n */\n contractSupportsBase721Interface = async (\n address: string,\n ): Promise<boolean> => {\n return this.contractSupportsInterface(address, ERC721_INTERFACE_ID);\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 getNftTokenId = async (\n address: string,\n selectedAddress: string,\n index: number,\n ): Promise<string> => {\n const contract = new Contract(address, abiERC721, this.provider);\n return contract.tokenOfOwnerByIndex(selectedAddress, index);\n };\n\n /**\n * Query for tokenURI for a given asset.\n *\n * @param address - ERC721 asset contract address.\n * @param tokenId - ERC721 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, abiERC721, this.provider);\n const supportsMetadata = await this.contractSupportsMetadataInterface(\n address,\n );\n if (!supportsMetadata) {\n throw new Error('Contract does not support ERC721 metadata interface.');\n }\n return contract.tokenURI(tokenId);\n };\n\n /**\n * Query for name for a given asset.\n *\n * @param address - ERC721 asset contract address.\n * @returns Promise resolving to the 'name'.\n */\n getAssetName = async (address: string): Promise<string> => {\n const contract = new Contract(address, abiERC721, this.provider);\n return contract.name();\n };\n\n /**\n * Query for symbol for a given asset.\n *\n * @param address - ERC721 asset contract address.\n * @returns Promise resolving to the 'symbol'.\n */\n getAssetSymbol = async (address: string): Promise<string> => {\n const contract = new Contract(address, abiERC721, this.provider);\n return contract.symbol();\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 getOwnerOf(address: string, tokenId: string): Promise<string> {\n const contract = new Contract(address, abiERC721, this.provider);\n return contract.ownerOf(tokenId);\n }\n\n /**\n * Query if a contract implements an interface.\n *\n * @param address - Asset contract address.\n * @param interfaceId - Interface identifier.\n * @returns Promise resolving to whether the contract implements `interfaceID`.\n */\n private readonly contractSupportsInterface = async (\n address: string,\n interfaceId: string,\n ): Promise<boolean> => {\n const contract = new Contract(address, abiERC721, this.provider);\n try {\n return await contract.supportsInterface(interfaceId);\n } catch (err: any) {\n // Mirror previous implementation\n if (err.message.includes('call revert exception')) {\n return false;\n }\n throw err;\n }\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 symbol: string | undefined;\n name: string | undefined;\n image: string | undefined;\n }> => {\n const isERC721 = await this.contractSupportsBase721Interface(address);\n if (!isERC721) {\n throw new Error(\"This isn't a valid ERC721 contract\");\n }\n\n let tokenURI, image, symbol, name;\n\n // TODO upgrade to use Promise.allSettled for name/symbol when we can refactor to use es2020 in tsconfig\n try {\n symbol = await this.getAssetSymbol(address);\n } catch {\n // ignore\n }\n\n try {\n name = await this.getAssetName(address);\n } catch {\n // ignore\n }\n\n if (tokenId) {\n try {\n tokenURI = await this.getTokenURI(address, tokenId);\n if (tokenURI.startsWith('ipfs://')) {\n tokenURI = getFormattedIpfsUrl(ipfsGateway, tokenURI, true);\n }\n\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 return {\n standard: ERC721,\n tokenURI,\n symbol,\n name,\n image,\n };\n };\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { abiERC20, abiERC1155, abiERC721 } from '@metamask/metamask-eth-abis';
|
|
2
|
+
declare type Contract = {
|
|
3
|
+
at(address: string): any;
|
|
4
|
+
};
|
|
5
|
+
export declare type Web3 = {
|
|
6
|
+
eth: {
|
|
7
|
+
call(payload: {
|
|
8
|
+
to: string;
|
|
9
|
+
data: string;
|
|
10
|
+
}, block: undefined, callback: (error: Error, result: string) => void): void;
|
|
11
|
+
contract(abi: typeof abiERC20 | typeof abiERC721 | typeof abiERC1155): Contract;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=standards-types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standards-types.d.ts","sourceRoot":"","sources":["../../src/Standards/standards-types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,QAAQ,EACR,UAAU,EACV,SAAS,EACV,MAAM,6BAA6B,CAAC;AAErC,aAAK,QAAQ,GAAG;IACd,EAAE,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC;CAC1B,CAAC;AAEF,oBAAY,IAAI,GAAG;IACjB,GAAG,EAAE;QACH,IAAI,CACF,OAAO,EAAE;YAAE,EAAE,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,MAAM,CAAA;SAAE,EACrC,KAAK,EAAE,SAAS,EAChB,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,GAC/C,IAAI,CAAC;QACR,QAAQ,CACN,GAAG,EAAE,OAAO,QAAQ,GAAG,OAAO,SAAS,GAAG,OAAO,UAAU,GAC1D,QAAQ,CAAC;KACb,CAAC;CACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standards-types.js","sourceRoot":"","sources":["../../src/Standards/standards-types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n abiERC20,\n abiERC1155,\n abiERC721,\n} from '@metamask/metamask-eth-abis';\n\ntype Contract = {\n at(address: string): any;\n};\n\nexport type Web3 = {\n eth: {\n call(\n payload: { to: string; data: string },\n block: undefined,\n callback: (error: Error, result: string) => void,\n ): void;\n contract(\n abi: typeof abiERC20 | typeof abiERC721 | typeof abiERC1155,\n ): Contract;\n };\n};\n"]}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
2
|
+
import type { BaseConfig, BaseState } from '@metamask/base-controller';
|
|
3
|
+
import { BaseController } from '@metamask/base-controller';
|
|
4
|
+
import type { PreferencesState } from '@metamask/preferences-controller';
|
|
5
|
+
import { BN } from 'ethereumjs-util';
|
|
6
|
+
import type { AssetsContractController } from './AssetsContractController';
|
|
7
|
+
import type { Token } from './TokenRatesController';
|
|
8
|
+
import type { TokensState } from './TokensController';
|
|
9
|
+
export { BN };
|
|
10
|
+
/**
|
|
11
|
+
* @type TokenBalancesConfig
|
|
12
|
+
*
|
|
13
|
+
* Token balances controller configuration
|
|
14
|
+
* @property interval - Polling interval used to fetch new token balances
|
|
15
|
+
* @property tokens - List of tokens to track balances for
|
|
16
|
+
*/
|
|
17
|
+
export interface TokenBalancesConfig extends BaseConfig {
|
|
18
|
+
interval: number;
|
|
19
|
+
tokens: Token[];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* @type TokenBalancesState
|
|
23
|
+
*
|
|
24
|
+
* Token balances controller state
|
|
25
|
+
* @property contractBalances - Hash of token contract addresses to balances
|
|
26
|
+
*/
|
|
27
|
+
export interface TokenBalancesState extends BaseState {
|
|
28
|
+
contractBalances: {
|
|
29
|
+
[address: string]: BN;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Controller that passively polls on a set interval token balances
|
|
34
|
+
* for tokens stored in the TokensController
|
|
35
|
+
*/
|
|
36
|
+
export declare class TokenBalancesController extends BaseController<TokenBalancesConfig, TokenBalancesState> {
|
|
37
|
+
private handle?;
|
|
38
|
+
/**
|
|
39
|
+
* Name of this controller used during composition
|
|
40
|
+
*/
|
|
41
|
+
name: string;
|
|
42
|
+
private readonly getSelectedAddress;
|
|
43
|
+
private readonly getERC20BalanceOf;
|
|
44
|
+
/**
|
|
45
|
+
* Creates a TokenBalancesController instance.
|
|
46
|
+
*
|
|
47
|
+
* @param options - The controller options.
|
|
48
|
+
* @param options.onTokensStateChange - Allows subscribing to assets controller state changes.
|
|
49
|
+
* @param options.getSelectedAddress - Gets the current selected address.
|
|
50
|
+
* @param options.getERC20BalanceOf - Gets the balance of the given account at the given contract address.
|
|
51
|
+
* @param config - Initial options used to configure this controller.
|
|
52
|
+
* @param state - Initial state to set on this controller.
|
|
53
|
+
*/
|
|
54
|
+
constructor({ onTokensStateChange, getSelectedAddress, getERC20BalanceOf, }: {
|
|
55
|
+
onTokensStateChange: (listener: (tokenState: TokensState) => void) => void;
|
|
56
|
+
getSelectedAddress: () => PreferencesState['selectedAddress'];
|
|
57
|
+
getERC20BalanceOf: AssetsContractController['getERC20BalanceOf'];
|
|
58
|
+
}, config?: Partial<TokenBalancesConfig>, state?: Partial<TokenBalancesState>);
|
|
59
|
+
/**
|
|
60
|
+
* Starts a new polling interval.
|
|
61
|
+
*
|
|
62
|
+
* @param interval - Polling interval used to fetch new token balances.
|
|
63
|
+
*/
|
|
64
|
+
poll(interval?: number): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Updates balances for all tokens.
|
|
67
|
+
*/
|
|
68
|
+
updateBalances(): Promise<void>;
|
|
69
|
+
}
|
|
70
|
+
export default TokenBalancesController;
|
|
71
|
+
//# sourceMappingURL=TokenBalancesController.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TokenBalancesController.d.ts","sourceRoot":"","sources":["../src/TokenBalancesController.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACvE,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAE3D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACzE,OAAO,EAAE,EAAE,EAAE,MAAM,iBAAiB,CAAC;AAErC,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AAC3E,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAGtD,OAAO,EAAE,EAAE,EAAE,CAAC;AAEd;;;;;;GAMG;AACH,MAAM,WAAW,mBAAoB,SAAQ,UAAU;IACrD,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,KAAK,EAAE,CAAC;CACjB;AAED;;;;;GAKG;AACH,MAAM,WAAW,kBAAmB,SAAQ,SAAS;IACnD,gBAAgB,EAAE;QAAE,CAAC,OAAO,EAAE,MAAM,GAAG,EAAE,CAAA;KAAE,CAAC;CAC7C;AAED;;;GAGG;AACH,qBAAa,uBAAwB,SAAQ,cAAc,CACzD,mBAAmB,EACnB,kBAAkB,CACnB;IACC,OAAO,CAAC,MAAM,CAAC,CAAgC;IAE/C;;OAEG;IACM,IAAI,SAA6B;IAE1C,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAA4C;IAE/E,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAgD;IAElF;;;;;;;;;OASG;gBAED,EACE,mBAAmB,EACnB,kBAAkB,EAClB,iBAAiB,GAClB,EAAE;QACD,mBAAmB,EAAE,CACnB,QAAQ,EAAE,CAAC,UAAU,EAAE,WAAW,KAAK,IAAI,KACxC,IAAI,CAAC;QACV,kBAAkB,EAAE,MAAM,gBAAgB,CAAC,iBAAiB,CAAC,CAAC;QAC9D,iBAAiB,EAAE,wBAAwB,CAAC,mBAAmB,CAAC,CAAC;KAClE,EACD,MAAM,CAAC,EAAE,OAAO,CAAC,mBAAmB,CAAC,EACrC,KAAK,CAAC,EAAE,OAAO,CAAC,kBAAkB,CAAC;IAkBrC;;;;OAIG;IACG,IAAI,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAS5C;;OAEG;IACG,cAAc;CAqBrB;AAED,eAAe,uBAAuB,CAAC"}
|
|
@@ -0,0 +1,94 @@
|
|
|
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.TokenBalancesController = exports.BN = void 0;
|
|
13
|
+
const base_controller_1 = require("@metamask/base-controller");
|
|
14
|
+
const controller_utils_1 = require("@metamask/controller-utils");
|
|
15
|
+
const ethereumjs_util_1 = require("ethereumjs-util");
|
|
16
|
+
Object.defineProperty(exports, "BN", { enumerable: true, get: function () { return ethereumjs_util_1.BN; } });
|
|
17
|
+
/**
|
|
18
|
+
* Controller that passively polls on a set interval token balances
|
|
19
|
+
* for tokens stored in the TokensController
|
|
20
|
+
*/
|
|
21
|
+
class TokenBalancesController extends base_controller_1.BaseController {
|
|
22
|
+
/**
|
|
23
|
+
* Creates a TokenBalancesController instance.
|
|
24
|
+
*
|
|
25
|
+
* @param options - The controller options.
|
|
26
|
+
* @param options.onTokensStateChange - Allows subscribing to assets controller state changes.
|
|
27
|
+
* @param options.getSelectedAddress - Gets the current selected address.
|
|
28
|
+
* @param options.getERC20BalanceOf - Gets the balance of the given account at the given contract address.
|
|
29
|
+
* @param config - Initial options used to configure this controller.
|
|
30
|
+
* @param state - Initial state to set on this controller.
|
|
31
|
+
*/
|
|
32
|
+
constructor({ onTokensStateChange, getSelectedAddress, getERC20BalanceOf, }, config, state) {
|
|
33
|
+
super(config, state);
|
|
34
|
+
/**
|
|
35
|
+
* Name of this controller used during composition
|
|
36
|
+
*/
|
|
37
|
+
this.name = 'TokenBalancesController';
|
|
38
|
+
this.defaultConfig = {
|
|
39
|
+
interval: 180000,
|
|
40
|
+
tokens: [],
|
|
41
|
+
};
|
|
42
|
+
this.defaultState = { contractBalances: {} };
|
|
43
|
+
this.initialize();
|
|
44
|
+
onTokensStateChange(({ tokens, detectedTokens }) => {
|
|
45
|
+
this.configure({ tokens: [...tokens, ...detectedTokens] });
|
|
46
|
+
this.updateBalances();
|
|
47
|
+
});
|
|
48
|
+
this.getSelectedAddress = getSelectedAddress;
|
|
49
|
+
this.getERC20BalanceOf = getERC20BalanceOf;
|
|
50
|
+
this.poll();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Starts a new polling interval.
|
|
54
|
+
*
|
|
55
|
+
* @param interval - Polling interval used to fetch new token balances.
|
|
56
|
+
*/
|
|
57
|
+
poll(interval) {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
interval && this.configure({ interval }, false, false);
|
|
60
|
+
this.handle && clearTimeout(this.handle);
|
|
61
|
+
yield (0, controller_utils_1.safelyExecute)(() => this.updateBalances());
|
|
62
|
+
this.handle = setTimeout(() => {
|
|
63
|
+
this.poll(this.config.interval);
|
|
64
|
+
}, this.config.interval);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Updates balances for all tokens.
|
|
69
|
+
*/
|
|
70
|
+
updateBalances() {
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
if (this.disabled) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const { tokens } = this.config;
|
|
76
|
+
const newContractBalances = {};
|
|
77
|
+
for (const i in tokens) {
|
|
78
|
+
const { address } = tokens[i];
|
|
79
|
+
try {
|
|
80
|
+
newContractBalances[address] = yield this.getERC20BalanceOf(address, this.getSelectedAddress());
|
|
81
|
+
tokens[i].balanceError = null;
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
newContractBalances[address] = new ethereumjs_util_1.BN(0);
|
|
85
|
+
tokens[i].balanceError = error;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
this.update({ contractBalances: newContractBalances });
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.TokenBalancesController = TokenBalancesController;
|
|
93
|
+
exports.default = TokenBalancesController;
|
|
94
|
+
//# sourceMappingURL=TokenBalancesController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TokenBalancesController.js","sourceRoot":"","sources":["../src/TokenBalancesController.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,+DAA2D;AAC3D,iEAA2D;AAE3D,qDAAqC;AAO5B,mFAPA,oBAAE,OAOA;AAwBX;;;GAGG;AACH,MAAa,uBAAwB,SAAQ,gCAG5C;IAYC;;;;;;;;;OASG;IACH,YACE,EACE,mBAAmB,EACnB,kBAA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IAED;;OAEG;IACG,cAAc;;YAClB,IAAI,IAAI,CAAC,QAAQ,EAAE;gBACjB,OAAO;aACR;YACD,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC;YAC/B,MAAM,mBAAmB,GAA8B,EAAE,CAAC;YAC1D,KAAK,MAAM,CAAC,IAAI,MAAM,EAAE;gBACtB,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;gBAC9B,IAAI;oBACF,mBAAmB,CAAC,OAAO,CAAC,GAAG,MAAM,IAAI,CAAC,iBAAiB,CACzD,OAAO,EACP,IAAI,CAAC,kBAAkB,EAAE,CAC1B,CAAC;oBACF,MAAM,CAAC,CAAC,CAAC,CAAC,YAAY,GAAG,IAAI,CAAC;iBAC/B;gBAAC,OAAO,KAAK,EAAE;oBACd,mBAAmB,CAAC,OAAO,CAAC,GAAG,IAAI,oBAAE,CAAC,CAAC,CAAC,CAAC;oBACzC,MAAM,CAAC,CAAC,CAAC,CAAC,YAAY,GAAG,KAAK,CAAC;iBAChC;aACF;YACD,IAAI,CAAC,MAAM,CAAC,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,CAAC,CAAC;QACzD,CAAC;KAAA;CACF;AA9FD,0DA8FC;AAED,kBAAe,uBAAuB,CAAC","sourcesContent":["import type { BaseConfig, BaseState } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { safelyExecute } from '@metamask/controller-utils';\nimport type { PreferencesState } from '@metamask/preferences-controller';\nimport { BN } from 'ethereumjs-util';\n\nimport type { AssetsContractController } from './AssetsContractController';\nimport type { Token } from './TokenRatesController';\nimport type { TokensState } from './TokensController';\n\n// TODO: Remove this export in the next major release\nexport { BN };\n\n/**\n * @type TokenBalancesConfig\n *\n * Token balances controller configuration\n * @property interval - Polling interval used to fetch new token balances\n * @property tokens - List of tokens to track balances for\n */\nexport interface TokenBalancesConfig extends BaseConfig {\n interval: number;\n tokens: Token[];\n}\n\n/**\n * @type TokenBalancesState\n *\n * Token balances controller state\n * @property contractBalances - Hash of token contract addresses to balances\n */\nexport interface TokenBalancesState extends BaseState {\n contractBalances: { [address: string]: BN };\n}\n\n/**\n * Controller that passively polls on a set interval token balances\n * for tokens stored in the TokensController\n */\nexport class TokenBalancesController extends BaseController<\n TokenBalancesConfig,\n TokenBalancesState\n> {\n private handle?: ReturnType<typeof setTimeout>;\n\n /**\n * Name of this controller used during composition\n */\n override name = 'TokenBalancesController';\n\n private readonly getSelectedAddress: () => PreferencesState['selectedAddress'];\n\n private readonly getERC20BalanceOf: AssetsContractController['getERC20BalanceOf'];\n\n /**\n * Creates a TokenBalancesController instance.\n *\n * @param options - The controller options.\n * @param options.onTokensStateChange - Allows subscribing to assets controller state changes.\n * @param options.getSelectedAddress - Gets the current selected address.\n * @param options.getERC20BalanceOf - Gets the balance of the given account at the given contract address.\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 onTokensStateChange,\n getSelectedAddress,\n getERC20BalanceOf,\n }: {\n onTokensStateChange: (\n listener: (tokenState: TokensState) => void,\n ) => void;\n getSelectedAddress: () => PreferencesState['selectedAddress'];\n getERC20BalanceOf: AssetsContractController['getERC20BalanceOf'];\n },\n config?: Partial<TokenBalancesConfig>,\n state?: Partial<TokenBalancesState>,\n ) {\n super(config, state);\n this.defaultConfig = {\n interval: 180000,\n tokens: [],\n };\n this.defaultState = { contractBalances: {} };\n this.initialize();\n onTokensStateChange(({ tokens, detectedTokens }) => {\n this.configure({ tokens: [...tokens, ...detectedTokens] });\n this.updateBalances();\n });\n this.getSelectedAddress = getSelectedAddress;\n this.getERC20BalanceOf = getERC20BalanceOf;\n this.poll();\n }\n\n /**\n * Starts a new polling interval.\n *\n * @param interval - Polling interval used to fetch new token balances.\n */\n async poll(interval?: number): Promise<void> {\n interval && this.configure({ interval }, false, false);\n this.handle && clearTimeout(this.handle);\n await safelyExecute(() => this.updateBalances());\n this.handle = setTimeout(() => {\n this.poll(this.config.interval);\n }, this.config.interval);\n }\n\n /**\n * Updates balances for all tokens.\n */\n async updateBalances() {\n if (this.disabled) {\n return;\n }\n const { tokens } = this.config;\n const newContractBalances: { [address: string]: BN } = {};\n for (const i in tokens) {\n const { address } = tokens[i];\n try {\n newContractBalances[address] = await this.getERC20BalanceOf(\n address,\n this.getSelectedAddress(),\n );\n tokens[i].balanceError = null;\n } catch (error) {\n newContractBalances[address] = new BN(0);\n tokens[i].balanceError = error;\n }\n }\n this.update({ contractBalances: newContractBalances });\n }\n}\n\nexport default TokenBalancesController;\n"]}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { BaseConfig, BaseState } from '@metamask/base-controller';
|
|
2
|
+
import { BaseController } from '@metamask/base-controller';
|
|
3
|
+
import type { NetworkState } from '@metamask/network-controller';
|
|
4
|
+
import type { PreferencesState } from '@metamask/preferences-controller';
|
|
5
|
+
import type { Hex } from '@metamask/utils';
|
|
6
|
+
import type { AssetsContractController } from './AssetsContractController';
|
|
7
|
+
import type { TokenListState } from './TokenListController';
|
|
8
|
+
import type { TokensController, TokensState } from './TokensController';
|
|
9
|
+
/**
|
|
10
|
+
* @type TokenDetectionConfig
|
|
11
|
+
*
|
|
12
|
+
* TokenDetection configuration
|
|
13
|
+
* @property interval - Polling interval used to fetch new token rates
|
|
14
|
+
* @property selectedAddress - Vault selected address
|
|
15
|
+
* @property chainId - The chain ID of the current network
|
|
16
|
+
* @property isDetectionEnabledFromPreferences - Boolean to track if detection is enabled from PreferencesController
|
|
17
|
+
* @property isDetectionEnabledForNetwork - Boolean to track if detected is enabled for current network
|
|
18
|
+
*/
|
|
19
|
+
export interface TokenDetectionConfig extends BaseConfig {
|
|
20
|
+
interval: number;
|
|
21
|
+
selectedAddress: string;
|
|
22
|
+
chainId: Hex;
|
|
23
|
+
isDetectionEnabledFromPreferences: boolean;
|
|
24
|
+
isDetectionEnabledForNetwork: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Controller that passively polls on a set interval for Tokens auto detection
|
|
28
|
+
*/
|
|
29
|
+
export declare class TokenDetectionController extends BaseController<TokenDetectionConfig, BaseState> {
|
|
30
|
+
private intervalId?;
|
|
31
|
+
/**
|
|
32
|
+
* Name of this controller used during composition
|
|
33
|
+
*/
|
|
34
|
+
name: string;
|
|
35
|
+
private readonly getBalancesInSingleCall;
|
|
36
|
+
private readonly addDetectedTokens;
|
|
37
|
+
private readonly getTokensState;
|
|
38
|
+
private readonly getTokenListState;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a TokenDetectionController instance.
|
|
41
|
+
*
|
|
42
|
+
* @param options - The controller options.
|
|
43
|
+
* @param options.onPreferencesStateChange - Allows subscribing to preferences controller state changes.
|
|
44
|
+
* @param options.onNetworkStateChange - Allows subscribing to network controller state changes.
|
|
45
|
+
* @param options.onTokenListStateChange - Allows subscribing to token list controller state changes.
|
|
46
|
+
* @param options.getBalancesInSingleCall - Gets the balances of a list of tokens for the given address.
|
|
47
|
+
* @param options.addDetectedTokens - Add a list of detected tokens.
|
|
48
|
+
* @param options.getTokenListState - Gets the current state of the TokenList controller.
|
|
49
|
+
* @param options.getTokensState - Gets the current state of the Tokens controller.
|
|
50
|
+
* @param options.getNetworkState - Gets the state of the network controller.
|
|
51
|
+
* @param options.getPreferencesState - Gets the state of the preferences controller.
|
|
52
|
+
* @param config - Initial options used to configure this controller.
|
|
53
|
+
* @param state - Initial state to set on this controller.
|
|
54
|
+
*/
|
|
55
|
+
constructor({ onPreferencesStateChange, onNetworkStateChange, onTokenListStateChange, getBalancesInSingleCall, addDetectedTokens, getTokenListState, getTokensState, getNetworkState, getPreferencesState, }: {
|
|
56
|
+
onPreferencesStateChange: (listener: (preferencesState: PreferencesState) => void) => void;
|
|
57
|
+
onNetworkStateChange: (listener: (networkState: NetworkState) => void) => void;
|
|
58
|
+
onTokenListStateChange: (listener: (tokenListState: TokenListState) => void) => void;
|
|
59
|
+
getBalancesInSingleCall: AssetsContractController['getBalancesInSingleCall'];
|
|
60
|
+
addDetectedTokens: TokensController['addDetectedTokens'];
|
|
61
|
+
getTokenListState: () => TokenListState;
|
|
62
|
+
getTokensState: () => TokensState;
|
|
63
|
+
getNetworkState: () => NetworkState;
|
|
64
|
+
getPreferencesState: () => PreferencesState;
|
|
65
|
+
}, config?: Partial<TokenDetectionConfig>, state?: Partial<BaseState>);
|
|
66
|
+
/**
|
|
67
|
+
* Start polling for detected tokens.
|
|
68
|
+
*/
|
|
69
|
+
start(): Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* Stop polling for detected tokens.
|
|
72
|
+
*/
|
|
73
|
+
stop(): void;
|
|
74
|
+
private stopPolling;
|
|
75
|
+
/**
|
|
76
|
+
* Starts a new polling interval.
|
|
77
|
+
*
|
|
78
|
+
* @param interval - An interval on which to poll.
|
|
79
|
+
*/
|
|
80
|
+
private startPolling;
|
|
81
|
+
/**
|
|
82
|
+
* Triggers asset ERC20 token auto detection for each contract address in contract metadata on mainnet.
|
|
83
|
+
*/
|
|
84
|
+
detectTokens(): Promise<void>;
|
|
85
|
+
}
|
|
86
|
+
export default TokenDetectionController;
|
|
87
|
+
//# sourceMappingURL=TokenDetectionController.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TokenDetectionController.d.ts","sourceRoot":"","sources":["../src/TokenDetectionController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACvE,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAK3D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACzE,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAE3C,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AAE3E,OAAO,KAAK,EAAE,cAAc,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