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