Skip to content

Commit

Permalink
fix: various
Browse files Browse the repository at this point in the history
  • Loading branch information
clockworkgr committed Dec 20, 2024
1 parent 75683cf commit 3b04a67
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type {
import {
QueryDepositsResponseSDKType,
QueryTallyResultResponseSDKType,
QueryVotesResponseSDKType,
QueryVotesRequest,
QueryVotesResponse,
} from '@atomone/atomone-types-long/atomone/gov/v1/query';
import { EncodeObject } from '@cosmjs/proto-signing';
import { longify } from '@cosmjs/stargate/build/queryclient';
import { QueryTallyResultResponseSDKType as DefaultQueryTallyResultResponseSDKType } from '@hicommonwealth/chains';
import { ProposalType } from '@hicommonwealth/shared';
import BN from 'bn.js';
import type {
Expand All @@ -24,6 +26,7 @@ import {
} from 'models/types';
import { DepositVote } from 'models/votes';
import moment from 'moment';
import { PageRequest } from 'node_modules/@atomone/atomone-types-long/cosmos/base/query/v1beta1/pagination';
import CosmosAccount from '../../account';
import type CosmosAccounts from '../../accounts';
import type CosmosChain from '../../chain';
Expand All @@ -35,14 +38,12 @@ import { marshalTallyV1 } from './utils-v1';

const voteToEnumV1 = (voteOption: number | string): CosmosVoteChoice => {
switch (voteOption) {
case 'VOTE_OPTION_YES':
case 1:
return 'Yes';
case 'VOTE_OPTION_NO':
case 3:
return 'No';
case 'VOTE_OPTION_ABSTAIN':
case 2:
return 'Abstain';
case 'VOTE_OPTION_NO_WITH_VETO':
return 'NoWithVeto';
default:
// @ts-expect-error StrictNullChecks
return null;
Expand Down Expand Up @@ -75,7 +76,7 @@ export class CosmosProposalV1AtomOne extends Proposal<
public get author() {
return this.data.proposer
? this._Accounts.fromAddress(this.data.proposer)
: null;
: this.data.proposer;
}

public get votingType() {
Expand Down Expand Up @@ -167,24 +168,46 @@ export class CosmosProposalV1AtomOne extends Proposal<
return deposits;
}

public async fetchTally(): Promise<QueryTallyResultResponseSDKType> {
public async fetchTally(): Promise<DefaultQueryTallyResultResponseSDKType> {
const proposalId = longify(this.data.identifier) as Long;
// @ts-expect-error StrictNullChecks
if (!isAtomoneLCD(this._Chain.lcd)) return;
const tally = await this._Chain.lcd.atomone.gov.v1.tallyResult({
proposalId,
});
this.setTally(tally);
return tally;
return tally as DefaultQueryTallyResultResponseSDKType;
}

public async fetchVotes(): Promise<QueryVotesResponseSDKType> {
public async fetchVotes(): Promise<QueryVotesResponse> {
const proposalId = longify(this.data.identifier) as Long;
// @ts-expect-error StrictNullChecks
if (!isAtomoneLCD(this._Chain.lcd)) return;
const votes = await this._Chain.lcd.atomone.gov.v1.votes({
proposalId,
});
const q = QueryVotesRequest.fromPartial({ proposalId });
const query = QueryVotesRequest.encode(q).finish();
const resp = await this._Chain.api.queryAbci(
'/atomone.gov.v1.Query/Votes',
query,
);
const votes = QueryVotesResponse.decode(resp.value);
let nextKey = votes.pagination?.nextKey;
while (nextKey && nextKey?.byteLength != 0) {
const moreq = QueryVotesRequest.fromPartial({
proposalId,
pagination: {
key: nextKey,
limit: longify(500),
} as unknown as PageRequest,
});
const morequery = QueryVotesRequest.encode(moreq).finish();
const moreresp = await this._Chain.api.queryAbci(
'/atomone.gov.v1.Query/Votes',
morequery,
);
const moreVotes = QueryVotesResponse.decode(moreresp.value);
votes.votes = [...votes.votes, ...moreVotes.votes];
nextKey = moreVotes.pagination?.nextKey;
}
this.setVotes(votes);
return votes;
}
Expand All @@ -208,7 +231,7 @@ export class CosmosProposalV1AtomOne extends Proposal<
}
}

public setVotes(votesResp: QueryVotesResponseSDKType) {
public setVotes(votesResp: QueryVotesResponse) {
if (votesResp) {
for (const voter of votesResp.votes) {
const vote = voteToEnumV1(voter.options[0].option);
Expand Down Expand Up @@ -298,7 +321,7 @@ export class CosmosProposalV1AtomOne extends Proposal<
case 'Rejected':
return ProposalStatus.Failed;
case 'VotingPeriod':
return +this.support > 0.5 && this.veto <= 1 / 3
return +this.support > 0.667 && this.veto <= 1 / 3
? ProposalStatus.Passing
: ProposalStatus.Failing;
case 'DepositPeriod':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import type {
import { EncodeObject } from '@cosmjs/proto-signing';
import { CosmosToken } from 'controllers/chain/cosmos/types';
import { Any } from 'cosmjs-types/google/protobuf/any';
import { isCompleted } from '../v1beta1/utils-v1beta1';
import Cosmos from '../../adapter';
import { CosmosDepositParams, isCompleted } from '../v1beta1/utils-v1beta1';
import CosmosGovernanceV1AtomOne from './governance-v1';

/* Governance helper methods for Cosmos chains with gov module v1 (as of Cosmos SDK v0.46.11) */

Expand Down Expand Up @@ -151,8 +153,7 @@ export const propToIProposal = (p: ProposalSDKType): ICosmosProposal | null => {
// @ts-expect-error StrictNullChecks
new Date(p.voting_start_time).valueOf() / 1000,
),
// @ts-expect-error StrictNullChecks
proposer: null,
proposer: p.proposer,
state: {
identifier,
completed: isCompleted(status),
Expand Down Expand Up @@ -218,3 +219,32 @@ export const encodeMsgSubmitProposalAtomOne = (
},
};
};

export const getDepositParams = async (
cosmosChain: Cosmos,
stakingDenom?: string,
): Promise<CosmosDepositParams> => {
const govController = cosmosChain.governance as CosmosGovernanceV1AtomOne;
let minDeposit;
const { depositParams } =
await cosmosChain.chain.api.atomone.params('deposit');

// TODO: support off-denom deposits
// @ts-expect-error StrictNullChecks
const depositCoins = depositParams.minDeposit.find(
({ denom }) => denom === stakingDenom,
);
if (depositCoins) {
minDeposit = new CosmosToken(
depositCoins.denom,
new BN(depositCoins.amount),
);
} else {
throw new Error(
`Gov minDeposit in wrong denom (${minDeposit}) or stake denom not loaded:
${cosmosChain.chain.denom}`,
);
}
govController.setMinDeposit(minDeposit);
return { minDeposit };
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChainBase } from '@hicommonwealth/shared';
import { useQuery } from '@tanstack/react-query';
import Cosmos from 'controllers/chain/cosmos/adapter';
import { getDepositParams as getAtomOneDepositParams } from 'controllers/chain/cosmos/gov/atomone/utils-v1';
import { getDepositParams as getGovgenDepositParams } from 'controllers/chain/cosmos/gov/govgen/utils-v1beta1';
import {
CosmosDepositParams,
Expand All @@ -13,22 +14,24 @@ const DEPOSIT_PARAMS_STALE_TIME = 1000 * 60 * 15;

const fetchDepositParams = async (
stakingDenom: string,
isGovgen: boolean = false,
chainTtype?: string,
): Promise<CosmosDepositParams> => {
return isGovgen
? getGovgenDepositParams(app.chain as Cosmos, stakingDenom)
: getDepositParams(app.chain as Cosmos, stakingDenom);
switch (chainTtype) {
case 'govgen':
return getGovgenDepositParams(app.chain as Cosmos, stakingDenom);
case 'atomone':
return getAtomOneDepositParams(app.chain as Cosmos, stakingDenom);
default:
return getDepositParams(app.chain as Cosmos, stakingDenom);
}
};

const useDepositParamsQuery = (
stakingDenom: string,
isGovgen: boolean = false,
) => {
const useDepositParamsQuery = (stakingDenom: string, chainTtype?: string) => {
const communityId = app.activeChainId();
return useQuery({
// fetchDepositParams depends on stakingDenom being defined
queryKey: ['depositParams', communityId, stakingDenom, isGovgen],
queryFn: () => fetchDepositParams(stakingDenom, isGovgen),
queryKey: ['depositParams', communityId, stakingDenom, chainTtype],
queryFn: () => fetchDepositParams(stakingDenom, chainTtype),
enabled: app.chain?.base === ChainBase.CosmosSDK && !!stakingDenom,
cacheTime: DEPOSIT_PARAMS_CACHE_TIME,
staleTime: DEPOSIT_PARAMS_STALE_TIME,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { QueryTallyResultResponseSDKType } from '@hicommonwealth/chains';
import { ChainBase } from '@hicommonwealth/shared';
import { useQuery } from '@tanstack/react-query';
import { CosmosProposalV1AtomOne } from 'client/scripts/controllers/chain/cosmos/gov/atomone/proposal-v1';
import { CosmosProposalGovgen } from 'client/scripts/controllers/chain/cosmos/gov/govgen/proposal-v1beta1';
import { CosmosProposalV1 } from 'controllers/chain/cosmos/gov/v1/proposal-v1';
import { CosmosProposal } from 'controllers/chain/cosmos/gov/v1beta1/proposal-v1beta1';
import type { QueryTallyResultResponse } from 'cosmjs-types/cosmos/gov/v1beta1/query';
Expand All @@ -15,8 +17,10 @@ const fetchCosmosTally = async (
proposal: AnyProposal,
): Promise<QueryTallyResultResponse | QueryTallyResultResponseSDKType> => {
if (
proposal instanceof CosmosProposalV1AtomOne ||
proposal instanceof CosmosProposalV1 ||
proposal instanceof CosmosProposal
proposal instanceof CosmosProposal ||
proposal instanceof CosmosProposalGovgen
) {
return proposal.fetchTally();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { QueryVotesResponse as AtomOneQueryVotesResponse } from '@atomone/atomone-types-long/atomone/gov/v1/query';
import type { QueryVotesResponseSDKType } from '@hicommonwealth/chains';
import { ChainBase } from '@hicommonwealth/shared';
import { useQuery } from '@tanstack/react-query';
import { CosmosProposalV1AtomOne } from 'client/scripts/controllers/chain/cosmos/gov/atomone/proposal-v1';
import { CosmosProposalGovgen } from 'client/scripts/controllers/chain/cosmos/gov/govgen/proposal-v1beta1';
import { CosmosProposalV1 } from 'controllers/chain/cosmos/gov/v1/proposal-v1';
import { CosmosProposal } from 'controllers/chain/cosmos/gov/v1beta1/proposal-v1beta1';
import type { QueryVotesResponse } from 'cosmjs-types/cosmos/gov/v1beta1/query';
Expand All @@ -13,9 +16,13 @@ const VOTES_STALE_TIME = 1000 * 30;

const fetchCosmosVotes = async (
proposal: AnyProposal,
): Promise<QueryVotesResponse | QueryVotesResponseSDKType> => {
): Promise<
QueryVotesResponse | QueryVotesResponseSDKType | AtomOneQueryVotesResponse
> => {
if (
proposal instanceof CosmosProposalV1AtomOne ||
proposal instanceof CosmosProposalV1 ||
proposal instanceof CosmosProposalGovgen ||
proposal instanceof CosmosProposal
) {
return proposal.fetchVotes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ export const ProposalExtensions = (props: ProposalExtensionsProps) => {
const { setCosmosDepositAmount, setDemocracyVoteAmount, proposal } = props;
const { data: stakingDenom } = useStakingParamsQuery();

let isGovgen = false;
let chainTtype;
if (proposal instanceof CosmosProposalGovgen) {
isGovgen = true;
chainTtype = 'govgen';
}

if (proposal instanceof CosmosProposalV1AtomOne) {
chainTtype = 'atomone';
}
const { data: cosmosDepositParams } = useDepositParamsQuery(
// @ts-expect-error <StrictNullChecks/>
stakingDenom,
isGovgen,
chainTtype,
);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ export const VotingActions = ({
if (
proposal instanceof CosmosProposal ||
proposal instanceof CosmosProposalV1 ||
proposal instanceof CosmosProposalGovgen ||
proposal instanceof CosmosProposalV1AtomOne
proposal instanceof CosmosProposalGovgen
) {
proposal
.voteTx(new CosmosVote(user, 'NoWithVeto'))
Expand Down Expand Up @@ -253,18 +252,32 @@ export const VotingActions = ({
</>
);
} else if (proposal.votingType === VotingType.YesNoAbstainVeto) {
votingActionObj = (
<>
<div className="button-row">
{yesButton}
{noButton}
{abstainButton}
{noWithVetoButton}
</div>
{/* @ts-expect-error StrictNullChecks*/}
<ProposalExtensions proposal={proposal} />
</>
);
if (!(proposal instanceof CosmosProposalV1AtomOne)) {
votingActionObj = (
<>
<div className="button-row">
{yesButton}
{noButton}
{abstainButton}
{noWithVetoButton}
</div>
{/* @ts-expect-error StrictNullChecks*/}
<ProposalExtensions proposal={proposal} />
</>
);
} else {
votingActionObj = (
<>
<div className="button-row">
{yesButton}
{noButton}
{abstainButton}
</div>
{/* @ts-expect-error StrictNullChecks*/}
<ProposalExtensions proposal={proposal} />
</>
);
}
} else {
votingActionObj = <CannotVote label="Unsupported proposal type" />;
}
Expand Down
Loading

0 comments on commit 3b04a67

Please sign in to comment.