Holy medians batman

This commit is contained in:
Jordan Prince 2021-09-11 13:32:56 -05:00
parent ff339ed482
commit b2ebf9be87
3 changed files with 98 additions and 9 deletions

View File

@ -4,7 +4,11 @@ import { program } from 'commander';
import * as anchor from '@project-serum/anchor';
import { LAMPORTS_PER_SOL } from '@solana/web3.js';
import { Token } from '@solana/spl-token';
import { CACHE_PATH, TOKEN_PROGRAM_ID } from './helpers/constants';
import {
CACHE_PATH,
FAIR_LAUNCH_PROGRAM_ID,
TOKEN_PROGRAM_ID,
} from './helpers/constants';
import {
loadFairLaunchProgram,
loadWalletKey,
@ -278,6 +282,79 @@ program
},
signers: [],
});
console.log('Created seq');
});
program
.command('create_missing_sequences')
.option(
'-e, --env <string>',
'Solana cluster env name',
'devnet', //mainnet-beta, testnet, devnet
)
.option(
'-k, --keypair <path>',
`Solana wallet location`,
'--keypair not provided',
)
.option('-f, --fair-launch <string>', 'fair launch id')
.action(async (_, cmd) => {
const { env, keypair, fairLaunch } = cmd.opts();
const fairLaunchTicketSeqStart = 8 + 32 + 32 + 8 + 1 + 1;
const fairLaunchTicketState = 8 + 32 + 32 + 8;
const walletKeyPair = loadWalletKey(keypair);
const anchorProgram = await loadFairLaunchProgram(walletKeyPair, env);
const fairLaunchObj = await anchorProgram.account.fairLaunch.fetch(
fairLaunch,
);
const tickets = await anchorProgram.provider.connection.getProgramAccounts(
FAIR_LAUNCH_PROGRAM_ID,
{
filters: [
{
memcmp: {
offset: 8,
bytes: fairLaunch,
},
},
],
},
);
for (let i = 0; i < tickets.length; i++) {
const accountAndPubkey = tickets[i];
const { account, pubkey } = accountAndPubkey;
const state = account.data[fairLaunchTicketState];
if (state == 0) {
console.log('Missing sequence for ticket', pubkey.toBase58());
const [fairLaunchTicketSeqLookup, seqBump] =
await getFairLaunchTicketSeqLookup(
//@ts-ignore
fairLaunchObj.tokenMint,
new anchor.BN(
account.data.slice(
fairLaunchTicketSeqStart,
fairLaunchTicketSeqStart + 8,
),
undefined,
'le',
),
);
await anchorProgram.rpc.createTicketSeq(seqBump, {
accounts: {
fairLaunchTicketSeqLookup,
fairLaunch,
fairLaunchTicket: pubkey,
payer: walletKeyPair.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
},
signers: [],
});
console.log('Created...');
}
}
});
program
@ -292,17 +369,12 @@ program
`Solana wallet location`,
'--keypair not provided',
)
.option('-u, --uuid <string>', 'uuid')
.option('-f, --fair-launch <string>', 'fair launch id')
.action(async (options, cmd) => {
const { env, uuid, keypair } = cmd.opts();
const realUuid = uuid.slice(0, 6);
const { env, fairLaunch, keypair } = cmd.opts();
const walletKeyPair = loadWalletKey(keypair);
const anchorProgram = await loadFairLaunchProgram(walletKeyPair, env);
const tokenMint = (
await getTokenMint(walletKeyPair.publicKey, realUuid)
)[0];
const fairLaunch = (await getFairLaunch(tokenMint))[0];
const fairLaunchObj = await anchorProgram.account.fairLaunch.fetch(
fairLaunch,

View File

@ -133,7 +133,7 @@ export const getFairLaunchTicketSeqLookup = async (
seq: anchor.BN,
): Promise<[anchor.web3.PublicKey, number]> => {
return await anchor.web3.PublicKey.findProgramAddress(
[Buffer.from('fair_launch'), tokenMint.toBuffer(), seq.toBuffer()],
[Buffer.from('fair_launch'), tokenMint.toBuffer(), seq.toBuffer('le', 8)],
FAIR_LAUNCH_PROGRAM_ID,
);
};

View File

@ -127,13 +127,30 @@ pub fn adjust_counts(
let mut counter: u64 = 0;
let mut ticks: u64 = 0;
let mut last_seen_tick_value_with_positive_counts: u64 = 0;
for n in &fair_launch.counts_at_each_tick {
counter = counter
.checked_add(*n)
.ok_or(ErrorCode::NumericalOverflowError)?;
if counter > median_location {
if let Some(val) = median_location.checked_rem(2) {
if val == 0 {
let half_way = ticks
.checked_sub(last_seen_tick_value_with_positive_counts)
.ok_or(ErrorCode::NumericalOverflowError)?;
ticks = half_way
.checked_div(2)
.ok_or(ErrorCode::NumericalOverflowError)?;
ticks = last_seen_tick_value_with_positive_counts
.checked_add(ticks)
.ok_or(ErrorCode::NumericalOverflowError)?;
}
}
break;
}
if n > &0 {
last_seen_tick_value_with_positive_counts = ticks;
}
ticks = ticks
.checked_add(fair_launch.data.tick_size)
.ok_or(ErrorCode::NumericalOverflowError)?;