mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-01 13:47:06 +01:00
212 lines
10 KiB
Diff
212 lines
10 KiB
Diff
diff --git a/dist/TokensController.js b/dist/TokensController.js
|
|
index 0e03b88e8a46dce73a5cc87fb432b0b2431b3797..fc8893fa6bad76d65aa34fa1bfb0b233b1259ae6 100644
|
|
--- a/dist/TokensController.js
|
|
+++ b/dist/TokensController.js
|
|
@@ -25,13 +25,6 @@ const base_controller_1 = require("@metamask/base-controller");
|
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
const assetsUtil_1 = require("./assetsUtil");
|
|
const token_service_1 = require("./token-service");
|
|
-var SuggestedAssetStatus;
|
|
-(function (SuggestedAssetStatus) {
|
|
- SuggestedAssetStatus["accepted"] = "accepted";
|
|
- SuggestedAssetStatus["failed"] = "failed";
|
|
- SuggestedAssetStatus["pending"] = "pending";
|
|
- SuggestedAssetStatus["rejected"] = "rejected";
|
|
-})(SuggestedAssetStatus || (SuggestedAssetStatus = {}));
|
|
/**
|
|
* The name of the {@link TokensController}.
|
|
*/
|
|
@@ -93,10 +86,6 @@ class TokensController extends base_controller_1.BaseController {
|
|
});
|
|
});
|
|
}
|
|
- failSuggestedAsset(suggestedAssetMeta, error) {
|
|
- const failedSuggestedAssetMeta = Object.assign(Object.assign({}, suggestedAssetMeta), { status: SuggestedAssetStatus.failed, error });
|
|
- this.hub.emit(`${suggestedAssetMeta.id}:finished`, failedSuggestedAssetMeta);
|
|
- }
|
|
/**
|
|
* Fetch metadata for a token.
|
|
*
|
|
@@ -412,9 +401,10 @@ class TokensController extends base_controller_1.BaseController {
|
|
_generateRandomId() {
|
|
return (0, uuid_1.v1)();
|
|
}
|
|
+ // THIS PATCHED METHOD HAS ALREADY BEEN RELEASED IN VERSION 8.0.0 of @metamask/assets-controllers
|
|
/**
|
|
- * Adds a new suggestedAsset to state. Parameters will be validated according to
|
|
- * asset type being watched. A `<suggestedAssetMeta.id>:pending` hub event will be emitted once added.
|
|
+ * Adds a new suggestedAsset to the list of watched assets.
|
|
+ * Parameters will be validated according to the asset type being watched.
|
|
*
|
|
* @param asset - The asset to be watched. For now only ERC20 tokens are accepted.
|
|
* @param type - The asset type.
|
|
@@ -423,103 +413,22 @@ class TokensController extends base_controller_1.BaseController {
|
|
*/
|
|
watchAsset(asset, type, interactingAddress) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
+ if (type !== controller_utils_1.ERC20) {
|
|
+ throw new Error(`Asset of type ${type} not supported`);
|
|
+ }
|
|
const { selectedAddress } = this.config;
|
|
const suggestedAssetMeta = {
|
|
asset,
|
|
id: this._generateRandomId(),
|
|
- status: SuggestedAssetStatus.pending,
|
|
time: Date.now(),
|
|
type,
|
|
interactingAddress: interactingAddress || selectedAddress,
|
|
};
|
|
- try {
|
|
- switch (type) {
|
|
- case 'ERC20':
|
|
- (0, assetsUtil_1.validateTokenToWatch)(asset);
|
|
- break;
|
|
- default:
|
|
- throw new Error(`Asset of type ${type} not supported`);
|
|
- }
|
|
- }
|
|
- catch (error) {
|
|
- this.failSuggestedAsset(suggestedAssetMeta, error);
|
|
- return Promise.reject(error);
|
|
- }
|
|
- const result = new Promise((resolve, reject) => {
|
|
- this.hub.once(`${suggestedAssetMeta.id}:finished`, (meta) => {
|
|
- switch (meta.status) {
|
|
- case SuggestedAssetStatus.accepted:
|
|
- return resolve(meta.asset.address);
|
|
- case SuggestedAssetStatus.rejected:
|
|
- return reject(new Error('User rejected to watch the asset.'));
|
|
- case SuggestedAssetStatus.failed:
|
|
- return reject(new Error(meta.error.message));
|
|
- /* istanbul ignore next */
|
|
- default:
|
|
- return reject(new Error(`Unknown status: ${meta.status}`));
|
|
- }
|
|
- });
|
|
- });
|
|
- const { suggestedAssets } = this.state;
|
|
- suggestedAssets.push(suggestedAssetMeta);
|
|
- this.update({ suggestedAssets: [...suggestedAssets] });
|
|
- this._requestApproval(suggestedAssetMeta);
|
|
- return { result, suggestedAssetMeta };
|
|
- });
|
|
- }
|
|
- /**
|
|
- * Accepts to watch an asset and updates it's status and deletes the suggestedAsset from state,
|
|
- * adding the asset to corresponding asset state. In this case ERC20 tokens.
|
|
- * A `<suggestedAssetMeta.id>:finished` hub event is fired after accepted or failure.
|
|
- *
|
|
- * @param suggestedAssetID - The ID of the suggestedAsset to accept.
|
|
- */
|
|
- acceptWatchAsset(suggestedAssetID) {
|
|
- return __awaiter(this, void 0, void 0, function* () {
|
|
- const { selectedAddress } = this.config;
|
|
- const { suggestedAssets } = this.state;
|
|
- const index = suggestedAssets.findIndex(({ id }) => suggestedAssetID === id);
|
|
- const suggestedAssetMeta = suggestedAssets[index];
|
|
- try {
|
|
- switch (suggestedAssetMeta.type) {
|
|
- case 'ERC20':
|
|
- const { address, symbol, decimals, image } = suggestedAssetMeta.asset;
|
|
- yield this.addToken(address, symbol, decimals, image, (suggestedAssetMeta === null || suggestedAssetMeta === void 0 ? void 0 : suggestedAssetMeta.interactingAddress) || selectedAddress);
|
|
- this._acceptApproval(suggestedAssetID);
|
|
- const acceptedSuggestedAssetMeta = Object.assign(Object.assign({}, suggestedAssetMeta), { status: SuggestedAssetStatus.accepted });
|
|
- this.hub.emit(`${suggestedAssetMeta.id}:finished`, acceptedSuggestedAssetMeta);
|
|
- break;
|
|
- default:
|
|
- throw new Error(`Asset of type ${suggestedAssetMeta.type} not supported`);
|
|
- }
|
|
- }
|
|
- catch (error) {
|
|
- this.failSuggestedAsset(suggestedAssetMeta, error);
|
|
- this._rejectApproval(suggestedAssetID);
|
|
- }
|
|
- const newSuggestedAssets = suggestedAssets.filter(({ id }) => id !== suggestedAssetID);
|
|
- this.update({ suggestedAssets: [...newSuggestedAssets] });
|
|
+ (0, assetsUtil_1.validateTokenToWatch)(asset);
|
|
+ yield this._requestApproval(suggestedAssetMeta);
|
|
+ yield this.addToken(asset.address, asset.symbol, asset.decimals, asset.image, suggestedAssetMeta.interactingAddress);
|
|
});
|
|
}
|
|
- /**
|
|
- * Rejects a watchAsset request based on its ID by setting its status to "rejected"
|
|
- * and emitting a `<suggestedAssetMeta.id>:finished` hub event.
|
|
- *
|
|
- * @param suggestedAssetID - The ID of the suggestedAsset to accept.
|
|
- */
|
|
- rejectWatchAsset(suggestedAssetID) {
|
|
- const { suggestedAssets } = this.state;
|
|
- const index = suggestedAssets.findIndex(({ id }) => suggestedAssetID === id);
|
|
- const suggestedAssetMeta = suggestedAssets[index];
|
|
- if (!suggestedAssetMeta) {
|
|
- return;
|
|
- }
|
|
- const rejectedSuggestedAssetMeta = Object.assign(Object.assign({}, suggestedAssetMeta), { status: SuggestedAssetStatus.rejected });
|
|
- this.hub.emit(`${suggestedAssetMeta.id}:finished`, rejectedSuggestedAssetMeta);
|
|
- const newSuggestedAssets = suggestedAssets.filter(({ id }) => id !== suggestedAssetID);
|
|
- this.update({ suggestedAssets: [...newSuggestedAssets] });
|
|
- this._rejectApproval(suggestedAssetID);
|
|
- }
|
|
/**
|
|
* Takes a new tokens and ignoredTokens array for the current network/account combination
|
|
* and returns new allTokens and allIgnoredTokens state to update to.
|
|
@@ -576,43 +485,26 @@ class TokensController extends base_controller_1.BaseController {
|
|
clearIgnoredTokens() {
|
|
this.update({ ignoredTokens: [], allIgnoredTokens: {} });
|
|
}
|
|
+ // THIS PATCHED METHOD HAS ALREADY BEEN RELEASED IN VERSION 8.0.0 of @metamask/assets-controllers
|
|
_requestApproval(suggestedAssetMeta) {
|
|
- this.messagingSystem
|
|
- .call('ApprovalController:addRequest', {
|
|
- id: suggestedAssetMeta.id,
|
|
- origin: controller_utils_1.ORIGIN_METAMASK,
|
|
- type: controller_utils_1.ApprovalType.WatchAsset,
|
|
- requestData: {
|
|
+ return __awaiter(this, void 0, void 0, function* () {
|
|
+ return this.messagingSystem.call('ApprovalController:addRequest', {
|
|
id: suggestedAssetMeta.id,
|
|
- interactingAddress: suggestedAssetMeta.interactingAddress,
|
|
- asset: {
|
|
- address: suggestedAssetMeta.asset.address,
|
|
- decimals: suggestedAssetMeta.asset.decimals,
|
|
- symbol: suggestedAssetMeta.asset.symbol,
|
|
- image: suggestedAssetMeta.asset.image || null,
|
|
+ origin: controller_utils_1.ORIGIN_METAMASK,
|
|
+ type: controller_utils_1.ApprovalType.WatchAsset,
|
|
+ requestData: {
|
|
+ id: suggestedAssetMeta.id,
|
|
+ interactingAddress: suggestedAssetMeta.interactingAddress,
|
|
+ asset: {
|
|
+ address: suggestedAssetMeta.asset.address,
|
|
+ decimals: suggestedAssetMeta.asset.decimals,
|
|
+ symbol: suggestedAssetMeta.asset.symbol,
|
|
+ image: suggestedAssetMeta.asset.image || null,
|
|
+ },
|
|
},
|
|
- },
|
|
- }, true)
|
|
- .catch(() => {
|
|
- // Intentionally ignored as promise not currently used
|
|
+ }, true);
|
|
});
|
|
}
|
|
- _acceptApproval(approvalRequestId) {
|
|
- try {
|
|
- this.messagingSystem.call('ApprovalController:acceptRequest', approvalRequestId);
|
|
- }
|
|
- catch (error) {
|
|
- console.error('Failed to accept token watch approval request', error);
|
|
- }
|
|
- }
|
|
- _rejectApproval(approvalRequestId) {
|
|
- try {
|
|
- this.messagingSystem.call('ApprovalController:rejectRequest', approvalRequestId, new Error('Rejected'));
|
|
- }
|
|
- catch (messageCallError) {
|
|
- console.error('Failed to reject token watch approval request', messageCallError);
|
|
- }
|
|
- }
|
|
}
|
|
exports.TokensController = TokensController;
|
|
exports.default = TokensController;
|