-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathpolkadotV0-reducers.js
904 lines (856 loc) · 24.4 KB
/
polkadotV0-reducers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
const _ = require('lodash')
const BigNumber = require('bignumber.js')
const {
fixDecimalsAndRoundUp,
fixDecimalsAndRoundUpBigNumbers,
toViewDenom
} = require('../../common/numbers.js')
const { getProposalSummary } = require('./common')
const { lunieMessageTypes } = require('../../lib/message-types')
const {
getMessageTitle,
getPushLink,
getIcon
} = require('../notifications/notifications')
const { hexToString } = require('@polkadot/util')
const CHAIN_TO_VIEW_COMMISSION_CONVERSION_FACTOR = 1e-9
const proposalTypeEnum = {
TEXT: 'TEXT',
TREASURY: 'TREASURY',
PARAMETER_CHANGE: 'PARAMETER_CHANGE'
}
function notificationReducer(notification, networks) {
return {
id: notification.id,
networkId: notification.networkId,
timestamp: notification.created_at,
title: getMessageTitle(networks, notification),
link: getPushLink(networks, notification),
icon: getIcon(notification)
}
}
function blockReducer(
networkId,
chainId,
blockHeight,
blockHash,
sessionIndex,
blockAuthor,
transactions,
data = {}
) {
return {
id: blockHash,
networkId,
height: blockHeight,
chainId,
hash: blockHash,
sessionIndex,
time: new Date().toISOString(), // TODO: Get from blockchain state
transactions,
proposer_address: blockAuthor,
data: JSON.stringify(data)
}
}
function validatorReducer(network, validator, fiatValuesResponse) {
return {
id: validator.accountId,
networkId: network.id,
chainId: network.chain_id,
operatorAddress: validator.accountId,
website: validator.identity.web ? validator.identity.web : ``,
identity: validator.identity.twitter,
name:
validator.identity && validator.accountId
? identityReducer(validator.accountId, validator.identity)
: undefined,
votingPower: validator.votingPower.toFixed(6),
startHeight: undefined,
uptimePercentage: undefined,
tokens: validator.tokens,
commissionUpdateTime: undefined,
commission: (
validator.validatorPrefs.commission *
CHAIN_TO_VIEW_COMMISSION_CONVERSION_FACTOR
).toFixed(6),
maxCommission: undefined,
maxChangeCommission: undefined,
status: validator.status,
statusDetailed: validator.status.toLowerCase(),
delegatorShares: undefined,
selfStake:
(
BigNumber(validator.exposure.own).toNumber() *
network.coinLookup[0].chainToViewConversionFactor
).toFixed(6) || 0,
expectedReturns: validator.expectedReturns,
nominations: validator.nominations,
popularity: validator.popularity,
totalStakedAssets: {
...fiatValuesResponse,
amount: fiatValuesResponse.amount.toFixed(2)
}
}
}
function validatorProfileReducer(
validator,
validatorProfile,
numberStakers,
network
) {
return {
...validator,
profile: {
name: validator.name,
rank: validator.rank,
nationality: validatorProfile ? validatorProfile.nationality : undefined,
headerImage: validatorProfile ? validatorProfile.headerImage : undefined,
description: validator.details,
teamMembers: validatorProfile
? JSON.parse(validatorProfile.teamMembers)
: undefined,
socialLinks: {
website: validatorProfile ? validatorProfile.website : undefined,
telegram: validatorProfile ? validatorProfile.telegram : undefined,
github: validatorProfile ? validatorProfile.github : undefined,
twitter: validatorProfile ? validatorProfile.twitter : undefined,
blog: validatorProfile ? validatorProfile.blog : undefined
},
numberStakers,
uptimePercentage: validator.uptimePercentage,
contributionLinks: JSON.parse(validatorProfile.contributionLinks),
network
}
}
}
function identityReducer(address, identity) {
if (
identity.displayParent &&
identity.displayParent !== `` &&
identity.display &&
identity.display !== ``
) {
return `${identity.displayParent}/${identity.display}`
} else {
return identity.display && identity.display !== ``
? identity.display
: address
}
}
async function balanceReducer(
network,
balance,
total,
fiatValueAPI,
fiatCurrency
) {
if (total === '0') {
return []
}
const lunieCoin = coinReducer(network, balance, 6)
const fiatValues = await fiatValueAPI.calculateFiatValues(
[lunieCoin],
fiatCurrency
)
return [
{
id: lunieCoin.denom,
amount: lunieCoin.amount,
total: fixDecimalsAndRoundUp(
BigNumber(total)
.times(network.coinLookup[0].chainToViewConversionFactor)
.toNumber(),
6
),
denom: lunieCoin.denom,
fiatValue: fiatValues[lunieCoin.denom]
}
]
}
async function balanceV2Reducer(
network,
balance,
total,
staked,
fiatValueAPI,
fiatCurrency
) {
const availableLunieCoin = coinReducer(network, balance, 6)
const totalLunieCoin = coinReducer(network, total, 6)
const stakedLunieCoin = coinReducer(network, staked, 6)
const availableFiatValue = (
await fiatValueAPI.calculateFiatValues([availableLunieCoin], fiatCurrency)
)[availableLunieCoin.denom]
const totalFiatValue = (
await fiatValueAPI.calculateFiatValues([totalLunieCoin], fiatCurrency)
)[totalLunieCoin.denom]
if (total === '0') {
return {
id: availableLunieCoin.denom,
type: 'STAKE',
available: 0,
total: 0,
staked: 0,
denom: availableLunieCoin.denom,
availableFiatValue,
fiatValue: totalFiatValue
}
}
return {
id: availableLunieCoin.denom,
type: 'STAKE', // just a staking denom on Kusama for now
available: availableLunieCoin.amount,
total: totalLunieCoin.amount,
staked: stakedLunieCoin.amount,
denom: availableLunieCoin.denom,
availableFiatValue,
fiatValue: totalFiatValue
}
}
function delegationReducer(network, delegation, validator, active) {
return {
id: validator.operatorAddress,
validatorAddress: validator.operatorAddress,
delegatorAddress: delegation.who,
validator,
amount: delegation.value
? BigNumber(delegation.value)
.times(network.coinLookup[0].chainToViewConversionFactor)
.toFixed(6)
: 0,
active
}
}
function undelegationReducer(undelegation, address, network) {
return {
id: `${address}_${undelegation.value}`,
delegatorAddress: address,
amount: toViewDenom(network, undelegation.value),
startHeight: '',
endTime: undelegation.endTime
}
}
async function transactionsReducerV2(
network,
extrinsics,
blockHeight,
db,
api
) {
// Filter Polkadot tx to Lunie supported types
let reducedTxs = []
for (let index = 0; index < extrinsics.length; index++) {
const extrinsic = extrinsics[index]
reducedTxs = reducedTxs.concat(
await transactionReducerV2(
network,
extrinsic,
index,
blockHeight,
db,
api
)
)
}
return reducedTxs
}
// Map Polkadot event method to Lunie message types
function getMessageType(section, method) {
switch (`${section}.${method}`) {
case 'balances.transfer':
return lunieMessageTypes.SEND
case 'balances.transferKeepAlive':
return lunieMessageTypes.SEND
case 'staking.payoutStakers':
return lunieMessageTypes.CLAIM_REWARDS
case 'lunie.staking':
return lunieMessageTypes.STAKE
default:
return lunieMessageTypes.UNKNOWN
}
}
async function parsePolkadotTransaction(
hash,
message,
index,
messageIndex,
signer,
success,
log,
network,
blockHeight,
isBatch,
db,
events
) {
const lunieTransactionType = getMessageType(message.section, message.method)
return {
id: hash,
type: lunieTransactionType,
hash,
height: blockHeight,
key: isBatch ? `${hash}_${index}_${messageIndex}` : `${hash}_${index}`,
details: await transactionDetailsReducer(
network,
lunieTransactionType,
signer,
message,
db
),
timestamp: new Date().getTime(), // FIXME!: pass it from block, we should get current timestamp from blockchain for new blocks
memo: ``,
fees: [
{
amount: `0`,
denom: network.coinLookup[0].viewDenom
}
], // FIXME!
success,
log,
involvedAddresses: extractInvolvedAddresses(
lunieTransactionType,
signer,
message,
events
)
}
}
async function transactionReducerV2(
network,
extrinsic,
index,
blockHeight,
db,
api
) {
const hash = extrinsic.hash
const signer = extrinsic.signature === null ? '' : extrinsic.signature.signer
const isBatch =
extrinsic.method.pallet === `utility` && extrinsic.method.method === `batch`
const messages = aggregateLunieStaking(
isBatch ? extrinsic.args.calls : [extrinsic]
)
const events = extrinsic.events
// if tx is a batch, we need to check if all of the batched txs went through
let success
if (isBatch) {
success = !!extrinsic.events.find(
(event) =>
event.method.pallet === `utility` &&
event.method.method === `BatchCompleted`
)
} else {
success = !!extrinsic.events.find(
(event) =>
event.method.pallet === `system` &&
event.method.method === `ExtrinsicSuccess`
)
}
// add error
let log = undefined
if (!success) {
try {
const failureEvent = extrinsic.events.find(
(event) =>
(event.method.pallet === `system` &&
event.method.method === `ExtrinsicFailed`) ||
(event.method.pallet === `utility` &&
event.method.method === `BatchInterrupted`)
)
const failureEventData = failureEvent.data.find(({ Module }) => !!Module) // data has Module property to get error
const { index, error } = failureEventData.Module
const errorIndex = new Uint8Array([index, error])
const { documentation } = api.registry.findMetaError(errorIndex)
log = documentation.join(' ').trim()
} catch (err) {
console.error(err)
Sentry.withScope(function (scope) {
scope.setExtra('extrinsic', extrinsic)
scope.setExtra('height', blockHeight)
scope.setExtra('hash', hash)
Sentry.captureException(error)
})
log = `Transaction failed. Exact error could not been extracted. Extrinsic was included in block ${blockHeight} and has hash ${hash}.`
}
}
const returnedMessages = await Promise.all(
messages.map(
async (message, messageIndex) =>
await parsePolkadotTransaction(
hash,
message,
index,
messageIndex,
signer,
success,
log,
network,
blockHeight,
isBatch,
db,
events
)
)
)
return returnedMessages
}
// we display staking as one tx where in Polkadot this can be 2
// so we aggregate the messages into 1
// ATTENTION this could be weird for some users
function aggregateLunieStaking(messages) {
// lunie staking message
let aggregatedLunieStaking = {
method: 'staking',
section: 'lunie',
validators: [],
amount: 0
}
let hasBond = false
let hasNominate = false
let reducedMessages = []
messages.forEach((current) => {
if (
current.method.pallet === 'staking' &&
current.method.method === 'bond'
) {
aggregatedLunieStaking.amount =
aggregatedLunieStaking.amount + current.args.value
hasBond = true
}
if (
current.method.pallet === 'staking' &&
current.method.method === 'bondExtra'
) {
aggregatedLunieStaking.amount =
aggregatedLunieStaking.amount + current.args.max_additional
hasBond = true
}
if (
current.method.pallet === 'staking' &&
current.method.method === 'nominate'
) {
aggregatedLunieStaking.validators = aggregatedLunieStaking.validators.concat(
current.args.targets
)
hasNominate = true
}
reducedMessages.push({
section: current.method.pallet,
method: current.method.method,
args: current.args
})
})
return hasBond && hasNominate
? reducedMessages.concat(aggregatedLunieStaking)
: reducedMessages
}
// Map polkadot messages to our details format
async function transactionDetailsReducer(
network,
lunieTransactionType,
signer,
message,
db
) {
let details
switch (lunieTransactionType) {
case lunieMessageTypes.SEND:
details = sendDetailsReducer(network, message, signer)
break
case lunieMessageTypes.STAKE:
details = stakeDetailsReducer(network, message)
break
case lunieMessageTypes.CLAIM_REWARDS:
details = await claimRewardsDetailsReducer(network, message, db)
break
default:
details = {}
}
return {
type: lunieTransactionType,
...details
}
}
function coinReducer(network, amount, decimals = 6) {
if (!amount && amount !== 0) {
return {
amount: 0,
denom: ''
}
}
return {
denom: network.stakingDenom || network.coinLookup[0].viewDenom,
amount: fixDecimalsAndRoundUp(
BigNumber(amount).times(
network.coinLookup[0].chainToViewConversionFactor
),
decimals
)
}
}
function sendDetailsReducer(network, message, signer) {
return {
from: [signer],
to: [message.args.dest],
amount: coinReducer(network, message.args.value)
}
}
// the message for staking is created by `aggregateLunieStaking`
function stakeDetailsReducer(network, message) {
return {
to: message.validators,
amount: coinReducer(network, message.amount)
}
}
async function claimRewardsDetailsReducer(network, message, db) {
const validator = message.args.validator_stash
const height = message.args.era
const dbRewards =
validator && height
? await db.getRewardsValidatorHeight(validator, height)
: []
return {
amounts: [
{
amount: dbRewards
.map(({ amount }) => ({ amount }))
.reduce((amountAccumulator, reward) => {
return (amountAccumulator += reward.amount)
}, 0)
.toFixed(6),
denom: network.stakingDenom
}
],
from: [validator],
rewards: dbRewards || []
}
}
function extractInvolvedAddresses(
lunieTransactionType,
signer,
message,
events
) {
let involvedAddresses = []
if (lunieTransactionType === lunieMessageTypes.SEND) {
involvedAddresses = involvedAddresses.concat([signer, message.args.dest])
} else if (lunieTransactionType === lunieMessageTypes.STAKE) {
involvedAddresses = involvedAddresses.concat([signer], message.validators)
} else if (lunieTransactionType === lunieMessageTypes.CLAIM_REWARDS) {
// we get all reward target addresses from extrinsic events
involvedAddresses = events
.filter(
(event) =>
event.method.pallet === 'staking' && event.method.method === `Reward`
)
.map((event) => event.data[0])
.concat([signer])
} else if (signer) {
involvedAddresses = involvedAddresses.concat([signer])
}
return _.uniq(involvedAddresses)
}
function rewardsReducer(network, validators, rewards) {
const allRewards = []
const validatorsDict = _.keyBy(validators, 'operatorAddress')
rewards.forEach((reward) => {
// reward reducer returns an array
allRewards.push(...rewardReducer(network, validatorsDict, reward))
})
return allRewards
}
function dbRewardsReducer(validatorsDictionary, dbRewards, withHeight) {
if (withHeight) {
return dbRewards.map((reward) => ({
id: `${reward.validator}_${reward.denom}_${reward.height}`,
...reward,
validator: validatorsDictionary[reward.validator]
}))
}
const aggregatedRewards = dbRewards.reduce((sum, reward) => {
sum[reward.validator] = sum[reward.validator] || {}
sum[reward.validator][reward.denom] =
(sum[reward.validator][reward.denom] || 0) + reward.amount
return sum
}, {})
const flattenedAggregatedRewards = Object.entries(aggregatedRewards).reduce(
(sum, [validator, reward]) => {
sum = sum.concat(
Object.entries(reward).map(([denom, amount]) => ({
validator,
denom,
amount: amount.toFixed(6)
}))
)
return sum
},
[]
)
return flattenedAggregatedRewards.map((reward) => ({
id: `${reward.validator}_${reward.denom}`,
...reward,
validator: validatorsDictionary[reward.validator]
}))
}
function rewardReducer(network, validators, reward) {
let parsedRewards = []
Object.entries(reward.validators).forEach((validatorReward) => {
const validator = validators[validatorReward[0]]
if (!validator) return
const lunieReward = {
id: validatorReward[0] + (reward.era ? '_' + reward.era : ''),
...coinReducer(network, validatorReward[1].toString(10)),
height: reward.era,
address: reward.address,
validator, // used for user facing rewards in the API
validatorAddress: validatorReward[0] // added for writing the validator to the db even it it is not in the dictionary
}
parsedRewards.push(lunieReward)
})
return parsedRewards
}
function depositReducer(deposit, depositer, network) {
return {
id: depositer.address,
amount: [
{
amount: fixDecimalsAndRoundUpBigNumbers(deposit.balance, 6, network),
denom: network.stakingDenom
}
],
depositer
}
}
function networkAccountReducer(address, account, store) {
const validator = address ? store.validators[address] : undefined
if (validator) {
return {
name: validator.name,
address,
picture: validator.picture,
validator
}
}
return {
name: account.value ? hexToString(account.value.info.display.Raw) : '',
address,
picture: '' // TODO: we need to get images from twitter account
}
}
function democracyProposalReducer(network, proposal, detailedVotes) {
return {
id: `democracy-`.concat(proposal.index),
proposalId: proposal.index,
networkId: network.id,
type: proposalTypeEnum.PARAMETER_CHANGE,
title: `Proposal #${proposal.index}`,
description: proposal.description,
creationTime: proposal.creationTime,
status: `DepositPeriod`, // trying to adjust to the Cosmos status
statusBeginTime: proposal.creationTime,
tally: democracyTallyReducer(proposal),
deposit: toViewDenom(network, proposal.balance),
summary: getProposalSummary(proposalTypeEnum.PARAMETER_CHANGE),
proposer: proposal.proposer,
detailedVotes
}
}
function democracyReferendumReducer(
network,
proposal,
totalIssuance,
blockHeight,
detailedVotes
) {
return {
id: `referendum-`.concat(proposal.index),
proposalId: proposal.index,
networkId: network.id,
type: proposalTypeEnum.PARAMETER_CHANGE,
title: `Proposal #${proposal.index}`,
description: proposal.description,
creationTime: proposal.creationTime,
status: `VotingPeriod`,
statusBeginTime: proposal.creationTime,
statusEndTime: getStatusEndTime(blockHeight, proposal.status.end),
tally: tallyReducer(network, proposal.status.tally, totalIssuance),
deposit: toViewDenom(network, proposal.status.tally.turnout),
summary: getProposalSummary(proposalTypeEnum.PARAMETER_CHANGE),
proposer: proposal.proposer,
detailedVotes
}
}
function treasuryProposalReducer(
network,
proposal,
councilMembers,
blockHeight,
electionInfo,
detailedVotes
) {
return {
id: `treasury-`.concat(proposal.index || proposal.votes.index),
proposalId: proposal.index || proposal.votes.index,
networkId: network.id,
type: proposalTypeEnum.TREASURY,
title: `Treasury Proposal #${proposal.index || proposal.votes.index}`,
description: proposal.description,
creationTime: proposal.creationTime,
status: `VotingPeriod`,
statusEndTime: proposal.votes
? getStatusEndTime(blockHeight, proposal.votes.end)
: null,
tally: proposal.votes
? councilTallyReducer(proposal.votes, councilMembers, electionInfo)
: {},
deposit: toViewDenom(network, Number(proposal.deposit)),
proposer: proposal.proposer,
beneficiary: proposal.beneficiary, // the account getting the tip
summary:
getProposalSummary(proposalTypeEnum.TREASURY) +
`\nTreasury proposals on ${network.title} can only be voted on by council members.`,
detailedVotes
}
}
function tallyReducer(network, tally, totalIssuance) {
//
// tally chain format:
//
// "tally": {
// "ayes": "0x0000000000000000001e470441298100",
// "nays": "0x00000000000000000186de726fc56000",
// "turnout": "0x000000000000000000dc3dd9ad3d0800"
// }
//
// turnout is the real amount deposited by voters
// in polkadot you can vote with "conviction", that means
// ayes and nays are amplified by the selected lockup period:
//
// 1x voting balance, locked for 1x enactment (8.00 days)
// 2x voting balance, locked for 2x enactment (16.00 days)
// 3x voting balance, locked for 4x enactment (32.00 days)
// 4x voting balance, locked for 8x enactment (64.00 days)
// 5x voting balance, locked for 16x enactment (128.00 days)
// 6x voting balance, locked for 32x enactment (256.00 days)
//
const turnout = BigNumber(tally.turnout)
const totalVoted = BigNumber(tally.ayes).plus(tally.nays)
const total = toViewDenom(network, totalVoted.toString(10))
const yes = toViewDenom(network, tally.ayes)
const no = toViewDenom(network, tally.nays)
const totalVotedPercentage = turnout
.div(BigNumber(totalIssuance))
.toNumber()
.toFixed(4) // the percent conversion is done in the FE. We just send the decimals here
return {
yes,
no,
abstain: 0,
veto: 0,
total,
totalVotedPercentage
}
}
function councilTallyReducer(votes, councilMembers, electionInfo) {
const total = votes.ayes.length + votes.nays.length
// to calculated the totalVotedPercentage we need to add up the voting power of the council members that did vote on the proposal
// first of all we need to calculate the total voting power of the council
// TODO: we also need to take into account the Prime council member vote
const totalCouncilVotingPower = electionInfo.members.reduce(
(votingPowerAggregator, member) => {
return (votingPowerAggregator = BigNumber(votingPowerAggregator).plus(
member[1]
))
},
0
)
const totalVoted = councilMembers.reduce((totalVotedAggregator, member) => {
const memberElectionInfo = electionInfo.members.find(
(memberElectionInfo) =>
memberElectionInfo[0].toHuman() === member.toHuman()
)
if (memberElectionInfo) {
totalVotedAggregator = BigNumber(totalVotedAggregator).plus(
memberElectionInfo[1]
)
}
return totalVotedAggregator
}, 0)
return {
yes: votes.ayes.length,
no: votes.nays.length,
abstain: 0,
veto: 0,
total,
totalVotedPercentage: BigNumber(totalCouncilVotingPower)
.div(totalVoted)
.toNumber()
.toFixed(4) // the percent conversion is done in the FE. No need to multiply by 100
}
}
function democracyTallyReducer(proposal) {
// if we consider democracyProposals like the parallel to Cosmos proposals in deposit periods, then
// we would have to turn the seconds concept into a deposit somehow
return {
yes: proposal.seconds.length
}
}
function topVoterReducer(
topVoterAddress,
electionInfo,
accountInfo,
totalIssuance,
validators,
network
) {
const councilMemberInfo = electionInfo.members.find(
(electionInfoMember) => electionInfoMember[0].toHuman() === topVoterAddress
)
return {
name: accountInfo.name,
address: topVoterAddress,
votingPower:
councilMemberInfo && totalIssuance
? BigNumber(councilMemberInfo[1])
.div(BigNumber(totalIssuance))
.toNumber()
: '',
picture: validators[topVoterAddress]
? validators[topVoterAddress].picture
: undefined,
validator: validators[topVoterAddress]
}
}
// the status end time is a time "so and so days from the creation of the proposal opening"
function getStatusEndTime(blockHeight, endBlock) {
return new Date(
new Date().getTime() + (endBlock - blockHeight) * 6000
).toUTCString()
}
module.exports = {
notificationReducer,
blockReducer,
validatorReducer,
validatorProfileReducer,
balanceReducer,
balanceV2Reducer,
delegationReducer,
undelegationReducer,
extractInvolvedAddresses,
transactionsReducerV2,
transactionDetailsReducer,
sendDetailsReducer,
coinReducer,
rewardReducer,
rewardsReducer,
dbRewardsReducer,
depositReducer,
networkAccountReducer,
identityReducer,
democracyProposalReducer,
democracyReferendumReducer,
treasuryProposalReducer,
tallyReducer,
topVoterReducer,
getProposalSummary
}