fix: use optional for council mint

This commit is contained in:
Sebastian.Bor 2021-04-16 10:27:37 +01:00
parent 46c0c69e68
commit c33bf0e11e
10 changed files with 43 additions and 32 deletions

View File

@ -45,7 +45,7 @@ export const createProposal = async (
connection,
useGovernance
? timelockConfig.info.governanceMint
: timelockConfig.info.councilMint,
: timelockConfig.info.councilMint!,
)
).decimals;
@ -135,7 +135,7 @@ export const createProposal = async (
sourceHoldingAccount,
useGovernance
? timelockConfig.info.governanceMint
: timelockConfig.info.councilMint,
: timelockConfig.info.councilMint!,
authority,
description,
name,
@ -342,7 +342,7 @@ async function getAssociatedAccountsAndInstructions(
accountRentExempt,
useGovernance
? timelockConfig.info.governanceMint
: timelockConfig.info.councilMint,
: timelockConfig.info.councilMint!,
authority,
holdingSigners,
);

View File

@ -45,7 +45,7 @@ export const mintSourceTokens = async (
accountRentExempt,
useGovernance
? timelockConfig.info.governanceMint
: timelockConfig.info.councilMint,
: timelockConfig.info.councilMint!,
e.owner,
signers,
);
@ -55,7 +55,7 @@ export const mintSourceTokens = async (
PROGRAM_IDS.token,
useGovernance
? timelockConfig.info.governanceMint
: timelockConfig.info.councilMint,
: timelockConfig.info.councilMint!,
e.sourceAccount,
wallet.publicKey,
[],

View File

@ -26,6 +26,7 @@ export const registerProgramGovernance = async (
connection: Connection,
wallet: any,
uninitializedTimelockConfig: Partial<TimelockConfig>,
useCouncil: boolean,
): Promise<PublicKey> => {
const PROGRAM_IDS = utils.programIds();
let signers: Account[] = [];
@ -43,7 +44,7 @@ export const registerProgramGovernance = async (
if (!uninitializedTimelockConfig.program)
uninitializedTimelockConfig.program = new Account().publicKey; // Random generation if none given
if (!uninitializedTimelockConfig.councilMint) {
if (!uninitializedTimelockConfig.councilMint && useCouncil) {
// Initialize the mint, an account for the admin, and give them one council token
// to start their lives with.
uninitializedTimelockConfig.councilMint = createMint(
@ -111,11 +112,15 @@ export const registerProgramGovernance = async (
);
}
const council_mint_seed = uninitializedTimelockConfig.councilMint
? uninitializedTimelockConfig.councilMint.toBuffer()
: Buffer.from('');
const [timelockConfigKey] = await PublicKey.findProgramAddress(
[
PROGRAM_IDS.timelock.programId.toBuffer(),
uninitializedTimelockConfig.governanceMint.toBuffer(),
uninitializedTimelockConfig.councilMint.toBuffer(),
council_mint_seed,
uninitializedTimelockConfig.program.toBuffer(),
],
PROGRAM_IDS.timelock.programId,
@ -126,8 +131,8 @@ export const registerProgramGovernance = async (
timelockConfigKey,
uninitializedTimelockConfig.program,
uninitializedTimelockConfig.governanceMint,
uninitializedTimelockConfig.councilMint,
wallet.publicKey,
uninitializedTimelockConfig.councilMint,
),
);
instructions.push(
@ -135,7 +140,7 @@ export const registerProgramGovernance = async (
timelockConfigKey,
uninitializedTimelockConfig.program,
uninitializedTimelockConfig.governanceMint,
uninitializedTimelockConfig.councilMint,
uninitializedTimelockConfig.consensusAlgorithm ||
ConsensusAlgorithm.Majority,
uninitializedTimelockConfig.executionType || ExecutionType.Independent,
@ -145,6 +150,7 @@ export const registerProgramGovernance = async (
uninitializedTimelockConfig.minimumSlotWaitingPeriod || new BN(0),
uninitializedTimelockConfig.timeLimit || new BN(0),
uninitializedTimelockConfig.name || '',
uninitializedTimelockConfig.councilMint,
),
);

View File

@ -33,7 +33,7 @@ export default function MintSourceTokens({
const connection = useConnection();
const mintKey = useGovernance
? timelockConfig.info.governanceMint
: timelockConfig.info.councilMint;
: timelockConfig.info.councilMint!;
const mint = useMint(mintKey);
const [saving, setSaving] = useState(false);

View File

@ -13,16 +13,16 @@ import * as Layout from '../utils/layout';
/// program account key, governance mint key, council mint key, and timelock program account key.
/// 1. `[]` Program account to tie this config to.
/// 2. `[]` Governance mint to tie this config to
/// 3. `[]` Council mint [optional] to tie this config to [Pass in 0s otherwise]
/// 4. `[]` Payer
/// 6. `[]` Timelock program pub key.
/// 7. `[]` System account.
/// 3. `[]` Payer
/// 4. `[]` Timelock program pub key.
/// 5. `[]` System account.
/// 6. `[]` Council mint [optional] to tie this config to [Optional]
export const createEmptyTimelockConfigInstruction = (
timelockConfigAccount: PublicKey,
programAccount: PublicKey,
governanceMint: PublicKey,
councilMint: PublicKey,
payer: PublicKey,
councilMint?: PublicKey,
): TransactionInstruction => {
const PROGRAM_IDS = utils.programIds();
@ -41,7 +41,6 @@ export const createEmptyTimelockConfigInstruction = (
{ pubkey: timelockConfigAccount, isSigner: false, isWritable: false },
{ pubkey: programAccount, isSigner: false, isWritable: false },
{ pubkey: governanceMint, isSigner: false, isWritable: false },
{ pubkey: councilMint, isSigner: false, isWritable: false },
{ pubkey: payer, isSigner: true, isWritable: false },
{
@ -51,6 +50,11 @@ export const createEmptyTimelockConfigInstruction = (
},
{ pubkey: PROGRAM_IDS.system, isSigner: false, isWritable: false },
];
if (councilMint) {
keys.push({ pubkey: councilMint, isSigner: false, isWritable: false });
}
return new TransactionInstruction({
keys,
programId: PROGRAM_IDS.timelock.programId,

View File

@ -13,12 +13,11 @@ import * as Layout from '../utils/layout';
/// program account key, governance mint key, council mint key, timelock program account key.
/// 1. `[]` Program account that this config uses
/// 2. `[]` Governance mint that this config uses
/// 3. `[]` Council mint that this config uses [Optional] [Pass in 0s otherwise]
/// 3. `[]` Council mint that this config uses [Optional]
export const initTimelockConfigInstruction = (
timelockConfigAccount: PublicKey,
programAccount: PublicKey,
governanceMint: PublicKey,
councilMint: PublicKey,
consensusAlgorithm: number,
executionType: number,
timelockType: number,
@ -26,6 +25,7 @@ export const initTimelockConfigInstruction = (
minimumSlotWaitingPeriod: BN,
timeLimit: BN,
name: string,
councilMint?: PublicKey,
): TransactionInstruction => {
const PROGRAM_IDS = utils.programIds();
@ -69,8 +69,12 @@ export const initTimelockConfigInstruction = (
{ pubkey: timelockConfigAccount, isSigner: false, isWritable: true },
{ pubkey: programAccount, isSigner: false, isWritable: false },
{ pubkey: governanceMint, isSigner: false, isWritable: false },
{ pubkey: councilMint, isSigner: false, isWritable: false },
];
if (councilMint) {
keys.push({ pubkey: councilMint, isSigner: false, isWritable: false });
}
return new TransactionInstruction({
keys,
programId: PROGRAM_IDS.timelock.programId,

View File

@ -10,8 +10,6 @@ export const CONFIG_NAME_LENGTH = 32;
export const INSTRUCTION_LIMIT = 450;
export const TRANSACTION_SLOTS = 5;
export const TEMP_FILE_TXN_SIZE = 1000;
// Key chosen to represent an unused key, a dummy empty. Points to system program.
export const ZERO_KEY = '11111111111111111111111111111111';
export enum TimelockInstruction {
InitTimelockSet = 1,
@ -71,7 +69,7 @@ export interface TimelockConfig {
/// Governance mint
governanceMint: PublicKey;
/// Council mint (Optional)
councilMint: PublicKey;
councilMint?: PublicKey;
/// Program ID that is tied to this config (optional)
program: PublicKey;
/// Time limit in slots for proposal to be open to voting
@ -91,12 +89,13 @@ export const TimelockConfigLayout: typeof BufferLayout.Structure = BufferLayout.
BufferLayout.u8('votingEntryRule'),
Layout.uint64('minimumSlotWaitingPeriod'),
Layout.publicKey('governanceMint'),
BufferLayout.u8('councilMintOption'),
Layout.publicKey('councilMint'),
Layout.publicKey('program'),
Layout.uint64('timeLimit'),
BufferLayout.seq(BufferLayout.u8(), CONFIG_NAME_LENGTH, 'name'),
BufferLayout.u32('count'),
BufferLayout.seq(BufferLayout.u8(), 296, 'padding'),
BufferLayout.seq(BufferLayout.u8(), 295, 'padding'),
],
);
@ -428,7 +427,7 @@ export const TimelockConfigParser = (
votingEntryRule: data.votingEntryRule,
minimumSlotWaitingPeriod: data.minimumSlotWaitingPeriod,
governanceMint: data.governanceMint,
councilMint: data.councilMint,
councilMint: data.councilMintOption == 1 ? data.councilMint : null,
program: data.program,
timeLimit: data.timeLimit,
name: utils.fromUTF8Array(data.name).replaceAll('\u0000', ''),

View File

@ -16,7 +16,6 @@ import {
TimelockConfig,
TimelockType,
VotingEntryRule,
ZERO_KEY,
} from '../../models/timelock';
import { PublicKey } from '@solana/web3.js';
import { Table } from 'antd';
@ -91,7 +90,7 @@ const columns = [
render: (config: ParsedAccount<TimelockConfig>) => (
<>
<MintSourceTokens timelockConfig={config} useGovernance={true} />
{config.info.councilMint.toBase58() != ZERO_KEY && (
{config.info.councilMint && (
<MintSourceTokens timelockConfig={config} useGovernance={false} />
)}
</>

View File

@ -8,7 +8,6 @@ import {
ExecutionType,
TimelockType,
VotingEntryRule,
ZERO_KEY,
} from '../../models/timelock';
import { LABELS } from '../../constants';
import { contexts, utils, tryParseKey } from '@oyster/common';
@ -132,9 +131,8 @@ export function NewForm({
: undefined,
councilMint: values.councilMint
? new PublicKey(values.councilMint)
: councilVisible
? undefined // if visible but empty, set undefined so we instantiate one
: new PublicKey(ZERO_KEY), // default empty case, just make it padding since user doesnt want one.
: undefined,
program: new PublicKey(values.program),
name: values.name,
timeLimit: new BN(values.timeLimit),
@ -144,6 +142,7 @@ export function NewForm({
connection,
wallet.wallet,
uninitializedConfig,
councilVisible,
);
handleOk(newConfig);
};

View File

@ -2,7 +2,7 @@ import React, { useState } from 'react';
import { Button, ButtonProps, Modal, Radio } from 'antd';
import { Form, Input, Select } from 'antd';
import { Account } from '@solana/web3.js';
import { DESC_SIZE, NAME_SIZE, ZERO_KEY } from '../../models/timelock';
import { DESC_SIZE, NAME_SIZE } from '../../models/timelock';
import { LABELS } from '../../constants';
import { contexts, utils } from '@oyster/common';
import { createProposal } from '../../actions/createProposal';
@ -80,7 +80,7 @@ export function NewForm({
if (
values.proposalMintType === ProposalMintType.Council &&
config.info.councilMint.toBase58() === ZERO_KEY
!config.info.councilMint
) {
notify({
message: LABELS.THIS_CONFIG_LACKS_COUNCIL,