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

Rebasing-branch #163

Open
wants to merge 6 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
4 changes: 3 additions & 1 deletion contracts/margined_insurance_fund/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ pub fn shutdown_all_vamm(deps: DepsMut, env: Env, info: MessageInfo) -> StdResul
msgs.push(execute_vamm_shutdown(vamm.clone())?);
}

Ok(Response::default().add_submessages(msgs))
Ok(Response::default()
.add_submessages(msgs)
.add_attributes(vec![("action", "vamm_shutdown")]))
}

pub fn withdraw(
Expand Down
3 changes: 2 additions & 1 deletion contracts/margined_vamm/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use margined_common::{
};
use margined_perp::margined_vamm::{ExecuteMsg, InstantiateMsg, QueryMsg};

use crate::error::ContractError;
use crate::querier::{query_underlying_price, query_underlying_twap_price};
use crate::{error::ContractError, handle::rebase_vamm};
use crate::{
handle::{set_open, settle_funding, swap_input, swap_output, update_config, update_owner},
query::{
Expand Down Expand Up @@ -170,6 +170,7 @@ pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> S
),
ExecuteMsg::SettleFunding {} => settle_funding(deps, env, info),
ExecuteMsg::SetOpen { open } => set_open(deps, env, info, open),
ExecuteMsg::RebaseVamm {} => rebase_vamm(deps, info, env),
}
}

Expand Down
33 changes: 32 additions & 1 deletion contracts/margined_vamm/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
contract::{
ONE_DAY_IN_SECONDS, ONE_HOUR_IN_SECONDS, ONE_MINUTE_IN_SECONDS, ONE_WEEK_IN_SECONDS, OWNER,
},
querier::query_underlying_twap_price,
querier::{query_underlying_price, query_underlying_twap_price},
query::query_twap_price,
state::{read_config, read_state, store_config, store_state, Config, State},
utils::{
Expand Down Expand Up @@ -129,6 +129,37 @@ pub fn set_open(deps: DepsMut, env: Env, info: MessageInfo, open: bool) -> StdRe
Ok(Response::new().add_attribute("action", "set_open"))
}

// This function will rebase the vamm according to the current oracle price
pub fn rebase_vamm(deps: DepsMut, info: MessageInfo, env: Env) -> StdResult<Response> {
let config: Config = read_config(deps.storage)?;
let mut state: State = read_state(deps.storage)?;

if !OWNER.is_admin(deps.as_ref(), &info.sender)? {
return Err(StdError::generic_err("unauthorized"));
}

let oracle_price = query_underlying_price(&deps.as_ref())?;

// let P be oracle price, Q and B_old be current quote and base asset reserves
// B_new is the final base_asset_reserve we want.
// Current price is Q / B_old, P = Q / B_new
// Therefore B_new = Q / P

state.base_asset_reserve = state
.quote_asset_reserve
.checked_mul(config.decimals)?
.checked_div(oracle_price)?;

store_state(deps.storage, &state)?;

Ok(Response::new().add_attributes(vec![
("action", "rebase_vamm"),
("vamm", env.contract.address.as_ref()),
("new_base_asset", &state.base_asset_reserve.to_string()),
("new_price", &oracle_price.to_string()),
]))
}

// Function should only be called by the margin engine
pub fn swap_input(
deps: DepsMut,
Expand Down
31 changes: 30 additions & 1 deletion contracts/margined_vamm/src/testing/get_price_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use crate::contract::{instantiate, query};
use crate::handle::{get_input_price_with_reserves, get_output_price_with_reserves};
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{from_binary, Uint128};
use cw_multi_test::Executor;
use margined_perp::margined_vamm::{Direction, InstantiateMsg, QueryMsg, StateResponse};
use margined_utils::scenarios::to_decimals;
use margined_utils::scenarios::{to_decimals, SimpleScenario};

/// Unit tests
#[test]
Expand Down Expand Up @@ -329,3 +330,31 @@ fn test_get_input_and_output_price_with_reserves() {
.unwrap();
assert_eq!(result, to_decimals(600));
}

#[test]
fn test_rebase_vamm() {
let SimpleScenario {
mut router,
owner,
vamm,
pricefeed,
..
} = SimpleScenario::new();

let spot_price = vamm.spot_price(&router).unwrap();
assert_eq!(spot_price, to_decimals(10u64));

let price: Uint128 = Uint128::from(20u128);
let timestamp: u64 = 1_000_000_000;

let msg = pricefeed
.append_price("ETH".to_string(), price, timestamp)
.unwrap();
router.execute(owner.clone(), msg).unwrap();

let msg = vamm.rebase_vamm().unwrap();
router.execute(owner.clone(), msg).unwrap();

let new_spot_price = vamm.spot_price(&router).unwrap();
assert_eq!(new_spot_price, Uint128::from(20u64));
}
1 change: 1 addition & 0 deletions packages/margined_perp/src/margined_vamm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub enum ExecuteMsg {
SetOpen {
open: bool,
},
RebaseVamm {},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ impl VammController {
self.call(msg, vec![])
}

pub fn rebase_vamm(&self) -> StdResult<CosmosMsg> {
let msg = ExecuteMsg::RebaseVamm {};
self.call(msg, vec![])
}

pub fn set_toll_ratio(&self, toll_ratio: Uint128) -> StdResult<CosmosMsg> {
let msg = ExecuteMsg::UpdateConfig {
base_asset_holding_cap: None,
Expand Down