Skip to content

Commit

Permalink
check if decayed and only claim upto endTs (#649)
Browse files Browse the repository at this point in the history
* check if decayed and only claim upto endTs

* decayedEpoch

* Add smart contract changes for fix

* Bump cargo version

---------

Co-authored-by: Noah Prince <[email protected]>
  • Loading branch information
bryzettler and ChewingGlass authored May 22, 2024
1 parent 60225c1 commit 66f5242
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,23 @@ export const useClaimAllPositionsRewards = () => {
}, {} as Record<string, SubDao>);

for (const [idx, position] of positions.entries()) {
const { lockup } = position;
const lockupKind = Object.keys(lockup.kind)[0] as string;
const isConstant = lockupKind === "constant";
const isDecayed = !isConstant && lockup.endTs.lte(new BN(unixNow));
const decayedEpoch = lockup.endTs.div(new BN(EPOCH_LENGTH));
const delegatedPosition = delegatedPositions[idx];
bucketedEpochsByPosition[position.pubkey.toBase58()] =
bucketedEpochsByPosition[position.pubkey.toBase58()] || [];
const { lastClaimedEpoch, claimedEpochsBitmap } =
delegatedPosition.account;
const epoch = lastClaimedEpoch.add(new BN(1));
const epochsToClaim = Array.from(
{ length: currentEpoch.sub(epoch).toNumber() },
{
length: !isDecayed
? currentEpoch.sub(epoch).toNumber()
: decayedEpoch.sub(epoch).toNumber(),
},
(_v, k) => epoch.addn(k)
).filter(
(epoch) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export const useClaimPositionRewards = () => {
if (isInvalid || !hsdProgram) {
throw new Error("Unable to Claim Rewards, Invalid params");
} else {
const { lockup } = position;
const lockupKind = Object.keys(lockup.kind)[0] as string;
const isConstant = lockupKind === "constant";
const isDecayed = !isConstant && lockup.endTs.lte(new BN(unixNow));
const decayedEpoch = lockup.endTs.div(new BN(EPOCH_LENGTH));
const currentEpoch = new BN(unixNow).div(new BN(EPOCH_LENGTH));
const delegatedPosKey = delegatedPositionKey(position.pubkey)[0];
const delegatedPosAcc =
Expand All @@ -54,7 +59,11 @@ export const useClaimPositionRewards = () => {
const { lastClaimedEpoch, claimedEpochsBitmap } = delegatedPosAcc;
const epoch = lastClaimedEpoch.add(new BN(1));
const epochsToClaim = Array.from(
{ length: currentEpoch.sub(epoch).toNumber() },
{
length: !isDecayed
? currentEpoch.sub(epoch).toNumber()
: decayedEpoch.sub(epoch).toNumber(),
},
(_v, k) => epoch.addn(k)
).filter(
(epoch) =>
Expand Down
2 changes: 1 addition & 1 deletion programs/helium-sub-daos/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "helium-sub-daos"
version = "0.1.5"
version = "0.1.6"
description = "Created with Anchor"
edition = "2021"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,16 @@ pub fn handler(ctx: Context<CloseDelegationV0>) -> Result<()> {
msg!("Vehnt calculations: {:?}", vehnt_info);

// don't allow unstake without claiming available rewards
// make sure to account for when the position ends
// unless we're testing, in which case we don't care
let curr_epoch = current_epoch(curr_ts);
assert!((ctx.accounts.delegated_position.last_claimed_epoch >= curr_epoch - 1) || TESTING);
let to_claim_to_epoch =
if position.lockup.end_ts < curr_ts && position.lockup.kind == LockupKind::Cliff {
current_epoch(position.lockup.end_ts)
} else {
curr_epoch - 1
};
assert!((ctx.accounts.delegated_position.last_claimed_epoch >= to_claim_to_epoch) || TESTING);

let delegated_position = &mut ctx.accounts.delegated_position;
let sub_dao = &mut ctx.accounts.sub_dao;
Expand Down

0 comments on commit 66f5242

Please sign in to comment.