-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1173 from novasamatech/feature/historical-votes-f…
…or-ended-referendums Feature/historical votes for ended referendums
- Loading branch information
Showing
14 changed files
with
653 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 0 additions & 46 deletions
46
...e/Governance/Operation/Fetch/OffchainVoting/GovernanceSplitAbstainTotalVotesFactory.swift
This file was deleted.
Oops, something went wrong.
153 changes: 153 additions & 0 deletions
153
.../Modules/Vote/Governance/Operation/Fetch/OffchainVoting/GovernanceTotalVotesFactory.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import Foundation | ||
import Operation_iOS | ||
import BigInt | ||
|
||
struct ReferendumVotingAmount { | ||
let aye: BigUInt | ||
let nay: BigUInt | ||
let abstain: BigUInt | ||
} | ||
|
||
protocol GovernanceTotalVotesFactoryProtocol { | ||
func createOperation( | ||
referendumId: ReferendumIdLocal, | ||
votersType: ReferendumVotersType? | ||
) -> BaseOperation<ReferendumVotingAmount> | ||
} | ||
|
||
final class GovernanceTotalVotesFactory: SubqueryBaseOperationFactory { | ||
private func prepareSplitAbstainVotesQuery(referendumId: ReferendumIdLocal) -> String { | ||
""" | ||
{ | ||
castingVotings( | ||
filter: { | ||
referendumId: { equalTo: "\(referendumId)" }, | ||
splitAbstainVote: { isNull: false } | ||
} | ||
) { | ||
nodes { | ||
voter | ||
referendumId | ||
splitAbstainVote | ||
} | ||
} | ||
} | ||
""" | ||
} | ||
|
||
private func prepareStandardVotesQuery(referendumId: ReferendumIdLocal, isAye: Bool) -> String { | ||
""" | ||
{ | ||
castingVotings (filter: { | ||
referendumId: {equalTo: "\(referendumId)"}, | ||
or: [ | ||
{splitVote: { isNull: false }}, | ||
{splitAbstainVote: {isNull: false}}, | ||
{standardVote: { contains: { aye: \(isAye)}}} | ||
] | ||
}) { | ||
nodes { | ||
referendumId | ||
standardVote | ||
splitVote | ||
splitAbstainVote | ||
voters | ||
delegatorVotes { | ||
nodes { | ||
delegator | ||
vote | ||
} | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
} | ||
|
||
private func prepareAllVotesQuery(referendumId: ReferendumIdLocal) -> String { | ||
""" | ||
{ | ||
castingVotings( | ||
filter: { | ||
referendumId: { equalTo: "\(referendumId)" } | ||
} | ||
) { | ||
nodes { | ||
referendumId | ||
voter | ||
splitVote | ||
splitAbstainVote | ||
standardVote | ||
delegatorVotes { | ||
nodes { | ||
delegator | ||
vote | ||
} | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
} | ||
} | ||
|
||
extension GovernanceTotalVotesFactory: GovernanceTotalVotesFactoryProtocol { | ||
func createOperation(referendumId: ReferendumIdLocal, votersType: ReferendumVotersType?) -> BaseOperation<ReferendumVotingAmount> { | ||
let query = if let votersType { | ||
switch votersType { | ||
case .ayes: | ||
prepareStandardVotesQuery(referendumId: referendumId, isAye: true) | ||
case .nays: | ||
prepareStandardVotesQuery(referendumId: referendumId, isAye: false) | ||
case .abstains: | ||
prepareSplitAbstainVotesQuery(referendumId: referendumId) | ||
} | ||
|
||
} else { | ||
prepareAllVotesQuery(referendumId: referendumId) | ||
} | ||
|
||
let operation = createOperation( | ||
for: query | ||
) { [weak self] (response: SubqueryVotingResponse.ReferendumVotesResponse) -> ReferendumVotingAmount in | ||
response.castingVotings.nodes | ||
.compactMap { ReferendumVoterLocal(from: $0) } | ||
.reduce( | ||
ReferendumVotingAmount( | ||
aye: 0, | ||
nay: 0, | ||
abstain: 0 | ||
) | ||
) { self?.sum($0, with: $1) ?? $0 } | ||
} | ||
|
||
return operation | ||
} | ||
|
||
private func sum( | ||
_ amount: ReferendumVotingAmount, | ||
with voter: ReferendumVoterLocal | ||
) -> ReferendumVotingAmount { | ||
var accAye = amount.aye | ||
var accNay = amount.nay | ||
var accAbstain = amount.abstain | ||
|
||
switch voter.vote { | ||
case .split: | ||
accAye += voter.vote.ayes | ||
accNay += voter.vote.nays | ||
case .splitAbstain: | ||
accAbstain += voter.vote.abstains | ||
case let .standard(model) where model.vote.aye: | ||
accAye += (voter.vote.ayes + voter.delegatorsVotes) | ||
case let .standard(model): | ||
accNay += (voter.vote.nays + voter.delegatorsVotes) | ||
} | ||
|
||
return ReferendumVotingAmount( | ||
aye: accAye, | ||
nay: accNay, | ||
abstain: accAbstain | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.