Skip to content
Permalink
Browse files

fix: capture any error when sending transactions and warn about broad…

…casting (#829)
  • Loading branch information...
j-a-m-l authored and alexbarnsley committed Dec 21, 2018
1 parent 25fbd4b commit c29a9c52133c00d50d35d801d24fec83976fc5bf
@@ -58,8 +58,39 @@ describe('TransactionModal', () => {
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', () => {
beforeEach(() => {
response.data.errors = null
response.data.data.invalid = []
})

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

describe('when the response does not include accepted and broadcasted transactions', () => {
beforeEach(() => {
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()
})
it('should return `false`', () => {
expect(wrapper.vm.isSuccessfulResponse(response)).toBeFalse()
})
})
})
@@ -113,30 +113,55 @@ export default {
async onConfirm () {
// Produce the messages before closing the modal to avoid `$t` scope errors
const success = this.$t(`TRANSACTION.SUCCESS.${this.transactionKey}`)
const errorLowFee = this.$t('TRANSACTION.ERROR.FEE_TOO_LOW', {
fee: this.formatter_networkCurrency(this.transaction.fee)
})
const messages = {
success: this.$t(`TRANSACTION.SUCCESS.${this.transactionKey}`),
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()
let response
if (this.alternativeWallet) {
const peer = await this.$store.dispatch('peer/findBest', {
refresh: true,
network: this.walletNetwork
})
const apiClient = await this.$store.dispatch('peer/clientServiceFromPeer', peer)
response = await apiClient.broadcastTransaction(this.transaction)
} else {
response = await this.$client.broadcastTransaction(this.transaction)
}
try {
if (this.alternativeWallet) {
const peer = await this.$store.dispatch('peer/findBest', {
refresh: true,
network: this.walletNetwork
})
const apiClient = await this.$store.dispatch('peer/clientServiceFromPeer', peer)
response = await apiClient.broadcastTransaction(this.transaction)
} else {
response = await this.$client.broadcastTransaction(this.transaction)
}
if (this.isSuccessfulResponse(response)) {
this.storeTransaction(this.transaction)
this.$success(success)
} else {
this.$error(errorLowFee)
const { data, errors } = response.data
if (this.isSuccessfulResponse(response)) {
this.storeTransaction(this.transaction)
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)
}
},
@@ -150,7 +175,7 @@ export default {
/**
* 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
* @return {Boolean}
*/
@@ -163,15 +188,8 @@ export default {
if (this.$client.version === 1) {
return response.data.success
} else {
const { data } = response.data
if (data && data.invalid.length === 0) {
return true
} else {
if (data && data.accept.length === 0 && data.broadcast.length === 0) {
return false
}
return true
}
const { data, errors } = response.data
return data && data.invalid.length === 0 && errors === null
}
},
@@ -684,6 +684,9 @@ export default {
VOTE_DELEGATE: 'Vote for delegate {delegate}',
UNVOTE_DELEGATE: 'Unvote delegate {delegate}'
},
WARNING: {
BROADCAST: 'Transaction was broadcasted to other peers. It may not be accepted by them'
},
AMOUNT: 'Amount',
BLOCK_ID: 'Block ID',
CONFIRMATION_COUNT: '{0} Confirmations',

0 comments on commit c29a9c5

Please sign in to comment.
You can’t perform that action at this time.