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

Repair marker sizes and add close expired vote #446

Merged
merged 5 commits into from
Oct 17, 2023
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exclude = [
[workspace.dependencies]
anchor-lang = { version = "0.28.0", features = ["init-if-needed"] }
anchor-spl = { version = "0.28.0", features = ["mint", "token"] }
mpl-token-metadata = { version = "3.0.1" }
mpl-token-metadata = { version = "3.1.0" }
account-compression-cpi = { rev = "a2d12e4a157d91feb73aa40a97fe297fd477dd87", git = "https://github.com/helium/account-compression-anchor-gen.git", features = ["cpi"]}
bubblegum-cpi = { rev = "a2d12e4a157d91feb73aa40a97fe297fd477dd87", git = "https://github.com/helium/account-compression-anchor-gen.git", features = ["cpi"]}
solana-security-txt = "1.1.1"
Expand Down
4 changes: 4 additions & 0 deletions packages/crons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,19 @@
"@helium/helium-sub-daos-sdk": "^0.4.0",
"@helium/lazy-distributor-sdk": "^0.4.0",
"@helium/mobile-entity-manager-sdk": "^0.4.0",
"@helium/organization-sdk": "^0.0.5",
bryzettler marked this conversation as resolved.
Show resolved Hide resolved
"@helium/price-oracle-sdk": "^0.4.0",
"@helium/proposal-sdk": "^0.0.5",
"@helium/rewards-oracle-sdk": "^0.4.0",
"@helium/spl-utils": "^0.4.0",
"@helium/state-controller-sdk": "^0.0.5",
"@helium/treasury-management-sdk": "^0.4.0",
"@solana/spl-token": "^0.3.8",
"@solana/web3.js": "^1.78.4",
"axios": "^1.3.6",
"bn.js": "^5.2.0",
"bs58": "^4.0.1",
"p-limit": "3.1.0",
"yargs": "^17.7.1"
},
"devDependencies": {
Expand Down
113 changes: 113 additions & 0 deletions packages/crons/src/close-governance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import * as anchor from "@coral-xyz/anchor";
import { AccountFetchCache, chunks } from "@helium/account-fetch-cache";
import {
init as initOrg,
organizationKey,
proposalKey,
} from "@helium/organization-sdk";
import { init as initProposal } from "@helium/proposal-sdk";
import { bulkSendTransactions } from "@helium/spl-utils";
import { init as initState } from "@helium/state-controller-sdk";
import { init as initVsr, positionKey } from "@helium/voter-stake-registry-sdk";
import { SystemProgram, Transaction } from "@solana/web3.js";
import pLimit from "p-limit";

(async () => {
try {
if (!process.env.ANCHOR_WALLET)
throw new Error("ANCHOR_WALLET not provided");

if (!process.env.SOLANA_URL) throw new Error("SOLANA_URL not provided");

process.env.ANCHOR_PROVIDER_URL = process.env.SOLANA_URL;
anchor.setProvider(anchor.AnchorProvider.local(process.env.SOLANA_URL));

const provider = anchor.getProvider() as anchor.AnchorProvider;
new AccountFetchCache({
connection: provider.connection,
commitment: "confirmed",
extendConnection: true,
});
const orgProgram = await initOrg(provider);
const stateProgram = await initState(provider);
const proposalProgram = await initProposal(provider);
const vsrProgram = await initVsr(provider);
for (const orgName of ["Helium", "Helium MOBILE", "Helium IOT"]) {
console.log(`Checking for expired proposals in ${orgName}`);
const organizationK = organizationKey(orgName)[0];
const organization =
await orgProgram.account.organizationV0.fetchNullable(organizationK);
if (!organization) {
continue;
}
const proposalKeys = Array(organization?.numProposals)
.fill(0)
.map((_, index) => proposalKey(organizationK, index)[0])
.reverse();

const proposals = await Promise.all(
proposalKeys.map(async (p) => ({
account: await proposalProgram.account.proposalV0.fetch(p),
pubkey: p,
}))
);
const openProposals = proposals.filter(
(p) => typeof p.account.state.voting !== "undefined"
);

const resolveIxs = await Promise.all(
openProposals.map(async (p) => {
return await stateProgram.methods
.resolveV0()
.accounts({
proposal: p.pubkey,
})
.instruction();
})
);

const txs = chunks(resolveIxs, 10).map((ixs) => {
const tx = new Transaction({
feePayer: provider.wallet.publicKey,
});
tx.add(...ixs);
return tx;
});

await bulkSendTransactions(provider, txs);
}

const markers = (await vsrProgram.account.voteMarkerV0.all()).filter(
(m) => !m.account.relinquished
);
const limit = pLimit(100);
const relinquishIxns = await Promise.all(
markers.map((marker) =>
limit(async () => {
return await vsrProgram.methods
.relinquishExpiredVoteV0()
.accountsStrict({
marker: marker.publicKey,
position: positionKey(marker.account.mint)[0],
proposal: marker.account.proposal,
systemProgram: SystemProgram.programId,
})
.instruction();
})
)
);
const txns = chunks(relinquishIxns, 10).map((ixs) => {
const tx = new Transaction({
feePayer: provider.wallet.publicKey,
});
tx.add(...ixs);
return tx;
});
await bulkSendTransactions(provider, txns);

process.exit(0);
} catch (err) {
console.log(err);
process.exit(1);
}
})();
62 changes: 62 additions & 0 deletions packages/helium-admin-cli/src/temp-repair-marker-sizes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
currentEpoch,
daoEpochInfoKey,
daoKey,
EPOCH_LENGTH,
init as initDao,
subDaoEpochInfoKey,
subDaoKey,
} from "@helium/helium-sub-daos-sdk";
import * as anchor from "@coral-xyz/anchor";
import {
ComputeBudgetProgram,
ConfirmedSignatureInfo,
Connection,
PublicKey,
} from "@solana/web3.js";
import { BN } from "bn.js";
import b58 from "bs58";
import os from "os";
import yargs from "yargs/yargs";
import { IOT_MINT, MOBILE_MINT } from "@helium/spl-utils";
import { init } from "@helium/voter-stake-registry-sdk";

export async function run(args: any = process.argv) {
const yarg = yargs(args).options({
wallet: {
alias: "k",
describe: "Anchor wallet keypair",
default: `${os.homedir()}/.config/solana/id.json`,
},
url: {
alias: "u",
default: "http://127.0.0.1:8899",
describe: "The solana url",
},
});

const argv = await yarg.argv;
process.env.ANCHOR_WALLET = argv.wallet;
process.env.ANCHOR_PROVIDER_URL = argv.url;
anchor.setProvider(anchor.AnchorProvider.local(argv.url));

const provider = anchor.getProvider() as anchor.AnchorProvider;
const voterStakeRegistryProgram = await init(provider);
const markers = await voterStakeRegistryProgram.account.voteMarkerV0.all();

console.log(`${markers.length} total`)
let i = 0;
for (const marker of markers) {
i++
await voterStakeRegistryProgram.methods
.repairVoteMarkerSizes()
.accounts({
marker: marker.publicKey,
voter: marker.account.voter,
payer: provider.wallet.publicKey,
})
.rpc({ skipPreflight: true });
console.log(`Closed ${i}/${markers.length}`);
}
console.log("Done")
}
4 changes: 4 additions & 0 deletions programs/voter-stake-registry/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ pub use deposit_v0::*;
pub use initialize_position_v0::*;
pub use initialize_registrar_v0::*;
pub use ledger_transfer_position_v0::*;
pub use relinquish_expired_vote_v0::*;
pub use relinquish_vote_v0::*;
pub use relinquish_vote_v1::*;
pub use repair_vote_marker_sizes::*;
pub use reset_lockup_v0::*;
pub use set_time_offset_v0::*;
pub use transfer_v0::*;
Expand All @@ -23,8 +25,10 @@ pub mod deposit_v0;
pub mod initialize_position_v0;
pub mod initialize_registrar_v0;
pub mod ledger_transfer_position_v0;
pub mod relinquish_expired_vote_v0;
pub mod relinquish_vote_v0;
pub mod relinquish_vote_v1;
pub mod repair_vote_marker_sizes;
pub mod reset_lockup_v0;
pub mod set_time_offset_v0;
pub mod transfer_v0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use anchor_lang::prelude::*;
use proposal::{ProposalState, ProposalV0};

use crate::state::*;

// Allow anyone to permissionlessly close expired votes and refund to user
#[derive(Accounts)]
pub struct RelinquishExpiredVoteV0<'info> {
#[account(
mut,
seeds = [b"marker", marker.mint.as_ref(), proposal.key().as_ref()],
bump = marker.bump_seed,
has_one = proposal,
)]
pub marker: Box<Account<'info, VoteMarkerV0>>,
#[account(
mut,
constraint = position.mint == marker.mint
)]
pub position: Box<Account<'info, PositionV0>>,
#[account(
mut,
constraint = !matches!(proposal.state, ProposalState::Voting { .. })
)]
pub proposal: Account<'info, ProposalV0>,
pub system_program: Program<'info, System>,
}

pub fn handler(ctx: Context<RelinquishExpiredVoteV0>) -> Result<()> {
ctx.accounts.position.num_active_votes -= ctx.accounts.marker.choices.len() as u16;
ctx.accounts.marker.relinquished = true;

Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use proposal::ProposalV0;
use std::str::FromStr;

use anchor_lang::prelude::*;

use crate::state::*;

#[derive(Accounts)]
pub struct RepairVoteMarkerSizes<'info> {
#[account(
mut,
address = Pubkey::from_str(
"hprdnjkbziK8NqhThmAn5Gu4XqrBbctX8du4PfJdgvW"
).unwrap()
)]
pub payer: Signer<'info>,
#[account(
mut,
has_one = voter,
has_one = proposal,
)]
pub marker: Box<Account<'info, VoteMarkerV0>>,
pub proposal: Box<Account<'info, ProposalV0>>,
/// CHECK: Just refunding
#[account(mut)]
pub voter: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}

pub fn handler(ctx: Context<RepairVoteMarkerSizes>) -> Result<()> {
let new_size =
8 + 32 + std::mem::size_of::<VoteMarkerV0>() + 1 + 2 * ctx.accounts.proposal.choices.len();

ctx
.accounts
.marker
.to_account_info()
.realloc(new_size, false)?;

Ok(())
}
2 changes: 1 addition & 1 deletion programs/voter-stake-registry/src/instructions/vote_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct VoteV0<'info> {
#[account(
init_if_needed,
payer = payer,
space = 8 + 60 + std::mem::size_of::<Registrar>(),
space = 8 + 32 + std::mem::size_of::<VoteMarkerV0>() + 1 + 2 * proposal.choices.len(),
seeds = [b"marker", mint.key().as_ref(), proposal.key().as_ref()],
bump
)]
Expand Down
8 changes: 8 additions & 0 deletions programs/voter-stake-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,12 @@ pub mod voter_stake_registry {
) -> Result<()> {
instructions::relinquish_vote_v1::handler(ctx, args)
}

pub fn repair_vote_marker_sizes(ctx: Context<RepairVoteMarkerSizes>) -> Result<()> {
instructions::repair_vote_marker_sizes::handler(ctx)
}

pub fn relinquish_expired_vote_v0(ctx: Context<RelinquishExpiredVoteV0>) -> Result<()> {
instructions::relinquish_expired_vote_v0::handler(ctx)
}
}
2 changes: 2 additions & 0 deletions programs/voter-stake-registry/src/state/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ pub struct VoteMarkerV0 {
pub choices: Vec<u16>,
pub weight: u128,
pub bump_seed: u8,
/// Whether this vote has been cleared on the position after proposal expireds
pub relinquished: bool,
}
Loading
Loading