diff --git a/app/actions/lpEvents.js b/app/actions/lpEvents.js index 5e176b5..54cf963 100644 --- a/app/actions/lpEvents.js +++ b/app/actions/lpEvents.js @@ -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) +} diff --git a/app/actions/pledges.js b/app/actions/pledges.js index 75f50c3..77e869b 100644 --- a/app/actions/pledges.js +++ b/app/actions/pledges.js @@ -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) } diff --git a/app/actions/vaultEvents.js b/app/actions/vaultEvents.js new file mode 100644 index 0000000..fb55f3c --- /dev/null +++ b/app/actions/vaultEvents.js @@ -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) +} diff --git a/app/components/PledgesTable.jsx b/app/components/PledgesTable.jsx index bf9a02b..b589881 100644 --- a/app/components/PledgesTable.jsx +++ b/app/components/PledgesTable.jsx @@ -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 () => { diff --git a/app/db.js b/app/db.js index 39a2def..9ef5cd6 100644 --- a/app/db.js +++ b/app/db.js @@ -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 ], diff --git a/app/model/schema.js b/app/model/schema.js index 7811ac8..e330a59 100644 --- a/app/model/schema.js +++ b/app/model/schema.js @@ -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: [ diff --git a/app/model/vaultEvent.js b/app/model/vaultEvent.js new file mode 100644 index 0000000..e3b7f1c --- /dev/null +++ b/app/model/vaultEvent.js @@ -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 + }) + } +} + diff --git a/app/utils/events.js b/app/utils/events.js index bbf3350..617701d 100644 --- a/app/utils/events.js +++ b/app/utils/events.js @@ -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()])