Skip to content

Commit

Permalink
Feat/events refund (#648)
Browse files Browse the repository at this point in the history
This PR aims to revert the changes to the `refund0` and `refund1`
attributes from a previous PR that broke our Subgraph indexer. I patched
and reingested the Subgraph, but since this seems redundant and
unnecessary, I would like to revert to the previous version.
`refund0_denom` and `refund1_denom` are useless, as 0 and 1 already
provide clients with sufficient information, and it is inconsistent with
how we still treat `amount0` and `amount1`.

Minor changes include moving helpers to the helpers mod.
  • Loading branch information
magiodev authored Jul 10, 2024
1 parent 66c2210 commit bc81ce7
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 38 deletions.
3 changes: 2 additions & 1 deletion smart-contracts/contracts/cl-vault/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use osmosis_std::types::osmosis::concentratedliquidity::v1beta1::Concentratedliq

use crate::error::ContractError;
use crate::helpers::generic::sort_tokens;
use crate::helpers::getters::get_range_admin;
use crate::helpers::prepend::prepend_claim_msg;
use crate::instantiate::{
handle_create_denom_reply, handle_instantiate, handle_instantiate_create_position_reply,
Expand Down Expand Up @@ -39,7 +40,7 @@ use crate::vault::merge::{
handle_merge_withdraw_position_reply,
};
use crate::vault::range::{
execute_update_range, get_range_admin, handle_initial_create_position_reply,
execute_update_range, handle_initial_create_position_reply,
handle_iteration_create_position_reply, handle_merge_reply, handle_swap_reply,
handle_withdraw_position_reply,
};
Expand Down
15 changes: 13 additions & 2 deletions smart-contracts/contracts/cl-vault/src/helpers/assert.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use cosmwasm_std::{coin, Addr, Coin, Deps, MessageInfo};
use cosmwasm_std::{coin, Addr, Coin, Deps, MessageInfo, Storage};

use crate::{state::ADMIN_ADDRESS, ContractError};
use crate::{
state::{ADMIN_ADDRESS, RANGE_ADMIN},
ContractError,
};

/// This function compares the address of the message sender (caller) with the current admin
/// address stored in the state. This provides a convenient way to verify if the caller
Expand All @@ -13,6 +16,14 @@ pub fn assert_admin(deps: Deps, caller: &Addr) -> Result<Addr, ContractError> {
}
}

pub fn assert_range_admin(storage: &mut dyn Storage, sender: &Addr) -> Result<(), ContractError> {
let admin = RANGE_ADMIN.load(storage)?;
if admin != sender {
return Err(ContractError::Unauthorized {});
}
Ok(())
}

/// Returns the Coin of the needed denoms in the order given in denoms
pub(crate) fn must_pay_one_or_two(
info: &MessageInfo,
Expand Down
8 changes: 7 additions & 1 deletion smart-contracts/contracts/cl-vault/src/helpers/getters.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::math::tick::tick_to_price;
use crate::state::RANGE_ADMIN;
use std::str::FromStr;

use osmosis_std::shim::Timestamp as OsmoTimestamp;
Expand All @@ -8,12 +9,17 @@ use osmosis_std::types::osmosis::twap::v1beta1::TwapQuerier;
use crate::vault::concentrated_liquidity::{get_cl_pool_info, get_position};
use crate::{state::POOL_CONFIG, ContractError};
use cosmwasm_std::{
Coin, Decimal, Decimal256, DepsMut, Env, Fraction, QuerierWrapper, Storage, Uint128, Uint256,
Addr, Coin, Decimal, Decimal256, Deps, DepsMut, Env, Fraction, QuerierWrapper, Storage,
Uint128, Uint256,
};
use osmosis_std::try_proto_to_cosmwasm_coins;

use super::coinlist::CoinList;

pub fn get_range_admin(deps: Deps) -> Result<Addr, ContractError> {
Ok(RANGE_ADMIN.load(deps.storage)?)
}

/// Calculate the total value of two assets in asset0.
pub fn get_asset0_value(
storage: &dyn Storage,
Expand Down
6 changes: 2 additions & 4 deletions smart-contracts/contracts/cl-vault/src/helpers/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ pub fn refund_bank_msg(

if let Some(refund0) = refund0 {
if refund0.amount > Uint128::zero() {
attributes.push(attr("refund0_amount", refund0.amount));
attributes.push(attr("refund0_denom", refund0.denom.as_str()));
attributes.push(attr("refund0", refund0.amount));
coins.push(refund0)
}
}
if let Some(refund1) = refund1 {
if refund1.amount > Uint128::zero() {
attributes.push(attr("refund1_amount", refund1.amount));
attributes.push(attr("refund1_denom", refund1.denom.as_str()));
attributes.push(attr("refund1", refund1.amount));
coins.push(refund1)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ mod tests {

// Assert balance refunded is either the expected value or not empty for token0
let refund0_amount =
get_event_attributes_by_ty_and_key(&exact_deposit, "wasm", vec!["refund0_amount"]);
get_event_attributes_by_ty_and_key(&exact_deposit, "wasm", vec!["refund0"]);
let mut refund0_amount_parsed: u128 = 0;
if expected_refund0 > 0 {
assert_approx_eq!(
Expand All @@ -159,7 +159,7 @@ mod tests {

// Assert balance refunded is either the expected value or not empty for token1
let refund1_amount =
get_event_attributes_by_ty_and_key(&exact_deposit, "wasm", vec!["refund1_amount"]);
get_event_attributes_by_ty_and_key(&exact_deposit, "wasm", vec!["refund1"]);
let mut refund1_amount_parsed: u128 = 0;
if expected_refund1 > 0 {
assert_approx_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,14 @@ mod tests {
.unwrap();

// assert that the refund + used funds are equal to what we deposited
let refund0: u128 =
get_event_attributes_by_ty_and_key(&response, "wasm", vec!["refund0_amount"])
.get(0)
.map(|attr| attr.value.parse().unwrap())
.unwrap_or(0);
let refund1: u128 =
get_event_attributes_by_ty_and_key(&response, "wasm", vec!["refund1_amount"])
.get(0)
.map(|attr| attr.value.parse().unwrap())
.unwrap_or(0);
let refund0: u128 = get_event_attributes_by_ty_and_key(&response, "wasm", vec!["refund0"])
.get(0)
.map(|attr| attr.value.parse().unwrap())
.unwrap_or(0);
let refund1: u128 = get_event_attributes_by_ty_and_key(&response, "wasm", vec!["refund1"])
.get(0)
.map(|attr| attr.value.parse().unwrap())
.unwrap_or(0);

let deposited0: u128 =
get_event_attributes_by_ty_and_key(&response, "wasm", vec!["amount0"])
Expand Down
22 changes: 6 additions & 16 deletions smart-contracts/contracts/cl-vault/src/vault/range.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
helpers::{
assert::assert_range_admin,
generic::extract_attribute_value_by_ty_and_key,
getters::{
get_single_sided_deposit_0_to_1_swap_amount,
Expand All @@ -12,7 +13,7 @@ use crate::{
reply::Replies,
state::{
ModifyRangeState, Position, SwapDepositMergeState, CURRENT_BALANCE, CURRENT_SWAP,
MODIFY_RANGE_STATE, POOL_CONFIG, POSITION, RANGE_ADMIN, SWAP_DEPOSIT_MERGE_STATE,
MODIFY_RANGE_STATE, POOL_CONFIG, POSITION, SWAP_DEPOSIT_MERGE_STATE,
},
vault::{
concentrated_liquidity::{create_position, get_position},
Expand All @@ -22,8 +23,8 @@ use crate::{
};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
attr, to_json_binary, Addr, Coin, Decimal, Decimal256, Deps, DepsMut, Env, Fraction,
MessageInfo, Response, Storage, SubMsg, SubMsgResult, Uint128,
attr, to_json_binary, Coin, Decimal, Decimal256, DepsMut, Env, Fraction, MessageInfo, Response,
SubMsg, SubMsgResult, Uint128,
};
use osmosis_std::types::osmosis::{
concentratedliquidity::v1beta1::{
Expand All @@ -39,18 +40,6 @@ use super::{
swap::{SwapCalculationResult, SwapParams},
};

pub fn assert_range_admin(storage: &mut dyn Storage, sender: &Addr) -> Result<(), ContractError> {
let admin = RANGE_ADMIN.load(storage)?;
if admin != sender {
return Err(ContractError::Unauthorized {});
}
Ok(())
}

pub fn get_range_admin(deps: Deps) -> Result<Addr, ContractError> {
Ok(RANGE_ADMIN.load(deps.storage)?)
}

/// This function is the entrypoint into the dsm routine that will go through the following steps
/// * how much liq do we have in current range
/// * so how much of each asset given liq would we have at current price
Expand Down Expand Up @@ -687,6 +676,7 @@ mod tests {
use osmosis_std::types::osmosis::concentratedliquidity::v1beta1::MsgWithdrawPositionResponse;

use crate::{
helpers::getters::get_range_admin,
math::tick::build_tick_exp_cache,
state::{MODIFY_RANGE_STATE, RANGE_ADMIN},
test_helpers::{mock_deps_with_querier, mock_deps_with_querier_with_balance},
Expand Down Expand Up @@ -717,7 +707,7 @@ mod tests {

RANGE_ADMIN.save(&mut deps.storage, &info.sender).unwrap();

assert_eq!(super::get_range_admin(deps.as_ref()).unwrap(), info.sender);
assert_eq!(get_range_admin(deps.as_ref()).unwrap(), info.sender);
}

#[test]
Expand Down
3 changes: 1 addition & 2 deletions smart-contracts/contracts/cl-vault/src/vault/swap.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use cosmwasm_std::{CosmosMsg, DepsMut, Env, Fraction, MessageInfo, Response, Uint128};
use osmosis_std::types::osmosis::poolmanager::v1beta1::SwapAmountInRoute;

use crate::helpers::assert::assert_range_admin;
use crate::helpers::msgs::swap_msg;
use crate::msg::SwapOperation;
use crate::state::POOL_CONFIG;
use crate::{state::VAULT_CONFIG, ContractError};

use super::range::assert_range_admin;

/// SwapCalculationResult holds the result of a swap calculation
pub struct SwapCalculationResult {
pub swap_msg: CosmosMsg,
Expand Down

0 comments on commit bc81ce7

Please sign in to comment.