classic-ui-dev/modules/account/store/actions/utils.js

78 lines
1.9 KiB
JavaScript
Raw Normal View History

2022-06-09 12:41:41 -04:00
import { graph } from '@/services'
import networkConfig from '@/networkConfig'
2022-04-21 23:05:56 -04:00
function createMutation({ commit, rootState }, { type, payload }) {
const { netId } = rootState.metamask
commit(type, { ...payload, netId })
}
function clearState({ dispatch }, { key }) {
dispatch('createMutation', {
type: 'CLEAR_STATE',
payload: { key }
})
}
2022-06-09 12:41:41 -04:00
async function getEventsFromBlockPart({ echoContract, address, currentBlockNumber, netId }) {
try {
const { events: graphEvents, lastSyncBlock } = await graph.getNoteAccounts({ address, netId })
if (graphEvents.length) {
return graphEvents
}
let { NOTE_ACCOUNT_BLOCK: fromBlock } = networkConfig[`netId${netId}`].constants
if (lastSyncBlock) {
fromBlock = lastSyncBlock
}
const blockDifference = Math.ceil(currentBlockNumber - lastSyncBlock)
let blockRange = blockDifference
if (Number(netId) === 56) {
blockRange = 4950
}
let numberParts = blockDifference === 0 ? 1 : Math.ceil(blockDifference / blockRange)
const part = Math.ceil(blockDifference / numberParts)
let events = []
let toBlock = lastSyncBlock + part
if (toBlock >= currentBlockNumber) {
toBlock = 'latest'
numberParts = 1
}
for (let i = 0; i < numberParts; i++) {
const partOfEvents = await echoContract.getEvents({
fromBlock,
toBlock,
address
})
if (partOfEvents) {
events = events.concat(
partOfEvents.map((event) => ({
address: event.returnValues.who,
encryptedAccount: event.returnValues.data
}))
)
2022-06-09 12:41:41 -04:00
}
fromBlock = toBlock
toBlock += part
}
events = graphEvents.concat(events)
return events
} catch (err) {
console.log(`getEventsFromBlock has error: ${err.message}`)
return false
}
}
export { clearState, createMutation, getEventsFromBlockPart }