1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

watchAsset returns result wether token was added or not

This commit is contained in:
Esteban MIno 2018-08-20 22:32:14 -03:00
parent 81cd29df43
commit 68c1b4c170
3 changed files with 30 additions and 10 deletions

View File

@ -256,7 +256,7 @@ function setupController (initState, initLangCode) {
showUnconfirmedMessage: triggerUi, showUnconfirmedMessage: triggerUi,
unlockAccountMessage: triggerUi, unlockAccountMessage: triggerUi,
showUnapprovedTx: triggerUi, showUnapprovedTx: triggerUi,
showAddTokenUi: triggerUi, showWatchAssetUi: showWatchAssetUi,
// initial state // initial state
initState, initState,
// initial locale code // initial locale code
@ -444,6 +444,24 @@ function triggerUi () {
}) })
} }
/**
* Opens the browser popup for user confirmation of watchAsset
* then it waits until user interact with the UI
*/
function showWatchAssetUi () {
triggerUi()
return new Promise(
(resolve) => {
var interval = setInterval(() => {
if (!notificationIsOpen) {
clearInterval(interval)
resolve()
}
}, 1000)
}
)
}
// On first install, open a window to MetaMask website to how-it-works. // On first install, open a window to MetaMask website to how-it-works.
extension.runtime.onInstalled.addListener(function (details) { extension.runtime.onInstalled.addListener(function (details) {
if ((details.reason === 'install') && (!METAMASK_DEBUG)) { if ((details.reason === 'install') && (!METAMASK_DEBUG)) {

View File

@ -41,7 +41,7 @@ class PreferencesController {
this.diagnostics = opts.diagnostics this.diagnostics = opts.diagnostics
this.network = opts.network this.network = opts.network
this.store = new ObservableStore(initState) this.store = new ObservableStore(initState)
this.showAddTokenUi = opts.showAddTokenUi this.showWatchAssetUi = opts.showWatchAssetUi
this._subscribeProviderType() this._subscribeProviderType()
} }
// PUBLIC METHODS // PUBLIC METHODS
@ -82,13 +82,12 @@ class PreferencesController {
* @param {Function} - next * @param {Function} - next
* @param {Function} - end * @param {Function} - end
*/ */
requestAddToken (req, res, next, end) { async requestWatchAsset (req, res, next, end) {
if (req.method === 'metamask_watchAsset') { if (req.method === 'metamask_watchAsset') {
const { type, options } = req.params const { type, options } = req.params
switch (type) { switch (type) {
case 'ERC20': case 'ERC20':
this._handleWatchAssetERC20(options, res) res.result = await this._handleWatchAssetERC20(options)
res.result = options.address
end() end()
break break
default: default:
@ -521,17 +520,20 @@ class PreferencesController {
/** /**
* Handle the suggestion of an ERC20 asset through `watchAsset` * Handle the suggestion of an ERC20 asset through `watchAsset`
* * * *
* @param {Object} options Parameters according to addition of ERC20 token * @param {Boolean} assetAdded Boolean according to addition of ERC20 token
* *
*/ */
_handleWatchAssetERC20 (options) { async _handleWatchAssetERC20 (options) {
// TODO handle bad parameters // TODO handle bad parameters
const { address, symbol, decimals, imageUrl } = options const { address, symbol, decimals, imageUrl } = options
const rawAddress = address const rawAddress = address
this._validateSuggestedTokenParams({ rawAddress, symbol, decimals }) this._validateSuggestedTokenParams({ rawAddress, symbol, decimals })
const tokenOpts = { rawAddress, decimals, symbol, imageUrl } const tokenOpts = { rawAddress, decimals, symbol, imageUrl }
this.addSuggestedToken(tokenOpts) this.addSuggestedToken(tokenOpts)
this.showAddTokenUi() return this.showWatchAssetUi().then(() => {
const tokenAddresses = this.getTokens().filter(token => token.address === normalizeAddress(rawAddress))
return tokenAddresses.length > 0
})
} }
} }

View File

@ -88,7 +88,7 @@ module.exports = class MetamaskController extends EventEmitter {
this.preferencesController = new PreferencesController({ this.preferencesController = new PreferencesController({
initState: initState.PreferencesController, initState: initState.PreferencesController,
initLangCode: opts.initLangCode, initLangCode: opts.initLangCode,
showAddTokenUi: opts.showAddTokenUi, showWatchAssetUi: opts.showWatchAssetUi,
network: this.networkController, network: this.networkController,
}) })
@ -1241,7 +1241,7 @@ module.exports = class MetamaskController extends EventEmitter {
engine.push(createOriginMiddleware({ origin })) engine.push(createOriginMiddleware({ origin }))
engine.push(createLoggerMiddleware({ origin })) engine.push(createLoggerMiddleware({ origin }))
engine.push(filterMiddleware) engine.push(filterMiddleware)
engine.push(this.preferencesController.requestAddToken.bind(this.preferencesController)) engine.push(this.preferencesController.requestWatchAsset.bind(this.preferencesController))
engine.push(createProviderMiddleware({ provider: this.provider })) engine.push(createProviderMiddleware({ provider: this.provider }))
// setup connection // setup connection