Skip to content

Commit

Permalink
Agrippa/pyth-cleanup (solana-labs#1752)
Browse files Browse the repository at this point in the history
  • Loading branch information
asktree authored Aug 6, 2023
1 parent 9365c3c commit 636257d
Show file tree
Hide file tree
Showing 16 changed files with 20 additions and 293 deletions.
3 changes: 1 addition & 2 deletions actions/createLUTproposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ export const createLUTProposal = async (
const plugin = await client?.withUpdateVoterWeightRecord(
instructions,
tokenOwnerRecord,
'createProposal',
governance
'createProposal'
)

const proposalAddress = await withCreateProposal(
Expand Down
3 changes: 1 addition & 2 deletions actions/createProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ export const createProposal = async (
const plugin = await client?.withUpdateVoterWeightRecord(
instructions,
tokenOwnerRecord,
'createProposal',
governance
'createProposal'
)

const proposalAddress = await withCreateProposal(
Expand Down
13 changes: 2 additions & 11 deletions components/ProposalVotingPower/getNumTokens.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import type { ProgramAccount, TokenOwnerRecord } from '@solana/spl-governance'
import type { MintInfo } from '@solana/spl-token'
import { REALM_ID as PYTH_REALM_ID, PythBalance } from 'pyth-staking-api'
import { BigNumber } from 'bignumber.js'

import { getMintDecimalAmount } from '@tools/sdk/units'
import type {
VoteRegistryVoterWeight,
VoteNftWeight,
SwitchboardQueueVoteWeight,
PythVoterWeight,
SimpleGatedVoterWeight,
VoterWeight,
} from '../../models/voteWeights'
Expand All @@ -18,22 +16,15 @@ type OwnVoterWeight =
| VoteRegistryVoterWeight
| VoteNftWeight
| SwitchboardQueueVoteWeight
| PythVoterWeight
| SimpleGatedVoterWeight
| VoterWeight

export default function getNumTokens(
ownVoterWeight: OwnVoterWeight,
_ownVoterWeight: OwnVoterWeight,
depositTokenRecord?: ProgramAccount<TokenOwnerRecord>,
mint?: MintInfo,
realmInfo?: RealmInfo
_realmInfo?: RealmInfo
) {
const isPyth = realmInfo?.realmId.toBase58() === PYTH_REALM_ID.toBase58()

if (isPyth && ownVoterWeight.votingPower) {
return new BigNumber(new PythBalance(ownVoterWeight.votingPower).toString())
}

if (depositTokenRecord && mint) {
return getMintDecimalAmount(
mint,
Expand Down
23 changes: 9 additions & 14 deletions components/TokenBalance/TokenBalanceCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import { ExclamationIcon } from '@heroicons/react/outline'
import { useEffect, useState } from 'react'
import useVotePluginsClientStore from 'stores/useVotePluginsClientStore'
import { VSR_PLUGIN_PKS } from '@constants/plugins'
import { REALM_ID as PYTH_REALM_ID } from 'pyth-staking-api'
import DelegateTokenBalanceCard from '@components/TokenBalance/DelegateTokenBalanceCard'
import SerumGovernanceTokenWrapper from './SerumGovernanceTokenWrapper'
import getNumTokens from '@components/ProposalVotingPower/getNumTokens'
Expand Down Expand Up @@ -430,17 +429,13 @@ export const TokenDeposit = ({
? 'You have to many outstanding proposals to withdraw.'
: ''

//Todo: move to own components with refactor to dao folder structure
const isPyth = realmInfo?.realmId.toBase58() === PYTH_REALM_ID.toBase58()

const availableTokens = isPyth
? fmtMintAmount(mint, ownVoterWeight.votingPower!)
: depositTokenRecord && mint
? fmtMintAmount(
mint,
depositTokenRecord.account.governingTokenDepositAmount
)
: '0'
const availableTokens =
depositTokenRecord && mint
? fmtMintAmount(
mint,
depositTokenRecord.account.governingTokenDepositAmount
)
: '0'

useEffect(() => {
if (availableTokens != '0' || hasTokensDeposited || hasTokensInWallet) {
Expand Down Expand Up @@ -494,7 +489,7 @@ export const TokenDeposit = ({
</div>
)}

{!isPyth && (
{
<>
<div
className={`my-4 opacity-70 text-xs ${
Expand Down Expand Up @@ -545,7 +540,7 @@ export const TokenDeposit = ({
)}
</div>
</>
)}
}
{isVsr && (
<small className="flex items-center mt-3 text-xs">
<ExclamationIcon className="w-5 h-5 mr-2"></ExclamationIcon>
Expand Down
3 changes: 0 additions & 3 deletions constants/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { SWITCHBOARD_ADDIN_ID } from 'SwitchboardVotePlugin/SwitchboardQueueVoterClient'
import { STAKING_ADDRESS as PYTH_STAKING_ADDRESS } from 'pyth-staking-api'
import * as heliumVsrSdk from '@helium/voter-stake-registry-sdk'
import { DEFAULT_NFT_VOTER_PLUGIN } from '@tools/constants'

Expand Down Expand Up @@ -28,5 +27,3 @@ export const GATEWAY_PLUGINS_PKS: string[] = [
export const SWITCHBOARD_PLUGINS_PKS: string[] = [
SWITCHBOARD_ADDIN_ID.toBase58(),
]

export const PYTH_PLUGINS_PKS: string[] = [PYTH_STAKING_ADDRESS.toBase58()]
35 changes: 1 addition & 34 deletions hooks/useRealm.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { ProgramAccount, TokenOwnerRecord } from '@solana/spl-governance'
import { useRouter } from 'next/router'
import useNftPluginStore from 'NftVotePlugin/store/nftPluginStore'
import { PythBalance } from 'pyth-staking-api'
import { useEffect, useMemo, useState } from 'react'
import useVotePluginsClientStore from 'stores/useVotePluginsClientStore'
import { useMemo } from 'react'
import useDepositStore from 'VoteStakeRegistry/stores/useDepositStore'
import {
PythVoterWeight,
SimpleGatedVoterWeight,
VoteNftWeight,
SwitchboardQueueVoteWeight,
Expand All @@ -17,7 +14,6 @@ import {
NFT_PLUGINS_PKS,
VSR_PLUGIN_PKS,
SWITCHBOARD_PLUGINS_PKS,
PYTH_PLUGINS_PKS,
GATEWAY_PLUGINS_PKS,
HELIUM_VSR_PLUGINS_PKS,
} from '../constants/plugins'
Expand Down Expand Up @@ -64,33 +60,10 @@ export default function useRealm() {
const gatewayVotingPower = useGatewayPluginStore((s) => s.state.votingPower)
const sbVotingPower = useSwitchboardPluginStore((s) => s.state.votingPower)
const currentPluginPk = config?.account?.communityTokenConfig.voterWeightAddin
const pythClient = useVotePluginsClientStore((s) => s.state.pythClient)
const [pythVoterWeight, setPythVoterWeight] = useState<PythBalance>()
const isPythclientMode =
currentPluginPk && PYTH_PLUGINS_PKS.includes(currentPluginPk?.toBase58())

const ownTokenRecord = useUserCommunityTokenOwnerRecord().data?.result
const ownCouncilTokenRecord = useUserCouncilTokenOwnerRecord().data?.result

//Move to store + move useEffect to main app index,
//useRealm is used very often across application
//and in every instance of useRealm it will shot with getMainAccount spamming rpc.
useEffect(() => {
const getPythVoterWeight = async () => {
if (connected && wallet?.publicKey && pythClient && isPythclientMode) {
const sa = await pythClient.stakeConnection.getMainAccount(
wallet.publicKey
)
const vw = sa?.getVoterWeight(
await pythClient.stakeConnection.getTime()
)
setPythVoterWeight(vw)
}
}
getPythVoterWeight()
// eslint-disable-next-line react-hooks/exhaustive-deps -- TODO please fix, it can cause difficult bugs. You might wanna check out https://bobbyhadz.com/blog/react-hooks-exhaustive-deps for info. -@asktree
}, [wallet?.publicKey])

const realmTokenAccount = useMemo(
() =>
realm &&
Expand Down Expand Up @@ -131,14 +104,12 @@ export default function useRealm() {
const vsrMode = useVsrMode()
const isNftMode =
currentPluginPk && NFT_PLUGINS_PKS.includes(currentPluginPk?.toBase58())
const pythVotingPower = pythVoterWeight?.toBN() || new BN(0)
const ownVoterWeight = getVoterWeight(
currentPluginPk,
ownTokenRecord,
votingPower,
nftVotingPower,
sbVotingPower,
pythVotingPower,
gatewayVotingPower,
ownCouncilTokenRecord,
heliumVotingPower
Expand Down Expand Up @@ -199,7 +170,6 @@ const getVoterWeight = (
votingPower: BN,
nftVotingPower: BN,
sbVotingPower: BN,
pythVotingPower: BN,
gatewayVotingPower: BN,
ownCouncilTokenRecord: ProgramAccount<TokenOwnerRecord> | undefined,
heliumVotingPower: BN
Expand Down Expand Up @@ -229,9 +199,6 @@ const getVoterWeight = (
if (SWITCHBOARD_PLUGINS_PKS.includes(currentPluginPk.toBase58())) {
return new SwitchboardQueueVoteWeight(ownTokenRecord, sbVotingPower)
}
if (PYTH_PLUGINS_PKS.includes(currentPluginPk.toBase58())) {
return new PythVoterWeight(ownTokenRecord, pythVotingPower)
}
if (GATEWAY_PLUGINS_PKS.includes(currentPluginPk.toBase58())) {
return new SimpleGatedVoterWeight(
ownTokenRecord,
Expand Down
24 changes: 1 addition & 23 deletions hooks/useVotingPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
HELIUM_VSR_PLUGINS_PKS,
VSR_PLUGIN_PKS,
GATEWAY_PLUGINS_PKS,
PYTH_PLUGINS_PKS,
SWITCHBOARD_PLUGINS_PKS,
} from '../constants/plugins'
import useUserOrDelegator from './useUserOrDelegator'
Expand All @@ -56,7 +55,6 @@ export function useVotingPlugins() {
//handleSetSwitchboardClient,
handleSetNftRegistrar,
handleSetGatewayRegistrar,
handleSetPythClient,
handleSetCurrentRealmVotingClient,
} = useVotePluginsClientStore()

Expand All @@ -83,7 +81,7 @@ export function useVotingPlugins() {
vsrClient,
gatewayClient,
//switchboardClient,
pythClient,
//pythClient,
nftClient,
nftMintRegistrar,
heliumVsrClient,
Expand All @@ -92,7 +90,6 @@ export function useVotingPlugins() {
s.state.vsrClient,
s.state.gatewayClient,
//s.state.switchboardClient,
s.state.pythClient,
s.state.nftClient,
s.state.nftMintRegistrar,
s.state.heliumVsrClient,
Expand Down Expand Up @@ -164,15 +161,13 @@ export function useVotingPlugins() {
handleSetNftClient(wallet, connection)
//handleSetSwitchboardClient(wallet, connection)
handleSetGatewayClient(wallet, connection)
handleSetPythClient(wallet, connection)
}
}, [
connection,
currentPluginPk,
handleSetGatewayClient,
handleSetHeliumVsrClient,
handleSetNftClient,
handleSetPythClient,
handleSetVsrClient,
wallet,
])
Expand Down Expand Up @@ -252,21 +247,6 @@ export function useVotingPlugins() {
}
}

const handlePythPlugin = () => {
if (
pythClient &&
currentPluginPk &&
PYTH_PLUGINS_PKS.includes(currentPluginPk.toBase58())
) {
if (voterPk) {
handleSetCurrentRealmVotingClient({
client: pythClient,
realm,
walletPk: voterPk,
})
}
}
}
/*
const handleSwitchboardPlugin = () => {
if (
Expand Down Expand Up @@ -299,7 +279,6 @@ export function useVotingPlugins() {
handleVsrPlugin()
handleHeliumVsrPlugin()
//handleSwitchboardPlugin()
handlePythPlugin()
}
}, [
currentClient,
Expand All @@ -314,7 +293,6 @@ export function useVotingPlugins() {
heliumVsrClient,
nftClient,
voterPk,
pythClient,
realm,
vsrClient,
])
Expand Down
4 changes: 1 addition & 3 deletions hub/components/EditRealmConfig/AddressValidator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import {
NFT_PLUGINS_PKS,
GATEWAY_PLUGINS_PKS,
SWITCHBOARD_PLUGINS_PKS,
PYTH_PLUGINS_PKS,
} from '@constants/plugins';
import { Program, AnchorProvider } from '@coral-xyz/anchor';
import { PublicKey } from '@solana/web3.js';
import React, { useEffect, useState } from 'react';
import { useEffect, useState } from 'react';

import { Input } from '@hub/components/controls/Input';
import { useCluster } from '@hub/hooks/useCluster';
Expand All @@ -22,7 +21,6 @@ const RECOGNIZED_PLUGINS = new Set([
...NFT_PLUGINS_PKS,
...GATEWAY_PLUGINS_PKS,
...SWITCHBOARD_PLUGINS_PKS,
...PYTH_PLUGINS_PKS,
]);

interface Props {
Expand Down
8 changes: 0 additions & 8 deletions hub/providers/Proposal/createProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
VSR_PLUGIN_PKS,
NFT_PLUGINS_PKS,
GATEWAY_PLUGINS_PKS,
PYTH_PLUGINS_PKS,
} from '@constants/plugins';
import { Wallet } from '@coral-xyz/anchor';
import {
Expand Down Expand Up @@ -241,13 +240,6 @@ export async function createProposal(args: Args) {
client = votingPlugins.gatewayClient;
}

if (
PYTH_PLUGINS_PKS.includes(pluginPublicKeyStr) &&
votingPlugins.pythClient
) {
client = votingPlugins.pythClient;
}

if (client) {
votingClient = new VotingClient({
realm,
Expand Down
5 changes: 1 addition & 4 deletions hub/providers/Proposal/fetchPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AnchorProvider, Wallet } from '@coral-xyz/anchor';
import { GatewayClient } from '@solana/governance-program-library';
import { Connection, PublicKey } from '@solana/web3.js';

import { PythClient } from 'pyth-staking-api';
import { VsrClient } from 'VoteStakeRegistry/sdk/client';

import { NftVoterClient } from '@utils/uiTypes/NftVoterClient';
Expand All @@ -17,17 +16,15 @@ export async function fetchPlugins(
const defaultOptions = AnchorProvider.defaultOptions();
const anchorProvider = new AnchorProvider(connection, wallet, defaultOptions);

const [vsrClient, nftClient, gatewayClient, pythClient] = await Promise.all([
const [vsrClient, nftClient, gatewayClient] = await Promise.all([
VsrClient.connect(anchorProvider, pluginProgram, isDevnet),
NftVoterClient.connect(anchorProvider, isDevnet),
GatewayClient.connect(anchorProvider, isDevnet),
PythClient.connect(anchorProvider, connection.rpcEndpoint),
]);

return {
vsrClient,
nftClient,
gatewayClient,
pythClient,
};
}
Loading

0 comments on commit 636257d

Please sign in to comment.