2019-02-05 18:41:17 +00:00
|
|
|
import web3 from 'Embark/web3'
|
2018-12-21 19:31:46 +00:00
|
|
|
import LiquidPledging from 'Embark/contracts/LiquidPledging'
|
2018-12-03 15:29:14 +00:00
|
|
|
|
2018-12-21 19:31:46 +00:00
|
|
|
const { getPledgeAdmin, numberOfPledges, getPledge } = LiquidPledging.methods
|
2018-12-03 15:29:14 +00:00
|
|
|
export const formatPledge = async (pledgePromise, idx) => {
|
|
|
|
const pledge = await pledgePromise
|
2019-02-05 18:41:17 +00:00
|
|
|
const blockNumber = await web3.eth.getBlockNumber()
|
2018-12-03 15:29:14 +00:00
|
|
|
return {
|
|
|
|
...pledge,
|
2019-02-05 18:41:17 +00:00
|
|
|
blockNumber,
|
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-21 19:31:46 +00:00
|
|
|
const numPledges = await LiquidPledging.methods.numberOfPledges().call()
|
2018-12-03 15:29:14 +00:00
|
|
|
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())
|
|
|
|
}
|
2018-12-10 19:04:31 +00:00
|
|
|
return Promise.all(pledges.map(formatPledge))
|
2018-12-03 15:29:14 +00:00
|
|
|
}
|
2018-12-03 17:15:16 +00:00
|
|
|
|
2019-02-05 18:41:17 +00:00
|
|
|
export const getPledges = async (pledges = []) => {
|
|
|
|
const updated = []
|
|
|
|
pledges.forEach(p => {
|
|
|
|
updated[p.pledgeId] = getPledge(p.pledgeId - 1).call()
|
|
|
|
})
|
|
|
|
return Promise.all(updated.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: {
|
2018-12-04 22:10:43 +00:00
|
|
|
...state.allPledges,
|
2018-12-03 17:15:16 +00:00
|
|
|
...newPledges
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
2018-12-04 22:10:43 +00:00
|
|
|
|
|
|
|
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 ]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|