2018-12-21 19:31:46 +00:00
|
|
|
import LiquidPledging from 'Embark/contracts/LiquidPledging'
|
2018-12-12 19:55:43 +00:00
|
|
|
import LPVault from 'Embark/contracts/LPVault'
|
2018-12-01 23:32:56 +00:00
|
|
|
import web3 from 'Embark/web3'
|
|
|
|
|
2018-12-12 19:55:43 +00:00
|
|
|
const AUTHORIZE_PAYMENT = 'AuthorizePayment'
|
2018-12-03 13:50:15 +00:00
|
|
|
const GIVER_ADDED = 'GiverAdded'
|
|
|
|
const DELEGATE_ADDED = 'DelegateAdded'
|
2018-12-05 18:36:44 +00:00
|
|
|
const PROJECT_ADDED = 'ProjectAdded'
|
2018-12-14 17:48:42 +00:00
|
|
|
const ALL_EVENTS = 'allEvents'
|
2018-12-03 13:50:15 +00:00
|
|
|
const lookups = {
|
|
|
|
[GIVER_ADDED]: {
|
|
|
|
id: 'idGiver',
|
|
|
|
type: 'Funder'
|
|
|
|
},
|
|
|
|
[DELEGATE_ADDED]: {
|
|
|
|
id: 'idDelegate',
|
|
|
|
type: 'Delegate'
|
2018-12-05 18:36:44 +00:00
|
|
|
},
|
|
|
|
[PROJECT_ADDED]: {
|
|
|
|
id: 'idProject',
|
|
|
|
type: 'Project'
|
2018-12-03 13:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 15:45:19 +00:00
|
|
|
const hexToDecimal = hex => Number(parseInt(hex, 16))
|
2018-12-12 19:55:43 +00:00
|
|
|
const formatVaultEvent = async event => {
|
|
|
|
const { returnValues } = event
|
|
|
|
return {
|
|
|
|
...returnValues,
|
2019-01-11 15:45:19 +00:00
|
|
|
ref: hexToDecimal(returnValues.ref)
|
2018-12-12 19:55:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-15 10:49:08 +00:00
|
|
|
const getPastVaultEvents = async (event, raw = false) => {
|
2018-12-12 19:55:43 +00:00
|
|
|
const events = await LPVault.getPastEvents(event, {
|
|
|
|
addr: await web3.eth.getCoinbase(),
|
|
|
|
fromBlock: 0,
|
|
|
|
toBlock: 'latest'
|
|
|
|
})
|
2018-12-15 10:49:08 +00:00
|
|
|
if (raw) return events
|
2018-12-12 19:55:43 +00:00
|
|
|
const formattedEvents = await Promise.all(
|
|
|
|
events.map(formatVaultEvent)
|
|
|
|
)
|
|
|
|
return formattedEvents
|
|
|
|
}
|
|
|
|
|
2018-12-21 19:31:46 +00:00
|
|
|
const { getPledgeAdmin } = LiquidPledging.methods
|
2018-12-02 14:46:20 +00:00
|
|
|
export const formatFundProfileEvent = async event => {
|
2018-12-03 13:50:15 +00:00
|
|
|
const lookup = lookups[event.event]
|
2018-12-13 20:41:40 +00:00
|
|
|
const { returnValues: { url, idProject } } = event
|
2018-12-03 13:50:15 +00:00
|
|
|
const idProfile = event.returnValues[lookup.id]
|
2018-12-05 18:36:44 +00:00
|
|
|
const { addr, commitTime, name, canceled } = await getPledgeAdmin(idProfile).call()
|
2018-12-01 23:32:56 +00:00
|
|
|
return {
|
2018-12-03 13:50:15 +00:00
|
|
|
idProfile,
|
2018-12-13 20:41:40 +00:00
|
|
|
idProject,
|
2018-12-01 23:32:56 +00:00
|
|
|
url,
|
|
|
|
commitTime,
|
|
|
|
name,
|
2018-12-05 18:36:44 +00:00
|
|
|
addr,
|
2018-12-03 13:50:15 +00:00
|
|
|
canceled,
|
|
|
|
type: lookups[event.event].type
|
2018-12-01 23:32:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-14 17:48:42 +00:00
|
|
|
const getPastEvents = async (event, raw = false) => {
|
2018-12-21 19:31:46 +00:00
|
|
|
const events = await LiquidPledging.getPastEvents(event, {
|
2018-12-01 23:32:56 +00:00
|
|
|
addr: await web3.eth.getCoinbase(),
|
|
|
|
fromBlock: 0,
|
|
|
|
toBlock: 'latest'
|
|
|
|
})
|
2018-12-14 17:48:42 +00:00
|
|
|
if (raw) return events
|
2018-12-03 13:50:15 +00:00
|
|
|
const formattedEvents = await Promise.all(
|
|
|
|
events.map(formatFundProfileEvent)
|
|
|
|
)
|
2018-12-01 23:32:56 +00:00
|
|
|
return formattedEvents
|
|
|
|
}
|
2018-12-14 17:48:42 +00:00
|
|
|
|
|
|
|
export const lpEventsSubscription = async () => {
|
|
|
|
//todo add on event handlers
|
2018-12-21 19:31:46 +00:00
|
|
|
const events = await LiquidPledging.events.allEvents({
|
2018-12-14 17:48:42 +00:00
|
|
|
fromBlock: 0,
|
|
|
|
toBlock: 'latest'
|
|
|
|
})
|
|
|
|
return events
|
|
|
|
}
|
2018-12-05 18:36:44 +00:00
|
|
|
export const getFunderProfiles = async () => await getPastEvents(GIVER_ADDED)
|
|
|
|
export const getDelegateProfiles = async () => await getPastEvents(DELEGATE_ADDED)
|
|
|
|
export const getProjectProfiles = async () => await getPastEvents(PROJECT_ADDED)
|
2018-12-14 17:48:42 +00:00
|
|
|
export const getAllLPEvents = async () => await getPastEvents(ALL_EVENTS, true)
|
2018-12-12 19:55:43 +00:00
|
|
|
export const getAuthorizedPayments = async () => getPastVaultEvents(AUTHORIZE_PAYMENT)
|
2018-12-15 10:49:08 +00:00
|
|
|
export const getAllVaultEvents = async () => getPastVaultEvents(ALL_EVENTS,true)
|
2018-12-03 13:50:15 +00:00
|
|
|
export const getProfileEvents = async () => {
|
2018-12-05 18:36:44 +00:00
|
|
|
const [ funderProfiles, delegateProfiles, projectProfiles]
|
|
|
|
= await Promise.all([getFunderProfiles(), getDelegateProfiles(), getProjectProfiles()])
|
|
|
|
return [ ...funderProfiles, ...delegateProfiles, ...projectProfiles]
|
2018-12-03 13:50:15 +00:00
|
|
|
}
|