Skip to content

Commit

Permalink
fix: remove unlocking duration from sdk, tests, and wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvankhademi committed Aug 28, 2024
1 parent 6b1ff18 commit 16d0231
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 41 deletions.
24 changes: 5 additions & 19 deletions staking/app/StakeConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,7 @@ export class StakeConnection {
[wasm.PositionState.LOCKED, wasm.PositionState.LOCKING].includes(
stakeAccount.stakeAccountPositionsWasm.getPositionState(
el.index,
BigInt(currentEpoch.toString()),
this.config.unlockingDuration
BigInt(currentEpoch.toString())
)
)
)
Expand Down Expand Up @@ -1060,13 +1059,10 @@ export class StakeConnection {

const time = new BN(Date.now() / 1000);
const currentEpoch = time.div(this.config.epochDuration);
const unlockingDuration = this.config.unlockingDuration;
const currentEpochBI = BigInt(currentEpoch.toString());

const lockedBalanceSummary = positionAccountWasm.getLockedBalanceSummary(
currentEpochBI,
unlockingDuration
);
const lockedBalanceSummary =
positionAccountWasm.getLockedBalanceSummary(currentEpochBI);

const epochOfFirstStake: BN = positionAccountJs.positions.reduce(
(prev: BN | undefined, curr) => {
Expand Down Expand Up @@ -1250,15 +1246,11 @@ export class StakeAccount {
);

const currentEpoch = unixTime.div(this.config.epochDuration);
const unlockingDuration = this.config.unlockingDuration;
const currentEpochBI = BigInt(currentEpoch.toString());

const unvestedBN = new BN(unvestedBalance.toString());
const lockedSummaryBI =
this.stakeAccountPositionsWasm.getLockedBalanceSummary(
currentEpochBI,
unlockingDuration
);
this.stakeAccountPositionsWasm.getLockedBalanceSummary(currentEpochBI);

let lockingBN = new BN(lockedSummaryBI.locking.toString());
let lockedBN = new BN(lockedSummaryBI.locked.toString());
Expand Down Expand Up @@ -1372,11 +1364,9 @@ export class StakeAccount {

public getVoterWeight(unixTime: BN): PythBalance {
let currentEpoch = unixTime.div(this.config.epochDuration);
let unlockingDuration = this.config.unlockingDuration;

const voterWeightBI = this.stakeAccountPositionsWasm.getVoterWeight(
BigInt(currentEpoch.toString()),
unlockingDuration,
BigInt(
this.votingAccountMetadataWasm.getCurrentAmountLocked(
BigInt(currentEpoch.toString())
Expand Down Expand Up @@ -1430,11 +1420,7 @@ export class StakeAccount {
}

private addUnlockingPeriod(unixTime: BN) {
return unixTime.add(
this.config.epochDuration.mul(
new BN(this.config.unlockingDuration).add(new BN(1))
)
);
return unixTime.add(this.config.epochDuration.mul(new BN(2)));
}

public getNetExcessGovernanceAtVesting(unixTime: BN): BN {
Expand Down
14 changes: 4 additions & 10 deletions staking/programs/staking/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,20 @@ impl WasmPositionData {
&self,
index: u16,
current_epoch: u64,
unlocking_duration: u8,
) -> Result<PositionState, JsValue> {
convert_error(self.get_position_state_impl(index, current_epoch, unlocking_duration))
convert_error(self.get_position_state_impl(index, current_epoch))
}
fn get_position_state_impl(
&self,
index: u16,
current_epoch: u64,
unlocking_duration: u8,
) -> anchor_lang::Result<PositionState> {
let mut account = DynamicPositionArrayAccount::default_with_data(&self.wrapped);
account
.to_dynamic_position_array()
.read_position(index as usize)?
.ok_or_else(|| error!(ErrorCode::PositionNotInUse))?
.get_current_position(current_epoch, unlocking_duration)
.get_current_position(current_epoch)
}
#[wasm_bindgen(js_name=isPositionVoting)]
pub fn is_position_voting(&self, index: u16) -> Result<bool, JsValue> {
Expand All @@ -93,14 +91,12 @@ impl WasmPositionData {
pub fn get_locked_balance_summary(
&self,
current_epoch: u64,
unlocking_duration: u8,
) -> Result<LockedBalanceSummary, JsValue> {
convert_error(self.get_locked_balance_summary_impl(current_epoch, unlocking_duration))
convert_error(self.get_locked_balance_summary_impl(current_epoch))
}
fn get_locked_balance_summary_impl(
&self,
current_epoch: u64,
unlocking_duration: u8,
) -> anchor_lang::Result<LockedBalanceSummary> {
let mut account = DynamicPositionArrayAccount::default_with_data(&self.wrapped);
let positions = account.to_dynamic_position_array();
Expand All @@ -112,7 +108,7 @@ impl WasmPositionData {

for i in 0..positions.get_position_capacity() {
if let Some(position) = positions.read_position(i)? {
match position.get_current_position(current_epoch, unlocking_duration)? {
match position.get_current_position(current_epoch)? {
PositionState::LOCKING => {
locking = locking
.checked_add(position.amount)
Expand Down Expand Up @@ -149,14 +145,12 @@ impl WasmPositionData {
pub fn get_voter_weight(
&self,
current_epoch: u64,
unlocking_duration: u8,
current_locked: u64,
) -> Result<u64, JsValue> {
let mut account = DynamicPositionArrayAccount::default_with_data(&self.wrapped);
convert_error(crate::utils::voter_weight::compute_voter_weight(
&account.to_dynamic_position_array(),
current_epoch,
unlocking_duration,
current_locked,
MAX_VOTER_WEIGHT,
))
Expand Down
24 changes: 12 additions & 12 deletions staking/tests/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe("config", async () => {
governanceAuthority: program.provider.publicKey,
pythTokenMint: pythMintAccount.publicKey,
pythGovernanceRealm,
unlockingDuration: 2,
unlockingDuration: 0,
epochDuration: new BN(3600),
freeze: false,
pdaAuthority: pdaAuthority,
Expand Down Expand Up @@ -99,7 +99,7 @@ describe("config", async () => {
governanceAuthority: program.provider.publicKey,
pythTokenMint: pythMintAccount.publicKey,
pythGovernanceRealm,
unlockingDuration: 2,
unlockingDuration: 0,
epochDuration: new BN(3600),
freeze: false,
pdaAuthority: pdaAuthority,
Expand All @@ -126,7 +126,7 @@ describe("config", async () => {
governanceAuthority: program.provider.publicKey,
pythTokenMint: pythMintAccount.publicKey,
pythGovernanceRealm,
unlockingDuration: 2,
unlockingDuration: 0,
epochDuration: new BN(3600),
freeze: false,
pdaAuthority: pdaAuthority,
Expand All @@ -149,7 +149,7 @@ describe("config", async () => {
governanceAuthority: program.provider.publicKey,
pythTokenMint: pythMintAccount.publicKey,
pythGovernanceRealm,
unlockingDuration: 2,
unlockingDuration: 0,
epochDuration: new BN(3600),
freeze: false,
pdaAuthority: pdaAuthority,
Expand All @@ -176,7 +176,7 @@ describe("config", async () => {
governanceAuthority: program.provider.publicKey,
pythTokenMint: pythMintAccount.publicKey,
pythGovernanceRealm,
unlockingDuration: 2,
unlockingDuration: 0,
epochDuration: new BN(3600),
freeze: false,
pdaAuthority: pdaAuthority,
Expand All @@ -199,7 +199,7 @@ describe("config", async () => {
governanceAuthority: program.provider.publicKey,
pythTokenMint: pythMintAccount.publicKey,
pythGovernanceRealm,
unlockingDuration: 2,
unlockingDuration: 0,
epochDuration: new BN(3600),
freeze: false,
pdaAuthority: pdaAuthority,
Expand All @@ -224,7 +224,7 @@ describe("config", async () => {
governanceAuthority: program.provider.publicKey,
pythTokenMint: pythMintAccount.publicKey,
pythGovernanceRealm,
unlockingDuration: 2,
unlockingDuration: 0,
epochDuration: new BN(3600),
freeze: false,
pdaAuthority: pdaAuthority,
Expand Down Expand Up @@ -332,7 +332,7 @@ describe("config", async () => {
governanceAuthority: program.provider.publicKey,
pythTokenMint: pythMintAccount.publicKey,
pythGovernanceRealm,
unlockingDuration: 2,
unlockingDuration: 0,
epochDuration: new BN(3600),
freeze: false,
pdaAuthority: program.provider.publicKey,
Expand All @@ -356,7 +356,7 @@ describe("config", async () => {
governanceAuthority: program.provider.publicKey,
pythTokenMint: pythMintAccount.publicKey,
pythGovernanceRealm,
unlockingDuration: 2,
unlockingDuration: 0,
epochDuration: new BN(3600),
freeze: false,
pdaAuthority: pdaAuthority,
Expand Down Expand Up @@ -384,7 +384,7 @@ describe("config", async () => {
governanceAuthority: program.provider.publicKey,
pythTokenMint: pythMintAccount.publicKey,
pythGovernanceRealm,
unlockingDuration: 2,
unlockingDuration: 0,
epochDuration: new BN(3600),
freeze: false,
pdaAuthority,
Expand All @@ -407,7 +407,7 @@ describe("config", async () => {
governanceAuthority: program.provider.publicKey,
pythTokenMint: pythMintAccount.publicKey,
pythGovernanceRealm,
unlockingDuration: 2,
unlockingDuration: 0,
epochDuration: new BN(3600),
freeze: false,
pdaAuthority,
Expand Down Expand Up @@ -438,7 +438,7 @@ describe("config", async () => {
governanceAuthority: program.provider.publicKey,
pythTokenMint: pythMintAccount.publicKey,
pythGovernanceRealm,
unlockingDuration: 2,
unlockingDuration: 0,
epochDuration: new BN(3600),
freeze: false,
pdaAuthority: pdaAuthority,
Expand Down

0 comments on commit 16d0231

Please sign in to comment.