@metamask/assets-controllers 26.0.0 → 27.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -1
- package/dist/AccountTrackerController.js +9 -236
- package/dist/AccountTrackerController.js.map +1 -1
- package/dist/AccountTrackerController.mjs +10 -0
- package/dist/AccountTrackerController.mjs.map +1 -0
- package/dist/AssetsContractController.js +17 -384
- package/dist/AssetsContractController.js.map +1 -1
- package/dist/AssetsContractController.mjs +18 -0
- package/dist/AssetsContractController.mjs.map +1 -0
- package/dist/CurrencyRateController.js +10 -157
- package/dist/CurrencyRateController.js.map +1 -1
- package/dist/CurrencyRateController.mjs +11 -0
- package/dist/CurrencyRateController.mjs.map +1 -0
- package/dist/NftController.js +15 -1041
- package/dist/NftController.js.map +1 -1
- package/dist/NftController.mjs +16 -0
- package/dist/NftController.mjs.map +1 -0
- package/dist/NftDetectionController.js +12 -213
- package/dist/NftDetectionController.js.map +1 -1
- package/dist/NftDetectionController.mjs +13 -0
- package/dist/NftDetectionController.mjs.map +1 -0
- package/dist/Standards/ERC20Standard.js +8 -142
- package/dist/Standards/ERC20Standard.js.map +1 -1
- package/dist/Standards/ERC20Standard.mjs +9 -0
- package/dist/Standards/ERC20Standard.mjs.map +1 -0
- package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.js +8 -217
- package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.js.map +1 -1
- package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.mjs +9 -0
- package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.mjs.map +1 -0
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.js +8 -174
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.js.map +1 -1
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.mjs +9 -0
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.mjs.map +1 -0
- package/dist/TokenBalancesController.js +11 -141
- package/dist/TokenBalancesController.js.map +1 -1
- package/dist/TokenBalancesController.mjs +12 -0
- package/dist/TokenBalancesController.mjs.map +1 -0
- package/dist/TokenDetectionController.js +16 -362
- package/dist/TokenDetectionController.js.map +1 -1
- package/dist/TokenDetectionController.mjs +17 -0
- package/dist/TokenDetectionController.mjs.map +1 -0
- package/dist/TokenListController.js +13 -250
- package/dist/TokenListController.js.map +1 -1
- package/dist/TokenListController.mjs +14 -0
- package/dist/TokenListController.mjs.map +1 -0
- package/dist/TokenRatesController.js +11 -328
- package/dist/TokenRatesController.js.map +1 -1
- package/dist/TokenRatesController.mjs +12 -0
- package/dist/TokenRatesController.mjs.map +1 -0
- package/dist/TokensController.js +15 -629
- package/dist/TokensController.js.map +1 -1
- package/dist/TokensController.mjs +16 -0
- package/dist/TokensController.mjs.map +1 -0
- package/dist/assetsUtil.js +41 -384
- package/dist/assetsUtil.js.map +1 -1
- package/dist/assetsUtil.mjs +42 -0
- package/dist/assetsUtil.mjs.map +1 -0
- package/dist/chunk-23F5W3A2.mjs +157 -0
- package/dist/chunk-23F5W3A2.mjs.map +1 -0
- package/dist/chunk-27KOXCQK.mjs +1 -0
- package/dist/chunk-27KOXCQK.mjs.map +1 -0
- package/dist/chunk-3FMWI46J.mjs +361 -0
- package/dist/chunk-3FMWI46J.mjs.map +1 -0
- package/dist/chunk-3FOTFZIL.mjs +411 -0
- package/dist/chunk-3FOTFZIL.mjs.map +1 -0
- package/dist/chunk-4LY47RPI.mjs +139 -0
- package/dist/chunk-4LY47RPI.mjs.map +1 -0
- package/dist/chunk-5C7ZVZNM.mjs +267 -0
- package/dist/chunk-5C7ZVZNM.mjs.map +1 -0
- package/dist/chunk-64EHFYLM.mjs +245 -0
- package/dist/chunk-64EHFYLM.mjs.map +1 -0
- package/dist/chunk-6C2ZIK7A.js +245 -0
- package/dist/chunk-6C2ZIK7A.js.map +1 -0
- package/dist/chunk-6DTCHPBB.mjs +178 -0
- package/dist/chunk-6DTCHPBB.mjs.map +1 -0
- package/dist/chunk-74IP34EL.js +1177 -0
- package/dist/chunk-74IP34EL.js.map +1 -0
- package/dist/chunk-BZI7P3TD.js +115 -0
- package/dist/chunk-BZI7P3TD.js.map +1 -0
- package/dist/chunk-C4PQK53K.js +32 -0
- package/dist/chunk-C4PQK53K.js.map +1 -0
- package/dist/chunk-CP3HC7AQ.mjs +12 -0
- package/dist/chunk-CP3HC7AQ.mjs.map +1 -0
- package/dist/chunk-GHZX5DE4.js +215 -0
- package/dist/chunk-GHZX5DE4.js.map +1 -0
- package/dist/chunk-GMKIOYCB.js +361 -0
- package/dist/chunk-GMKIOYCB.js.map +1 -0
- package/dist/chunk-H6TOSWUM.js +157 -0
- package/dist/chunk-H6TOSWUM.js.map +1 -0
- package/dist/chunk-HWFBJFHS.js +284 -0
- package/dist/chunk-HWFBJFHS.js.map +1 -0
- package/dist/chunk-J2JQQNHN.mjs +115 -0
- package/dist/chunk-J2JQQNHN.mjs.map +1 -0
- package/dist/chunk-J6HPEQL3.mjs +704 -0
- package/dist/chunk-J6HPEQL3.mjs.map +1 -0
- package/dist/chunk-JC4WAN2J.mjs +1177 -0
- package/dist/chunk-JC4WAN2J.mjs.map +1 -0
- package/dist/chunk-LD4GC7OR.js +139 -0
- package/dist/chunk-LD4GC7OR.js.map +1 -0
- package/dist/chunk-NG2UOKDD.js +178 -0
- package/dist/chunk-NG2UOKDD.js.map +1 -0
- package/dist/chunk-NGIXA5M5.mjs +418 -0
- package/dist/chunk-NGIXA5M5.mjs.map +1 -0
- package/dist/chunk-NLNXQHAU.js +704 -0
- package/dist/chunk-NLNXQHAU.js.map +1 -0
- package/dist/chunk-OBUR4TXH.js +1 -0
- package/dist/chunk-OBUR4TXH.js.map +1 -0
- package/dist/chunk-PAJTKWEC.mjs +246 -0
- package/dist/chunk-PAJTKWEC.mjs.map +1 -0
- package/dist/chunk-PRIXT2R6.js +411 -0
- package/dist/chunk-PRIXT2R6.js.map +1 -0
- package/dist/chunk-PUFSYRJZ.js +74 -0
- package/dist/chunk-PUFSYRJZ.js.map +1 -0
- package/dist/chunk-QHRPRO5U.mjs +74 -0
- package/dist/chunk-QHRPRO5U.mjs.map +1 -0
- package/dist/chunk-QR4CX2JT.mjs +284 -0
- package/dist/chunk-QR4CX2JT.mjs.map +1 -0
- package/dist/chunk-RGHTNZQ6.js +12 -0
- package/dist/chunk-RGHTNZQ6.js.map +1 -0
- package/dist/chunk-TCO22VIO.mjs +32 -0
- package/dist/chunk-TCO22VIO.mjs.map +1 -0
- package/dist/chunk-TDKCVCGP.mjs +215 -0
- package/dist/chunk-TDKCVCGP.mjs.map +1 -0
- package/dist/chunk-UAH5YURZ.js +343 -0
- package/dist/chunk-UAH5YURZ.js.map +1 -0
- package/dist/chunk-V4ZO3F2S.js +246 -0
- package/dist/chunk-V4ZO3F2S.js.map +1 -0
- package/dist/chunk-VDJBJAUB.js +418 -0
- package/dist/chunk-VDJBJAUB.js.map +1 -0
- package/dist/chunk-WRQ7POD7.mjs +343 -0
- package/dist/chunk-WRQ7POD7.mjs.map +1 -0
- package/dist/chunk-XUI43LEZ.mjs +30 -0
- package/dist/chunk-XUI43LEZ.mjs.map +1 -0
- package/dist/chunk-YD3NRMFC.js +267 -0
- package/dist/chunk-YD3NRMFC.js.map +1 -0
- package/dist/chunk-Z4BLTVTB.js +30 -0
- package/dist/chunk-Z4BLTVTB.js.map +1 -0
- package/dist/constants.js +7 -9
- package/dist/constants.js.map +1 -1
- package/dist/constants.mjs +8 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/crypto-compare.js +7 -66
- package/dist/crypto-compare.js.map +1 -1
- package/dist/crypto-compare.mjs +8 -0
- package/dist/crypto-compare.mjs.map +1 -0
- package/dist/index.js +72 -39
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +73 -0
- package/dist/index.mjs.map +1 -0
- package/dist/token-prices-service/abstract-token-prices-service.js +1 -3
- package/dist/token-prices-service/abstract-token-prices-service.js.map +1 -1
- package/dist/token-prices-service/abstract-token-prices-service.mjs +1 -0
- package/dist/token-prices-service/abstract-token-prices-service.mjs.map +1 -0
- package/dist/token-prices-service/codefi-v2.js +11 -353
- package/dist/token-prices-service/codefi-v2.js.map +1 -1
- package/dist/token-prices-service/codefi-v2.mjs +12 -0
- package/dist/token-prices-service/codefi-v2.mjs.map +1 -0
- package/dist/token-prices-service/index.js +8 -5
- package/dist/token-prices-service/index.js.map +1 -1
- package/dist/token-prices-service/index.mjs +9 -0
- package/dist/token-prices-service/index.mjs.map +1 -0
- package/dist/token-service.js +14 -133
- package/dist/token-service.js.map +1 -1
- package/dist/token-service.mjs +15 -0
- package/dist/token-service.mjs.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/types/AccountTrackerController.d.ts.map +1 -0
- package/dist/types/AssetsContractController.d.ts.map +1 -0
- package/dist/types/CurrencyRateController.d.ts.map +1 -0
- package/dist/types/NftController.d.ts.map +1 -0
- package/dist/types/NftDetectionController.d.ts.map +1 -0
- package/dist/types/Standards/ERC20Standard.d.ts.map +1 -0
- package/dist/types/Standards/NftStandards/ERC1155/ERC1155Standard.d.ts.map +1 -0
- package/dist/types/Standards/NftStandards/ERC721/ERC721Standard.d.ts.map +1 -0
- package/dist/types/TokenBalancesController.d.ts.map +1 -0
- package/dist/types/TokenDetectionController.d.ts.map +1 -0
- package/dist/types/TokenListController.d.ts.map +1 -0
- package/dist/types/TokenRatesController.d.ts.map +1 -0
- package/dist/{TokensController.d.ts → types/TokensController.d.ts} +10 -3
- package/dist/types/TokensController.d.ts.map +1 -0
- package/dist/types/assetsUtil.d.ts.map +1 -0
- package/dist/types/constants.d.ts.map +1 -0
- package/dist/types/crypto-compare.d.ts.map +1 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/token-prices-service/abstract-token-prices-service.d.ts.map +1 -0
- package/dist/types/token-prices-service/codefi-v2.d.ts.map +1 -0
- package/dist/types/token-prices-service/index.d.ts.map +1 -0
- package/dist/types/token-service.d.ts.map +1 -0
- package/package.json +24 -14
- package/dist/AccountTrackerController.d.ts.map +0 -1
- package/dist/AssetsContractController.d.ts.map +0 -1
- package/dist/CurrencyRateController.d.ts.map +0 -1
- package/dist/NftController.d.ts.map +0 -1
- package/dist/NftDetectionController.d.ts.map +0 -1
- package/dist/Standards/ERC20Standard.d.ts.map +0 -1
- package/dist/Standards/NftStandards/ERC1155/ERC1155Standard.d.ts.map +0 -1
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.d.ts.map +0 -1
- package/dist/TokenBalancesController.d.ts.map +0 -1
- package/dist/TokenDetectionController.d.ts.map +0 -1
- package/dist/TokenListController.d.ts.map +0 -1
- package/dist/TokenRatesController.d.ts.map +0 -1
- package/dist/TokensController.d.ts.map +0 -1
- package/dist/assetsUtil.d.ts.map +0 -1
- package/dist/constants.d.ts.map +0 -1
- package/dist/crypto-compare.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/token-prices-service/abstract-token-prices-service.d.ts.map +0 -1
- package/dist/token-prices-service/codefi-v2.d.ts.map +0 -1
- package/dist/token-prices-service/index.d.ts.map +0 -1
- package/dist/token-service.d.ts.map +0 -1
- /package/dist/{AccountTrackerController.d.ts → types/AccountTrackerController.d.ts} +0 -0
- /package/dist/{AssetsContractController.d.ts → types/AssetsContractController.d.ts} +0 -0
- /package/dist/{CurrencyRateController.d.ts → types/CurrencyRateController.d.ts} +0 -0
- /package/dist/{NftController.d.ts → types/NftController.d.ts} +0 -0
- /package/dist/{NftDetectionController.d.ts → types/NftDetectionController.d.ts} +0 -0
- /package/dist/{Standards → types/Standards}/ERC20Standard.d.ts +0 -0
- /package/dist/{Standards → types/Standards}/NftStandards/ERC1155/ERC1155Standard.d.ts +0 -0
- /package/dist/{Standards → types/Standards}/NftStandards/ERC721/ERC721Standard.d.ts +0 -0
- /package/dist/{TokenBalancesController.d.ts → types/TokenBalancesController.d.ts} +0 -0
- /package/dist/{TokenDetectionController.d.ts → types/TokenDetectionController.d.ts} +0 -0
- /package/dist/{TokenListController.d.ts → types/TokenListController.d.ts} +0 -0
- /package/dist/{TokenRatesController.d.ts → types/TokenRatesController.d.ts} +0 -0
- /package/dist/{assetsUtil.d.ts → types/assetsUtil.d.ts} +0 -0
- /package/dist/{constants.d.ts → types/constants.d.ts} +0 -0
- /package/dist/{crypto-compare.d.ts → types/crypto-compare.d.ts} +0 -0
- /package/dist/{index.d.ts → types/index.d.ts} +0 -0
- /package/dist/{token-prices-service → types/token-prices-service}/abstract-token-prices-service.d.ts +0 -0
- /package/dist/{token-prices-service → types/token-prices-service}/codefi-v2.d.ts +0 -0
- /package/dist/{token-prices-service → types/token-prices-service}/index.d.ts +0 -0
- /package/dist/{token-service.d.ts → types/token-service.d.ts} +0 -0
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ERC1155Standard
|
|
3
|
+
} from "./chunk-TDKCVCGP.mjs";
|
|
4
|
+
import {
|
|
5
|
+
ERC20Standard
|
|
6
|
+
} from "./chunk-J2JQQNHN.mjs";
|
|
7
|
+
import {
|
|
8
|
+
TOKEN_METADATA_NO_SUPPORT_ERROR,
|
|
9
|
+
fetchTokenMetadata
|
|
10
|
+
} from "./chunk-QHRPRO5U.mjs";
|
|
11
|
+
import {
|
|
12
|
+
formatAggregatorNames,
|
|
13
|
+
formatIconUrlWithProxy
|
|
14
|
+
} from "./chunk-5C7ZVZNM.mjs";
|
|
15
|
+
|
|
16
|
+
// src/TokensController.ts
|
|
17
|
+
import { Contract } from "@ethersproject/contracts";
|
|
18
|
+
import { Web3Provider } from "@ethersproject/providers";
|
|
19
|
+
import { BaseControllerV1 } from "@metamask/base-controller";
|
|
20
|
+
import contractsMap from "@metamask/contract-metadata";
|
|
21
|
+
import {
|
|
22
|
+
toChecksumHexAddress,
|
|
23
|
+
ERC721_INTERFACE_ID,
|
|
24
|
+
ORIGIN_METAMASK,
|
|
25
|
+
ApprovalType,
|
|
26
|
+
ERC20,
|
|
27
|
+
ERC721,
|
|
28
|
+
ERC1155,
|
|
29
|
+
isValidHexAddress,
|
|
30
|
+
safelyExecute
|
|
31
|
+
} from "@metamask/controller-utils";
|
|
32
|
+
import { abiERC721 } from "@metamask/metamask-eth-abis";
|
|
33
|
+
import { rpcErrors } from "@metamask/rpc-errors";
|
|
34
|
+
import { Mutex } from "async-mutex";
|
|
35
|
+
import { EventEmitter } from "events";
|
|
36
|
+
import { v1 as random } from "uuid";
|
|
37
|
+
var controllerName = "TokensController";
|
|
38
|
+
var getDefaultTokensState = () => {
|
|
39
|
+
return {
|
|
40
|
+
tokens: [],
|
|
41
|
+
ignoredTokens: [],
|
|
42
|
+
detectedTokens: [],
|
|
43
|
+
allTokens: {},
|
|
44
|
+
allIgnoredTokens: {},
|
|
45
|
+
allDetectedTokens: {}
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
var TokensController = class extends BaseControllerV1 {
|
|
49
|
+
/**
|
|
50
|
+
* Creates a TokensController instance.
|
|
51
|
+
*
|
|
52
|
+
* @param options - The controller options.
|
|
53
|
+
* @param options.chainId - The chain ID of the current network.
|
|
54
|
+
* @param options.config - Initial options used to configure this controller.
|
|
55
|
+
* @param options.state - Initial state to set on this controller.
|
|
56
|
+
* @param options.messenger - The controller messenger.
|
|
57
|
+
*/
|
|
58
|
+
constructor({
|
|
59
|
+
chainId: initialChainId,
|
|
60
|
+
config,
|
|
61
|
+
state,
|
|
62
|
+
messenger
|
|
63
|
+
}) {
|
|
64
|
+
super(config, state);
|
|
65
|
+
this.mutex = new Mutex();
|
|
66
|
+
/**
|
|
67
|
+
* EventEmitter instance used to listen to specific EIP747 events
|
|
68
|
+
*/
|
|
69
|
+
this.hub = new EventEmitter();
|
|
70
|
+
/**
|
|
71
|
+
* Name of this controller used during composition
|
|
72
|
+
*/
|
|
73
|
+
this.name = "TokensController";
|
|
74
|
+
this.defaultConfig = {
|
|
75
|
+
selectedAddress: "",
|
|
76
|
+
chainId: initialChainId,
|
|
77
|
+
provider: void 0,
|
|
78
|
+
...config
|
|
79
|
+
};
|
|
80
|
+
this.defaultState = {
|
|
81
|
+
...getDefaultTokensState(),
|
|
82
|
+
...state
|
|
83
|
+
};
|
|
84
|
+
this.initialize();
|
|
85
|
+
this.abortController = new AbortController();
|
|
86
|
+
this.messagingSystem = messenger;
|
|
87
|
+
this.messagingSystem.registerActionHandler(
|
|
88
|
+
`${controllerName}:addDetectedTokens`,
|
|
89
|
+
this.addDetectedTokens.bind(this)
|
|
90
|
+
);
|
|
91
|
+
this.messagingSystem.subscribe(
|
|
92
|
+
"PreferencesController:stateChange",
|
|
93
|
+
({ selectedAddress }) => {
|
|
94
|
+
const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
|
|
95
|
+
const { chainId } = this.config;
|
|
96
|
+
this.configure({ selectedAddress });
|
|
97
|
+
this.update({
|
|
98
|
+
tokens: allTokens[chainId]?.[selectedAddress] ?? [],
|
|
99
|
+
ignoredTokens: allIgnoredTokens[chainId]?.[selectedAddress] ?? [],
|
|
100
|
+
detectedTokens: allDetectedTokens[chainId]?.[selectedAddress] ?? []
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
);
|
|
104
|
+
this.messagingSystem.subscribe(
|
|
105
|
+
"NetworkController:networkDidChange",
|
|
106
|
+
({ providerConfig }) => {
|
|
107
|
+
const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
|
|
108
|
+
const { selectedAddress } = this.config;
|
|
109
|
+
const { chainId } = providerConfig;
|
|
110
|
+
this.abortController.abort();
|
|
111
|
+
this.abortController = new AbortController();
|
|
112
|
+
this.configure({ chainId });
|
|
113
|
+
this.update({
|
|
114
|
+
tokens: allTokens[chainId]?.[selectedAddress] || [],
|
|
115
|
+
ignoredTokens: allIgnoredTokens[chainId]?.[selectedAddress] || [],
|
|
116
|
+
detectedTokens: allDetectedTokens[chainId]?.[selectedAddress] || []
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
);
|
|
120
|
+
this.messagingSystem.subscribe(
|
|
121
|
+
"TokenListController:stateChange",
|
|
122
|
+
({ tokenList }) => {
|
|
123
|
+
const { tokens } = this.state;
|
|
124
|
+
if (tokens.length && !tokens[0].name) {
|
|
125
|
+
this.updateTokensAttribute(tokenList, "name");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Fetch metadata for a token.
|
|
132
|
+
*
|
|
133
|
+
* @param tokenAddress - The address of the token.
|
|
134
|
+
* @returns The token metadata.
|
|
135
|
+
*/
|
|
136
|
+
async fetchTokenMetadata(tokenAddress) {
|
|
137
|
+
try {
|
|
138
|
+
const token = await fetchTokenMetadata(
|
|
139
|
+
this.config.chainId,
|
|
140
|
+
tokenAddress,
|
|
141
|
+
this.abortController.signal
|
|
142
|
+
);
|
|
143
|
+
return token;
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if (error instanceof Error && error.message.includes(TOKEN_METADATA_NO_SUPPORT_ERROR)) {
|
|
146
|
+
return void 0;
|
|
147
|
+
}
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Adds a token to the stored token list.
|
|
153
|
+
*
|
|
154
|
+
* @param options - The method argument object.
|
|
155
|
+
* @param options.address - Hex address of the token contract.
|
|
156
|
+
* @param options.symbol - Symbol of the token.
|
|
157
|
+
* @param options.decimals - Number of decimals the token uses.
|
|
158
|
+
* @param options.name - Name of the token.
|
|
159
|
+
* @param options.image - Image of the token.
|
|
160
|
+
* @param options.interactingAddress - The address of the account to add a token to.
|
|
161
|
+
* @param options.networkClientId - Network Client ID.
|
|
162
|
+
* @returns Current token list.
|
|
163
|
+
*/
|
|
164
|
+
async addToken({
|
|
165
|
+
address,
|
|
166
|
+
symbol,
|
|
167
|
+
decimals,
|
|
168
|
+
name,
|
|
169
|
+
image,
|
|
170
|
+
interactingAddress,
|
|
171
|
+
networkClientId
|
|
172
|
+
}) {
|
|
173
|
+
const { chainId, selectedAddress } = this.config;
|
|
174
|
+
const releaseLock = await this.mutex.acquire();
|
|
175
|
+
const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
|
|
176
|
+
let currentChainId = chainId;
|
|
177
|
+
if (networkClientId) {
|
|
178
|
+
currentChainId = this.messagingSystem.call(
|
|
179
|
+
"NetworkController:getNetworkClientById",
|
|
180
|
+
networkClientId
|
|
181
|
+
).configuration.chainId;
|
|
182
|
+
}
|
|
183
|
+
const accountAddress = interactingAddress || selectedAddress;
|
|
184
|
+
const isInteractingWithWalletAccount = accountAddress === selectedAddress;
|
|
185
|
+
try {
|
|
186
|
+
address = toChecksumHexAddress(address);
|
|
187
|
+
const tokens = allTokens[currentChainId]?.[accountAddress] || [];
|
|
188
|
+
const ignoredTokens = allIgnoredTokens[currentChainId]?.[accountAddress] || [];
|
|
189
|
+
const detectedTokens = allDetectedTokens[currentChainId]?.[accountAddress] || [];
|
|
190
|
+
const newTokens = [...tokens];
|
|
191
|
+
const [isERC721, tokenMetadata] = await Promise.all([
|
|
192
|
+
this._detectIsERC721(address, networkClientId),
|
|
193
|
+
// TODO parameterize the token metadata fetch by networkClientId
|
|
194
|
+
this.fetchTokenMetadata(address)
|
|
195
|
+
]);
|
|
196
|
+
if (!networkClientId && currentChainId !== this.config.chainId) {
|
|
197
|
+
throw new Error(
|
|
198
|
+
"TokensController Error: Switched networks while adding token"
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
const newEntry = {
|
|
202
|
+
address,
|
|
203
|
+
symbol,
|
|
204
|
+
decimals,
|
|
205
|
+
image: image || formatIconUrlWithProxy({
|
|
206
|
+
chainId: currentChainId,
|
|
207
|
+
tokenAddress: address
|
|
208
|
+
}),
|
|
209
|
+
isERC721,
|
|
210
|
+
aggregators: formatAggregatorNames(tokenMetadata?.aggregators || []),
|
|
211
|
+
name
|
|
212
|
+
};
|
|
213
|
+
const previousIndex = newTokens.findIndex(
|
|
214
|
+
(token) => token.address.toLowerCase() === address.toLowerCase()
|
|
215
|
+
);
|
|
216
|
+
if (previousIndex !== -1) {
|
|
217
|
+
newTokens[previousIndex] = newEntry;
|
|
218
|
+
} else {
|
|
219
|
+
newTokens.push(newEntry);
|
|
220
|
+
}
|
|
221
|
+
const newIgnoredTokens = ignoredTokens.filter(
|
|
222
|
+
(tokenAddress) => tokenAddress.toLowerCase() !== address.toLowerCase()
|
|
223
|
+
);
|
|
224
|
+
const newDetectedTokens = detectedTokens.filter(
|
|
225
|
+
(token) => token.address.toLowerCase() !== address.toLowerCase()
|
|
226
|
+
);
|
|
227
|
+
const { newAllTokens, newAllIgnoredTokens, newAllDetectedTokens } = this._getNewAllTokensState({
|
|
228
|
+
newTokens,
|
|
229
|
+
newIgnoredTokens,
|
|
230
|
+
newDetectedTokens,
|
|
231
|
+
interactingAddress: accountAddress,
|
|
232
|
+
interactingChainId: currentChainId
|
|
233
|
+
});
|
|
234
|
+
let newState = {
|
|
235
|
+
allTokens: newAllTokens,
|
|
236
|
+
allIgnoredTokens: newAllIgnoredTokens,
|
|
237
|
+
allDetectedTokens: newAllDetectedTokens
|
|
238
|
+
};
|
|
239
|
+
if (isInteractingWithWalletAccount) {
|
|
240
|
+
newState = {
|
|
241
|
+
...newState,
|
|
242
|
+
tokens: newTokens,
|
|
243
|
+
ignoredTokens: newIgnoredTokens,
|
|
244
|
+
detectedTokens: newDetectedTokens
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
this.update(newState);
|
|
248
|
+
return newTokens;
|
|
249
|
+
} finally {
|
|
250
|
+
releaseLock();
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Add a batch of tokens.
|
|
255
|
+
*
|
|
256
|
+
* @param tokensToImport - Array of tokens to import.
|
|
257
|
+
* @param networkClientId - Optional network client ID used to determine interacting chain ID.
|
|
258
|
+
*/
|
|
259
|
+
async addTokens(tokensToImport, networkClientId) {
|
|
260
|
+
const releaseLock = await this.mutex.acquire();
|
|
261
|
+
const { tokens, detectedTokens, ignoredTokens } = this.state;
|
|
262
|
+
const importedTokensMap = {};
|
|
263
|
+
const newTokensMap = tokens.reduce((output, current) => {
|
|
264
|
+
output[current.address] = current;
|
|
265
|
+
return output;
|
|
266
|
+
}, {});
|
|
267
|
+
try {
|
|
268
|
+
tokensToImport.forEach((tokenToAdd) => {
|
|
269
|
+
const { address, symbol, decimals, image, aggregators, name } = tokenToAdd;
|
|
270
|
+
const checksumAddress = toChecksumHexAddress(address);
|
|
271
|
+
const formattedToken = {
|
|
272
|
+
address: checksumAddress,
|
|
273
|
+
symbol,
|
|
274
|
+
decimals,
|
|
275
|
+
image,
|
|
276
|
+
aggregators,
|
|
277
|
+
name
|
|
278
|
+
};
|
|
279
|
+
newTokensMap[address] = formattedToken;
|
|
280
|
+
importedTokensMap[address.toLowerCase()] = true;
|
|
281
|
+
return formattedToken;
|
|
282
|
+
});
|
|
283
|
+
const newTokens = Object.values(newTokensMap);
|
|
284
|
+
const newDetectedTokens = detectedTokens.filter(
|
|
285
|
+
(token) => !importedTokensMap[token.address.toLowerCase()]
|
|
286
|
+
);
|
|
287
|
+
const newIgnoredTokens = ignoredTokens.filter(
|
|
288
|
+
(tokenAddress) => !newTokensMap[tokenAddress.toLowerCase()]
|
|
289
|
+
);
|
|
290
|
+
let interactingChainId;
|
|
291
|
+
if (networkClientId) {
|
|
292
|
+
interactingChainId = this.messagingSystem.call(
|
|
293
|
+
"NetworkController:getNetworkClientById",
|
|
294
|
+
networkClientId
|
|
295
|
+
).configuration.chainId;
|
|
296
|
+
}
|
|
297
|
+
const { newAllTokens, newAllDetectedTokens, newAllIgnoredTokens } = this._getNewAllTokensState({
|
|
298
|
+
newTokens,
|
|
299
|
+
newDetectedTokens,
|
|
300
|
+
newIgnoredTokens,
|
|
301
|
+
interactingChainId
|
|
302
|
+
});
|
|
303
|
+
this.update({
|
|
304
|
+
tokens: newTokens,
|
|
305
|
+
allTokens: newAllTokens,
|
|
306
|
+
detectedTokens: newDetectedTokens,
|
|
307
|
+
allDetectedTokens: newAllDetectedTokens,
|
|
308
|
+
ignoredTokens: newIgnoredTokens,
|
|
309
|
+
allIgnoredTokens: newAllIgnoredTokens
|
|
310
|
+
});
|
|
311
|
+
} finally {
|
|
312
|
+
releaseLock();
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Ignore a batch of tokens.
|
|
317
|
+
*
|
|
318
|
+
* @param tokenAddressesToIgnore - Array of token addresses to ignore.
|
|
319
|
+
*/
|
|
320
|
+
ignoreTokens(tokenAddressesToIgnore) {
|
|
321
|
+
const { ignoredTokens, detectedTokens, tokens } = this.state;
|
|
322
|
+
const ignoredTokensMap = {};
|
|
323
|
+
let newIgnoredTokens = [...ignoredTokens];
|
|
324
|
+
const checksummedTokenAddresses = tokenAddressesToIgnore.map((address) => {
|
|
325
|
+
const checksumAddress = toChecksumHexAddress(address);
|
|
326
|
+
ignoredTokensMap[address.toLowerCase()] = true;
|
|
327
|
+
return checksumAddress;
|
|
328
|
+
});
|
|
329
|
+
newIgnoredTokens = [...ignoredTokens, ...checksummedTokenAddresses];
|
|
330
|
+
const newDetectedTokens = detectedTokens.filter(
|
|
331
|
+
(token) => !ignoredTokensMap[token.address.toLowerCase()]
|
|
332
|
+
);
|
|
333
|
+
const newTokens = tokens.filter(
|
|
334
|
+
(token) => !ignoredTokensMap[token.address.toLowerCase()]
|
|
335
|
+
);
|
|
336
|
+
const { newAllIgnoredTokens, newAllDetectedTokens, newAllTokens } = this._getNewAllTokensState({
|
|
337
|
+
newIgnoredTokens,
|
|
338
|
+
newDetectedTokens,
|
|
339
|
+
newTokens
|
|
340
|
+
});
|
|
341
|
+
this.update({
|
|
342
|
+
ignoredTokens: newIgnoredTokens,
|
|
343
|
+
tokens: newTokens,
|
|
344
|
+
detectedTokens: newDetectedTokens,
|
|
345
|
+
allIgnoredTokens: newAllIgnoredTokens,
|
|
346
|
+
allDetectedTokens: newAllDetectedTokens,
|
|
347
|
+
allTokens: newAllTokens
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Adds a batch of detected tokens to the stored token list.
|
|
352
|
+
*
|
|
353
|
+
* @param incomingDetectedTokens - Array of detected tokens to be added or updated.
|
|
354
|
+
* @param detectionDetails - An object containing the chain ID and address of the currently selected network on which the incomingDetectedTokens were detected.
|
|
355
|
+
* @param detectionDetails.selectedAddress - the account address on which the incomingDetectedTokens were detected.
|
|
356
|
+
* @param detectionDetails.chainId - the chainId on which the incomingDetectedTokens were detected.
|
|
357
|
+
*/
|
|
358
|
+
async addDetectedTokens(incomingDetectedTokens, detectionDetails) {
|
|
359
|
+
const releaseLock = await this.mutex.acquire();
|
|
360
|
+
const chainId = detectionDetails?.chainId ?? this.config.chainId;
|
|
361
|
+
const accountAddress = detectionDetails?.selectedAddress ?? this.config.selectedAddress;
|
|
362
|
+
const { allTokens, allDetectedTokens, allIgnoredTokens } = this.state;
|
|
363
|
+
let newTokens = [...allTokens?.[chainId]?.[accountAddress] ?? []];
|
|
364
|
+
let newDetectedTokens = [
|
|
365
|
+
...allDetectedTokens?.[chainId]?.[accountAddress] ?? []
|
|
366
|
+
];
|
|
367
|
+
try {
|
|
368
|
+
incomingDetectedTokens.forEach((tokenToAdd) => {
|
|
369
|
+
const {
|
|
370
|
+
address,
|
|
371
|
+
symbol,
|
|
372
|
+
decimals,
|
|
373
|
+
image,
|
|
374
|
+
aggregators,
|
|
375
|
+
isERC721,
|
|
376
|
+
name
|
|
377
|
+
} = tokenToAdd;
|
|
378
|
+
const checksumAddress = toChecksumHexAddress(address);
|
|
379
|
+
const newEntry = {
|
|
380
|
+
address: checksumAddress,
|
|
381
|
+
symbol,
|
|
382
|
+
decimals,
|
|
383
|
+
image,
|
|
384
|
+
isERC721,
|
|
385
|
+
aggregators,
|
|
386
|
+
name
|
|
387
|
+
};
|
|
388
|
+
const previousImportedIndex = newTokens.findIndex(
|
|
389
|
+
(token) => token.address.toLowerCase() === checksumAddress.toLowerCase()
|
|
390
|
+
);
|
|
391
|
+
if (previousImportedIndex !== -1) {
|
|
392
|
+
newTokens[previousImportedIndex] = newEntry;
|
|
393
|
+
} else {
|
|
394
|
+
const ignoredTokenIndex = allIgnoredTokens?.[chainId]?.[accountAddress]?.indexOf(address) ?? -1;
|
|
395
|
+
if (ignoredTokenIndex === -1) {
|
|
396
|
+
const previousDetectedIndex = newDetectedTokens.findIndex(
|
|
397
|
+
(token) => token.address.toLowerCase() === checksumAddress.toLowerCase()
|
|
398
|
+
);
|
|
399
|
+
if (previousDetectedIndex !== -1) {
|
|
400
|
+
newDetectedTokens[previousDetectedIndex] = newEntry;
|
|
401
|
+
} else {
|
|
402
|
+
newDetectedTokens.push(newEntry);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
const { newAllTokens, newAllDetectedTokens } = this._getNewAllTokensState(
|
|
408
|
+
{
|
|
409
|
+
newTokens,
|
|
410
|
+
newDetectedTokens,
|
|
411
|
+
interactingAddress: accountAddress,
|
|
412
|
+
interactingChainId: chainId
|
|
413
|
+
}
|
|
414
|
+
);
|
|
415
|
+
const { chainId: currentChain, selectedAddress: currentAddress } = this.config;
|
|
416
|
+
newTokens = newAllTokens?.[currentChain]?.[currentAddress] || [];
|
|
417
|
+
newDetectedTokens = newAllDetectedTokens?.[currentChain]?.[currentAddress] || [];
|
|
418
|
+
this.update({
|
|
419
|
+
tokens: newTokens,
|
|
420
|
+
allTokens: newAllTokens,
|
|
421
|
+
detectedTokens: newDetectedTokens,
|
|
422
|
+
allDetectedTokens: newAllDetectedTokens
|
|
423
|
+
});
|
|
424
|
+
} finally {
|
|
425
|
+
releaseLock();
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Adds isERC721 field to token object. This is called when a user attempts to add tokens that
|
|
430
|
+
* were previously added which do not yet had isERC721 field.
|
|
431
|
+
*
|
|
432
|
+
* @param tokenAddress - The contract address of the token requiring the isERC721 field added.
|
|
433
|
+
* @returns The new token object with the added isERC721 field.
|
|
434
|
+
*/
|
|
435
|
+
async updateTokenType(tokenAddress) {
|
|
436
|
+
const isERC721 = await this._detectIsERC721(tokenAddress);
|
|
437
|
+
const { tokens } = this.state;
|
|
438
|
+
const tokenIndex = tokens.findIndex((token) => {
|
|
439
|
+
return token.address.toLowerCase() === tokenAddress.toLowerCase();
|
|
440
|
+
});
|
|
441
|
+
tokens[tokenIndex].isERC721 = isERC721;
|
|
442
|
+
this.update({ tokens });
|
|
443
|
+
return tokens[tokenIndex];
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* This is a function that updates the tokens name for the tokens name if it is not defined.
|
|
447
|
+
*
|
|
448
|
+
* @param tokenList - Represents the fetched token list from service API
|
|
449
|
+
* @param tokenAttribute - Represents the token attribute that we want to update on the token list
|
|
450
|
+
*/
|
|
451
|
+
updateTokensAttribute(tokenList, tokenAttribute) {
|
|
452
|
+
const { tokens } = this.state;
|
|
453
|
+
const newTokens = tokens.map((token) => {
|
|
454
|
+
const newToken = tokenList[token.address.toLowerCase()];
|
|
455
|
+
return !token[tokenAttribute] && newToken?.[tokenAttribute] ? { ...token, [tokenAttribute]: newToken[tokenAttribute] } : { ...token };
|
|
456
|
+
});
|
|
457
|
+
this.update({ tokens: newTokens });
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Detects whether or not a token is ERC-721 compatible.
|
|
461
|
+
*
|
|
462
|
+
* @param tokenAddress - The token contract address.
|
|
463
|
+
* @param networkClientId - Optional network client ID to fetch contract info with.
|
|
464
|
+
* @returns A boolean indicating whether the token address passed in supports the EIP-721
|
|
465
|
+
* interface.
|
|
466
|
+
*/
|
|
467
|
+
async _detectIsERC721(tokenAddress, networkClientId) {
|
|
468
|
+
const checksumAddress = toChecksumHexAddress(tokenAddress);
|
|
469
|
+
if (contractsMap[checksumAddress]?.erc721 === true) {
|
|
470
|
+
return Promise.resolve(true);
|
|
471
|
+
} else if (contractsMap[checksumAddress]?.erc20 === true) {
|
|
472
|
+
return Promise.resolve(false);
|
|
473
|
+
}
|
|
474
|
+
const tokenContract = this._createEthersContract(
|
|
475
|
+
tokenAddress,
|
|
476
|
+
abiERC721,
|
|
477
|
+
networkClientId
|
|
478
|
+
);
|
|
479
|
+
try {
|
|
480
|
+
return await tokenContract.supportsInterface(ERC721_INTERFACE_ID);
|
|
481
|
+
} catch (error) {
|
|
482
|
+
return false;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
_getProvider(networkClientId) {
|
|
486
|
+
return new Web3Provider(
|
|
487
|
+
// @ts-expect-error TODO: remove this annotation once the `Eip1193Provider` class is released
|
|
488
|
+
networkClientId ? this.messagingSystem.call(
|
|
489
|
+
"NetworkController:getNetworkClientById",
|
|
490
|
+
networkClientId
|
|
491
|
+
).provider : this.config.provider
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
_createEthersContract(tokenAddress, abi, networkClientId) {
|
|
495
|
+
const web3provider = this._getProvider(networkClientId);
|
|
496
|
+
const tokenContract = new Contract(tokenAddress, abi, web3provider);
|
|
497
|
+
return tokenContract;
|
|
498
|
+
}
|
|
499
|
+
_generateRandomId() {
|
|
500
|
+
return random();
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Adds a new suggestedAsset to the list of watched assets.
|
|
504
|
+
* Parameters will be validated according to the asset type being watched.
|
|
505
|
+
*
|
|
506
|
+
* @param options - The method options.
|
|
507
|
+
* @param options.asset - The asset to be watched. For now only ERC20 tokens are accepted.
|
|
508
|
+
* @param options.type - The asset type.
|
|
509
|
+
* @param options.interactingAddress - The address of the account that is requesting to watch the asset.
|
|
510
|
+
* @param options.networkClientId - Network Client ID.
|
|
511
|
+
* @returns A promise that resolves if the asset was watched successfully, and rejects otherwise.
|
|
512
|
+
*/
|
|
513
|
+
async watchAsset({
|
|
514
|
+
asset,
|
|
515
|
+
type,
|
|
516
|
+
interactingAddress,
|
|
517
|
+
networkClientId
|
|
518
|
+
}) {
|
|
519
|
+
if (type !== ERC20) {
|
|
520
|
+
throw new Error(`Asset of type ${type} not supported`);
|
|
521
|
+
}
|
|
522
|
+
if (!asset.address) {
|
|
523
|
+
throw rpcErrors.invalidParams("Address must be specified");
|
|
524
|
+
}
|
|
525
|
+
if (!isValidHexAddress(asset.address)) {
|
|
526
|
+
throw rpcErrors.invalidParams(`Invalid address "${asset.address}"`);
|
|
527
|
+
}
|
|
528
|
+
if (await this._detectIsERC721(asset.address, networkClientId)) {
|
|
529
|
+
throw rpcErrors.invalidParams(
|
|
530
|
+
`Contract ${asset.address} must match type ${type}, but was detected as ${ERC721}`
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
const provider = this._getProvider(networkClientId);
|
|
534
|
+
const isErc1155 = await safelyExecute(
|
|
535
|
+
() => new ERC1155Standard(provider).contractSupportsBase1155Interface(
|
|
536
|
+
asset.address
|
|
537
|
+
)
|
|
538
|
+
);
|
|
539
|
+
if (isErc1155) {
|
|
540
|
+
throw rpcErrors.invalidParams(
|
|
541
|
+
`Contract ${asset.address} must match type ${type}, but was detected as ${ERC1155}`
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
const erc20 = new ERC20Standard(provider);
|
|
545
|
+
const [contractName, contractSymbol, contractDecimals] = await Promise.all([
|
|
546
|
+
safelyExecute(() => erc20.getTokenName(asset.address)),
|
|
547
|
+
safelyExecute(() => erc20.getTokenSymbol(asset.address)),
|
|
548
|
+
safelyExecute(async () => erc20.getTokenDecimals(asset.address))
|
|
549
|
+
]);
|
|
550
|
+
asset.name = contractName;
|
|
551
|
+
if (!asset.symbol && !contractSymbol) {
|
|
552
|
+
throw rpcErrors.invalidParams(
|
|
553
|
+
"A symbol is required, but was not found in either the request or contract"
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
if (contractSymbol !== void 0 && asset.symbol !== void 0 && asset.symbol.toUpperCase() !== contractSymbol.toUpperCase()) {
|
|
557
|
+
throw rpcErrors.invalidParams(
|
|
558
|
+
`The symbol in the request (${asset.symbol}) does not match the symbol in the contract (${contractSymbol})`
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
asset.symbol = contractSymbol ?? asset.symbol;
|
|
562
|
+
if (typeof asset.symbol !== "string") {
|
|
563
|
+
throw rpcErrors.invalidParams(`Invalid symbol: not a string`);
|
|
564
|
+
}
|
|
565
|
+
if (asset.symbol.length > 11) {
|
|
566
|
+
throw rpcErrors.invalidParams(
|
|
567
|
+
`Invalid symbol "${asset.symbol}": longer than 11 characters`
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
if (asset.decimals === void 0 && contractDecimals === void 0) {
|
|
571
|
+
throw rpcErrors.invalidParams(
|
|
572
|
+
"Decimals are required, but were not found in either the request or contract"
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
if (contractDecimals !== void 0 && asset.decimals !== void 0 && String(asset.decimals) !== contractDecimals) {
|
|
576
|
+
throw rpcErrors.invalidParams(
|
|
577
|
+
`The decimals in the request (${asset.decimals}) do not match the decimals in the contract (${contractDecimals})`
|
|
578
|
+
);
|
|
579
|
+
}
|
|
580
|
+
const decimalsStr = contractDecimals ?? asset.decimals;
|
|
581
|
+
const decimalsNum = parseInt(decimalsStr, 10);
|
|
582
|
+
if (!Number.isInteger(decimalsNum) || decimalsNum > 36 || decimalsNum < 0) {
|
|
583
|
+
throw rpcErrors.invalidParams(
|
|
584
|
+
`Invalid decimals "${decimalsStr}": must be an integer 0 <= 36`
|
|
585
|
+
);
|
|
586
|
+
}
|
|
587
|
+
asset.decimals = decimalsNum;
|
|
588
|
+
const suggestedAssetMeta = {
|
|
589
|
+
asset,
|
|
590
|
+
id: this._generateRandomId(),
|
|
591
|
+
time: Date.now(),
|
|
592
|
+
type,
|
|
593
|
+
interactingAddress: interactingAddress || this.config.selectedAddress
|
|
594
|
+
};
|
|
595
|
+
await this._requestApproval(suggestedAssetMeta);
|
|
596
|
+
const { address, symbol, decimals, name, image } = asset;
|
|
597
|
+
await this.addToken({
|
|
598
|
+
address,
|
|
599
|
+
symbol,
|
|
600
|
+
decimals,
|
|
601
|
+
name,
|
|
602
|
+
image,
|
|
603
|
+
interactingAddress: suggestedAssetMeta.interactingAddress,
|
|
604
|
+
networkClientId
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Takes a new tokens and ignoredTokens array for the current network/account combination
|
|
609
|
+
* and returns new allTokens and allIgnoredTokens state to update to.
|
|
610
|
+
*
|
|
611
|
+
* @param params - Object that holds token params.
|
|
612
|
+
* @param params.newTokens - The new tokens to set for the current network and selected account.
|
|
613
|
+
* @param params.newIgnoredTokens - The new ignored tokens to set for the current network and selected account.
|
|
614
|
+
* @param params.newDetectedTokens - The new detected tokens to set for the current network and selected account.
|
|
615
|
+
* @param params.interactingAddress - The account address to use to store the tokens.
|
|
616
|
+
* @param params.interactingChainId - The chainId to use to store the tokens.
|
|
617
|
+
* @returns The updated `allTokens` and `allIgnoredTokens` state.
|
|
618
|
+
*/
|
|
619
|
+
_getNewAllTokensState(params) {
|
|
620
|
+
const {
|
|
621
|
+
newTokens,
|
|
622
|
+
newIgnoredTokens,
|
|
623
|
+
newDetectedTokens,
|
|
624
|
+
interactingAddress,
|
|
625
|
+
interactingChainId
|
|
626
|
+
} = params;
|
|
627
|
+
const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
|
|
628
|
+
const { chainId, selectedAddress } = this.config;
|
|
629
|
+
const userAddressToAddTokens = interactingAddress ?? selectedAddress;
|
|
630
|
+
const chainIdToAddTokens = interactingChainId ?? chainId;
|
|
631
|
+
let newAllTokens = allTokens;
|
|
632
|
+
if (newTokens?.length || newTokens && allTokens && allTokens[chainIdToAddTokens] && allTokens[chainIdToAddTokens][userAddressToAddTokens]) {
|
|
633
|
+
const networkTokens = allTokens[chainIdToAddTokens];
|
|
634
|
+
const newNetworkTokens = {
|
|
635
|
+
...networkTokens,
|
|
636
|
+
...{ [userAddressToAddTokens]: newTokens }
|
|
637
|
+
};
|
|
638
|
+
newAllTokens = {
|
|
639
|
+
...allTokens,
|
|
640
|
+
...{ [chainIdToAddTokens]: newNetworkTokens }
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
let newAllIgnoredTokens = allIgnoredTokens;
|
|
644
|
+
if (newIgnoredTokens?.length || newIgnoredTokens && allIgnoredTokens && allIgnoredTokens[chainIdToAddTokens] && allIgnoredTokens[chainIdToAddTokens][userAddressToAddTokens]) {
|
|
645
|
+
const networkIgnoredTokens = allIgnoredTokens[chainIdToAddTokens];
|
|
646
|
+
const newIgnoredNetworkTokens = {
|
|
647
|
+
...networkIgnoredTokens,
|
|
648
|
+
...{ [userAddressToAddTokens]: newIgnoredTokens }
|
|
649
|
+
};
|
|
650
|
+
newAllIgnoredTokens = {
|
|
651
|
+
...allIgnoredTokens,
|
|
652
|
+
...{ [chainIdToAddTokens]: newIgnoredNetworkTokens }
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
let newAllDetectedTokens = allDetectedTokens;
|
|
656
|
+
if (newDetectedTokens?.length || newDetectedTokens && allDetectedTokens && allDetectedTokens[chainIdToAddTokens] && allDetectedTokens[chainIdToAddTokens][userAddressToAddTokens]) {
|
|
657
|
+
const networkDetectedTokens = allDetectedTokens[chainIdToAddTokens];
|
|
658
|
+
const newDetectedNetworkTokens = {
|
|
659
|
+
...networkDetectedTokens,
|
|
660
|
+
...{ [userAddressToAddTokens]: newDetectedTokens }
|
|
661
|
+
};
|
|
662
|
+
newAllDetectedTokens = {
|
|
663
|
+
...allDetectedTokens,
|
|
664
|
+
...{ [chainIdToAddTokens]: newDetectedNetworkTokens }
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
return { newAllTokens, newAllIgnoredTokens, newAllDetectedTokens };
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* Removes all tokens from the ignored list.
|
|
671
|
+
*/
|
|
672
|
+
clearIgnoredTokens() {
|
|
673
|
+
this.update({ ignoredTokens: [], allIgnoredTokens: {} });
|
|
674
|
+
}
|
|
675
|
+
async _requestApproval(suggestedAssetMeta) {
|
|
676
|
+
return this.messagingSystem.call(
|
|
677
|
+
"ApprovalController:addRequest",
|
|
678
|
+
{
|
|
679
|
+
id: suggestedAssetMeta.id,
|
|
680
|
+
origin: ORIGIN_METAMASK,
|
|
681
|
+
type: ApprovalType.WatchAsset,
|
|
682
|
+
requestData: {
|
|
683
|
+
id: suggestedAssetMeta.id,
|
|
684
|
+
interactingAddress: suggestedAssetMeta.interactingAddress,
|
|
685
|
+
asset: {
|
|
686
|
+
address: suggestedAssetMeta.asset.address,
|
|
687
|
+
decimals: suggestedAssetMeta.asset.decimals,
|
|
688
|
+
symbol: suggestedAssetMeta.asset.symbol,
|
|
689
|
+
image: suggestedAssetMeta.asset.image || null
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
},
|
|
693
|
+
true
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
var TokensController_default = TokensController;
|
|
698
|
+
|
|
699
|
+
export {
|
|
700
|
+
getDefaultTokensState,
|
|
701
|
+
TokensController,
|
|
702
|
+
TokensController_default
|
|
703
|
+
};
|
|
704
|
+
//# sourceMappingURL=chunk-J6HPEQL3.mjs.map
|