Skip to content

Commit

Permalink
Feat (asset info): remove the tax
Browse files Browse the repository at this point in the history
  • Loading branch information
P-Yevhenii authored Apr 19, 2023
1 parent cc302a3 commit 285543c
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 228 deletions.
170 changes: 118 additions & 52 deletions Cargo.lock

Large diffs are not rendered by default.

15 changes: 4 additions & 11 deletions contracts/pair/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,8 @@ pub fn withdraw_liquidity(

// Update the pool info
let messages: Vec<CosmosMsg> = vec![
refund_assets[0]
.clone()
.into_msg(&deps.querier, sender.clone())?,
refund_assets[1]
.clone()
.into_msg(&deps.querier, sender.clone())?,
refund_assets[0].clone().into_msg(sender.clone())?,
refund_assets[1].clone().into_msg(sender.clone())?,
CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: config.pair_info.liquidity_token.to_string(),
msg: to_binary(&Cw20ExecuteMsg::Burn { amount })?,
Expand Down Expand Up @@ -678,17 +674,15 @@ pub fn swap(
spread_amount,
)?;

// Compute the tax for the receiving asset (if it is a native one)
let return_asset = Asset {
info: ask_pool.info.clone(),
amount: return_amount,
};

let tax_amount = return_asset.compute_tax(&deps.querier)?;
let receiver = to.unwrap_or_else(|| sender.clone());
let mut messages = vec![];
if !return_amount.is_zero() {
messages.push(return_asset.into_msg(&deps.querier, receiver.clone())?)
messages.push(return_asset.into_msg(receiver.clone())?)
}

// Compute the Maker fee
Expand All @@ -698,7 +692,7 @@ pub fn swap(
calculate_maker_fee(&ask_pool.info, commission_amount, fee_info.maker_fee_rate)
{
maker_fee_amount = f.amount;
messages.push(f.into_msg(&deps.querier, fee_address)?);
messages.push(f.into_msg(fee_address)?);
}
}

Expand Down Expand Up @@ -741,7 +735,6 @@ pub fn swap(
attr("ask_asset", ask_pool.info.to_string()),
attr("offer_amount", offer_amount),
attr("return_amount", return_amount),
attr("tax_amount", tax_amount),
attr("spread_amount", spread_amount),
attr("commission_amount", commission_amount),
attr("maker_fee_amount", maker_fee_amount),
Expand Down
33 changes: 0 additions & 33 deletions contracts/pair/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,6 @@ fn try_native_to_token() {
let expected_return_amount = expected_ret_amount
.checked_sub(expected_commission_amount)
.unwrap();
let expected_tax_amount = Uint128::zero(); // no tax for token

// Check simulation result
deps.querier.with_balance(&[(
Expand Down Expand Up @@ -920,7 +919,6 @@ fn try_native_to_token() {
attr("ask_asset", "asset0000"),
attr("offer_amount", offer_amount.to_string()),
attr("return_amount", expected_return_amount.to_string()),
attr("tax_amount", expected_tax_amount.to_string()),
attr("spread_amount", expected_spread_amount.to_string()),
attr("commission_amount", expected_commission_amount.to_string()),
attr("maker_fee_amount", expected_maker_fee_amount.to_string()),
Expand Down Expand Up @@ -1111,7 +1109,6 @@ fn try_token_to_native() {
attr("ask_asset", "uusd"),
attr("offer_amount", offer_amount.to_string()),
attr("return_amount", expected_return_amount.to_string()),
attr("tax_amount", Uint128::zero().to_string()),
attr("spread_amount", expected_spread_amount.to_string()),
attr("commission_amount", expected_commission_amount.to_string()),
attr("maker_fee_amount", expected_maker_fee_amount.to_string()),
Expand Down Expand Up @@ -1201,36 +1198,6 @@ fn test_max_spread() {
.unwrap_err();
}

#[test]
#[ignore]
fn test_deduct() {
let deps = mock_dependencies(&[]);

let tax_rate = Decimal::percent(2);
let tax_cap = Uint128::from(1_000_000u128);
// deps.querier.with_tax(
// Decimal::percent(2),
// &[(&"uusd".to_string(), &Uint128::from(1000000u128))],
// );

let amount = Uint128::new(1000_000_000u128);
let expected_after_amount = std::cmp::max(
amount.checked_sub(amount * tax_rate).unwrap(),
amount.checked_sub(tax_cap).unwrap(),
);

let after_amount = (Asset {
info: AssetInfo::NativeToken {
denom: "uusd".to_string(),
},
amount,
})
.deduct_tax(&deps.as_ref().querier)
.unwrap();

assert_eq!(expected_after_amount, after_amount.amount);
}

#[test]
fn test_query_pool() {
let total_share_amount = Uint128::from(111u128);
Expand Down
6 changes: 3 additions & 3 deletions contracts/pair_concentrated/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ fn withdraw_liquidity(
refund_assets
.iter()
.cloned()
.map(|asset| asset.into_msg(&deps.querier, &sender))
.map(|asset| asset.into_msg(&sender))
.collect::<StdResult<Vec<_>>>()?,
);
messages.push(
Expand Down Expand Up @@ -752,7 +752,7 @@ fn swap(
info: pools[ask_ind].info.clone(),
amount: return_amount,
}
.into_msg(&deps.querier, &receiver)?];
.into_msg(&receiver)?];

let mut maker_fee = Uint128::zero();
if let Some(fee_address) = fee_info.fee_address {
Expand All @@ -762,7 +762,7 @@ fn swap(
info: pools[ask_ind].info.clone(),
amount: maker_fee,
};
messages.push(fee.into_msg(&deps.querier, fee_address)?);
messages.push(fee.into_msg(fee_address)?);
}
}

Expand Down
6 changes: 3 additions & 3 deletions contracts/pair_stable/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ pub fn withdraw_liquidity(
let mut messages = refund_assets
.clone()
.into_iter()
.map(|asset| asset.into_msg(&deps.querier, &sender))
.map(|asset| asset.into_msg(&sender))
.collect::<StdResult<Vec<_>>>()?;
messages.push(
wasm_execute(
Expand Down Expand Up @@ -691,7 +691,7 @@ pub fn swap(

let mut messages = vec![];
if !return_amount.is_zero() {
messages.push(return_asset.into_msg(&deps.querier, receiver.clone())?)
messages.push(return_asset.into_msg(receiver.clone())?)
}

// Compute the Maker fee
Expand All @@ -701,7 +701,7 @@ pub fn swap(
calculate_maker_fee(&ask_pool.info, commission_amount, fee_info.maker_fee_rate)
{
maker_fee_amount = f.amount;
messages.push(f.into_msg(&deps.querier, fee_address)?);
messages.push(f.into_msg(fee_address)?);
}
}

Expand Down
26 changes: 1 addition & 25 deletions contracts/router/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,19 +331,7 @@ fn simulate_swap_operations(
&[offer_asset_info.clone(), ask_asset_info.clone()],
)?;

// Deduct tax
if let AssetInfo::NativeToken { denom } = &offer_asset_info {
let asset = Asset {
info: AssetInfo::NativeToken {
denom: denom.to_string(),
},
amount: return_amount,
};

return_amount = return_amount.checked_sub(asset.compute_tax(&deps.querier)?)?;
}

let mut res: SimulationResponse = deps.querier.query_wasm_smart(
let res: SimulationResponse = deps.querier.query_wasm_smart(
pair_info.contract_addr,
&PairQueryMsg::Simulation {
offer_asset: Asset {
Expand All @@ -354,18 +342,6 @@ fn simulate_swap_operations(
},
)?;

// Deduct tax
if let AssetInfo::NativeToken { denom } = ask_asset_info {
let asset = Asset {
info: AssetInfo::NativeToken { denom },
amount: res.return_amount,
};

res.return_amount = res
.return_amount
.checked_sub(asset.compute_tax(&deps.querier)?)?;
}

return_amount = res.return_amount;
}
SwapOperation::NativeSwap { .. } => {
Expand Down
42 changes: 17 additions & 25 deletions contracts/router/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ pub fn execute_swap_operation(
};

asset_into_swap_msg(
deps,
pair_info.contract_addr.to_string(),
offer_asset,
ask_asset_info,
Expand Down Expand Up @@ -85,7 +84,6 @@ pub fn execute_swap_operation(
///
/// * **single** defines whether this swap is single or part of a multi hop route.
pub fn asset_into_swap_msg(
deps: DepsMut,
pair_contract: String,
offer_asset: Asset,
ask_asset_info: AssetInfo,
Expand All @@ -97,29 +95,23 @@ pub fn asset_into_swap_msg(
let belief_price = if single { None } else { Some(Decimal::MAX) };

match &offer_asset.info {
AssetInfo::NativeToken { denom } => {
// Deduct tax first
let amount = offer_asset
.amount
.checked_sub(offer_asset.compute_tax(&deps.querier)?)?;
Ok(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: pair_contract,
funds: vec![Coin {
denom: denom.to_string(),
amount,
}],
msg: to_binary(&PairExecuteMsg::Swap {
offer_asset: Asset {
amount,
..offer_asset
},
ask_asset_info: Some(ask_asset_info),
belief_price,
max_spread,
to,
})?,
}))
}
AssetInfo::NativeToken { denom } => Ok(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: pair_contract,
funds: vec![Coin {
denom: denom.to_string(),
amount: offer_asset.amount,
}],
msg: to_binary(&PairExecuteMsg::Swap {
offer_asset: Asset {
amount: offer_asset.amount,
..offer_asset
},
ask_asset_info: Some(ask_asset_info),
belief_price,
max_spread,
to,
})?,
})),
AssetInfo::Token { contract_addr } => Ok(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: contract_addr.to_string(),
funds: vec![],
Expand Down
5 changes: 1 addition & 4 deletions contracts/router/src/testing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,17 +429,14 @@ fn query_buy_with_routes() {
assert_eq!(
res,
SimulateSwapOperationsResponse {
amount: Uint128::from(1000000u128) // tax rate in Terra is set to zero https://terrawiki.org/en/developers/tx-fees
// amount: Uint128::from(952380u128), // tax charged 1 times uusd => ukrw, ukrw => asset0000, asset0000 => uluna
amount: Uint128::from(1000000u128)
}
);

assert_eq!(
res,
SimulateSwapOperationsResponse {
amount: Uint128::from(1000000u128),
// tax rate in Terra is set to zero https://terrawiki.org/en/developers/tx-fees
// amount: Uint128::from(952380u128), // tax charged 1 times uusd => ukrw, ukrw => uluna
}
);
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/tokenomics/generator/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ pub fn send_pending_rewards(
info: asset_info,
amount: pending_proxy_rewards,
}
.into_msg(&deps.querier, to.clone())?],
.into_msg(to.clone())?],
})?,
});
}
Expand Down Expand Up @@ -1349,7 +1349,7 @@ fn send_orphan_proxy_rewards(
info: asset_info,
amount: *amount,
}
.into_msg(&deps.querier, &recipient)?],
.into_msg(&recipient)?],
})?,
})
}
Expand Down
12 changes: 4 additions & 8 deletions contracts/tokenomics/maker/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,6 @@ fn swap(
)?;

let msg = build_swap_msg(
&deps.querier,
cfg.max_spread,
&bridge_pool,
&from_token,
Expand Down Expand Up @@ -593,10 +592,9 @@ fn distribute(
amount,
};

result.push(SubMsg::new(asset.into_msg(
&deps.querier,
second_receiver_cfg.second_fee_receiver.to_string(),
)?))
result.push(SubMsg::new(
asset.into_msg(second_receiver_cfg.second_fee_receiver.to_string())?,
))
}

amount
Expand Down Expand Up @@ -632,9 +630,7 @@ fn distribute(
info: cfg.astro_token.clone(),
amount,
};
result.push(SubMsg::new(
to_staking_asset.into_msg(&deps.querier, staking_contract)?,
));
result.push(SubMsg::new(to_staking_asset.into_msg(staking_contract)?));
}
}

Expand Down
12 changes: 4 additions & 8 deletions contracts/tokenomics/maker/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn try_build_swap_msg(
amount_in: Uint128,
) -> Result<SubMsg, ContractError> {
let pool = get_pool(querier, &cfg.factory_contract, from, to)?;
let msg = build_swap_msg(querier, cfg.max_spread, &pool, from, Some(to), amount_in)?;
let msg = build_swap_msg(cfg.max_spread, &pool, from, Some(to), amount_in)?;
Ok(msg)
}

Expand All @@ -51,32 +51,28 @@ pub fn try_build_swap_msg(
///
/// * **amount_in** amount of tokens to swap.
pub fn build_swap_msg(
querier: &QuerierWrapper,
max_spread: Decimal,
pool: &PairInfo,
from: &AssetInfo,
to: Option<&AssetInfo>,
amount_in: Uint128,
) -> Result<SubMsg, ContractError> {
if from.is_native_token() {
let mut offer_asset = Asset {
let offer_asset = Asset {
info: from.clone(),
amount: amount_in,
};
// Deduct tax
let coin = offer_asset.deduct_tax(querier)?;
offer_asset.amount = coin.amount;

Ok(SubMsg::new(WasmMsg::Execute {
contract_addr: pool.contract_addr.to_string(),
msg: to_binary(&astroport::pair::ExecuteMsg::Swap {
offer_asset,
offer_asset: offer_asset.clone(),
ask_asset_info: to.cloned(),
belief_price: None,
max_spread: Some(max_spread),
to: None,
})?,
funds: vec![coin],
funds: vec![offer_asset.to_coin()?],
}))
} else {
Ok(SubMsg::new(WasmMsg::Execute {
Expand Down
Loading

0 comments on commit 285543c

Please sign in to comment.