Skip to content

Commit 11a4bb9

Browse files
authored
Merge pull request #126 from blocknative/fix/balance-state
Make sure balance state is kept in sync. Closes #125
2 parents c72e164 + c2f5dd0 commit 11a4bb9

File tree

3 files changed

+619
-610
lines changed

3 files changed

+619
-610
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@babel/preset-env": "^7.5.5",
2727
"@pyoner/svelte-ts-preprocess": "^1.2.1",
2828
"@rollup/plugin-json": "^4.0.0",
29+
"@types/lodash.debounce": "^4.0.6",
2930
"@types/node": "^12.12.3",
3031
"babel-plugin-external-helpers": "^6.18.0",
3132
"rimraf": "^3.0.0",
@@ -44,9 +45,10 @@
4445
"@walletconnect/web3-provider": "^1.0.0-beta.36",
4546
"authereum": "^0.0.4-beta.26",
4647
"bignumber.js": "^9.0.0",
47-
"bnc-sdk": "1.0.0",
48+
"bnc-sdk": "1.0.1",
4849
"bowser": "^2.5.2",
4950
"fortmatic": "^0.8.2",
51+
"lodash.debounce": "^4.0.8",
5052
"promise-cancelable": "^2.1.1",
5153
"regenerator-runtime": "^0.13.3",
5254
"squarelink": "^1.1.4"

src/stores.ts

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { writable, derived, get } from 'svelte/store'
2+
import debounce from 'lodash.debounce'
23
import { getBlocknative } from './services'
34
import { wait, makeQuerablePromise } from './utilities'
45
import { validateWalletInterface, validateType } from './validation'
@@ -171,26 +172,32 @@ function createBalanceStore(initialState: string | null): BalanceStore {
171172
cancel = syncStateWithTimeout({
172173
getState: stateSyncer.get,
173174
setState: set,
174-
timeout: 4000,
175+
timeout: 2000,
175176
currentBalance: get(balance)
176177
})
177178

178179
if (emitterAddress !== $address) {
179180
const blocknative = getBlocknative()
181+
180182
emitter = blocknative.account(blocknative.clientIndex, $address)
181183
.emitter
182-
emitter.on('txConfirmed', () => {
183-
if (stateSyncer.get) {
184-
cancel = syncStateWithTimeout({
185-
getState: stateSyncer.get,
186-
setState: set,
187-
timeout: 1500,
188-
currentBalance: get(balance)
189-
})
190-
}
191-
192-
return false
193-
})
184+
185+
emitter.on(
186+
'txConfirmed',
187+
debounce(() => {
188+
if (stateSyncer.get) {
189+
cancel = syncStateWithTimeout({
190+
getState: stateSyncer.get,
191+
setState: set,
192+
timeout: 2000,
193+
currentBalance: get(balance),
194+
pollStart: Date.now()
195+
})
196+
}
197+
198+
return false
199+
}, 500)
200+
)
194201

195202
emitter.on('all', () => false)
196203

@@ -233,59 +240,56 @@ function createBalanceStore(initialState: string | null): BalanceStore {
233240
}
234241
}
235242

236-
let timesTried: number = 0
237-
238243
function syncStateWithTimeout(options: {
239244
getState: () => Promise<string | number | null>
240245
setState: (newState: string) => void
241246
timeout: number
242247
currentBalance: string
248+
pollStart?: number
243249
}) {
244-
if (timesTried < 4) {
245-
timesTried++
246-
247-
const { getState, setState, timeout, currentBalance } = options
248-
const prom = makeQuerablePromise(
249-
new Cancelable(
250-
(
251-
resolve: (val: string | number | null) => void,
252-
reject: (err: any) => void,
253-
onCancel: (callback: () => void) => void
254-
) => {
255-
getState().then(resolve)
256-
257-
onCancel(() => {
258-
balanceSyncStatus.error =
259-
'There was a problem getting the balance of this wallet'
260-
})
261-
}
262-
).catch(() => {})
263-
)
250+
const { getState, setState, timeout, currentBalance, pollStart } = options
264251

265-
balanceSyncStatus.syncing = prom
252+
if (pollStart && Date.now() - pollStart > 25000) {
253+
return () => {}
254+
}
266255

267-
prom
268-
.then(async (result: string) => {
269-
if (result === currentBalance) {
270-
await wait(150)
271-
syncStateWithTimeout(options)
272-
} else {
273-
setState(result)
274-
}
275-
})
276-
.catch(() => {})
256+
const prom = makeQuerablePromise(
257+
new Cancelable(
258+
(
259+
resolve: (val: string | number | null) => void,
260+
reject: (err: any) => void,
261+
onCancel: (callback: () => void) => void
262+
) => {
263+
getState().then(resolve)
264+
265+
onCancel(() => {
266+
balanceSyncStatus.error =
267+
'There was a problem getting the balance of this wallet'
268+
})
269+
}
270+
).catch(() => {})
271+
)
277272

278-
const timedOut = wait(timeout)
273+
balanceSyncStatus.syncing = prom
279274

280-
timedOut.then(() => {
281-
if (!prom.isFulfilled()) {
282-
prom.cancel(() => {})
275+
prom
276+
.then(async (result: string) => {
277+
if (result === currentBalance && pollStart) {
278+
await wait(350)
279+
syncStateWithTimeout(options)
280+
} else {
281+
setState(result)
283282
}
284283
})
284+
.catch(() => {})
285285

286-
return () => prom.cancel(() => {})
287-
} else {
288-
timesTried = 0
289-
return () => {}
290-
}
286+
const timedOut = wait(timeout)
287+
288+
timedOut.then(() => {
289+
if (!prom.isFulfilled()) {
290+
prom.cancel(() => {})
291+
}
292+
})
293+
294+
return () => prom.cancel(() => {})
291295
}

0 commit comments

Comments
 (0)