feat(governance): replace proposal and vote busEvents with subscriptions (#2862)

This commit is contained in:
Sam Keen 2023-02-08 09:42:20 +00:00 committed by GitHub
parent 47e4861fdb
commit da551f9d3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 41 additions and 86 deletions

View File

@ -90,23 +90,16 @@ describe('Raw proposal form', () => {
},
result: {
data: {
busEvents: [
{
__typename: 'BusEvent',
type: Schema.BusEventType.Proposal,
event: {
proposals: {
__typename: 'Proposal',
id: '2fca514cebf9f465ae31ecb4c5721e3a6f5f260425ded887ca50ba15b81a5d50',
reference: 'proposal-reference',
state: Schema.ProposalState.STATE_OPEN,
rejectionReason:
Schema.ProposalRejectionReason
.PROPOSAL_ERROR_CLOSE_TIME_TOO_LATE,
Schema.ProposalRejectionReason.PROPOSAL_ERROR_CLOSE_TIME_TOO_LATE,
errorDetails: 'error-details',
},
},
],
},
},
delay: 300,
};

View File

@ -7,15 +7,10 @@ fragment ProposalEventFields on Proposal {
}
subscription ProposalEvent($partyId: ID!) {
busEvents(partyId: $partyId, batchSize: 0, types: [Proposal]) {
type
event {
... on Proposal {
proposals(partyId: $partyId) {
...ProposalEventFields
}
}
}
}
fragment UpdateNetworkParameterFields on Proposal {
id

View File

@ -10,7 +10,7 @@ export type ProposalEventSubscriptionVariables = Types.Exact<{
}>;
export type ProposalEventSubscription = { __typename?: 'Subscription', busEvents?: Array<{ __typename?: 'BusEvent', type: Types.BusEventType, event: { __typename?: 'AccountEvent' } | { __typename?: 'Asset' } | { __typename?: 'AuctionEvent' } | { __typename?: 'Deposit' } | { __typename?: 'LiquidityProvision' } | { __typename?: 'LossSocialization' } | { __typename?: 'MarginLevels' } | { __typename?: 'Market' } | { __typename?: 'MarketData' } | { __typename?: 'MarketEvent' } | { __typename?: 'MarketTick' } | { __typename?: 'NodeSignature' } | { __typename?: 'OracleSpec' } | { __typename?: 'Order' } | { __typename?: 'Party' } | { __typename?: 'PositionResolution' } | { __typename?: 'Proposal', id?: string | null, reference: string, state: Types.ProposalState, rejectionReason?: Types.ProposalRejectionReason | null, errorDetails?: string | null } | { __typename?: 'RiskFactor' } | { __typename?: 'SettleDistressed' } | { __typename?: 'SettlePosition' } | { __typename?: 'TimeUpdate' } | { __typename?: 'Trade' } | { __typename?: 'TransactionResult' } | { __typename?: 'TransferResponses' } | { __typename?: 'Vote' } | { __typename?: 'Withdrawal' } }> | null };
export type ProposalEventSubscription = { __typename?: 'Subscription', proposals: { __typename?: 'Proposal', id?: string | null, reference: string, state: Types.ProposalState, rejectionReason?: Types.ProposalRejectionReason | null, errorDetails?: string | null } };
export type UpdateNetworkParameterFieldsFragment = { __typename?: 'Proposal', id?: string | null, state: Types.ProposalState, datetime: any, terms: { __typename?: 'ProposalTerms', enactmentDatetime?: any | null, change: { __typename?: 'NewAsset' } | { __typename?: 'NewFreeform' } | { __typename?: 'NewMarket' } | { __typename?: 'UpdateAsset' } | { __typename?: 'UpdateMarket' } | { __typename?: 'UpdateNetworkParameter', networkParameter: { __typename?: 'NetworkParameter', key: string, value: string } } } };
@ -55,14 +55,9 @@ export const UpdateNetworkParameterFieldsFragmentDoc = gql`
`;
export const ProposalEventDocument = gql`
subscription ProposalEvent($partyId: ID!) {
busEvents(partyId: $partyId, batchSize: 0, types: [Proposal]) {
type
event {
... on Proposal {
proposals(partyId: $partyId) {
...ProposalEventFields
}
}
}
}
${ProposalEventFieldsFragmentDoc}`;

View File

@ -28,24 +28,15 @@ export const useProposalEvent = (transaction: VegaTxState) => {
variables: { partyId },
})
.subscribe(({ data }) => {
if (!data?.busEvents?.length) {
if (!data?.proposals) {
return;
}
// No types available for the subscription result
const matchingProposalEvent = data.busEvents.find((e) => {
if (e.event.__typename !== 'Proposal') {
return false;
}
// typo in 'proposals' - this should be singular
const proposal = data.proposals;
return e.event.id === id;
});
if (
matchingProposalEvent &&
matchingProposalEvent.event.__typename === 'Proposal'
) {
callback(matchingProposalEvent.event);
if (proposal.id === id) {
callback(proposal);
subRef.current?.unsubscribe();
}
});

View File

@ -1,18 +1,13 @@
fragment VoteEventFields on Vote {
fragment VoteEventFields on ProposalVote {
proposalId
vote {
value
datetime
}
}
subscription VoteEvent($partyId: ID!) {
busEvents(partyId: $partyId, batchSize: 0, types: [Vote]) {
type
event {
... on Vote {
proposalId
value
datetime
}
}
votes(partyId: $partyId) {
...VoteEventFields
}
}

View File

@ -3,36 +3,31 @@ import * as Types from '@vegaprotocol/types';
import { gql } from '@apollo/client';
import * as Apollo from '@apollo/client';
const defaultOptions = {} as const;
export type VoteEventFieldsFragment = { __typename?: 'Vote', proposalId: string, value: Types.VoteValue, datetime: any };
export type VoteEventFieldsFragment = { __typename?: 'ProposalVote', proposalId: string, vote: { __typename?: 'Vote', value: Types.VoteValue, datetime: any } };
export type VoteEventSubscriptionVariables = Types.Exact<{
partyId: Types.Scalars['ID'];
}>;
export type VoteEventSubscription = { __typename?: 'Subscription', busEvents?: Array<{ __typename?: 'BusEvent', type: Types.BusEventType, event: { __typename?: 'AccountEvent' } | { __typename?: 'Asset' } | { __typename?: 'AuctionEvent' } | { __typename?: 'Deposit' } | { __typename?: 'LiquidityProvision' } | { __typename?: 'LossSocialization' } | { __typename?: 'MarginLevels' } | { __typename?: 'Market' } | { __typename?: 'MarketData' } | { __typename?: 'MarketEvent' } | { __typename?: 'MarketTick' } | { __typename?: 'NodeSignature' } | { __typename?: 'OracleSpec' } | { __typename?: 'Order' } | { __typename?: 'Party' } | { __typename?: 'PositionResolution' } | { __typename?: 'Proposal' } | { __typename?: 'RiskFactor' } | { __typename?: 'SettleDistressed' } | { __typename?: 'SettlePosition' } | { __typename?: 'TimeUpdate' } | { __typename?: 'Trade' } | { __typename?: 'TransactionResult' } | { __typename?: 'TransferResponses' } | { __typename?: 'Vote', proposalId: string, value: Types.VoteValue, datetime: any } | { __typename?: 'Withdrawal' } }> | null };
export type VoteEventSubscription = { __typename?: 'Subscription', votes: { __typename?: 'ProposalVote', proposalId: string, vote: { __typename?: 'Vote', value: Types.VoteValue, datetime: any } } };
export const VoteEventFieldsFragmentDoc = gql`
fragment VoteEventFields on Vote {
fragment VoteEventFields on ProposalVote {
proposalId
vote {
value
datetime
}
}
`;
export const VoteEventDocument = gql`
subscription VoteEvent($partyId: ID!) {
busEvents(partyId: $partyId, batchSize: 0, types: [Vote]) {
type
event {
... on Vote {
proposalId
value
datetime
votes(partyId: $partyId) {
...VoteEventFields
}
}
}
}
`;
${VoteEventFieldsFragmentDoc}`;
/**
* __useVoteEventSubscription__

View File

@ -25,23 +25,14 @@ export const useVoteEvent = (transaction: VegaTxState) => {
variables: { partyId },
})
.subscribe(({ data }) => {
if (!data?.busEvents?.length) {
if (!data?.votes) {
return;
}
const matchingVoteEvent = data.busEvents.find((e) => {
if (e.event.__typename !== 'Vote') {
return false;
}
const vote = data.votes;
return e.event.proposalId === proposalId;
});
if (
matchingVoteEvent &&
matchingVoteEvent.event.__typename === 'Vote'
) {
callback(matchingVoteEvent.event);
if (vote.proposalId === proposalId) {
callback(vote);
subRef.current?.unsubscribe();
}
});