add vault_events

This commit is contained in:
Barry Gitarts 2019-01-25 12:18:44 -05:00
parent 17f7fb9011
commit a64ef2f89a
8 changed files with 113 additions and 11 deletions

View File

@ -1,6 +1,6 @@
import { Q } from '@nozbe/watermelondb'
import database from '../db'
import { GIVER_ADDED, DELEGATE_ADDED, PROJECT_ADDED } from '../utils/events'
import { getAllLPEvents, GIVER_ADDED, DELEGATE_ADDED, PROJECT_ADDED } from '../utils/events'
const lpCollection = database.collections.get('lp_events')
export const addEvent = async data => {
@ -59,3 +59,9 @@ export const getLastBlockStored = async () => {
: 0
return blockNumber
}
export const getAndAddLpEvents = async () => {
const lastBlock = await getLastBlockStored()
const events = await getAllLPEvents(lastBlock + 1)
batchAddEvents(events)
}

View File

@ -14,7 +14,6 @@ const createPledge = (pledge, data, profiles) => {
pledge.nDelegates = Number(nDelegates)
pledge.pledgeState = pledgeState
pledge.intendedProject = Number(intendedProject)
//pledge.profile.id = profile.id
pledge.profile.set(profile)
}

View File

@ -0,0 +1,52 @@
import { Q } from '@nozbe/watermelondb'
import database from '../db'
import { ALL_EVENTS, getAllVaultEvents } from '../utils/events'
const vaultCollection = database.collections.get('vault_events')
export const addEvent = async data => {
await database.action(async () => {
const res = await vaultCollection.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 vaultCollection.prepareCreate(lpEvent => {
const { event, address, id, blockNumber, returnValues } = e
lpEvent.eventId = id
lpEvent.address = address
lpEvent.event = event
lpEvent.blockNumber = blockNumber
lpEvent.returnValues = returnValues
})
})
return await database.action(async () => await database.batch(...batch))
}
export const getVaultEventById = async id => {
const event = await vaultCollection.query(
Q.where('event_id', id)
).fetch()
return event
}
export const getLastBlockStored = async () => {
const col = await vaultCollection.query().fetch()
const blockNumber = col.length
? col.sort((a,b) => b.blockNumber - a.blockNumber)[0].blockNumber
: 0
return blockNumber
}
export const getAndAddVaultEvents = async () => {
const lastBlock = await getLastBlockStored()
const events = await getAllVaultEvents(lastBlock + 1)
batchAddEvents(events)
}

View File

@ -45,10 +45,15 @@ class PledgesTable extends Component {
componentDidUpdate() {
const { pledges } = this.props
const { data } = this.state
pledges.some((pledge, idx) => {
const current = data[idx]
if (toEther(pledge.amount) != current.amount || pledgeStateMap[pledge.pledgeState] != current.pledgeState) this.setData()
})
if (data.length) {
pledges.some((pledge, idx) => {
const current = data[idx]
if (current) {
if (toEther(pledge.amount) != current.amount || pledgeStateMap[pledge.pledgeState] != current.pledgeState) this.setData()
}
})
}
if (pledges.length && !data.length) this.setData()
}
setData = async () => {

View File

@ -3,6 +3,7 @@ import LokiJSAdapter from '@nozbe/watermelondb/adapters/lokijs'
import schema from './model/schema'
import LpEvent from './model/lpEvents'
import VaultEvent from './model/vaultEvent'
import Profile from './model/profile'
import Pledge from './model/pledge'
@ -16,6 +17,7 @@ const database = new Database({
adapter,
modelClasses: [
LpEvent,
VaultEvent,
Profile,
Pledge
],

View File

@ -13,6 +13,16 @@ export default appSchema({
{ name : 'return_values', type: 'string', isOptional: true }
]
}),
tableSchema({
name: 'vault_events',
columns: [
{ name: 'event_id', type: 'string', isIndexed: true },
{ name: 'address', type: 'string' },
{ name: 'event', type: 'string', isIndexed: true },
{ name: 'block_number', type: 'number', isIndexed: true },
{ name : 'return_values', type: 'string', isOptional: true }
]
}),
tableSchema({
name: 'profiles',
columns: [

28
app/model/vaultEvent.js Normal file
View File

@ -0,0 +1,28 @@
import { Model } from '@nozbe/watermelondb'
import { action, field, json } from '@nozbe/watermelondb/decorators'
const sanitizeValues = json => json
export default class VaultEvent extends Model {
static table = 'vault_events'
@field('address') address
@field('event_id') eventId
@field('event') event
@field('block_number') blockNumber
@json('return_values', sanitizeValues) returnValues
@action async addEvent(data) {
return await this.create(lpEvent => {
const { event, address, id, blockNumber } = data
lpEvent.eventId = id
lpEvent.address = address
lpEvent.event = event
lpEvent.blockNumber = blockNumber
})
}
}

View File

@ -33,10 +33,10 @@ const formatVaultEvent = async event => {
}
}
const getPastVaultEvents = async (event, raw = false) => {
const getPastVaultEvents = async (event, raw = false, fromBlock = 0) => {
const events = await LPVault.getPastEvents(event, {
addr: await web3.eth.getCoinbase(),
fromBlock: 0,
fromBlock,
toBlock: 'latest'
})
if (raw) return events
@ -89,13 +89,13 @@ export const lpEventsSubscription = async () => {
export const getFunderProfiles = async () => await getPastEvents(GIVER_ADDED)
export const getDelegateProfiles = async () => await getPastEvents(DELEGATE_ADDED)
export const getProjectProfiles = async () => await getPastEvents(PROJECT_ADDED)
export const getAllLPEvents = async () => await getPastEvents(
export const getAllLPEvents = async fromBlock => await getPastEvents(
ALL_EVENTS,
true,
await getLastBlockStored() + 1
fromBlock
)
export const getAuthorizedPayments = async () => getPastVaultEvents(AUTHORIZE_PAYMENT)
export const getAllVaultEvents = async () => getPastVaultEvents(ALL_EVENTS,true)
export const getAllVaultEvents = async (fromBlock = 0) => getPastVaultEvents(ALL_EVENTS,true, fromBlock)
export const getProfileEvents = async () => {
const [ funderProfiles, delegateProfiles, projectProfiles]
= await Promise.all([getFunderProfiles(), getDelegateProfiles(), getProjectProfiles()])