From d36b1a98668244a5903373235c9380a510114fda Mon Sep 17 00:00:00 2001 From: Danil Kovtonyuk Date: Tue, 5 Jul 2022 18:29:31 +1000 Subject: [PATCH] fix: event service --- components/withdraw/Withdraw.vue | 4 +++- services/events.js | 24 +++++++++++++++++++++--- store/application.js | 8 ++------ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/components/withdraw/Withdraw.vue b/components/withdraw/Withdraw.vue index 6076b1a..b1a162d 100644 --- a/components/withdraw/Withdraw.vue +++ b/components/withdraw/Withdraw.vue @@ -133,8 +133,8 @@ :class="{ 'slide-animation-active': isLoading }" :disabled="isWithdrawalButtonDisable" :loading="isLoadingRelayers || isLoading" - @click="onWithdraw" data-test="button_start_withdraw" + @click="onWithdraw" > {{ $t('withdrawButton') }} @@ -382,6 +382,8 @@ export default { }, activeTab(newTab, oldTab) { if (newTab !== oldTab && newTab === 1) { + this.withdrawAddress = '' + this.withdrawNote = '' this.error = { type: '', message: '' diff --git a/services/events.js b/services/events.js index 1e7e1e9..bdc2ca5 100644 --- a/services/events.js +++ b/services/events.js @@ -36,10 +36,10 @@ class EventService { } return cachedEvents } - async updateEvents(type) { + async updateEvents(type, cachedEvents) { const { deployedBlock } = networkConfig[`netId${this.netId}`] - const savedEvents = await this.getEvents(type) + const savedEvents = cachedEvents || (await this.getEvents(type)) let fromBlock = deployedBlock if (savedEvents) { @@ -157,8 +157,17 @@ class EventService { } } - async getStatisticsRpc({ fromBlock, eventsCount }) { + async getStatisticsRpc({ eventsCount }) { + const { deployedBlock } = networkConfig[`netId${this.netId}`] + const savedEvents = await this.getEvents(eventsType.DEPOSIT) + + if (savedEvents.events.length) { + const { events } = await this.updateEvents(eventsType.DEPOSIT, savedEvents) + return events + } + const blockRange = 4950 + const fromBlock = deployedBlock const { blockDifference, currentBlockNumber } = await this.getBlocksDiff({ fromBlock }) let numberParts = blockDifference === 0 ? 1 : Math.ceil(blockDifference / blockRange) @@ -225,6 +234,15 @@ class EventService { async getEventsPartFromRpc({ fromBlock, toBlock, type }) { try { + const { currentBlockNumber } = await this.getBlocksDiff({ fromBlock }) + + if (fromBlock > currentBlockNumber) { + return { + events: [], + lastBlock: fromBlock + } + } + const events = await this.contract.getPastEvents(capitalizeFirstLetter(type), { fromBlock, toBlock diff --git a/store/application.js b/store/application.js index 1caf3cb..4d12d13 100644 --- a/store/application.js +++ b/store/application.js @@ -270,19 +270,15 @@ const actions = { async updateSelectEvents({ dispatch, commit, state, rootGetters, getters }) { const netId = rootGetters['metamask/netId'] const { currency, amount } = state.selectedStatistic - const { deployedBlock } = networkConfig[`netId${netId}`] const eventService = getters.eventsInterface.getService({ netId, amount, currency }) - const savedEvents = await eventService.getEvents(eventsType.DEPOSIT) - const fromBlock = savedEvents?.lastBlock || deployedBlock - - const graphEvents = await eventService.getEventsFromGraph({ fromBlock, methodName: 'getStatistic' }) + const graphEvents = await eventService.getEventsFromGraph({ methodName: 'getStatistic' }) let statistic = graphEvents?.events if (!statistic || !statistic.length) { - const fresh = await eventService.getStatisticsRpc({ fromBlock, eventsCount: 10 }) + const fresh = await eventService.getStatisticsRpc({ eventsCount: 10 }) statistic = fresh || [] }