Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staking: stake consolidation for stakes older than 60 days #404

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion contracts/stake/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use phoenix::ttl::{INSTANCE_BUMP_AMOUNT, INSTANCE_LIFETIME_THRESHOLD};
use phoenix::ttl::{DAY_IN_LEDGERS, INSTANCE_BUMP_AMOUNT, INSTANCE_LIFETIME_THRESHOLD};
use soroban_sdk::{
contract, contractimpl, contractmeta, log, map, panic_with_error, vec, Address, BytesN, Env,
Vec,
Expand All @@ -22,6 +22,8 @@ use crate::{
token_contract,
};

const SIXTY_DAYS_IN_LEDGER_TIMESTAMP: u64 = DAY_IN_LEDGERS as u64 * 60u64;

// Metadata that is added on to the WASM custom section
contractmeta!(
key = "Description",
Expand Down Expand Up @@ -56,6 +58,8 @@ pub trait StakingTrait {

fn withdraw_rewards(env: Env, sender: Address);

fn consolidate_stakes(env: Env, sender: Address, stake_timestamps: Vec<u64>);

// QUERIES

fn query_config(env: Env) -> ConfigResponse;
Expand Down Expand Up @@ -285,6 +289,71 @@ impl StakingTrait for Staking {
save_stakes(&env, &sender, &stakes);
}

fn consolidate_stakes(env: Env, sender: Address, stake_timestamps: Vec<u64>) {
sender.require_auth();
env.storage()
.instance()
.extend_ttl(INSTANCE_LIFETIME_THRESHOLD, INSTANCE_BUMP_AMOUNT);

let mut user_bonding_info = get_stakes(&env, &sender);
let present_timestamp = env.ledger().timestamp();
let mut removed_stakes: Vec<Stake> = Vec::new(&env);

// remove the stakes the sender sends us from storage in reverse order
for stake_timestamp in stake_timestamps.iter() {
// verify that the stakes we are about to remove are > 60 days
assert!(
present_timestamp - stake_timestamp >= SIXTY_DAYS_IN_LEDGER_TIMESTAMP,
"Stake: Consolidate Stake: Cannot consolidate stakes -> less than 60 days for stake."
);

let (idx_to_remove, stake_to_remove) = user_bonding_info
.stakes
.iter()
.enumerate()
.find(|(_, el)| el.stake_timestamp == stake_timestamp)
.unwrap_or_else(|| {
log!(
&env,
"Stake: Consolidate Stakes: Cannot find stake for given timestamp: {}",
stake_timestamp
);
panic_with_error!(&env, ContractError::StakeNotFound);
});

removed_stakes.push_back(stake_to_remove);

user_bonding_info
.stakes
.remove(idx_to_remove as u32)
.unwrap_or_else(|| {
log!(
&env,
"Stake: Consolidate Stakes: Cannot remove stake with given timestamp: {}",
stake_timestamp
);
panic_with_error!(&env, ContractError::StakeNotFound);
});
}

let mut new_stake = Stake {
stake: 0,
stake_timestamp: 0, // just a placeholder
};

// consolidate the stakes from sender into a single stake
for old_stake in removed_stakes.iter() {
new_stake.stake += old_stake.stake;
if new_stake.stake_timestamp < old_stake.stake_timestamp {
new_stake.stake_timestamp = old_stake.stake_timestamp;
}
}

// update the storage again using the new consolidated stake
user_bonding_info.stakes.push_back(new_stake);
save_stakes(&env, &sender, &user_bonding_info);
}

// QUERIES

fn query_config(env: Env) -> ConfigResponse {
Expand Down
Loading
Loading