Skip to content

Commit

Permalink
fix(farm_manager): add prefix to position identifiers to avoid id col…
Browse files Browse the repository at this point in the history
…lision
  • Loading branch information
mantricjavier committed Oct 11, 2024
1 parent 5ba80fc commit 2bb1700
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 117 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ uint = { version = "0.10.0" }
anyhow = { version = "1.0.71" }
cw-ownable = { version = "2.0.0" }
anybuf = { version = "0.5.0" }
sha2 = { version = "=0.10.8" }
sha2 = { version = "=0.10.8", default-features = false }
test-case = { version = "3.3.1" }
cw-migrate-error-derive = { version = "0.1.0" }
proptest = { version = "1.5.0" }
rand = { version = "0.8.5" }
hex = { version = "0.4.3" }

# contracts
epoch-manager = { path = "contracts/epoch-manager" }
Expand Down
47 changes: 27 additions & 20 deletions contracts/farm-manager/src/position/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use cosmwasm_std::{
};

use crate::helpers::validate_lp_denom;
use crate::position::helpers::{calculate_weight, get_latest_address_weight};
use crate::position::helpers::{
calculate_weight, get_latest_address_weight, AUTO_POSITION_ID_PREFIX,
EXPLICIT_POSITION_ID_PREFIX,
};
use crate::position::helpers::{
validate_positions_limit, validate_unlocking_duration_for_position,
};
Expand Down Expand Up @@ -52,8 +55,14 @@ pub(crate) fn create_position(
.unwrap_or_default()
+ 1u64;

// if no identifier was provided, use the counter as the identifier
let identifier = identifier.unwrap_or(position_id_counter.to_string());
// compute the identifier for this position
let identifier = if let Some(identifier) = identifier {
// prepend EXPLICIT_POSITION_ID_PREFIX to identifier
format!("{EXPLICIT_POSITION_ID_PREFIX}{identifier}")
} else {
// prepend AUTO_POSITION_ID_PREFIX to the position_id_counter
format!("{AUTO_POSITION_ID_PREFIX}{position_id_counter}")
};

// check if there's an existing position with the computed identifier
let position = get_position(deps.storage, Some(identifier.clone()))?;
Expand All @@ -72,27 +81,23 @@ pub(crate) fn create_position(

POSITION_ID_COUNTER.save(deps.storage, &position_id_counter)?;

POSITIONS.save(
deps.storage,
&identifier,
&Position {
identifier: identifier.clone(),
lp_asset: lp_asset.clone(),
unlocking_duration,
open: true,
expiring_at: None,
receiver: receiver.clone(),
},
)?;
let position = Position {
identifier: identifier.clone(),
lp_asset: lp_asset.clone(),
unlocking_duration,
open: true,
expiring_at: None,
receiver: receiver.clone(),
};

POSITIONS.save(deps.storage, &identifier, &position)?;

// Update weights for the LP and the user
update_weights(deps, env, &receiver, &lp_asset, unlocking_duration, true)?;

Ok(Response::default().add_attributes(vec![
("action", "open_position".to_string()),
("receiver", receiver.to_string()),
("lp_asset", lp_asset.to_string()),
("unlocking_duration", unlocking_duration.to_string()),
("position", position.to_string()),
]))
}

Expand Down Expand Up @@ -222,11 +227,13 @@ pub(crate) fn close_position(
position.lp_asset.amount = position.lp_asset.amount.saturating_sub(lp_asset.amount);

// add the partial closing position to the storage
let identifier = POSITION_ID_COUNTER
let position_id_counter = POSITION_ID_COUNTER
.may_load(deps.storage)?
.unwrap_or_default()
+ 1u64;
POSITION_ID_COUNTER.save(deps.storage, &identifier)?;
POSITION_ID_COUNTER.save(deps.storage, &position_id_counter)?;

let identifier = format!("{AUTO_POSITION_ID_PREFIX}{position_id_counter}");

let partial_position = Position {
identifier: identifier.to_string(),
Expand Down
3 changes: 3 additions & 0 deletions contracts/farm-manager/src/position/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use crate::ContractError;
const SECONDS_IN_DAY: u64 = 86400;
const SECONDS_IN_YEAR: u64 = 31556926;

pub const AUTO_POSITION_ID_PREFIX: &str = "p-";
pub const EXPLICIT_POSITION_ID_PREFIX: &str = "u-";

/// Calculates the weight size for a user filling a position
pub fn calculate_weight(
lp_asset: &Coin,
Expand Down
Loading

0 comments on commit 2bb1700

Please sign in to comment.