From e7e01c75d01808bc231eee4f07311b375a3aadde Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Tue, 5 Feb 2019 13:41:17 -0500 Subject: [PATCH] add process to update stale pledges --- app/actions/pledges.js | 32 ++++++++++++++++++++++++++++++-- app/dapp.js | 3 ++- app/model/pledge.js | 12 ++++++++++++ app/model/profile.js | 1 + app/model/schema.js | 6 ++++-- app/utils/pledges.js | 11 +++++++++++ 6 files changed, 60 insertions(+), 5 deletions(-) diff --git a/app/actions/pledges.js b/app/actions/pledges.js index 77e869b..6ec0366 100644 --- a/app/actions/pledges.js +++ b/app/actions/pledges.js @@ -1,10 +1,11 @@ +import web3 from 'Embark/web3' import { Q } from '@nozbe/watermelondb' import database from '../db' -import { getAllPledges } from '../utils/pledges' +import { getPledges, getAllPledges } from '../utils/pledges' import { getProfilesById } from './profiles' const createPledge = (pledge, data, profiles) => { - const { id, owner, amount, token, commitTime, nDelegates, pledgeState, intendedProject } = data + const { id, owner, amount, blockNumber, token, commitTime, nDelegates, pledgeState, intendedProject } = data const profile = profiles.find(p => p.idProfile == owner) pledge.pledgeId = Number(id) pledge.owner = Number(owner) @@ -14,6 +15,7 @@ const createPledge = (pledge, data, profiles) => { pledge.nDelegates = Number(nDelegates) pledge.pledgeState = pledgeState pledge.intendedProject = Number(intendedProject) + pledge.blockNumber = Number(blockNumber) pledge.profile.set(profile) } @@ -48,11 +50,37 @@ export const getAndAddPledges = async () => { batchAddPledges(newPledges, profiles) } +export const updateStalePledges = async () => { + //TODO optimize fetch based only on events + const stalePledges = await getStalePledges() + const updatedPledges = await getPledges(stalePledges) + const batch = stalePledges.map(p => { + const updated = updatedPledges[p.pledgeId] + return p.prepareUpdate(p => { + const { amount, nDelegates, pledgeState, blockNumber } = updated + p.amount = amount + p.nDelegates = Number(nDelegates) + p.pledgeState = pledgeState + p.blockNumber = blockNumber + }) + }) + database.action(() => database.batch(...batch)) + console.log('updated batch completed') +} + export const getLocalPledges = async () => { const events = await pledgesCollection.query().fetch() return events } +export const getStalePledges = async () => { + const blockNumber = await web3.eth.getBlockNumber() + const pledges = await pledgesCollection.query( + Q.where('block_number', Q.lt(blockNumber)) + ).fetch() + return pledges +} + export const getPledgeById = async id => { const event = await pledgesCollection.query( Q.where('id_profile', id) diff --git a/app/dapp.js b/app/dapp.js index 02a6748..af958af 100644 --- a/app/dapp.js +++ b/app/dapp.js @@ -10,7 +10,7 @@ import MainCointainer from './components/MainCointainer' import { getAndAddLpEvents } from './actions/lpEvents' import { getAndAddVaultEvents } from './actions/vaultEvents' import { addFormattedProfiles } from './actions/profiles' -import { getAndAddPledges } from './actions/pledges' +import { updateStalePledges, getAndAddPledges } from './actions/pledges' const { getNetworkType } = web3.eth.net @@ -53,6 +53,7 @@ class App extends React.Component { await getAndAddVaultEvents() await getAndAddPledges() await addFormattedProfiles() + await updateStalePledges() this.setState({ loading: false }) } diff --git a/app/model/pledge.js b/app/model/pledge.js index 3b3c8a4..74d558a 100644 --- a/app/model/pledge.js +++ b/app/model/pledge.js @@ -17,6 +17,7 @@ export default class Pledge extends LiquidModel { @field('n_delegates') nDelegates @field('intended_project') intendedProject @field('pledge_state') pledgeState + @field('block_number') blockNumber @relation('profiles', 'profile_id') profile @action async transferTo(to, amount) { @@ -33,4 +34,15 @@ export default class Pledge extends LiquidModel { }) ) } + + @action async updateFields(newPledge) { + const { amount, nDelegates, pledgeState, blockNumber } = newPledge + this.prepareUpdate(pledge => { + pledge.amount = amount + pledge.nDelegates = Number(nDelegates) + pledge.pledgeState = pledgeState + pledge.blockNumber = blockNumber + }) + + } } diff --git a/app/model/profile.js b/app/model/profile.js index e531b33..0729693 100644 --- a/app/model/profile.js +++ b/app/model/profile.js @@ -16,6 +16,7 @@ export default class Profile extends LiquidModel { @field('name') name @field('url') url @field('id_profile') idProfile + @field('block_number') blockNumber @children('pledges') pledges @action async markAsCanceled() { diff --git a/app/model/schema.js b/app/model/schema.js index e330a59..ac0cb02 100644 --- a/app/model/schema.js +++ b/app/model/schema.js @@ -33,7 +33,8 @@ export default appSchema({ { name: 'type', type: 'string' }, { name: 'name', type: 'string' }, { name: 'url', type: 'string' }, - { name: 'id_profile', type: 'number', isIndexed: true } + { name: 'id_profile', type: 'number', isIndexed: true }, + { name: 'block_number', type: 'number', isIndexed: true } ] }), tableSchema({ @@ -47,7 +48,8 @@ export default appSchema({ { name: 'n_delegates', type: 'number' }, { name: 'intended_project', type: 'number' }, { name: 'pledge_state', type: 'number' }, - { name: 'profile_id', type: 'string', isIndexed: true } + { name: 'profile_id', type: 'string', isIndexed: true }, + { name: 'block_number', type: 'number', isIndexed: true } ] }) ] diff --git a/app/utils/pledges.js b/app/utils/pledges.js index e3d4dd4..279935e 100644 --- a/app/utils/pledges.js +++ b/app/utils/pledges.js @@ -1,10 +1,13 @@ +import web3 from 'Embark/web3' import LiquidPledging from 'Embark/contracts/LiquidPledging' const { getPledgeAdmin, numberOfPledges, getPledge } = LiquidPledging.methods export const formatPledge = async (pledgePromise, idx) => { const pledge = await pledgePromise + const blockNumber = await web3.eth.getBlockNumber() return { ...pledge, + blockNumber, id: idx + 1 } } @@ -18,6 +21,14 @@ export const getAllPledges = async (start = 1) => { return Promise.all(pledges.map(formatPledge)) } +export const getPledges = async (pledges = []) => { + const updated = [] + pledges.forEach(p => { + updated[p.pledgeId] = getPledge(p.pledgeId - 1).call() + }) + return Promise.all(updated.map(formatPledge)) +} + export const appendToExistingPledges = async (pledges, setState) => { const numPledges = await numberOfPledges().call() const difference = numPledges - pledges.length