Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
abrzezinski94 committed Oct 2, 2023
2 parents 33b3a60 + 4b2ad58 commit 14e394b
Show file tree
Hide file tree
Showing 14 changed files with 390 additions and 347 deletions.
121 changes: 59 additions & 62 deletions actions/castVote.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Keypair, Transaction, TransactionInstruction } from '@solana/web3.js'
import {
Keypair,
PublicKey,
Transaction,
TransactionInstruction,
} from '@solana/web3.js'
import {
ChatMessageBody,
getGovernanceProgramVersion,
GovernanceAccountType,
GOVERNANCE_CHAT_PROGRAM_ID,
Proposal,
Realm,
TokenOwnerRecord,
VoteChoice,
VoteKind,
VoteType,
Expand Down Expand Up @@ -50,15 +54,15 @@ export async function castVote(
{ connection, wallet, programId, walletPubkey }: RpcContext,
realm: ProgramAccount<Realm>,
proposal: ProgramAccount<Proposal>,
tokenOwnerRecord: ProgramAccount<TokenOwnerRecord>,
tokenOwnerRecord: PublicKey,
voteKind: VoteKind,
message?: ChatMessageBody | undefined,
votingPlugin?: VotingClient,
runAfterConfirmation?: (() => void) | null,
voteWeights?: number[]
voteWeights?: number[],
_additionalTokenOwnerRecords?: []
) {
const signers: Keypair[] = []
const instructions: TransactionInstruction[] = []

const createCastNftVoteTicketIxs: TransactionInstruction[] = []
const createPostMessageTicketIxs: TransactionInstruction[] = []
Expand All @@ -72,13 +76,15 @@ export async function castVote(
programId
)

const pluginCastVoteIxs: TransactionInstruction[] = []
//will run only if any plugin is connected with realm
const plugin = await votingPlugin?.withCastPluginVote(
instructions,
pluginCastVoteIxs,
proposal,
tokenOwnerRecord.pubkey,
tokenOwnerRecord,
createCastNftVoteTicketIxs
)
console.log('PLUGIN IXS', pluginCastVoteIxs)

const isMulti =
proposal.account.voteType !== VoteType.SINGLE_CHOICE &&
Expand Down Expand Up @@ -132,15 +138,16 @@ export async function castVote(
? getVetoTokenMint(proposal, realm)
: proposal.account.governingTokenMint

const castVoteIxs: TransactionInstruction[] = []
await withCastVote(
instructions,
castVoteIxs,
programId,
programVersion,
realm.pubkey,
proposal.account.governance,
proposal.pubkey,
proposal.account.tokenOwnerRecord,
tokenOwnerRecord.pubkey,
tokenOwnerRecord,
governanceAuthority,
tokenMint,
vote,
Expand All @@ -149,23 +156,25 @@ export async function castVote(
plugin?.maxVoterWeightRecord
)

const pluginPostMessageIxs: TransactionInstruction[] = []
const postMessageIxs: TransactionInstruction[] = []
if (message) {
const plugin = await votingPlugin?.withUpdateVoterWeightRecord(
instructions,
tokenOwnerRecord.pubkey,
pluginPostMessageIxs,
tokenOwnerRecord,
'commentProposal',
createPostMessageTicketIxs
)

await withPostChatMessage(
instructions,
postMessageIxs,
signers,
GOVERNANCE_CHAT_PROGRAM_ID,
programId,
realm.pubkey,
proposal.account.governance,
proposal.pubkey,
tokenOwnerRecord.pubkey,
tokenOwnerRecord,
governanceAuthority,
payer,
undefined,
Expand All @@ -174,13 +183,19 @@ export async function castVote(
)
}

const ixChunkCount = message ? 4 : 2
const isNftVoter = votingPlugin?.client instanceof NftVoterClient
const isHeliumVoter = votingPlugin?.client instanceof HeliumVsrClient

if (!isNftVoter && !isHeliumVoter) {
const transaction = new Transaction()
transaction.add(...instructions)
transaction.add(
...[
...pluginCastVoteIxs,
...castVoteIxs,
...pluginPostMessageIxs,
...postMessageIxs,
]
)

await sendTransaction({ transaction, wallet, connection, signers })
if (runAfterConfirmation) {
Expand All @@ -190,37 +205,27 @@ export async function castVote(

// we need to chunk instructions
if (isHeliumVoter) {
// update voter weight + cast vote from spl gov need to be in one transaction
const ixsWithOwnChunk = instructions.slice(-ixChunkCount)
const remainingIxsToChunk = instructions.slice(
0,
instructions.length - ixChunkCount
// @asktree: I'm aware of no rationale for chunking in this particular manner
const chunkerz = chunks(
[
...pluginCastVoteIxs,
...castVoteIxs,
...pluginPostMessageIxs,
...postMessageIxs,
],
2
)

const splIxsWithAccountsChunk = chunks(ixsWithOwnChunk, 2)
const positionsAccountsChunks = chunks(remainingIxsToChunk, 2)
const ixsChunks = [
...positionsAccountsChunks.map((txBatch, batchIdx) => {
return {
instructionsSet: txBatchesToInstructionSetWithSigners(
txBatch,
[],
batchIdx
),
sequenceType: SequenceType.Parallel,
}
}),
...splIxsWithAccountsChunk.map((txBatch, batchIdx) => {
return {
instructionsSet: txBatchesToInstructionSetWithSigners(
txBatch,
message ? [[], signers] : [],
batchIdx
),
sequenceType: SequenceType.Sequential,
}
}),
]
const ixsChunks = chunkerz.map((txBatch, batchIdx) => {
return {
instructionsSet: txBatchesToInstructionSetWithSigners(
txBatch,
message ? [[], signers] : [],
batchIdx
),
sequenceType: SequenceType.Sequential,
}
})

await sendTransactionsV3({
connection,
Expand All @@ -247,12 +252,16 @@ export async function castVote(
[...createCastNftVoteTicketIxs, ...createPostMessageTicketIxs],
1
)
const otherChunks = chunks(
[
...pluginCastVoteIxs,
...castVoteIxs,
...pluginPostMessageIxs,
...postMessageIxs,
],
2
)

const splIxs = instructions.slice(-ixChunkCount)
const nftAccountIxs = instructions.slice(0, -ixChunkCount)

const splIxsWithAccountsChunk = chunks(splIxs, 2)
const nftsAccountsChunks = chunks(nftAccountIxs, 2)
const instructionsChunks = [
...createNftVoteTicketsChunks.map((txBatch, batchIdx) => {
return {
Expand All @@ -264,19 +273,7 @@ export async function castVote(
sequenceType: SequenceType.Parallel,
}
}),
...nftsAccountsChunks.map((txBatch, batchIdx) => {
return {
instructionsSet: txBatchesToInstructionSetWithSigners(
txBatch,
[],
batchIdx
),
sequenceType:
// this is to ensure create all the nft_action_tickets account first
batchIdx == 0 ? SequenceType.Sequential : SequenceType.Parallel,
}
}),
...splIxsWithAccountsChunk.map((txBatch, batchIdx) => {
...otherChunks.map((txBatch, batchIdx) => {
return {
instructionsSet: txBatchesToInstructionSetWithSigners(
txBatch,
Expand Down
74 changes: 30 additions & 44 deletions components/ProposalVotingPower/NftVotingPower.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,57 +104,43 @@ export default function NftVotingPower(props: Props) {

if (nfts.length === 0) {
return (
<Wrapper inAccountDetails={props.inAccountDetails}>
<div
className={classNames(props.className, 'text-xs', 'text-white/50')}
>
You do not have any voting power in this dao.
</div>
</Wrapper>
<div className={classNames(props.className, 'text-xs', 'text-white/50')}>
You do not have any voting power in this dao.
</div>
)
}

return (
<Wrapper inAccountDetails={props.inAccountDetails}>
<div className={props.className}>
<div className={classNames('p-3', 'rounded-md', 'bg-bkg-1')}>
<div className="text-white/50 text-xs">My NFT Votes</div>
<div className="flex items-center justify-between mt-1">
<div className="text-white flex items-center gap-1">
{displayNfts.slice(0, 3).map((nft, index) => (
<div
className="h-12 w-12 rounded-sm bg-bkg-2 bg-cover"
key={nft.content.metadata.name + index}
style={{
backgroundImage: `url("${nft.content.links.image}")`,
}}
/>
))}
{!!remainingCount && (
<div className="text-sm text-white ml-2">
+{remainingCount} more
</div>
)}
</div>
{max && !max.isZero() && (
<VotingPowerPct amount={amount} total={max} />
<div className={props.className}>
<div className={classNames('p-3', 'rounded-md', 'bg-bkg-1')}>
<div className="text-white/50 text-xs">My NFT Votes</div>
<div className="flex items-center justify-between mt-1">
<div className="text-white flex items-center gap-1">
{displayNfts.slice(0, 3).map((nft, index) => (
<div
className="h-12 w-12 rounded-sm bg-bkg-2 bg-cover"
key={nft.content.metadata.name + index}
style={{
backgroundImage: `url("${nft.content.links.image}")`,
}}
/>
))}
{!!remainingCount && (
<div className="text-sm text-white ml-2">
+{remainingCount} more
</div>
)}
</div>
{max && !max.isZero() && (
<VotingPowerPct amount={amount} total={max} />
)}
</div>
{connected && !ownTokenRecord && (
<Button className="w-full mt-3" onClick={handleRegister}>
Join
</Button>
)}
</div>
</Wrapper>
{connected && !ownTokenRecord && (
<Button className="w-full mt-3" onClick={handleRegister}>
Join
</Button>
)}
</div>
)
}

const Wrapper = (props: Props) => {
if (props.inAccountDetails) {
return <div className="my-4 space-y-4 w-1/2">{props.children}</div>
} else {
return <>{props.children}</>
}
}
44 changes: 10 additions & 34 deletions components/TokenBalance/TokenBalanceCardWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import ClaimUnreleasedNFTs from './ClaimUnreleasedNFTs'
import Link from 'next/link'
import { useAddressQuery_CommunityTokenOwner } from '@hooks/queries/addresses/tokenOwnerRecord'
import useWalletOnePointOh from '@hooks/useWalletOnePointOh'
import {
useUserCommunityTokenOwnerRecord,
useUserCouncilTokenOwnerRecord,
} from '@hooks/queries/tokenOwnerRecord'
import { useUserCommunityTokenOwnerRecord } from '@hooks/queries/tokenOwnerRecord'
import { useRealmConfigQuery } from '@hooks/queries/realmConfig'
import ClaimUnreleasedPositions from 'HeliumVotePlugin/components/ClaimUnreleasedPositions'
import VanillaAccountDetails from './VanillaAccountDetails'
Expand Down Expand Up @@ -68,10 +65,9 @@ const TokenBalanceCardInner = ({
inAccountDetails?: boolean
}) => {
const ownTokenRecord = useUserCommunityTokenOwnerRecord().data?.result
const ownCouncilTokenRecord = useUserCouncilTokenOwnerRecord().data?.result
const config = useRealmConfigQuery().data?.result

const { councilTokenAccount, vsrMode } = useRealm()
const { vsrMode } = useRealm()
const currentPluginPk = config?.account?.communityTokenConfig.voterWeightAddin
const isNftMode =
currentPluginPk && NFT_PLUGINS_PKS.includes(currentPluginPk?.toBase58())
Expand Down Expand Up @@ -100,35 +96,15 @@ const TokenBalanceCardInner = ({
)
}

if (
isNftMode &&
(!ownTokenRecord ||
ownTokenRecord.account.governingTokenDepositAmount.isZero())
) {
if (isNftMode && inAccountDetails) {
return (
<>
{(ownCouncilTokenRecord &&
!ownCouncilTokenRecord?.account.governingTokenDepositAmount.isZero()) ||
(councilTokenAccount &&
!councilTokenAccount?.account.amount.isZero()) ? (
<>
{!inAccountDetails && <GovernancePowerTitle />}
<NftVotingPower inAccountDetails={inAccountDetails} />
{inAccountDetails ? (
<VanillaAccountDetails />
) : (
<GovernancePowerCard />
)}
<ClaimUnreleasedNFTs inAccountDetails={inAccountDetails} />
</>
) : (
<>
{!inAccountDetails && <GovernancePowerTitle />}
<NftVotingPower inAccountDetails={inAccountDetails} />
<ClaimUnreleasedNFTs inAccountDetails={inAccountDetails} />
</>
)}
</>
<div className="grid grid-cols-2 gap-x-2 w-full">
<div>
<NftVotingPower inAccountDetails={inAccountDetails} />
<ClaimUnreleasedNFTs inAccountDetails={inAccountDetails} />
</div>
<VanillaAccountDetails />
</div>
)
}

Expand Down
Loading

0 comments on commit 14e394b

Please sign in to comment.