import { IAssetInfo } from '@waves/data-entities/dist/entities/Asset' ; class Background { static instance : Background ; background : any ; initPromise : Promise < void > ; updatedByUser = false ; _defer ; _assetsStore ; _lastUpdateIdle = 0 ; _tmr ; constructor ( ) { this . _assetsStore = { } ; this . _defer = { } ; this . initPromise = new Promise ( ( res , rej ) => { this . _defer . resolve = res ; this . _defer . reject = rej ; } ) ; this . _defer . promise = this . initPromise ; } init ( background ) { this . background = background ; this . _defer . resolve ( ) ; } async updateIdle ( ) { this . updatedByUser = true ; this . _updateIdle ( ) ; } async setIdleOptions ( options ) { await this . initPromise ; return this . background . setIdleOptions ( options ) ; } async allowOrigin ( origin ) { await this . initPromise ; return this . background . allowOrigin ( origin ) ; } async disableOrigin ( origin ) { await this . initPromise ; return this . background . disableOrigin ( origin ) ; } async deleteOrigin ( origin ) { await this . initPromise ; return this . background . deleteOrigin ( origin ) ; } async setAutoSign ( origin , options ) { await this . initPromise ; return this . background . setAutoSign ( origin , options ) ; } async setNotificationPermissions ( options ) { await this . initPromise ; return this . background . setNotificationPermissions ( options ) ; } async setCurrentLocale ( lng ) { await this . initPromise ; return this . background . setCurrentLocale ( lng ) ; } async setUiState ( newUiState ) { await this . initPromise ; return this . background . setUiState ( newUiState ) ; } async selectAccount ( address , network ) { await this . initPromise ; return this . background . selectAccount ( address , network ) ; } async addWallet ( data ) { await this . initPromise ; return this . background . addWallet ( data ) ; } async removeWallet ( address , network ) { await this . initPromise ; if ( address ) { return this . background . removeWallet ( address , network ) ; } return this . deleteVault ( ) ; } async deleteVault ( ) { await this . initPromise ; return this . background . deleteVault ( ) ; } async closeNotificationWindow ( ) { await this . initPromise ; return this . background . closeNotificationWindow ( ) ; } async lock ( ) { await this . initPromise ; return this . background . lock ( ) ; } async unlock ( password ) { await this . initPromise ; return this . background . unlock ( password ) ; } async initVault ( password ? ) { await this . initPromise ; return this . background . initVault ( password ) ; } async exportAccount ( address , password , network ) { await this . initPromise ; return this . background . exportAccount ( address , password , network ) ; } async exportSeed ( address , network ) { await this . initPromise ; return this . background . encryptedSeed ( address , network ) ; } async editWalletName ( address , name , network ) { await this . initPromise ; return this . background . editWalletName ( address , name , network ) ; } async newPassword ( oldPassword , newPassword ) { await this . initPromise ; return this . background . newPassword ( oldPassword , newPassword ) ; } async clearMessages ( ) { await this . initPromise ; return this . background . clearMessages ( ) ; } async deleteMessage ( id ) { await this . initPromise ; return this . background . deleteMessage ( id ) ; } async approve ( messageId , address , network ) { await this . initPromise ; return this . background . approve ( messageId , address , network ) ; } async reject ( messageId , forever = false ) { await this . initPromise ; return this . background . reject ( messageId , forever ) ; } async updateTransactionFee ( messageId , fee ) { await this . initPromise ; return this . background . updateTransactionFee ( messageId , fee ) ; } async setNetwork ( network ) { await this . initPromise ; return this . background . setNetwork ( network ) ; } async setCustomNode ( url , network ) { await this . initPromise ; return this . background . setCustomNode ( url , network ) ; } async setCustomCode ( code , network ) { await this . initPromise ; return this . background . setCustomCode ( code , network ) ; } async setCustomMatcher ( url , network ) { await this . initPromise ; return this . background . setCustomMatcher ( url , network ) ; } async assetInfo ( assetId ) { assetId = assetId || 'WAVES' ; if ( this . _assetsStore [ assetId ] ) { return await this . _assetsStore [ assetId ] ; } await this . initPromise ; this . _assetsStore [ assetId ] = this . background . assetInfo ( assetId ) ; try { return await this . _assetsStore [ assetId ] ; } catch ( e ) { delete this . _assetsStore [ assetId ] ; throw e ; } } async toggleAssetFavorite ( assetId ) { await this . initPromise ; return this . background . toggleAssetFavorite ( assetId ) ; } async deleteNotifications ( ids ) { await this . initPromise ; return this . background . deleteNotifications ( ids ) ; } async getUserList ( type , from , to ) { await this . initPromise ; return this . background . getUserList ( type , from , to ) ; } async sendEvent ( event , properties ) ; async sendEvent ( event , properties ) ; async sendEvent ( event , properties = { } ) { await this . initPromise ; return this . background . sendEvent ( event , properties ) ; } async updateBalances ( ) { await this . initPromise ; return this . background . updateBalances ( ) ; } async signAndPublishTransaction ( data ) { await this . initPromise ; return this . background . signAndPublishTransaction ( data ) ; } async updateExchangers ( network ) { await this . initPromise ; return this . background . updateExchangers ( network ) ; } async performSwap ( { exchangerId , fee , feeAssetId , fromAssetId , fromCoins , minReceivedCoins , toAssetId , toCoins , } ) { await this . initPromise ; return this . background . performSwap ( { exchangerId , fee , feeAssetId , fromAssetId , fromCoins , minReceivedCoins , toAssetId , toCoins , } ) ; } async getMinimumFee ( txType ) { await this . initPromise ; return this . background . getMinimumFee ( txType ) ; } async getExtraFee ( address , network ) { await this . initPromise ; return this . background . getExtraFee ( address , network ) ; } async _updateIdle ( ) { const now = Date . now ( ) ; clearTimeout ( this . _tmr ) ; this . _tmr = setTimeout ( ( ) => this . _updateIdle ( ) , 4000 ) ; if ( ! this . updatedByUser || now - this . _lastUpdateIdle < 4000 ) { return null ; } this . updatedByUser = false ; this . _lastUpdateIdle = now ; await this . initPromise ; return this . background . updateIdle ( ) ; } } export default new Background ( ) ; export enum WalletTypes { New = 'new' , Seed = 'seed' , Keystore = 'keystore' , KeystoreWx = 'keystore_wx' } export interface AssetDetail extends IAssetInfo { displayName : string ; originTransactionId : string ; issuer ? : string ; isFavorite ? : boolean ; isSuspicious ? : boolean ; }