Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/max mango accounts inst #213

Merged
merged 8 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions hooks/useGovernanceAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {
import { Instructions } from '@utils/uiTypes/proposalCreationTypes'
import useWalletStore from 'stores/useWalletStore'
import useRealm from './useRealm'

export default function useGovernanceAssets() {
const { governances, tokenMints, realmTokenAccounts } = useRealm()
const connection = useWalletStore((s) => s.connection.current)
const { ownVoterWeight, realm } = useRealm()

const { ownVoterWeight, realm, symbol } = useRealm()
const governancesArray = Object.keys(governances)
.filter((gpk) => !HIDDEN_GOVERNANCES.has(gpk))
.map((key) => governances[key])
Expand Down Expand Up @@ -94,6 +94,11 @@ export default function useGovernanceAssets() {
name: 'Execute Custom Instruction',
isVisible: canUseAnyInstruction,
},
{
id: Instructions.MangoMakeChangeMaxAccounts,
name: 'Mango - change max accounts',
isVisible: canUseProgramUpgradeInstruction && symbol === 'MNGO',
},
{
id: Instructions.None,
name: 'None',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import React, { useContext, useEffect, useState } from 'react'
import useRealm from '@hooks/useRealm'
import { PublicKey } from '@solana/web3.js'
import * as yup from 'yup'
import { isFormValid } from '@utils/formValidation'
import {
UiInstruction,
MangoMakeChangeMaxAccountsForm,
} from '@utils/uiTypes/proposalCreationTypes'
import { NewProposalContext } from '../../../new'
import useGovernanceAssets from '@hooks/useGovernanceAssets'
import { Governance, GovernanceAccountType } from '@models/accounts'
import { ParsedAccount } from '@models/core/accounts'
import useWalletStore from 'stores/useWalletStore'
import { serializeInstructionToBase64 } from '@models/serialisation'
import Input from '@components/inputs/Input'
import GovernedAccountSelect from '../../GovernedAccountSelect'
import { GovernedMultiTypeAccount } from '@utils/tokens'
import { makeChangeMaxMangoAccountsInstruction } from '@blockworks-foundation/mango-client'
import { BN } from '@project-serum/anchor'

const MakeChangeMaxAccounts = ({
index,
governance,
}: {
index: number
governance: ParsedAccount<Governance> | null
}) => {
const wallet = useWalletStore((s) => s.current)
const { realmInfo } = useRealm()
const { getGovernancesByAccountType } = useGovernanceAssets()
const governedProgramAccounts = getGovernancesByAccountType(
GovernanceAccountType.ProgramGovernance
).map((x) => {
return {
governance: x,
}
})
const shouldBeGoverned = index !== 0 && governance
const programId: PublicKey | undefined = realmInfo?.programId
const [form, setForm] = useState<MangoMakeChangeMaxAccountsForm>({
governedAccount: undefined,
programId: programId?.toString(),
mangoGroupKey: undefined,
maxMangoAccounts: 1,
})
const [formErrors, setFormErrors] = useState({})
const { handleSetInstructions } = useContext(NewProposalContext)
const handleSetForm = ({ propertyName, value }) => {
setFormErrors({})
setForm({ ...form, [propertyName]: value })
}
const validateInstruction = async (): Promise<boolean> => {
const { isValid, validationErrors } = await isFormValid(schema, form)
setFormErrors(validationErrors)
return isValid
}
async function getInstruction(): Promise<UiInstruction> {
const isValid = await validateInstruction()
let serializedInstruction = ''
if (
isValid &&
programId &&
form.governedAccount?.governance?.info &&
wallet?.publicKey
) {
//Mango instruction call and serialize
const setMaxMangoAccountsInstr = makeChangeMaxMangoAccountsInstruction(
form.governedAccount.governance.info.governedAccount,
new PublicKey(form.mangoGroupKey!),
form.governedAccount.governance.pubkey,
new BN(form.maxMangoAccounts)
)

serializedInstruction = serializeInstructionToBase64(
setMaxMangoAccountsInstr
)
}
const obj: UiInstruction = {
serializedInstruction: serializedInstruction,
isValid,
governance: form.governedAccount?.governance,
}
return obj
}
useEffect(() => {
handleSetForm({
propertyName: 'programId',
value: programId?.toString(),
})
}, [realmInfo?.programId])

useEffect(() => {
handleSetInstructions(
{ governedAccount: form.governedAccount?.governance, getInstruction },
index
)
}, [form])
const schema = yup.object().shape({
bufferAddress: yup.number(),
governedAccount: yup
.object()
.nullable()
.required('Program governed account is required'),
})

return (
<>
{/* if you need more fields add theme to interface MangoMakeChangeMaxAccountsForm
then you can add inputs in here */}
<GovernedAccountSelect
label="Program"
governedAccounts={governedProgramAccounts as GovernedMultiTypeAccount[]}
onChange={(value) => {
handleSetForm({ value, propertyName: 'governedAccount' })
}}
value={form.governedAccount}
error={formErrors['governedAccount']}
shouldBeGoverned={shouldBeGoverned}
governance={governance}
></GovernedAccountSelect>
<Input
label="Mango group key"
value={form.mangoGroupKey}
type="text"
onChange={(evt) =>
handleSetForm({
value: evt.target.value,
propertyName: 'mangoGroupKey',
})
}
error={formErrors['mangoGroupKey']}
/>
<Input
label="Max accounts"
value={form.maxMangoAccounts}
type="number"
min={1}
onChange={(evt) =>
handleSetForm({
value: evt.target.value,
propertyName: 'maxMangoAccounts',
})
}
error={formErrors['maxMangoAccounts']}
/>
</>
)
}

export default MakeChangeMaxAccounts
8 changes: 8 additions & 0 deletions pages/dao/[symbol]/proposal/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import Empty from './components/instructions/Empty'
import Mint from './components/instructions/Mint'
import CustomBase64 from './components/instructions/CustomBase64'
import { getTimestampFromDays } from '@tools/sdk/units'
import MakeChangeMaxAccounts from './components/instructions/Mango/MakeChangeMaxAccounts'
import VoteBySwitch from './components/VoteBySwitch'

const schema = yup.object().shape({
Expand Down Expand Up @@ -284,6 +285,13 @@ const New = () => {
return <CustomBase64 index={idx} governance={governance}></CustomBase64>
case Instructions.None:
return <Empty index={idx} governance={governance}></Empty>
case Instructions.MangoMakeChangeMaxAccounts:
return (
<MakeChangeMaxAccounts
index={idx}
governance={governance}
></MakeChangeMaxAccounts>
)
default:
null
}
Expand Down
8 changes: 8 additions & 0 deletions utils/uiTypes/proposalCreationTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ export interface ProgramUpgradeForm {
bufferAddress: string
}

export interface MangoMakeChangeMaxAccountsForm {
governedAccount: GovernedProgramAccount | undefined
programId: string | undefined
mangoGroupKey: string | undefined
maxMangoAccounts: number
}

export interface Base64InstructionForm {
governedAccount: GovernedMultiTypeAccount | undefined
base64: string
Expand All @@ -59,6 +66,7 @@ export enum Instructions {
Mint,
Base64,
None,
MangoMakeChangeMaxAccounts,
}

export type createParams = [
Expand Down
15 changes: 5 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4088,9 +4088,9 @@ istanbul-reports@^3.0.2:
istanbul-lib-report "^3.0.0"

jayson@^3.4.4:
version "3.6.5"
resolved "https://registry.yarnpkg.com/jayson/-/jayson-3.6.5.tgz#e560bcad4daf098c7391f46ba8efc9d6f34a4102"
integrity sha512-wmOjX+eQcnCDyPF4KORomaIj9wj3h0B5VEbeD0+2VHfTfErB+h1zpR7oBkgCZp36AFjp3+a4CLz6U72BYpFHAw==
version "3.6.6"
resolved "https://registry.yarnpkg.com/jayson/-/jayson-3.6.6.tgz#189984f624e398f831bd2be8e8c80eb3abf764a1"
integrity sha512-f71uvrAWTtrwoww6MKcl9phQTC+56AopLyEenWvKVAIMz+q0oVGj6tenLZ7Z6UiPBkJtKLj4kt0tACllFQruGQ==
dependencies:
"@types/connect" "^3.4.33"
"@types/express-serve-static-core" "^4.17.9"
Expand All @@ -4105,7 +4105,7 @@ jayson@^3.4.4:
isomorphic-ws "^4.0.1"
json-stringify-safe "^5.0.1"
lodash "^4.17.20"
uuid "^3.4.0"
uuid "^8.3.2"
ws "^7.4.5"

jest-changed-files@^27.4.2:
Expand Down Expand Up @@ -7227,12 +7227,7 @@ util@0.12.4, util@^0.12.0:
safe-buffer "^5.1.2"
which-typed-array "^1.1.2"

uuid@^3.4.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==

uuid@^8.3.0:
uuid@^8.3.0, uuid@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
Expand Down