Skip to content

Commit 1b02a52

Browse files
authored
Merge pull request #84 from blocknative/fix/watch-address-once
Make sure account is only watched once per address. Closes #82
2 parents ed62842 + 07abd81 commit 1b02a52

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/stores.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ function createWalletStateSliceStore(options: {
157157

158158
function createBalanceStore(initialState: string | null): BalanceStore {
159159
let stateSyncer: StateSyncer
160-
let emitter
160+
let emitter: any
161+
let emitterAddress: String
161162

162163
const { subscribe } = derived([address, network], ([$address]: string[], set: any) => {
163164
if (stateSyncer && !stateSyncer.onChange) {
@@ -167,18 +168,26 @@ function createBalanceStore(initialState: string | null): BalanceStore {
167168
setState: set,
168169
timeout: 2000
169170
})
170-
const blocknative = getBlocknative()
171-
emitter = blocknative.account(blocknative.clientIndex, $address).emitter
172-
emitter.on("txConfirmed", () => {
173-
stateSyncer.get &&
174-
syncStateWithTimeout({
175-
getState: stateSyncer.get,
176-
setState: set,
177-
timeout: 2000
178-
})
179-
return false
180-
})
181-
emitter.on("all", () => false)
171+
172+
if (emitterAddress !== $address) {
173+
const blocknative = getBlocknative()
174+
emitter = blocknative.account(blocknative.clientIndex, $address).emitter
175+
emitter.on("txConfirmed", () => {
176+
stateSyncer.get &&
177+
syncStateWithTimeout({
178+
getState: stateSyncer.get,
179+
setState: set,
180+
timeout: 2000
181+
})
182+
183+
return false
184+
})
185+
186+
emitter.on("all", () => false)
187+
188+
emitterAddress = $address
189+
}
190+
182191
} else {
183192
// no address, so set balance back to null
184193
set && set(undefined)

0 commit comments

Comments
 (0)