liquid-funding/app/actions/lpEvents.js

62 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-01-15 22:29:02 +00:00
import { Q } from '@nozbe/watermelondb'
import database from '../db'
2019-01-19 18:12:36 +00:00
import { GIVER_ADDED, DELEGATE_ADDED, PROJECT_ADDED } from '../utils/events'
2019-01-15 22:29:02 +00:00
const lpCollection = database.collections.get('lp_events')
export const addEvent = async data => {
await database.action(async () => {
const res = await lpCollection.create(lpEvent => {
const { event, address, id, blockNumber } = data
lpEvent.eventId = id
lpEvent.address = address
lpEvent.event = event
lpEvent.blockNumber = blockNumber
})
return res
})
}
export const batchAddEvents = async events => {
const batch = events.map(e => {
return lpCollection.prepareCreate(lpEvent => {
2019-01-19 18:12:36 +00:00
const { event, address, id, blockNumber, returnValues } = e
2019-01-15 22:29:02 +00:00
lpEvent.eventId = id
lpEvent.address = address
lpEvent.event = event
lpEvent.blockNumber = blockNumber
2019-01-19 18:12:36 +00:00
lpEvent.returnValues = returnValues
2019-01-15 22:29:02 +00:00
})
})
return await database.action(async () => await database.batch(...batch))
}
2019-01-19 18:12:36 +00:00
export const getLatestProfileEvents = async eventIds => {
const events = await lpCollection.query(
Q.where(
'id',
Q.notIn(eventIds)
),
Q.where(
'event',
Q.oneOf([GIVER_ADDED, DELEGATE_ADDED, PROJECT_ADDED])
)
).fetch()
return events
}
2019-01-19 15:08:16 +00:00
2019-01-15 22:29:02 +00:00
export const getLpEventById = async id => {
const event = await lpCollection.query(
2019-01-16 18:57:07 +00:00
Q.where('event_id', id)
2019-01-15 22:29:02 +00:00
).fetch()
return event
}
2019-01-19 01:49:14 +00:00
export const getLastBlockStored = async () => {
const col = await lpCollection.query().fetch()
2019-01-19 15:08:16 +00:00
const blockNumber = col.length
? col.sort((a,b) => b.blockNumber - a.blockNumber)[0].blockNumber
: 0
return blockNumber
2019-01-19 01:49:14 +00:00
}