Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into agrippa/delegation-re…
Browse files Browse the repository at this point in the history
…work-vsr
  • Loading branch information
asktree committed Dec 13, 2023
2 parents ffd4b98 + 76960b4 commit d586283
Show file tree
Hide file tree
Showing 67 changed files with 1,837 additions and 844 deletions.
19 changes: 7 additions & 12 deletions .github/workflows/ci-main-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,14 @@ jobs:
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: ['javascript']

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Initialise CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
languages: 'javascript'

- name: Run CodeQL
uses: github/codeql-action/analyze@v2
Expand All @@ -37,7 +32,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

# Report all vulnerabilities GitHub security tab
- name: Report on all vulnerabilities
Expand Down Expand Up @@ -71,12 +66,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: lts/*
node-version: 18
cache: yarn

- name: Cache dependencies
Expand All @@ -86,7 +81,7 @@ jobs:
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}

- name: Install dependencies
run: yarn install --frozen-lockfile
run: yarn ci

- name: Run tests
run: yarn test-all
Expand Down
3 changes: 2 additions & 1 deletion VoteStakeRegistry/components/Account/LockTokensModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ const LockTokensModal = ({
const currentMultiplier = calcMintMultiplier(
lockupPeriodDays * SECS_PER_DAY,
voteStakeRegistryRegistrar,
realm
realm,
lockupType.value !== 'constant'
)
const currentPercentOfMaxMultiplier =
(100 * currentMultiplier) / maxMultiplier
Expand Down
18 changes: 16 additions & 2 deletions VoteStakeRegistry/components/instructions/Clawback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import useVotePluginsClientStore from 'stores/useVotePluginsClientStore'
import { AssetAccount } from '@utils/uiTypes/assets'
import { useRealmQuery } from '@hooks/queries/realm'
import useLegacyConnectionContext from '@hooks/useLegacyConnectionContext'
import Input from '@components/inputs/Input'

const Clawback = ({
index,
Expand All @@ -54,13 +55,13 @@ const Clawback = ({
governedTokenAccountsWithoutNfts,
governancesArray,
} = useGovernanceAssets()
const shouldBeGoverned = !!(index !== 0 && governance)
const [voters, setVoters] = useState<Voter[]>([])
const [deposits, setDeposits] = useState<DepositWithMintAccount[]>([])
const [form, setForm] = useState<ClawbackForm>({
governedTokenAccount: undefined,
voter: null,
deposit: null,
holdupTime: 0,
})
const formDeposits = form.deposit
const schema = useMemo(
Expand All @@ -74,6 +75,7 @@ const Clawback = ({
}),
[]
)

const realmAuthorityGov = governancesArray.find(
(x) => x.pubkey.toBase58() === realm?.account.authority?.toBase58()
)
Expand Down Expand Up @@ -119,6 +121,7 @@ const Clawback = ({
isValid,
governance: realmAuthorityGov,
prerequisiteInstructions: prerequisiteInstructions,
customHoldUpTime: form.holdupTime,
}
return obj
}, [client, form, realmAuthorityGov, realm, schema])
Expand Down Expand Up @@ -265,6 +268,7 @@ const Clawback = ({
})}
</Select>
<GovernedAccountSelect
type={'token'}
label="Clawback destination"
governedAccounts={
governedTokenAccountsWithoutNfts.filter(
Expand All @@ -278,9 +282,19 @@ const Clawback = ({
}}
value={form.governedTokenAccount}
error={formErrors['governedTokenAccount']}
shouldBeGoverned={shouldBeGoverned}
governance={governance}
></GovernedAccountSelect>
<Input
label="Instruction hold up time (days)"
type="number"
value={form.holdupTime}
onChange={(evt) =>
handleSetForm({
value: evt.target.value,
propertyName: 'holdupTime',
})
}
></Input>
</>
)
}
Expand Down
46 changes: 25 additions & 21 deletions VoteStakeRegistry/tools/deposits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BN, EventParser, Idl, Program } from '@coral-xyz/anchor'
import { ProgramAccount, Realm } from '@solana/spl-governance'
import { PublicKey, Transaction, Connection } from '@solana/web3.js'
import { SIMULATION_WALLET } from '@tools/constants'
import { DAYS_PER_MONTH } from '@utils/dateTools'
import { DAYS_PER_MONTH, SECS_PER_DAY } from '@utils/dateTools'
import { chunks } from '@utils/helpers'
import { tryGetMint } from '@utils/tokens'
import {
Expand Down Expand Up @@ -259,31 +259,33 @@ export const calcMultiplier = ({
maxExtraLockupVoteWeightScaledFactor,
lockupSecs,
lockupSaturationSecs,
isVested = false,
}: {
depositScaledFactor: number
maxExtraLockupVoteWeightScaledFactor: number
lockupSecs: number
lockupSaturationSecs: number
isVested?: boolean
}) => {
// if (isVested) {
// const onMonthSecs = SECS_PER_DAY * DAYS_PER_MONTH
// const n_periods_before_saturation = lockupSaturationSecs / onMonthSecs
// const n_periods = lockupSecs / onMonthSecs
// const n_unsaturated_periods = Math.min(
// n_periods,
// n_periods_before_saturation
// )
// const n_saturated_periods = Math.max(0, n_periods - n_unsaturated_periods)
// const calc =
// (depositScaledFactor +
// (maxExtraLockupVoteWeightScaledFactor / n_periods) *
// (n_saturated_periods +
// ((n_unsaturated_periods + 1) * n_unsaturated_periods) /
// 2 /
// n_periods_before_saturation)) /
// depositScaledFactor
// return depositScaledFactor !== 0 ? calc : 0
// }
if (isVested) {
const onMonthSecs = SECS_PER_DAY * DAYS_PER_MONTH
const n_periods_before_saturation = lockupSaturationSecs / onMonthSecs
const n_periods = lockupSecs / onMonthSecs
const n_unsaturated_periods = Math.min(
n_periods,
n_periods_before_saturation
)
const n_saturated_periods = Math.max(0, n_periods - n_unsaturated_periods)
const calc =
(depositScaledFactor +
(maxExtraLockupVoteWeightScaledFactor / n_periods) *
(n_saturated_periods +
((n_unsaturated_periods + 1) * n_unsaturated_periods) /
2 /
n_periods_before_saturation)) /
depositScaledFactor
return depositScaledFactor !== 0 ? calc : 0
}
const calc =
(depositScaledFactor +
(maxExtraLockupVoteWeightScaledFactor *
Expand Down Expand Up @@ -318,7 +320,8 @@ export const getPeriod = (
export const calcMintMultiplier = (
lockupSecs: number,
registrar: Registrar | null,
realm: ProgramAccount<Realm> | undefined
realm: ProgramAccount<Realm> | undefined,
isVested?: boolean
) => {
const mintCfgs = registrar?.votingMints
const mintCfg = mintCfgs?.find(
Expand All @@ -339,6 +342,7 @@ export const calcMintMultiplier = (
maxExtraLockupVoteWeightScaledFactor: maxExtraLockupVoteWeightScaledFactorNum,
lockupSaturationSecs: lockupSaturationSecsNum,
lockupSecs,
isVested,
})

return parseFloat(calced.toFixed(2))
Expand Down
38 changes: 27 additions & 11 deletions actions/castVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
VoteType,
withPostChatMessage,
withCreateTokenOwnerRecord,
getVoteRecordAddress,
} from '@solana/spl-governance'
import { ProgramAccount } from '@solana/spl-governance'
import { RpcContext } from '@solana/spl-governance'
Expand All @@ -39,6 +40,7 @@ import { findPluginName } from '@hooks/queries/governancePower'
import { DELEGATOR_BATCH_VOTE_SUPPORT_BY_PLUGIN } from '@constants/flags'
import { fetchTokenOwnerRecordByPubkey } from '@hooks/queries/tokenOwnerRecord'
import { fetchProgramVersion } from '@hooks/queries/useProgramVersionQuery'
import { fetchVoteRecordByPubkey } from '@hooks/queries/voteRecord'

const getVetoTokenMint = (
proposal: ProgramAccount<Proposal>,
Expand Down Expand Up @@ -259,19 +261,33 @@ export async function castVote(
DELEGATOR_BATCH_VOTE_SUPPORT_BY_PLUGIN[
findPluginName(votingPlugin?.client?.program.programId)
]
? await Promise.all(
additionalTokenOwnerRecords.map((tokenOwnerRecordPk) =>
createDelegatorVote({
connection,
realmPk: realm.pubkey,
proposalPk: proposal.pubkey,
tokenOwnerRecordPk,
userPk: walletPubkey,
vote,
votingPlugin: votingPlugin,
? (
await Promise.all(
additionalTokenOwnerRecords.map(async (tokenOwnerRecordPk) => {
// Skip vote if already voted
const voteRecordPk = await getVoteRecordAddress(
realm.owner,
proposal.pubkey,
tokenOwnerRecordPk
)
const voteRecord = await fetchVoteRecordByPubkey(
connection,
voteRecordPk
)
if (voteRecord.found) return undefined

return createDelegatorVote({
connection,
realmPk: realm.pubkey,
proposalPk: proposal.pubkey,
tokenOwnerRecordPk,
userPk: walletPubkey,
vote,
votingPlugin: votingPlugin,
})
})
)
)
).filter((x): x is NonNullable<typeof x> => x !== undefined)
: []

const pluginPostMessageIxs: TransactionInstruction[] = []
Expand Down
13 changes: 7 additions & 6 deletions actions/createProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,20 @@ export const createProposal = async (

const voteType = isMulti
? VoteType.MULTI_CHOICE(
MultiChoiceType.FullWeight,
1,
options.length,
options.length
)
MultiChoiceType.FullWeight,
1,
options.length,
options.length
)
: VoteType.SINGLE_CHOICE

//will run only if plugin is connected with realm
const plugin = await client?.withUpdateVoterWeightRecord(
instructions,
tokenOwnerRecord.pubkey,
'createProposal',
createNftTicketsIxs
createNftTicketsIxs,
governance
)

const proposalAddress = await withCreateProposal(
Expand Down
Loading

0 comments on commit d586283

Please sign in to comment.