Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deep link opens already registered proposal #5701

Merged
merged 16 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { showAppNotification } from '@auxiliary/notification/actions'
import { closePopup, openPopup } from '@auxiliary/popup/actions'
import { isValidUrl } from '@core/utils/validation'
import { isProposalAlreadyAdded, isValidProposalId } from '@contexts/governance/utils'
import { getProposalFromEventId, isProposalAlreadyAdded, isValidProposalId } from '@contexts/governance/utils'

import { AddProposalOperationParameter } from '../../../enums'
import { selectedProposal } from '@contexts/governance/stores'
import { governanceRoute, GovernanceRoute, governanceRouter } from '@core/router'
import { get } from 'svelte/store'

export function handleDeepLinkAddProposalOperation(searchParams: URLSearchParams): void {
const eventId = searchParams.get(AddProposalOperationParameter.EventId)
Expand All @@ -14,6 +17,14 @@ export function handleDeepLinkAddProposalOperation(searchParams: URLSearchParams
* NOTE: If we throw an error as normal, it will be handled and displayed in the "failed link"
* popup.
*/
if (get(selectedProposal)?.id !== eventId) {
getProposalFromEventId(eventId).then((proposal) => {
selectedProposal.set(proposal)
if (get(governanceRoute) !== GovernanceRoute.Details) {
get(governanceRouter).goTo(GovernanceRoute.Details)
}
})
}
showAppNotification({
type: 'warning',
alert: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { get } from 'svelte/store'
import type { ParticipationEventWithNodes } from '@iota/wallet'
import type { ParticipationEventWithNodes } from '@iota/wallet/out/types'
import { selectedAccount } from '@core/account/stores'

export function getVotingEvents(): Promise<{ [eventId: string]: ParticipationEventWithNodes }> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IProposalFilter, ProposalOrderOption, ProposalStatus, ProposalType } from '@contexts/governance'
import { IProposalFilter } from '@contexts/governance'
import { ProposalStatus, ProposalType, ProposalOrderOption } from '@contexts/governance/enums'
jeeanribeiro marked this conversation as resolved.
Show resolved Hide resolved
import { BooleanFilterOption, OrderOption } from '@core/utils/enums/filters'

export const DEFAULT_PROPOSAL_FILTER: IProposalFilter = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { get } from 'svelte/store'
import type { ParticipationEventWithNodes } from '@iota/wallet/out/types'
import { OFFICIAL_NODE_URLS } from '@core/network/constants'
import { activeProfile, activeProfileId } from '@core/profile/stores'
import { proposalsState } from '../stores'
import { IProposal } from '../interfaces'
import { ProposalStatus, ProposalType } from '../enums'
import { getParticipationsForProposal } from './getParticipationsForProposal'
import { getLatestProposalStatus } from './getLatestProposalStatus'

export async function createProposalFromEvent(event: ParticipationEventWithNodes): Promise<IProposal> {
const { data, id } = event

const officialNodeUrls = OFFICIAL_NODE_URLS[get(activeProfile).networkProtocol][get(activeProfile).networkType]
const proposalNodeUrl = get(proposalsState)[get(activeProfileId)]?.[id].nodeUrl
const isOfficialNetwork = officialNodeUrls.includes(proposalNodeUrl)

const participated = (await getParticipationsForProposal(id)) !== undefined

const milestones = {
[ProposalStatus.Upcoming]: 0, // TODO: fix this
[ProposalStatus.Commencing]: data.milestoneIndexCommence,
[ProposalStatus.Holding]: data.milestoneIndexStart,
[ProposalStatus.Ended]: data.milestoneIndexEnd,
}

const status = getLatestProposalStatus(milestones)

const proposal: IProposal = {
id,
title: event.data.name,
additionalInfo: event.data.additionalInfo,
status: status ?? ProposalStatus.Upcoming,
milestones,
// TODO: figure out a better way to get the node URLs
nodeUrls: get(activeProfile)?.clientOptions?.nodes,
type: isOfficialNetwork ? ProposalType.Official : ProposalType.Custom,
participated,
}

return proposal
}
10 changes: 10 additions & 0 deletions packages/shared/lib/contexts/governance/utils/createProposals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { ParticipationEventWithNodes } from '@iota/wallet/out/types'
import { getVotingEvents } from '../actions'
import { IProposal } from '../interfaces'
import { createProposalFromEvent } from './createProposalFromEvent'

export async function createProposals(): Promise<IProposal[]> {
const events: ParticipationEventWithNodes[] = Object.values(await getVotingEvents())
const proposals: IProposal[] = await Promise.all(events?.map(async (event) => createProposalFromEvent(event)))
return proposals
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { get } from 'svelte/store'
import { nodeInfo } from '@core/network/stores'
import { ProposalStatus } from '../enums'

export function getLatestProposalStatus(milestones: Record<ProposalStatus, number>): ProposalStatus {
const latestMilestoneIndex = get(nodeInfo)?.status?.latestMilestone.index
const milestoneDifferences = Object.entries(milestones).map(([status, milestone]) => ({
status,
milestoneDifference: latestMilestoneIndex - milestone,
}))
const passedMilestones = milestoneDifferences.filter(({ milestoneDifference }) => milestoneDifference > 0)
const lastPassedMilestone = passedMilestones.pop()
return <ProposalStatus>lastPassedMilestone?.status
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getVotingEvents } from '@contexts/governance/actions'
jeeanribeiro marked this conversation as resolved.
Show resolved Hide resolved
import { IProposal } from '../interfaces'
import { createProposalFromEvent } from './createProposalFromEvent'

export async function getProposalFromEventId(eventId: string): Promise<IProposal> {
const events = await getVotingEvents()
const event = events[eventId]
/**
* NOTE: If createProposalFromEvent function starts having stateful behavior (store, persist value) then we should refactor this function to not use it.
*/
if (event === undefined) {
throw new Error(`Event with id ${eventId} not found`)
} else {
return createProposalFromEvent(event)
}
}
5 changes: 4 additions & 1 deletion packages/shared/lib/contexts/governance/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
export * from './calculateWeightedVotes'
export * from './createProposalsFromEvents'
export * from './createProposalFromEvent'
export * from './createProposals'
export * from './getActiveParticipation'
export * from './getLatestProposalStatus'
export * from './getNextProposalPhase'
export * from './getNumberOfActiveProposals'
export * from './getNumberOfTotalProposals'
export * from './getNumberOfVotedProposals'
export * from './getNumberOfVotingProposals'
export * from './getParticipationsForProposal'
export * from './getPercentagesFromAnswerStatuses'
export * from './getProposalFromEventId'
export * from './isAnyAccountVotingForSelectedProposal'
export * from './isParticipationOutput'
export * from './isProposalActive'
Expand Down