Skip to content

Commit

Permalink
Add script to backfill expiration to delegations
Browse files Browse the repository at this point in the history
  • Loading branch information
ChewingGlass committed Dec 12, 2024
1 parent 0ef5b93 commit 307b01b
Show file tree
Hide file tree
Showing 19 changed files with 145 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/crons/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
1 change: 1 addition & 0 deletions packages/data-credits-sdk/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
1 change: 1 addition & 0 deletions packages/distributor-oracle/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
1 change: 1 addition & 0 deletions packages/entity-invalidator/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
127 changes: 127 additions & 0 deletions packages/helium-admin-cli/src/add-expiration-to-delegations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import * as anchor from "@coral-xyz/anchor";
import { daoKey, init as initHsd, subDaoEpochInfoKey } from "@helium/helium-sub-daos-sdk";
import { init as initProxy } from "@helium/nft-proxy-sdk";
import { batchParallelInstructionsWithPriorityFee, HNT_MINT } from "@helium/spl-utils";
import { init as initVsr } from "@helium/voter-stake-registry-sdk";
import { AccountInfo, PublicKey, SystemProgram, SYSVAR_CLOCK_PUBKEY, TransactionInstruction } from "@solana/web3.js";
import { min } from "bn.js";
import os from "os";
import yargs from "yargs/yargs";
import { loadKeypair } from "./utils";

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",
},
hntMint: {
type: "string",
describe: "HNT mint of the dao to be updated",
default: HNT_MINT.toBase58(),
},
});
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 wallet = new anchor.Wallet(loadKeypair(argv.wallet));
const proxyProgram = await initProxy(provider);
const vsrProgram = await initVsr(provider);
const hsdProgram = await initHsd(provider);


const hntMint = new PublicKey(argv.hntMint);
const dao = daoKey(hntMint)[0];
const registrarK = (await hsdProgram.account.daoV0.fetch(dao)).registrar;
const registrar = await vsrProgram.account.registrar.fetch(registrarK);
const proxyConfig = await proxyProgram.account.proxyConfigV0.fetch(
registrar.proxyConfig
);

const instructions: TransactionInstruction[] = [];
const delegations = await hsdProgram.account.delegatedPositionV0.all()
const needsMigration = delegations.filter(d => d.account.expirationTs.isZero());
const positionKeys = needsMigration.map(d => d.account.position);
const coder = hsdProgram.coder.accounts
const positionAccs = (await getMultipleAccounts({
connection: provider.connection,
keys: positionKeys,
})).map(a => coder.decode("positionV0", a.data));

const currTs = await getSolanaUnixTimestamp(provider);
const currTsBN = new anchor.BN(currTs.toString());
const proxyEndTs = proxyConfig.seasons.find(s => currTsBN.gt(s.start))?.end;
for (const [delegation, position] of zip(needsMigration, positionAccs)) {
const subDao = delegation.account.subDao;
instructions.push(
await hsdProgram.methods
.addExpirationTs()
.accountsStrict({
payer: wallet.publicKey,
position: delegation.account.position,
delegatedPosition: delegation.publicKey,
registrar: registrarK,
dao,
subDao: delegation.account.subDao,
oldClosingTimeSubDaoEpochInfo: subDaoEpochInfoKey(
subDao,
position.lockup.endTs
)[0],
closingTimeSubDaoEpochInfo: subDaoEpochInfoKey(
subDao,
min(position.lockup.endTs, proxyEndTs!)
)[0],
genesisEndSubDaoEpochInfo: subDaoEpochInfoKey(
subDao,
position.genesisEnd
)[0],
proxyConfig: registrar.proxyConfig,
systemProgram: SystemProgram.programId,
})
.instruction()
);
}

await batchParallelInstructionsWithPriorityFee(provider, instructions, {
onProgress: (status) => {
console.log(status);
},
});
}

async function getMultipleAccounts({
connection,
keys,
}): Promise<AccountInfo<Buffer>[]> {
const batchSize = 100;
const batches = Math.ceil(keys.length / batchSize);
const results: AccountInfo<Buffer>[] = [];

for (let i = 0; i < batches; i++) {
const batchKeys = keys.slice(i * batchSize, (i + 1) * batchSize);
const batchResults = await connection.getMultipleAccountsInfo(batchKeys);
results.push(...batchResults);
}

return results;
}

function zip<T, U>(a: T[], b: U[]): [T, U][] {
return a.map((_, i) => [a[i], b[i]]);
}

async function getSolanaUnixTimestamp(
provider: anchor.AnchorProvider
): Promise<bigint> {
const clock = await provider.connection.getAccountInfo(SYSVAR_CLOCK_PUBKEY);
const unixTime = clock!.data.readBigInt64LE(8 * 4);
return unixTime;
}
1 change: 1 addition & 0 deletions packages/helium-admin-cli/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
1 change: 1 addition & 0 deletions packages/helium-entity-manager-sdk/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
2 changes: 1 addition & 1 deletion packages/helium-sub-daos-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
"@coral-xyz/anchor": "^0.28.0",
"@helium/anchor-resolvers": "^0.9.18",
"@helium/circuit-breaker-sdk": "^0.9.18",
"@helium/nft-proxy-sdk": "^0.0.15",
"@helium/spl-utils": "^0.9.18",
"@helium/treasury-management-sdk": "^0.9.18",
"@helium/voter-stake-registry-sdk": "^0.9.18",
"@helium/nft-proxy-sdk": "^0.0.15",
"bn.js": "^5.2.0",
"bs58": "^4.0.1"
},
Expand Down
1 change: 1 addition & 0 deletions packages/helium-sub-daos-sdk/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
1 change: 1 addition & 0 deletions packages/hexboosting-sdk/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
1 change: 1 addition & 0 deletions packages/hotspot-utils/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
1 change: 1 addition & 0 deletions packages/metadata-service/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
1 change: 1 addition & 0 deletions packages/migration-service/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
1 change: 1 addition & 0 deletions packages/mobile-entity-manager-sdk/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
1 change: 1 addition & 0 deletions packages/monitor-service/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
1 change: 1 addition & 0 deletions packages/voter-stake-registry-hooks/yarn.deploy.lock
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{i64, str::FromStr};
use std::{cmp::min, str::FromStr};

use anchor_lang::prelude::*;
use nft_proxy::ProxyConfigV0;
Expand Down Expand Up @@ -57,7 +57,7 @@ pub struct AddExpirationTs<'info> {
payer = payer,
space = SubDaoEpochInfoV0::SIZE,
seeds = ["sub_dao_epoch_info".as_bytes(), sub_dao.key().as_ref(), &current_epoch(
proxy_config.get_current_season(registrar.clock_unix_timestamp()).unwrap().end
min(proxy_config.get_current_season(registrar.clock_unix_timestamp()).unwrap().end, position.lockup.end_ts)
).to_le_bytes()],
bump,
)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::cmp::min;

use anchor_lang::prelude::*;
use anchor_spl::token::{Mint, TokenAccount};
use nft_proxy::ProxyConfigV0;
use voter_stake_registry::{
state::{LockupKind, PositionV0, Registrar},
VoterStakeRegistry,
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,7 @@ __metadata:
"@coral-xyz/anchor": ^0.28.0
"@helium/anchor-resolvers": ^0.9.18
"@helium/circuit-breaker-sdk": ^0.9.18
"@helium/nft-proxy-sdk": ^0.0.15
"@helium/spl-utils": ^0.9.18
"@helium/treasury-management-sdk": ^0.9.18
"@helium/voter-stake-registry-sdk": ^0.9.18
Expand Down

0 comments on commit 307b01b

Please sign in to comment.