liquid-funding/app/utils/pledges.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-12-03 15:29:14 +00:00
import LiquidPledgingMock from 'Embark/contracts/LiquidPledgingMock'
const { getPledgeAdmin, numberOfPledges, getPledge } = LiquidPledgingMock.methods
export const formatPledge = async (pledgePromise, idx) => {
const pledge = await pledgePromise
return {
...pledge,
2018-12-03 17:15:16 +00:00
id: idx + 1
2018-12-03 15:29:14 +00:00
}
}
2018-12-03 17:15:16 +00:00
export const getAllPledges = async (start = 1) => {
2018-12-03 15:29:14 +00:00
const numPledges = await numberOfPledges().call()
const pledges = []
2018-12-03 17:15:16 +00:00
for (let i = start; i <= numPledges; i++) {
2018-12-03 15:29:14 +00:00
pledges.push(getPledge(i).call())
}
return Promise.all(pledges.map(formatPledge))
}
2018-12-03 17:15:16 +00:00
export const appendToExistingPledges = async (pledges, setState) => {
const numPledges = await numberOfPledges().call()
const difference = numPledges - pledges.length
if (difference > 0) {
const newPledges = await getAllPledges(difference)
setState((state) => ({
...state,
allPledges: {
...state.allPledges,
2018-12-03 17:15:16 +00:00
...newPledges
}
}))
}
}
export const transferBetweenPledges = (setState, tx) => {
const { from, to, amount } = tx
setState((state) => {
const { allPledges } = state;
const updatedPledges = allPledges.map(pledge => {
if (pledge.id === Number(from)) {
pledge.amount = (BigInt(pledge.amount) - BigInt(amount)).toString()
return pledge
}
if (pledge.id === Number(to)) {
pledge.amount = (BigInt(pledge.amount) + BigInt(amount)).toString()
return pledge
}
return pledge
})
console.log({updatedPledges, tx})
return {
...state,
allPledges: [ ...updatedPledges ]
}
})
}