1
- import { addConsoleHandler } from "selenium-webdriver/lib/logging" ;
2
-
3
1
export const balancesController = ( utils , services , actions ) => {
4
2
let balancesStore = { } ;
5
3
let updatingBalances = { status : "waiting" , time : new Date ( ) } ;
6
4
7
- chrome . runtime . onSuspend . addListener ( removeSocketListeners )
8
- chrome . runtime . onSuspendCanceled . addListener ( addSocketListeners )
9
- addSocketListeners ( )
5
+ //chrome.runtime.onSuspend.addListener(removeSocketListeners)
6
+ //chrome.runtime.onSuspendCanceled.addListener(addSocketListeners)
7
+ //addSocketListeners()
8
+
9
+ document . addEventListener ( 'BlockServiceConnected' , ( e ) => {
10
+ addSocketListeners ( )
11
+ } )
10
12
11
13
chrome . storage . local . get ( { "balances" :{ } } , function ( getValue ) { balancesStore = getValue . balances ; } )
12
14
chrome . storage . onChanged . addListener ( function ( changes ) {
@@ -16,20 +18,17 @@ export const balancesController = (utils, services, actions) => {
16
18
}
17
19
}
18
20
} ) ;
19
-
21
+
20
22
chrome . runtime . onInstalled . addListener ( function ( details ) {
21
23
if ( details . reason === "update" ) clearAllNetworks ( )
22
24
} ) ;
23
25
24
26
function addSocketListeners ( ) {
25
- services . socketService . testnet_socket_on ( 'new-state-changes-one' , ( update ) => processBalanceSocketUpdate ( update , 'testnet' ) )
26
- services . socketService . mainnet_socket_on ( 'new-state-changes-one' , ( update ) => processBalanceSocketUpdate ( update , 'mainnet' ) )
27
- if ( ! actions . walletIsLocked ( ) ) joinAllSockets ( )
27
+ services . socketService . socket_on ( 'new-state-changes-one' , ( update ) => processBalanceSocketUpdate ( update ) )
28
28
}
29
29
30
30
function removeSocketListeners ( ) {
31
- services . socketService . testnet_socket_off ( 'new-state-changes-one' )
32
- services . socketService . mainnet_socket_off ( 'new-state-changes-one' )
31
+ services . socketService . socket_off ( 'new-state-changes-one' )
33
32
leaveAllSockets ( )
34
33
}
35
34
@@ -78,14 +77,29 @@ export const balancesController = (utils, services, actions) => {
78
77
key : account . vk
79
78
}
80
79
} )
81
- network . blockservice . getCurrentKeysValues ( keysToGet ) . then ( balances => {
82
- if ( balances . length > 0 ) {
83
- let newBalances = processBalances ( balances , accountsList )
84
- let netKey = network . networkKey
85
- balancesStore [ netKey ] = newBalances
86
- setStore ( balancesStore )
87
- }
88
- } )
80
+ if ( network . blockservice . host ) {
81
+ network . blockservice . getCurrentKeysValues ( keysToGet ) . then ( balances => {
82
+ if ( balances . length > 0 ) {
83
+ let newBalances = processBalances ( balances , accountsList )
84
+ let netKey = network . networkKey
85
+ balancesStore [ netKey ] = newBalances
86
+ setStore ( balancesStore )
87
+ }
88
+ } )
89
+ } else {
90
+ let res = keysToGet . map ( item => network . getVariable ( 'currency' , 'balances' , item . key ) . then ( res => {
91
+ res . key = item . key
92
+ return res
93
+ } ) )
94
+ Promise . all ( res ) . then ( balances => {
95
+ if ( balances . length > 0 ) {
96
+ let newBalances = processBalances ( balances , accountsList )
97
+ let netKey = network . networkKey
98
+ balancesStore [ netKey ] = newBalances
99
+ setStore ( balancesStore )
100
+ }
101
+ } )
102
+ }
89
103
}
90
104
91
105
const processBalance = ( balance , account ) => {
@@ -106,7 +120,7 @@ export const balancesController = (utils, services, actions) => {
106
120
return newBalancesObj ;
107
121
}
108
122
109
- const processBalanceSocketUpdate = ( update , networkType ) => {
123
+ const processBalanceSocketUpdate = ( update ) => {
110
124
if ( actions . walletIsLocked ( ) ) return
111
125
if ( ! update ) return
112
126
@@ -122,7 +136,7 @@ export const balancesController = (utils, services, actions) => {
122
136
if ( room !== `currency.balances:${ key } ` ) return
123
137
124
138
try {
125
- let network = utils . networks . getLamdenNetwork ( networkType )
139
+ let network = utils . networks . getCurrent ( )
126
140
let netKey = network . networkKey
127
141
128
142
let newValue = utils . getValueFromReturn ( value )
@@ -143,7 +157,7 @@ export const balancesController = (utils, services, actions) => {
143
157
144
158
const getUpdate = async ( account , network ) => {
145
159
let vk = account . vk
146
- let balance = await network . blockservice . getCurrentKeyValue ( 'currency' , 'balances' , vk )
160
+ let balance = await network . getVariable ( 'currency' , 'balances' , vk )
147
161
. then ( res => {
148
162
if ( ! res ) return { value : "0" }
149
163
if ( res . notFound ) return { value : "0" }
0 commit comments