liquid-funding/app/actions/pledges.js

98 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-02-05 18:41:17 +00:00
import web3 from 'Embark/web3'
import { Q } from '@nozbe/watermelondb'
import database from '../db'
2019-02-05 18:41:17 +00:00
import { getPledges, getAllPledges } from '../utils/pledges'
import { getProfilesById } from './profiles'
const createPledge = (pledge, data, profiles) => {
const { id, owner, amount, blockNumber, token, commitTime, nDelegates, pledgeState, intendedProject, delegates } = data
const profile = profiles.find(p => p.idProfile == owner)
pledge.idPledge = Number(id)
pledge.owner = Number(owner)
pledge.amount = amount
pledge.token = token
pledge.commitTime = Number(commitTime)
pledge.nDelegates = Number(nDelegates)
pledge.pledgeState = Number(pledgeState)
pledge.intendedProject = Number(intendedProject)
2019-02-05 18:41:17 +00:00
pledge.blockNumber = Number(blockNumber)
pledge.delegates = delegates
pledge.profile.set(profile)
}
const pledgesCollection = database.collections.get('pledges')
export const addPledge = async data => {
return await database.action(async () => {
const res = await pledgesCollection.create(pledge => createPledge(pledge, data))
return res
})
}
export const batchAddPledges = async (pledges, profiles = []) => {
const batch = pledges.map(data => {
return pledgesCollection.prepareCreate(pledge => createPledge(pledge, data, profiles))
})
console.log({batch})
return await database.action(async () => await database.batch(...batch))
}
const getLastPledge = pledges => {
const idPledge = pledges.length
? pledges.sort((a,b) => b.idPledge - a.idPledge)[0].idPledge
2019-01-22 19:58:11 +00:00
: 0
return idPledge
}
export const getAndAddPledges = async () => {
const pledges = await getLocalPledges()
const idPledge = getLastPledge(pledges)
const newPledges = await getAllPledges(idPledge + 1)
const idPledges = newPledges.map(p => p.owner)
const profiles = await getProfilesById(idPledges)
batchAddPledges(newPledges, profiles)
}
2019-02-05 18:41:17 +00:00
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.idPledge - 1]
2019-02-05 18:41:17 +00:00
return p.prepareUpdate(p => {
const { amount, nDelegates, pledgeState, blockNumber } = updated
p.amount = amount
p.nDelegates = Number(nDelegates)
2019-03-22 21:54:56 +00:00
p.pledgeState = Number(pledgeState)
2019-02-05 18:41:17 +00:00
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
}
2019-02-05 18:41:17 +00:00
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)
).fetch()
return event
}
export const getPledgesWithDelegates = async () => {
const event = await pledgesCollection.query(
Q.where('n_delegates', Q.gt(0))
).fetch()
return event
}