Skip to content

Commit 86523f4

Browse files
authored
fix: resolve issues with error handling and tx matching (#11)
1 parent 2abd30a commit 86523f4

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const Wallet = require('./src/lib/wallet.js')
1818
const WalletPayGeneric = require('./src/lib/wallet-pay-generic.js')
1919
const Provider = require('./src/modules/provider.js')
2020
const HdWallet = require('./src/modules/hdwallet.js')
21+
const StateDb = require('./src/modules/state.js')
2122
const TetherCurrency = require('./src/tether-currency.js')
2223
module.exports = {
2324
Currency,
@@ -26,5 +27,6 @@ module.exports = {
2627
WalletPayGeneric,
2728
Wallet,
2829
HdWallet,
30+
StateDb,
2931
TetherCurrency
3032
}

src/lib/wallet-pay-generic.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,12 @@ class WalletPayGeneric extends WalletPay {
152152
}
153153

154154
_listenToEvents () {
155-
this.provider.on('subscribeAccount', async (res) => {
155+
this.provider.on('subscribeAccount', async (err, res) => {
156+
if (err) {
157+
console.trace(err)
158+
return
159+
}
160+
156161
if (res.token) {
157162
this._eachToken(async (token) => {
158163
if (token.tokenContract.toLowerCase() !== res?.token.toLowerCase()) return
@@ -212,7 +217,7 @@ class WalletPayGeneric extends WalletPay {
212217
/**
213218
* Get balance of entire wallet or 1 address
214219
* @param {object} opts options
215-
* @param {string} opts.token token name, for getting balance of token
220+
* @param {string=} opts.token token name, for getting balance of token
216221
* @param {string} addr Pass address to get balance of specific address
217222
* @returns {Promise<Balance>}
218223
*/
@@ -246,7 +251,12 @@ class WalletPayGeneric extends WalletPay {
246251
return state
247252
}
248253

249-
async _syncAddressPath (addr, signal) {
254+
/**
255+
* @param {string} addr
256+
* @param {any} signal
257+
* @param {StateDb=} stateInstance
258+
*/
259+
async _syncAddressPath (addr, signal, stateInstance = undefined) {
250260
const provider = this.provider
251261
const path = addr.path
252262
const txs = await provider.getTransactionsByAddress({ address: addr.address })
@@ -257,34 +267,42 @@ class WalletPayGeneric extends WalletPay {
257267

258268
this._hdWallet.addAddress(addr)
259269
for (const t of txs) {
260-
await this._storeTx(t)
270+
await this._storeTx(t, stateInstance)
261271
}
262-
await this._setAddrBalance(addr.address)
272+
await this._setAddrBalance(addr.address, stateInstance)
273+
274+
const txIndex = await stateInstance.getTxIndex()
275+
console.log('txIndex', txIndex)
276+
263277
return txs.length > 0 ? signal.hasTx : signal.noTx
264278
}
265279

266-
async _setAddrBalance (addr) {
267-
const balances = await this.state.getBalances()
280+
async _setAddrBalance (addr, stateInstance = undefined) {
281+
const state = stateInstance ?? this.state
282+
283+
const balances = await state.getBalances()
268284
const bal = await this.getBalance({}, addr)
269285
await balances.setBal(addr, bal.confirmed)
270286
}
271287

272-
async _storeTx (tx) {
288+
async _storeTx (tx, stateInstance = undefined) {
289+
const state = stateInstance ?? this.state
290+
273291
const data = {
274292
from: tx.from,
275293
to: tx.to,
276294
value: new this._Curr(tx.value, 'base'),
277295
height: tx.blockNumber,
278296
txid: tx.hash
279297
}
280-
await this.state.storeTxHistory(data)
298+
await state.storeTxHistory(data)
281299
return data
282300
}
283301

284302
/**
285303
* Crawl HD wallet path, collect transactions and calculate balance of all addresses.
286304
* @param {object} opts
287-
* @param {bool=} opts.reset Reset all state and resync
305+
* @param {boolean=} opts.reset Reset all state and resync
288306
* @param {string=} opts.token Token name
289307
* @fires sync-path when a hd path is synced
290308
* @fires sync-end when entire HD path has been traversed, or when syncing is halted
@@ -309,7 +327,7 @@ class WalletPayGeneric extends WalletPay {
309327
return this.callToken('syncPath', opts.token, [addr, signal])
310328
}
311329

312-
const res = await this._syncAddressPath(addr, signal)
330+
const res = await this._syncAddressPath(addr, signal, state)
313331
this.emit('synced-path', syncState._addrType, syncState.path, res === signal.hasTx, syncState.toJSON())
314332
return res
315333
})

src/modules/provider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Provider extends EventEmitter {
6868
}
6969
const evname = res?.event
7070
if (!evname) return console.log('event has no name ignored ', res)
71-
this.emit(evname, res.data)
71+
this.emit(evname, res.error, res.data)
7272
})
7373
ws.on('open', resolve)
7474
})

src/modules/state.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ class StateDb {
122122
async storeTxHistory (data) {
123123
const i = Number(data.height)
124124
const blockTx = await this.getTxHistory(i)
125-
const exists = blockTx.filter(tx => tx.txid === data.txid).length > 0
125+
const exists = blockTx.filter(tx => {
126+
// Since there can be multiple txs with the same hash, we compare hash, to and from
127+
return tx.txid === data.txid && tx.to === data.to && tx.from === data.from
128+
}).length > 0
126129
if (exists) return
127130
blockTx.push(data)
128131
await this.updateTxIndex(i)

0 commit comments

Comments
 (0)