Skip to content

Commit

Permalink
More bugfixes for veHNT and 138
Browse files Browse the repository at this point in the history
  • Loading branch information
ChewingGlass committed Jan 9, 2025
1 parent 1eb631a commit 0e6eb78
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,20 @@ pub fn handler(ctx: Context<ExtendExpirationTsV0>) -> Result<()> {
.get_current_season(registrar.clock_unix_timestamp())
.unwrap()
.end;
ctx.accounts.delegated_position.expiration_ts = expiration_ts;
let epoch = current_epoch(registrar.clock_unix_timestamp());

// Calculate vehnt info once
msg!(
"Calculating vehnt info for old expiration ts {}",
ctx.accounts.delegated_position.expiration_ts
);
let vehnt_info_old = caclulate_vhnt_info(
ctx.accounts.delegated_position.start_ts,
position,
voting_mint_config,
i64::MAX,
ctx.accounts.delegated_position.expiration_ts,
)?;
ctx.accounts.delegated_position.expiration_ts = expiration_ts;
let vehnt_info_new = caclulate_vhnt_info(
ctx.accounts.delegated_position.start_ts,
position,
Expand Down Expand Up @@ -181,10 +185,14 @@ pub fn handler(ctx: Context<ExtendExpirationTsV0>) -> Result<()> {

let genesis_end_is_closing = ctx.accounts.genesis_end_sub_dao_epoch_info.key()
== ctx.accounts.closing_time_sub_dao_epoch_info.key();
if !ctx.accounts.genesis_end_sub_dao_epoch_info.data_len() == 0 {
let genesis_end_is_old_closing = ctx.accounts.genesis_end_sub_dao_epoch_info.key()
== ctx.accounts.old_closing_time_sub_dao_epoch_info.key();
if ctx.accounts.genesis_end_sub_dao_epoch_info.data_len() > 0 {
let mut parsed: Account<SubDaoEpochInfoV0>;
let genesis_end_sdei: &mut Account<SubDaoEpochInfoV0> = if genesis_end_is_closing {
&mut ctx.accounts.closing_time_sub_dao_epoch_info
} else if genesis_end_is_old_closing {
&mut ctx.accounts.old_closing_time_sub_dao_epoch_info
} else {
parsed = Account::try_from(
&ctx
Expand Down
2 changes: 1 addition & 1 deletion programs/helium-sub-daos/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ pub fn caclulate_vhnt_info(
.checked_add(i64::try_from(seconds_to_genesis).unwrap())
.unwrap(),
)?;
let vehnt_at_genesis_end_exact = if has_genesis && position.genesis_end < expiration_ts {
let vehnt_at_genesis_end_exact = if has_genesis {
position.voting_power_precise(voting_mint_config, position.genesis_end)?
} else {
position.voting_power_precise(voting_mint_config, curr_ts)?
Expand Down
130 changes: 81 additions & 49 deletions tests/helium-sub-daos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ describe("helium-sub-daos", () => {
daoKey(hntMint)[0],
genesisVotePowerMultiplierExpirationTs,
3,
proxySeasonEnd,
proxySeasonEnd
));

({
Expand Down Expand Up @@ -879,7 +879,9 @@ describe("helium-sub-daos", () => {
const preMobileBalance = AccountLayout.decode(
(await provider.connection.getAccountInfo(rewardsEscrow))?.data!
).amount;
const { pubkeys: { prevSubDaoEpochInfo, daoEpochInfo } } = await program.methods
const {
pubkeys: { prevSubDaoEpochInfo, daoEpochInfo },
} = await program.methods
.issueRewardsV0({
epoch,
})
Expand All @@ -888,9 +890,20 @@ describe("helium-sub-daos", () => {
})
.rpcAndKeys({ skipPreflight: true });

console.log("subDaoEpochInfo", await program.account.subDaoEpochInfoV0.fetch(subDaoEpochInfo!));
console.log("prevSubDaoEpochInfo", await program.account.subDaoEpochInfoV0.fetch(prevSubDaoEpochInfo!));
console.log("daoEpochInfo", await program.account.daoEpochInfoV0.fetch(daoEpochInfo!));
console.log(
"subDaoEpochInfo",
await program.account.subDaoEpochInfoV0.fetch(subDaoEpochInfo!)
);
console.log(
"prevSubDaoEpochInfo",
await program.account.subDaoEpochInfoV0.fetch(
prevSubDaoEpochInfo!
)
);
console.log(
"daoEpochInfo",
await program.account.daoEpochInfoV0.fetch(daoEpochInfo!)
);

const postBalance = AccountLayout.decode(
(await provider.connection.getAccountInfo(treasury))?.data!
Expand Down Expand Up @@ -1232,9 +1245,11 @@ describe("helium-sub-daos", () => {
});

it("allows adding expiration ts", async () => {
const registrarAcc = await vsrProgram.account.registrar.fetch(registrar);
const registrarAcc = await vsrProgram.account.registrar.fetch(
registrar
);
const proxyConfig = registrarAcc.proxyConfig;

({ position, vault } = await createPosition(
vsrProgram,
provider,
Expand All @@ -1248,7 +1263,9 @@ describe("helium-sub-daos", () => {
},
positionAuthorityKp
));
const { pubkeys: { closingTimeSubDaoEpochInfo, genesisEndSubDaoEpochInfo } } = await program.methods
const {
pubkeys: { closingTimeSubDaoEpochInfo, genesisEndSubDaoEpochInfo },
} = await program.methods
.delegateV0()
.accounts({
position,
Expand All @@ -1257,34 +1274,36 @@ describe("helium-sub-daos", () => {
})
.signers([positionAuthorityKp])
.rpcAndKeys({ skipPreflight: true });
const seasonEnd = new BN(
new Date().valueOf() / 1000 + EPOCH_LENGTH * 5
);
await proxyProgram.methods
.updateProxyConfigV0({
maxProxyTime: null,
seasons: [
{
start: new BN(0),
end: seasonEnd,
},
],
})
.accounts({
proxyConfig,
authority: me,
})
.rpc({ skipPreflight: true });
const seasonEnd = new BN(
new Date().valueOf() / 1000 + EPOCH_LENGTH * 5
);
await proxyProgram.methods
.updateProxyConfigV0({
maxProxyTime: null,
seasons: [
{
start: new BN(0),
end: seasonEnd,
},
],
})
.accounts({
proxyConfig,
authority: me,
})
.rpc({ skipPreflight: true });
const subDaoEpochInfo = await program.account.subDaoEpochInfoV0.fetch(
closingTimeSubDaoEpochInfo!
);
const expectedFallRates = subDaoEpochInfo.fallRatesFromClosingPositions.toString();
const expectedVehntInClosingPositions = subDaoEpochInfo.vehntInClosingPositions.toString();

const expectedFallRates =
subDaoEpochInfo.fallRatesFromClosingPositions.toString();
const expectedVehntInClosingPositions =
subDaoEpochInfo.vehntInClosingPositions.toString();
const newClosingTimeSubDaoEpochInfo = subDaoEpochInfoKey(
subDao,
seasonEnd
)[0]
)[0];

await program.methods
.extendExpirationTsV0()
.accounts({
Expand All @@ -1297,28 +1316,41 @@ describe("helium-sub-daos", () => {
.signers([positionAuthorityKp])
.rpc({ skipPreflight: true });

const oldSubDaoEpochInfo = await program.account.subDaoEpochInfoV0.fetch(closingTimeSubDaoEpochInfo!);
expect(oldSubDaoEpochInfo.fallRatesFromClosingPositions.toNumber()).to.eq(0);
expect(oldSubDaoEpochInfo.vehntInClosingPositions.toNumber()).to.eq(0);

const newSubDaoEpochInfo =
await program.account.subDaoEpochInfoV0.fetch(
newClosingTimeSubDaoEpochInfo!
);
expect(newSubDaoEpochInfo.fallRatesFromClosingPositions.toString()).to.eq(expectedFallRates);

const genesisEndEpoch = await program.account.subDaoEpochInfoV0.fetch(
genesisEndSubDaoEpochInfo!
console.log(
closingTimeSubDaoEpochInfo!.toBase58(),
newClosingTimeSubDaoEpochInfo!.toBase58()
);
const oldSubDaoEpochInfo =
await program.account.subDaoEpochInfoV0.fetch(
closingTimeSubDaoEpochInfo!
);
expect(genesisEndEpoch.fallRatesFromClosingPositions.toNumber()).to.eq(0);
expect(genesisEndEpoch.vehntInClosingPositions.toNumber()).to.eq(0);
expect(
oldSubDaoEpochInfo.fallRatesFromClosingPositions.toNumber()
).to.eq(0);
expect(oldSubDaoEpochInfo.vehntInClosingPositions.toNumber()).to.eq(0);

const newSubDaoEpochInfo =
await program.account.subDaoEpochInfoV0.fetch(
newClosingTimeSubDaoEpochInfo!
);
expect(
newSubDaoEpochInfo.fallRatesFromClosingPositions.toString()
).to.eq(expectedFallRates);

const genesisEndEpoch = await program.account.subDaoEpochInfoV0.fetch(
genesisEndSubDaoEpochInfo!
);
expect(genesisEndEpoch.fallRatesFromClosingPositions.toNumber()).to.eq(
0
);
expect(genesisEndEpoch.vehntInClosingPositions.toNumber()).to.eq(0);
});

describe("with proxy season that ends before genesis end", () => {
before(async () => {
// 15 days from now
proxySeasonEnd = new BN(
new Date().valueOf() / 1000 + (15 * EPOCH_LENGTH)
new Date().valueOf() / 1000 + 15 * EPOCH_LENGTH
);
});

Expand Down Expand Up @@ -1446,7 +1478,7 @@ describe("helium-sub-daos", () => {
subDaoEpochInfo = await getCurrEpochInfo();
let currTime = subDaoEpochInfo.epoch.toNumber() * EPOCH_LENGTH;
let timeStaked = currTime - stakeTime.toNumber();
let expected = 0
let expected = 0;
expect(toNumber(subDaoEpochInfo.vehntAtEpochStart, 8)).to.be.closeTo(
// Fall rates aren't a perfect measurement, we divide the total fall of the position by
// the total time staked. Imagine the total fall was 1 and the total time was 3. We would have
Expand All @@ -1461,7 +1493,7 @@ describe("helium-sub-daos", () => {
subDaoEpochInfo = await getCurrEpochInfo();
currTime = subDaoEpochInfo.epoch.toNumber() * EPOCH_LENGTH;
timeStaked = currTime - stakeTime.toNumber();
expected = 0
expected = 0;
expect(toNumber(subDaoEpochInfo.vehntAtEpochStart, 8)).to.be.closeTo(
expected,
0.0000001
Expand All @@ -1473,7 +1505,7 @@ describe("helium-sub-daos", () => {
subDaoEpochInfo = await getCurrEpochInfo();
currTime = subDaoEpochInfo.epoch.toNumber() * EPOCH_LENGTH;
timeStaked = currTime - stakeTime.toNumber();
expected = 0
expected = 0;
expect(toNumber(subDaoEpochInfo.vehntAtEpochStart, 8)).to.be.closeTo(
expected,
0.0000001
Expand All @@ -1488,7 +1520,7 @@ describe("helium-sub-daos", () => {
subDaoEpochInfo = await getCurrEpochInfo();
currTime = subDaoEpochInfo.epoch.toNumber() * EPOCH_LENGTH;
timeStaked = currTime - stakeTime.toNumber();
expected = 0
expected = 0;
expect(toNumber(subDaoEpochInfo.vehntAtEpochStart, 8)).to.be.closeTo(
expected,
0.0000001
Expand Down

0 comments on commit 0e6eb78

Please sign in to comment.