add process to update stale pledges

This commit is contained in:
Barry Gitarts 2019-02-05 13:41:17 -05:00
parent 94a850bb4b
commit e7e01c75d0
6 changed files with 60 additions and 5 deletions

View File

@ -1,10 +1,11 @@
import web3 from 'Embark/web3'
import { Q } from '@nozbe/watermelondb' import { Q } from '@nozbe/watermelondb'
import database from '../db' import database from '../db'
import { getAllPledges } from '../utils/pledges' import { getPledges, getAllPledges } from '../utils/pledges'
import { getProfilesById } from './profiles' import { getProfilesById } from './profiles'
const createPledge = (pledge, data, 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) const profile = profiles.find(p => p.idProfile == owner)
pledge.pledgeId = Number(id) pledge.pledgeId = Number(id)
pledge.owner = Number(owner) pledge.owner = Number(owner)
@ -14,6 +15,7 @@ const createPledge = (pledge, data, profiles) => {
pledge.nDelegates = Number(nDelegates) pledge.nDelegates = Number(nDelegates)
pledge.pledgeState = pledgeState pledge.pledgeState = pledgeState
pledge.intendedProject = Number(intendedProject) pledge.intendedProject = Number(intendedProject)
pledge.blockNumber = Number(blockNumber)
pledge.profile.set(profile) pledge.profile.set(profile)
} }
@ -48,11 +50,37 @@ export const getAndAddPledges = async () => {
batchAddPledges(newPledges, profiles) 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 () => { export const getLocalPledges = async () => {
const events = await pledgesCollection.query().fetch() const events = await pledgesCollection.query().fetch()
return events 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 => { export const getPledgeById = async id => {
const event = await pledgesCollection.query( const event = await pledgesCollection.query(
Q.where('id_profile', id) Q.where('id_profile', id)

View File

@ -10,7 +10,7 @@ import MainCointainer from './components/MainCointainer'
import { getAndAddLpEvents } from './actions/lpEvents' import { getAndAddLpEvents } from './actions/lpEvents'
import { getAndAddVaultEvents } from './actions/vaultEvents' import { getAndAddVaultEvents } from './actions/vaultEvents'
import { addFormattedProfiles } from './actions/profiles' import { addFormattedProfiles } from './actions/profiles'
import { getAndAddPledges } from './actions/pledges' import { updateStalePledges, getAndAddPledges } from './actions/pledges'
const { getNetworkType } = web3.eth.net const { getNetworkType } = web3.eth.net
@ -53,6 +53,7 @@ class App extends React.Component {
await getAndAddVaultEvents() await getAndAddVaultEvents()
await getAndAddPledges() await getAndAddPledges()
await addFormattedProfiles() await addFormattedProfiles()
await updateStalePledges()
this.setState({ loading: false }) this.setState({ loading: false })
} }

View File

@ -17,6 +17,7 @@ export default class Pledge extends LiquidModel {
@field('n_delegates') nDelegates @field('n_delegates') nDelegates
@field('intended_project') intendedProject @field('intended_project') intendedProject
@field('pledge_state') pledgeState @field('pledge_state') pledgeState
@field('block_number') blockNumber
@relation('profiles', 'profile_id') profile @relation('profiles', 'profile_id') profile
@action async transferTo(to, amount) { @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
})
}
} }

View File

@ -16,6 +16,7 @@ export default class Profile extends LiquidModel {
@field('name') name @field('name') name
@field('url') url @field('url') url
@field('id_profile') idProfile @field('id_profile') idProfile
@field('block_number') blockNumber
@children('pledges') pledges @children('pledges') pledges
@action async markAsCanceled() { @action async markAsCanceled() {

View File

@ -33,7 +33,8 @@ export default appSchema({
{ name: 'type', type: 'string' }, { name: 'type', type: 'string' },
{ name: 'name', type: 'string' }, { name: 'name', type: 'string' },
{ name: 'url', 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({ tableSchema({
@ -47,7 +48,8 @@ export default appSchema({
{ name: 'n_delegates', type: 'number' }, { name: 'n_delegates', type: 'number' },
{ name: 'intended_project', type: 'number' }, { name: 'intended_project', type: 'number' },
{ name: 'pledge_state', 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 }
] ]
}) })
] ]

View File

@ -1,10 +1,13 @@
import web3 from 'Embark/web3'
import LiquidPledging from 'Embark/contracts/LiquidPledging' import LiquidPledging from 'Embark/contracts/LiquidPledging'
const { getPledgeAdmin, numberOfPledges, getPledge } = LiquidPledging.methods const { getPledgeAdmin, numberOfPledges, getPledge } = LiquidPledging.methods
export const formatPledge = async (pledgePromise, idx) => { export const formatPledge = async (pledgePromise, idx) => {
const pledge = await pledgePromise const pledge = await pledgePromise
const blockNumber = await web3.eth.getBlockNumber()
return { return {
...pledge, ...pledge,
blockNumber,
id: idx + 1 id: idx + 1
} }
} }
@ -18,6 +21,14 @@ export const getAllPledges = async (start = 1) => {
return Promise.all(pledges.map(formatPledge)) 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) => { export const appendToExistingPledges = async (pledges, setState) => {
const numPledges = await numberOfPledges().call() const numPledges = await numberOfPledges().call()
const difference = numPledges - pledges.length const difference = numPledges - pledges.length