Skip to content

Commit

Permalink
Auction discount fix (#92)
Browse files Browse the repository at this point in the history
* lower auction discount for assets with low minCR

* change test name

* fix clippy

* remove previous mint migrate msg

* update mint version and changelog
  • Loading branch information
csanti authored Feb 27, 2022
1 parent fd34bd0 commit 70253e2
Show file tree
Hide file tree
Showing 19 changed files with 231 additions and 123 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# 2.1.3
# 2.1.4

* Migration for collateral oracle and collector to support LunaX as collateral #88
* Fixed max spread issue with Astroport pairs on Collector
* Auction discount fix for mint contract #92
19 changes: 3 additions & 16 deletions contracts/mirror_gov/src/testing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2708,12 +2708,7 @@ fn distribute_voting_rewards() {

// voting info has been deleted
assert!(poll_voter_read(&deps.storage, 1u64)
.load(
deps.api
.addr_canonicalize(&TEST_VOTER.to_string())
.unwrap()
.as_slice()
)
.load(deps.api.addr_canonicalize(TEST_VOTER).unwrap().as_slice())
.is_err())
}

Expand Down Expand Up @@ -2843,12 +2838,7 @@ fn stake_voting_rewards() {

// voting info has been deleted
assert!(poll_voter_read(&deps.storage, 1u64)
.load(
deps.api
.addr_canonicalize(&TEST_VOTER.to_string())
.unwrap()
.as_slice()
)
.load(deps.api.addr_canonicalize(TEST_VOTER).unwrap().as_slice())
.is_err());

let res = query(
Expand Down Expand Up @@ -4502,10 +4492,7 @@ fn happy_days_end_poll_with_controlled_quorum() {
let actual_staked_weight = load_token_balance(
&deps.as_ref().querier,
VOTING_TOKEN.to_string(),
&deps
.api
.addr_canonicalize(&MOCK_CONTRACT_ADDR.to_string())
.unwrap(),
&deps.api.addr_canonicalize(MOCK_CONTRACT_ADDR).unwrap(),
)
.unwrap()
.checked_sub(Uint128::new(DEFAULT_PROPOSAL_DEPOSIT))
Expand Down
2 changes: 1 addition & 1 deletion contracts/mirror_mint/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mirror-mint"
version = "2.1.1"
version = "2.1.4"
authors = ["Terraform Labs, PTE."]
edition = "2018"
description = "A Mint contract for Mirror Protocol - allows you to register and mint asset token"
Expand Down
3 changes: 1 addition & 2 deletions contracts/mirror_mint/examples/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fs::create_dir_all;
use cosmwasm_schema::{export_schema, remove_schemas, schema_for};

use mirror_protocol::mint::{
AssetConfigResponse, ConfigResponse, Cw20HookMsg, ExecuteMsg, InstantiateMsg, MigrateMsg,
AssetConfigResponse, ConfigResponse, Cw20HookMsg, ExecuteMsg, InstantiateMsg,
NextPositionIdxResponse, PositionResponse, PositionsResponse, QueryMsg, ShortParams,
};

Expand All @@ -19,7 +19,6 @@ fn main() {
export_schema(&schema_for!(ShortParams), &out_dir);
export_schema(&schema_for!(Cw20HookMsg), &out_dir);
export_schema(&schema_for!(QueryMsg), &out_dir);
export_schema(&schema_for!(MigrateMsg), &out_dir);
export_schema(&schema_for!(ConfigResponse), &out_dir);
export_schema(&schema_for!(NextPositionIdxResponse), &out_dir);
export_schema(&schema_for!(AssetConfigResponse), &out_dir);
Expand Down
4 changes: 2 additions & 2 deletions contracts/mirror_mint/schema/cw20_hook_msg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Cw20HookMsg",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down Expand Up @@ -103,7 +103,7 @@
"definitions": {
"AssetInfo": {
"description": "AssetInfo contract_addr is usually passed from the cw20 hook so we can trust the contract_addr is properly validated.",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down
4 changes: 2 additions & 2 deletions contracts/mirror_mint/schema/execute_msg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ExecuteMsg",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down Expand Up @@ -373,7 +373,7 @@
},
"AssetInfo": {
"description": "AssetInfo contract_addr is usually passed from the cw20 hook so we can trust the contract_addr is properly validated.",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down
25 changes: 0 additions & 25 deletions contracts/mirror_mint/schema/migrate_msg.json

This file was deleted.

2 changes: 1 addition & 1 deletion contracts/mirror_mint/schema/position_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
"AssetInfo": {
"description": "AssetInfo contract_addr is usually passed from the cw20 hook so we can trust the contract_addr is properly validated.",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down
2 changes: 1 addition & 1 deletion contracts/mirror_mint/schema/positions_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"AssetInfo": {
"description": "AssetInfo contract_addr is usually passed from the cw20 hook so we can trust the contract_addr is properly validated.",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down
2 changes: 1 addition & 1 deletion contracts/mirror_mint/schema/query_msg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "QueryMsg",
"anyOf": [
"oneOf": [
{
"type": "object",
"required": [
Expand Down
16 changes: 11 additions & 5 deletions contracts/mirror_mint/src/asserts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::state::{AssetConfig, Position};
use std::str::FromStr;

use crate::{
contract::MIN_CR_ALLOWED,
state::{AssetConfig, Position},
};
use cosmwasm_std::{Decimal, Deps, Env, StdError, StdResult};
use terraswap::asset::{Asset, AssetInfo};

Expand Down Expand Up @@ -57,10 +62,11 @@ pub fn assert_auction_discount(auction_discount: Decimal) -> StdResult<()> {
}

pub fn assert_min_collateral_ratio(min_collateral_ratio: Decimal) -> StdResult<()> {
if min_collateral_ratio < Decimal::one() {
Err(StdError::generic_err(
"min_collateral_ratio must be bigger than 1",
))
if min_collateral_ratio < Decimal::from_str(MIN_CR_ALLOWED)? {
Err(StdError::generic_err(format!(
"min_collateral_ratio must be bigger or equal than {}",
MIN_CR_ALLOWED
)))
} else {
Ok(())
}
Expand Down
8 changes: 5 additions & 3 deletions contracts/mirror_mint/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ use crate::{
use cosmwasm_std::entry_point;
use cosmwasm_std::{
attr, from_binary, to_binary, Addr, Binary, CanonicalAddr, CosmosMsg, Decimal, Deps, DepsMut,
Env, MessageInfo, Response, StdError, StdResult, Uint128, WasmMsg,
Empty, Env, MessageInfo, Response, StdError, StdResult, Uint128, WasmMsg,
};
use cw20::Cw20ReceiveMsg;
use mirror_protocol::collateral_oracle::{ExecuteMsg as CollateralOracleExecuteMsg, SourceType};
use mirror_protocol::mint::{
AssetConfigResponse, ConfigResponse, Cw20HookMsg, ExecuteMsg, IPOParams, InstantiateMsg,
MigrateMsg, QueryMsg,
QueryMsg,
};
use terraswap::asset::{Asset, AssetInfo};

pub const MIN_CR_ALLOWED: &str = "1.1";

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
Expand Down Expand Up @@ -543,6 +545,6 @@ pub fn query_asset_config(deps: Deps, asset_token: String) -> StdResult<AssetCon
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
pub fn migrate(_deps: DepsMut, _env: Env, _msg: Empty) -> StdResult<Response> {
Ok(Response::default())
}
8 changes: 8 additions & 0 deletions contracts/mirror_mint/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ pub fn decimal_division(a: Decimal, b: Decimal) -> Decimal {
pub fn decimal_multiplication(a: Decimal, b: Decimal) -> Decimal {
Decimal::from_ratio(a * DECIMAL_FRACTIONAL * b, DECIMAL_FRACTIONAL)
}

pub fn decimal_min(a: Decimal, b: Decimal) -> Decimal {
if a < b {
a
} else {
b
}
}
24 changes: 16 additions & 8 deletions contracts/mirror_mint/src/positions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use crate::{
assert_asset, assert_burn_period, assert_collateral, assert_migrated_asset,
assert_mint_period, assert_pre_ipo_collateral, assert_revoked_collateral,
},
math::{decimal_division, decimal_multiplication, decimal_subtraction, reverse_decimal},
math::{
decimal_division, decimal_min, decimal_multiplication, decimal_subtraction, reverse_decimal,
},
querier::{load_asset_price, load_collateral_info},
state::{
create_position, is_short_position, read_asset_config, read_config, read_position,
Expand Down Expand Up @@ -798,10 +800,15 @@ pub fn auction(
));
}

// auction discount is min(min_cr - 1, auction_discount)
let auction_discount: Decimal = decimal_min(
asset_config.auction_discount,
decimal_subtraction(asset_config.min_collateral_ratio, Decimal::one()),
);
// Compute discounted price
let discounted_price: Decimal = decimal_division(
collateral_price_in_asset,
decimal_subtraction(Decimal::one(), asset_config.auction_discount),
decimal_subtraction(Decimal::one(), auction_discount),
);

// Convert asset value in discounted collateral unit
Expand All @@ -820,12 +827,13 @@ pub fn auction(
.unwrap()
* reverse_decimal(discounted_price);

let refund_asset: Asset = Asset {
info: asset.info.clone(),
amount: refund_asset_amount,
};

messages.push(refund_asset.into_msg(&deps.querier, sender.clone())?);
if !refund_asset_amount.is_zero() {
let refund_asset: Asset = Asset {
info: asset.info.clone(),
amount: refund_asset_amount,
};
messages.push(refund_asset.into_msg(&deps.querier, sender.clone())?);
}

(position.collateral.amount, refund_asset_amount)
} else {
Expand Down
4 changes: 2 additions & 2 deletions contracts/mirror_mint/src/testing/contract_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ fn register_asset() {
let res = execute(deps.as_mut(), mock_env(), info, msg).unwrap_err();
match res {
StdError::GenericErr { msg, .. } => {
assert_eq!(msg, "min_collateral_ratio must be bigger than 1")
assert_eq!(msg, "min_collateral_ratio must be bigger or equal than 1.1")
}
_ => panic!("DO NOT ENTER HERE"),
}
Expand Down Expand Up @@ -300,7 +300,7 @@ fn update_asset() {
let res = execute(deps.as_mut(), mock_env(), info, msg).unwrap_err();
match res {
StdError::GenericErr { msg, .. } => {
assert_eq!(msg, "min_collateral_ratio must be bigger than 1")
assert_eq!(msg, "min_collateral_ratio must be bigger or equal than 1.1")
}
_ => panic!("Must return unauthorized error"),
}
Expand Down
5 changes: 1 addition & 4 deletions contracts/mirror_mint/src/testing/mock_querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ impl TerraswapPairQuerier {
pub(crate) fn paris_to_map(pairs: &[(&String, &String, &String)]) -> HashMap<String, String> {
let mut pairs_map: HashMap<String, String> = HashMap::new();
for (asset1, asset2, pair) in pairs.iter() {
pairs_map.insert(
(asset1.to_string() + &asset2.to_string()).clone(),
pair.to_string(),
);
pairs_map.insert((asset1.to_string() + asset2).clone(), pair.to_string());
}

pairs_map
Expand Down
Loading

0 comments on commit 70253e2

Please sign in to comment.