Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
fix: capture any error when sending transactions and warn about broad…
Browse files Browse the repository at this point in the history
…casting (#829)
  • Loading branch information
j-a-m-l authored and alexbarnsley committed Dec 21, 2018
1 parent 25fbd4b commit c29a9c5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 47 deletions.
51 changes: 33 additions & 18 deletions __tests__/unit/components/Transaction/TransactionModal.spec.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -58,8 +58,39 @@ describe('TransactionModal', () => {
response.status = 200 response.status = 200
}) })


describe('when the response does not include errors', () => {
beforeEach(() => {
response.data.errors = null
response.data.data = {
invalid: [],
accept: []
}
})

it('should return `true`', () => {
expect(wrapper.vm.isSuccessfulResponse(response)).toBeTrue()
})
})

describe('when the response includes errors', () => {
beforeEach(() => {
response.data.errors = {
tx1: [{ type: 'ERR_LOW_FEE' }]
}
response.data.data = {
invalid: [],
accept: []
}
})

it('should return `false`', () => {
expect(wrapper.vm.isSuccessfulResponse(response)).toBeFalse()
})
})

describe('when the response does not include invalid transactions', () => { describe('when the response does not include invalid transactions', () => {
beforeEach(() => { beforeEach(() => {
response.data.errors = null
response.data.data.invalid = [] response.data.data.invalid = []
}) })


Expand All @@ -75,24 +106,8 @@ describe('TransactionModal', () => {
} }
}) })


describe('when the response does not include accepted and broadcasted transactions', () => { it('should return `false`', () => {
beforeEach(() => { expect(wrapper.vm.isSuccessfulResponse(response)).toBeFalse()
response.data.data = {
invalid: ['tx1'],
accept: [],
broadcast: []
}
})

it('should return `false`', () => {
expect(wrapper.vm.isSuccessfulResponse(response)).toBeFalse()
})
})

xdescribe('when the response includes accepted or broadcasted transactions', () => {
it('should return `true`', () => {
expect(wrapper.vm.isSuccessfulResponse(response)).toBeTrue()
})
}) })
}) })
}) })
Expand Down
76 changes: 47 additions & 29 deletions src/renderer/components/Transaction/TransactionModal.vue
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -113,30 +113,55 @@ export default {
async onConfirm () { async onConfirm () {
// Produce the messages before closing the modal to avoid `$t` scope errors // Produce the messages before closing the modal to avoid `$t` scope errors
const success = this.$t(`TRANSACTION.SUCCESS.${this.transactionKey}`) const messages = {
const errorLowFee = this.$t('TRANSACTION.ERROR.FEE_TOO_LOW', { success: this.$t(`TRANSACTION.SUCCESS.${this.transactionKey}`),
fee: this.formatter_networkCurrency(this.transaction.fee) error: this.$t(`TRANSACTION.ERROR.${this.transactionKey}`),
}) errorLowFee: this.$t('TRANSACTION.ERROR.FEE_TOO_LOW', {
fee: this.formatter_networkCurrency(this.transaction.fee)
}),
warningBroadcast: this.$t('TRANSACTION.WARNING.BROADCAST')
}
this.emitSent() this.emitSent()
let response let response
if (this.alternativeWallet) { try {
const peer = await this.$store.dispatch('peer/findBest', { if (this.alternativeWallet) {
refresh: true, const peer = await this.$store.dispatch('peer/findBest', {
network: this.walletNetwork refresh: true,
}) network: this.walletNetwork
const apiClient = await this.$store.dispatch('peer/clientServiceFromPeer', peer) })
response = await apiClient.broadcastTransaction(this.transaction) const apiClient = await this.$store.dispatch('peer/clientServiceFromPeer', peer)
} else { response = await apiClient.broadcastTransaction(this.transaction)
response = await this.$client.broadcastTransaction(this.transaction) } else {
} response = await this.$client.broadcastTransaction(this.transaction)
}
if (this.isSuccessfulResponse(response)) { const { data, errors } = response.data
this.storeTransaction(this.transaction)
this.$success(success) if (this.isSuccessfulResponse(response)) {
} else { this.storeTransaction(this.transaction)
this.$error(errorLowFee)
if (data && data.accept.length === 0 && data.broadcast.length > 0) {
this.$warn(messages.warningBroadcast)
} else {
this.$success(messages.success)
}
} else {
const anyLowFee = Object.keys(errors).some(transactionId => {
return errors[transactionId].some(error => error.type === 'ERR_LOW_FEE')
})
// Be clear with the user about the error cause
if (anyLowFee) {
this.$error(messages.errorLowFee)
} else {
this.$error(messages.error)
}
}
} catch (error) {
this.$logger.error(error)
this.$error(messages.error)
} }
}, },
Expand All @@ -150,7 +175,7 @@ export default {
/** /**
* Checks if the response is successful: in case the transaction is rejected * Checks if the response is successful: in case the transaction is rejected
* due a low fee, it is broadcasted too, so it cannot be declared as invalid yet * due a low fee, but it is broadcasted too, it cannot be declared as invalid yet
* @param {Object} response * @param {Object} response
* @return {Boolean} * @return {Boolean}
*/ */
Expand All @@ -163,15 +188,8 @@ export default {
if (this.$client.version === 1) { if (this.$client.version === 1) {
return response.data.success return response.data.success
} else { } else {
const { data } = response.data const { data, errors } = response.data
if (data && data.invalid.length === 0) { return data && data.invalid.length === 0 && errors === null
return true
} else {
if (data && data.accept.length === 0 && data.broadcast.length === 0) {
return false
}
return true
}
} }
}, },
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/i18n/locales/en-US.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -684,6 +684,9 @@ export default {
VOTE_DELEGATE: 'Vote for delegate {delegate}', VOTE_DELEGATE: 'Vote for delegate {delegate}',
UNVOTE_DELEGATE: 'Unvote delegate {delegate}' UNVOTE_DELEGATE: 'Unvote delegate {delegate}'
}, },
WARNING: {
BROADCAST: 'Transaction was broadcasted to other peers. It may not be accepted by them'
},
AMOUNT: 'Amount', AMOUNT: 'Amount',
BLOCK_ID: 'Block ID', BLOCK_ID: 'Block ID',
CONFIRMATION_COUNT: '{0} Confirmations', CONFIRMATION_COUNT: '{0} Confirmations',
Expand Down

0 comments on commit c29a9c5

Please sign in to comment.