Skip to content

Commit 8465218

Browse files
authored
add Bio VSR Plugin (#195)
1 parent 8d614e9 commit 8465218

21 files changed

+290
-53
lines changed

VoteStakeRegistry/actions/getClawbackInstruction.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
TOKEN_PROGRAM_ID,
88
} from '@solana/spl-token'
99
import { VsrClient } from 'VoteStakeRegistry/sdk/client'
10+
import { TOKEN_2022_PROGRAM_ID } from '@solana/spl-token-new'
11+
import { CUSTOM_BIO_VSR_PLUGIN_PK } from '@constants/plugins'
1012

1113
export const getClawbackInstruction = async ({
1214
realmPk,
@@ -36,9 +38,13 @@ export const getClawbackInstruction = async ({
3638
)
3739
const { voter } = getVoterPDA(registrar, voterWalletAddress, clientProgramId)
3840

41+
const tokenProgram = client?.program.programId.toBase58() === CUSTOM_BIO_VSR_PLUGIN_PK ?
42+
TOKEN_2022_PROGRAM_ID :
43+
TOKEN_PROGRAM_ID
44+
3945
const voterATAPk = await Token.getAssociatedTokenAddress(
4046
ASSOCIATED_TOKEN_PROGRAM_ID,
41-
TOKEN_PROGRAM_ID,
47+
tokenProgram,
4248
grantMintPk,
4349
voter,
4450
true,
@@ -52,8 +58,17 @@ export const getClawbackInstruction = async ({
5258
voter,
5359
vault: voterATAPk,
5460
destination,
55-
tokenProgram: TOKEN_PROGRAM_ID,
61+
tokenProgram,
5662
})
5763
.instruction()
64+
65+
if (tokenProgram.equals(TOKEN_2022_PROGRAM_ID)) {
66+
clawbackIx?.keys.splice(4, 0, {
67+
pubkey: grantMintPk,
68+
isWritable: false,
69+
isSigner: false,
70+
})
71+
}
72+
5873
return clawbackIx
5974
}

VoteStakeRegistry/actions/getGrantInstruction.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
} from '@solana/spl-token'
1515
import { VsrClient } from 'VoteStakeRegistry/sdk/client'
1616
import { fmtDecimalToBN } from '@utils/formatting'
17+
import { CUSTOM_BIO_VSR_PLUGIN_PK } from '@constants/plugins'
18+
import { TOKEN_2022_PROGRAM_ID } from '@solana/spl-token-new'
1719

1820
export const getGrantInstruction = async ({
1921
fromPk,
@@ -49,6 +51,10 @@ export const getGrantInstruction = async ({
4951
const systemProgram = SystemProgram.programId
5052
const clientProgramId = client!.program.programId
5153

54+
const tokenProgram = client?.program.programId.toBase58() === CUSTOM_BIO_VSR_PLUGIN_PK ?
55+
TOKEN_2022_PROGRAM_ID :
56+
TOKEN_PROGRAM_ID
57+
5258
const { registrar } = getRegistrarPDA(
5359
realmPk,
5460
communityMintPk,
@@ -62,7 +68,7 @@ export const getGrantInstruction = async ({
6268
)
6369
const voterATAPk = await Token.getAssociatedTokenAddress(
6470
ASSOCIATED_TOKEN_PROGRAM_ID,
65-
TOKEN_PROGRAM_ID,
71+
tokenProgram,
6672
grantMintPk,
6773
voter,
6874
true,
@@ -90,7 +96,7 @@ export const getGrantInstruction = async ({
9096
depositMint: grantMintPk,
9197
payer: toPk,
9298
systemProgram: systemProgram,
93-
tokenProgram: TOKEN_PROGRAM_ID,
99+
tokenProgram,
94100
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
95101
rent: SYSVAR_RENT_PUBKEY,
96102
})

VoteStakeRegistry/actions/voteRegistryLockDeposit.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import {
44
Transaction,
55
TransactionInstruction,
66
} from '@solana/web3.js'
7-
import { RpcContext, TOKEN_PROGRAM_ID } from '@solana/spl-governance'
7+
import { RpcContext } from '@solana/spl-governance'
88
import { sendTransaction } from 'utils/send'
99

1010
import { BN } from '@coral-xyz/anchor'
1111
import { LockupType } from 'VoteStakeRegistry/sdk/accounts'
1212
import { withCreateNewDeposit } from '../sdk/withCreateNewDeposit'
1313
import { getPeriod } from 'VoteStakeRegistry/tools/deposits'
1414
import { VsrClient } from 'VoteStakeRegistry/sdk/client'
15+
import { TOKEN_2022_PROGRAM_ID } from '@solana/spl-token-new'
1516

1617
export const voteRegistryLockDeposit = async ({
1718
rpcContext,
@@ -59,7 +60,7 @@ export const voteRegistryLockDeposit = async ({
5960
amountFromVoteRegistryDeposit,
6061
)
6162
const instructions: TransactionInstruction[] = []
62-
const { depositIdx, voter, registrar, voterATAPk } =
63+
const { depositIdx, voter, registrar, voterATAPk, tokenProgram } =
6364
await withCreateNewDeposit({
6465
instructions,
6566
walletPk: rpcContext.walletPubkey,
@@ -101,9 +102,18 @@ export const voteRegistryLockDeposit = async ({
101102
vault: voterATAPk,
102103
depositToken: sourceTokenAccount,
103104
depositAuthority: wallet!.publicKey!,
104-
tokenProgram: TOKEN_PROGRAM_ID,
105+
tokenProgram,
105106
})
106107
.instruction()
108+
109+
if (tokenProgram.equals(TOKEN_2022_PROGRAM_ID)) {
110+
depositInstruction.keys.splice(3, 0, {
111+
pubkey: mintPk,
112+
isSigner: false,
113+
isWritable: false,
114+
})
115+
}
116+
107117
instructions.push(depositInstruction)
108118
}
109119

VoteStakeRegistry/components/Account/DepositCard.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ const DepositCard = ({
9898
queryClient.invalidateQueries(
9999
tokenAccountQueryKeys.byOwner(connection.rpcEndpoint, wallet!.publicKey!),
100100
)
101+
queryClient.invalidateQueries(
102+
['get-custom-vsr-token-account', {
103+
realm: realm?.pubkey.toBase58(),
104+
mint: realm?.account.communityMint.toBase58(),
105+
pubkey: wallet?.publicKey?.toBase58()
106+
}]
107+
)
101108
queryClient.invalidateQueries(['VoteRecord'])
102109
}
103110
const handleStartUnlock = () => {

VoteStakeRegistry/components/Account/LockTokensAccount.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
import { useConnection } from '@solana/wallet-adapter-react'
4848
import { useVsrGovpower } from '@hooks/queries/plugins/vsr'
4949
import { useVsrClient } from '../../../VoterWeightPlugins/useVsrClient'
50+
import { CUSTOM_BIO_VSR_PLUGIN_PK } from '@constants/plugins'
5051

5152
interface DepositBox {
5253
mintPk: PublicKey
@@ -62,7 +63,7 @@ const LockTokensAccount: React.FC<{
6263
}> = ({ tokenOwnerRecordPk, children }) => {
6364
const realm = useRealmQuery().data?.result
6465
const config = useRealmConfigQuery().data?.result
65-
const mint = useRealmCommunityMintInfoQuery().data?.result
66+
const defaultMint = useRealmCommunityMintInfoQuery().data?.result
6667
const councilMint = useRealmCouncilMintInfoQuery().data?.result
6768
const { realmInfo } = useRealm()
6869
const [isLockModalOpen, setIsLockModalOpen] = useState(false)
@@ -82,6 +83,11 @@ const LockTokensAccount: React.FC<{
8283
)
8384
const [isOwnerOfDeposits, setIsOwnerOfDeposits] = useState(true)
8485

86+
const mint =
87+
config?.account.communityTokenConfig.voterWeightAddin?.toBase58() === CUSTOM_BIO_VSR_PLUGIN_PK && deposits[0] ?
88+
deposits[0].mint.account :
89+
defaultMint
90+
8591
const { data: tokenOwnerRecord } =
8692
useTokenOwnerRecordByPubkeyQuery(tokenOwnerRecordPk)
8793
const tokenOwnerRecordWalletPk =

VoteStakeRegistry/components/Account/LockTokensModal.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import { useConnection } from '@solana/wallet-adapter-react'
5656
import { tokenAccountQueryKeys } from '@hooks/queries/tokenAccount'
5757
import queryClient from '@hooks/queries/queryClient'
5858
import { useVsrClient } from '../../../VoterWeightPlugins/useVsrClient'
59+
import { CUSTOM_BIO_VSR_PLUGIN_PK } from '@constants/plugins'
5960

6061
const YES = 'Yes'
6162
const NO = 'No'
@@ -71,7 +72,7 @@ const LockTokensModal = ({
7172
}) => {
7273
const { getOwnedDeposits } = useDepositStore()
7374
const realm = useRealmQuery().data?.result
74-
const mint = useRealmCommunityMintInfoQuery().data?.result
75+
const defaultMint = useRealmCommunityMintInfoQuery().data?.result
7576
const { realmTokenAccount, realmInfo } = useRealm()
7677
const { data: tokenOwnerRecordPk } = useAddressQuery_CommunityTokenOwner()
7778

@@ -177,9 +178,19 @@ const LockTokensModal = ({
177178
realm,
178179
)
179180

181+
const isCustomBioPlugin = client?.program.programId.toBase58() === CUSTOM_BIO_VSR_PLUGIN_PK && deposits[0] !== undefined
182+
183+
const depositMint = isCustomBioPlugin ?
184+
deposits[0].mint.publicKey
185+
: realm?.account.communityMint
186+
187+
const mint = isCustomBioPlugin ?
188+
deposits[0].mint.account
189+
: defaultMint
190+
180191
const depositRecord = deposits.find(
181192
(x) =>
182-
x.mint.publicKey.toBase58() === realm?.account.communityMint.toBase58() &&
193+
x.mint.publicKey.toBase58() === depositMint!.toBase58() &&
183194
x.lockup.kind.none,
184195
)
185196
const [lockupPeriodDays, setLockupPeriodDays] = useState<number>(0)
@@ -222,6 +233,7 @@ const LockTokensModal = ({
222233
)
223234
: getMintDecimalAmount(mint, depositRecord?.amountDepositedNative)
224235
: 0
236+
225237
const maxAmount = depositToUnlock ? maxAmountToUnlock : maxAmountToLock
226238
const maxAmountToLockFmt =
227239
depositRecord && mint && realmTokenAccount
@@ -303,7 +315,7 @@ const LockTokensModal = ({
303315
}
304316
await voteRegistryLockDeposit({
305317
rpcContext,
306-
mintPk: realm!.account.communityMint!,
318+
mintPk: depositMint!,
307319
communityMintPk: realm!.account.communityMint!,
308320
realmPk: realm!.pubkey!,
309321
programId: realm!.owner,
@@ -328,6 +340,13 @@ const LockTokensModal = ({
328340
queryClient.invalidateQueries(
329341
tokenAccountQueryKeys.byOwner(connection.rpcEndpoint, wallet!.publicKey!),
330342
)
343+
queryClient.invalidateQueries(
344+
['get-custom-vsr-token-account', {
345+
realm: realm?.pubkey.toBase58(),
346+
mint: realm?.account.communityMint.toBase58(),
347+
pubkey: wallet?.publicKey?.toBase58()
348+
}]
349+
)
331350
onClose()
332351
}
333352

VoteStakeRegistry/components/TokenBalance/DepositCommunityTokensBtn.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { useConnection } from '@solana/wallet-adapter-react'
1616
import queryClient from '@hooks/queries/queryClient'
1717
import { tokenAccountQueryKeys } from '@hooks/queries/tokenAccount'
1818
import { useVsrClient } from '../../../VoterWeightPlugins/useVsrClient'
19+
import { CUSTOM_BIO_VSR_PLUGIN_PK } from '@constants/plugins'
1920

2021
const DepositCommunityTokensBtn = ({ className = '', inAccountDetails }) => {
2122
const { getOwnedDeposits } = useDepositStore()
@@ -48,10 +49,15 @@ const DepositCommunityTokensBtn = ({ className = '', inAccountDetails }) => {
4849
endpoint,
4950
)
5051
try {
52+
const mintPk = vsrClient?.program.programId.toBase58() === CUSTOM_BIO_VSR_PLUGIN_PK ?
53+
await vsrClient.getRegistrarAccount(realm.pubkey, realm.account.communityMint!)
54+
.then(r => r?.votingMints[0].mint!) :
55+
realm.account.communityMint!
56+
5157
await voteRegistryDepositWithoutLockup({
5258
rpcContext,
5359
fromPk: realmTokenAccount!.publicKey,
54-
mintPk: realm.account.communityMint!,
60+
mintPk,
5561
realmPk: realm.pubkey,
5662
programId: realm.owner,
5763
programVersion: realmInfo?.programVersion!,
@@ -73,6 +79,14 @@ const DepositCommunityTokensBtn = ({ className = '', inAccountDetails }) => {
7379
wallet!.publicKey!,
7480
),
7581
)
82+
83+
queryClient.invalidateQueries(
84+
['get-custom-vsr-token-account', {
85+
realm: realm.pubkey.toBase58(),
86+
mint: realm.account.communityMint.toBase58(),
87+
pubkey: wallet?.publicKey?.toBase58()
88+
}]
89+
)
7690
} catch (e) {
7791
console.log(e)
7892
notify({ message: `Something went wrong ${e}`, type: 'error' })

VoteStakeRegistry/components/TokenBalance/LockPluginTokenBalanceCard.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { MintInfo } from '@solana/spl-token'
22
import { PublicKey } from '@solana/web3.js'
33
import BN from 'bn.js'
44
import useRealm from '@hooks/useRealm'
5-
import { getTokenOwnerRecordAddress, Proposal } from '@solana/spl-governance'
5+
import { getTokenOwnerRecordAddress, Proposal, RealmConfigAccount } from '@solana/spl-governance'
66
import { Option } from '@tools/core/option'
77
import { GoverningTokenRole } from '@solana/spl-governance'
88
import { fmtMintAmount } from '@tools/sdk/units'
@@ -26,6 +26,7 @@ import {
2626
useRealmCouncilMintInfoQuery,
2727
} from '@hooks/queries/mintInfo'
2828
import { useVsrGovpower } from '@hooks/queries/plugins/vsr'
29+
import { CUSTOM_BIO_VSR_PLUGIN_PK } from '@constants/plugins'
2930

3031
/** UNUSED */
3132
const LockPluginTokenBalanceCard = ({
@@ -125,6 +126,7 @@ const LockPluginTokenBalanceCard = ({
125126
mint={mint}
126127
tokenRole={GoverningTokenRole.Community}
127128
councilVote={false}
129+
realmConfig={config?.account}
128130
setHasGovPower={setHasGovPower}
129131
/>
130132
)}
@@ -151,11 +153,13 @@ const LockPluginTokenBalanceCard = ({
151153
const TokenDepositLock = ({
152154
mint,
153155
tokenRole,
156+
realmConfig,
154157
inAccountDetails,
155158
setHasGovPower,
156159
}: {
157160
mint: MintInfo | undefined
158161
tokenRole: GoverningTokenRole
162+
realmConfig?: RealmConfigAccount
159163
councilVote?: boolean
160164
inAccountDetails?: boolean
161165
setHasGovPower: (hasGovPower: boolean) => void
@@ -196,6 +200,11 @@ const TokenDepositLock = ({
196200

197201
const tokenName = getMintMetadata(depositMint)?.name ?? realm?.account.name
198202

203+
const displayMint =
204+
realmConfig?.communityTokenConfig.voterWeightAddin?.toBase58() === CUSTOM_BIO_VSR_PLUGIN_PK && deposits[0] ?
205+
deposits[0].mint.account :
206+
mint
207+
199208
const depositTokenName = `${tokenName} ${
200209
tokenRole === GoverningTokenRole.Community ? '' : 'Council'
201210
}`
@@ -229,7 +238,7 @@ const TokenDepositLock = ({
229238
: 0
230239

231240
// Do not show deposits for mints with zero supply because nobody can deposit anyway
232-
if (!mint || mint.supply.isZero()) {
241+
if (!displayMint || displayMint.supply.isZero()) {
233242
return null
234243
}
235244

@@ -249,7 +258,7 @@ const TokenDepositLock = ({
249258
<div className="flex space-x-4 items-center mt-4">
250259
<VotingPowerBox
251260
votingPower={votingPower}
252-
mint={mint}
261+
mint={displayMint}
253262
votingPowerFromDeposits={votingPowerFromDeposits}
254263
className="w-full px-4 py-2"
255264
></VotingPowerBox>

0 commit comments

Comments
 (0)