Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 38 additions & 49 deletions src/MoneroEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class MoneroEngine implements EdgeCurrencyEngine {
})
}

let edgeTransaction: EdgeTransaction = {
const edgeTransaction: EdgeTransaction = {
blockHeight,
currencyCode: 'XMR',
date,
Expand All @@ -284,33 +284,9 @@ export class MoneroEngine implements EdgeCurrencyEngine {
walletId: this.walletId
}

const idx = this.findTransaction(PRIMARY_CURRENCY_TOKEN_ID, tx.hash)
if (idx === -1) {
this.log(`New transaction: ${tx.hash}`)

// New transaction not in database
this.addTransaction(PRIMARY_CURRENCY_TOKEN_ID, edgeTransaction)

this.edgeTxLibCallbacks.onTransactions(this.transactionEventArray)
this.transactionEventArray = []
} else {
// Already have this tx in the database. See if anything changed
const transactionsArray = this.getTxs(PRIMARY_CURRENCY_TOKEN_ID)
const edgeTx = transactionsArray[idx]

if (edgeTx.blockHeight !== edgeTransaction.blockHeight) {
// The native amounts returned from the API take some time before they're accurate. We can trust the amounts we saved instead.
edgeTransaction = {
...edgeTransaction,
nativeAmount: edgeTx.nativeAmount
}

this.log(`Update transaction: ${tx.hash} height:${tx.height}`)
this.updateTransaction(PRIMARY_CURRENCY_TOKEN_ID, edgeTransaction, idx)
this.edgeTxLibCallbacks.onTransactions(this.transactionEventArray)
this.transactionEventArray = []
}
}
this.saveTransactionState(PRIMARY_CURRENCY_TOKEN_ID, edgeTransaction)
this.edgeTxLibCallbacks.onTransactions(this.transactionEventArray)
this.transactionEventArray = []

return blockHeight
}
Expand Down Expand Up @@ -379,11 +355,15 @@ export class MoneroEngine implements EdgeCurrencyEngine {
return txs as EdgeTransaction[]
}

addTransaction(tokenId: EdgeTokenId, edgeTransaction: EdgeTransaction): void {
saveTransactionState(
tokenId: EdgeTokenId,
edgeTransaction: EdgeTransaction
): void {
// Add or update tx in transactionsObj
const idx = this.findTransaction(tokenId, edgeTransaction.txid)

if (idx === -1) {
this.log(`New transaction: ${edgeTransaction.txid}`)
this.log.warn(
'addTransaction: adding and sorting:' +
edgeTransaction.txid +
Expand All @@ -408,26 +388,35 @@ export class MoneroEngine implements EdgeCurrencyEngine {
transaction: edgeTransaction
})
} else {
this.updateTransaction(tokenId, edgeTransaction, idx)
}
}
const txs = this.getTxs(tokenId)
const edgeTx = txs[idx]

updateTransaction(
tokenId: EdgeTokenId,
edgeTransaction: EdgeTransaction,
idx: number
): void {
// Update the transaction
const txs = this.getTxs(tokenId)
txs[idx] = edgeTransaction
this.walletLocalDataDirty = true
this.transactionEventArray.push({
isNew: false,
transaction: edgeTransaction
})
this.log.warn(
'updateTransaction' + edgeTransaction.txid + edgeTransaction.nativeAmount
)
// Already have this tx in the database. Consider a change if blockHeight changed
if (edgeTx.blockHeight === edgeTransaction.blockHeight) return
this.log(
`Update transaction: ${edgeTransaction.txid} height:${edgeTransaction.blockHeight}`
)

// The native amounts returned from the API take some time before they're
// accurate. We can trust the amounts we saved instead.
edgeTransaction = {
...edgeTransaction,
nativeAmount: edgeTx.nativeAmount
}

// Update the transaction
txs[idx] = edgeTransaction
this.walletLocalDataDirty = true
this.transactionEventArray.push({
isNew: false,
transaction: edgeTransaction
})
this.log.warn(
'updateTransaction' +
edgeTransaction.txid +
edgeTransaction.nativeAmount
)
}
}

// *************************************
Expand Down Expand Up @@ -746,7 +735,7 @@ export class MoneroEngine implements EdgeCurrencyEngine {
}

async saveTx(edgeTransaction: EdgeTransaction): Promise<void> {
await this.addTransaction(edgeTransaction.tokenId, edgeTransaction)
await this.saveTransactionState(edgeTransaction.tokenId, edgeTransaction)
}

getDisplayPrivateSeed(privateKeys: JsonObject): string {
Expand Down
Loading