Skip to content

Commit

Permalink
feat(fee): add fee deduction per swap
Browse files Browse the repository at this point in the history
  • Loading branch information
TropicalDog17 committed Dec 1, 2023
1 parent a5e4c49 commit 53f9eab
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
28 changes: 11 additions & 17 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cw2::set_contract_version;

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::state::{State, STATE};
use crate::state::{State, FEE_RATE, STATE};
use astroport::asset::AssetInfo;
use astroport::pair::{self};
use cosmwasm_std::{Addr, Order, StdError, WasmMsg};
Expand All @@ -21,13 +21,14 @@ pub fn instantiate(
deps: DepsMut,
_env: Env,
info: MessageInfo,
_msg: InstantiateMsg,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
let state = State {
owner: info.sender.clone(),
};
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
STATE.save(deps.storage, &state)?;
FEE_RATE.save(deps.storage, &msg.fee_rate)?;

Ok(Response::new()
.add_attribute("method", "instantiate")
Expand Down Expand Up @@ -77,19 +78,7 @@ fn handle_swap_reply(_deps: DepsMut, _env: Env, msg: Reply) -> StdResult<Respons
.iter()
.find(|e| e.key == "return_amount")
.ok_or_else(|| StdError::generic_err("unable to find coin spent event".to_string()))?;
// let spender_address = coin_spent_event
// .attributes
// .iter()
// .find(|a| a.key == "spender")
// .unwrap()
// .value
// .clone();
// let coin = Coin::from_str(&spend_amount).unwrap();
// // transfer back to user
// let msg = BankMsg::Send {
// to_address: spender_address,
// amount: vec![coin],
// };

Ok(Response::new())
}

Expand Down Expand Up @@ -211,8 +200,12 @@ pub mod execute {
asset_infos[0].to_string()
};

// Fee deduction bank
let fee_rate = FEE_RATE.load(deps.storage)?;
let fee = amount * fee_rate as u128 / 10000;

let swap_astro_msg = pair::ExecuteMsg::Swap {
offer_asset: Asset::native(&offer_asset, amount),
offer_asset: Asset::native(&offer_asset, amount - fee),
ask_asset_info: None,
belief_price: None,
max_spread: Some(Decimal::percent(50)),
Expand All @@ -221,9 +214,10 @@ pub mod execute {
let exec_cw20_mint_msg = WasmMsg::Execute {
contract_addr: pool_address.clone().to_string(),

Check failure on line 215 in src/contract.rs

View workflow job for this annotation

GitHub Actions / Lints

redundant clone
msg: to_json_binary(&swap_astro_msg)?,
funds: coins(amount, &offer_asset),
funds: coins(amount - fee, &offer_asset),
};
let submessage = SubMsg::reply_on_success(exec_cw20_mint_msg, SWAP_REPLY_ID);

let res = Response::new()
.add_submessage(submessage)
.add_attribute("action", "swap")
Expand Down
8 changes: 4 additions & 4 deletions src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mod tests {
}
fn instantiate_my_contract(app: &mut App) -> CwTemplateContract {
let my_contract_id = app.store_code(my_contract());
let msg = InstantiateMsg {};
let msg = InstantiateMsg { fee_rate: 5 };
let my_contract_addr = app
.instantiate_contract(
my_contract_id,
Expand Down Expand Up @@ -291,7 +291,7 @@ mod tests {
let mut app = mock_app();
let cw_template_id = app.store_code(contract_template());
let pair_id = app.store_code(pair_contract());
let msg = InstantiateMsg {};
let msg = InstantiateMsg { fee_rate: 5 };
let cw_template_contract_addr = app
.instantiate_contract(
cw_template_id,
Expand Down Expand Up @@ -454,7 +454,7 @@ mod tests {
},
)
.unwrap();
assert_eq!(res.unwrap(), Uint128::new(999998006002));
assert_eq!(res.unwrap(), Uint128::new(999998006501));

// let another user do swap

Expand Down Expand Up @@ -498,7 +498,7 @@ mod tests {
},
)
.unwrap();
assert_eq!(res.unwrap(), Uint128::new(999997009006));
assert_eq!(res.unwrap(), Uint128::new(999997010004));

// Check current balance of user
assert_eq!(
Expand Down
4 changes: 3 additions & 1 deletion src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::Addr;

#[cw_serde]
pub struct InstantiateMsg {}
pub struct InstantiateMsg {
pub fee_rate: u64,
}

#[cw_serde]
pub enum ExecuteMsg {
Expand Down
2 changes: 2 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ pub const POOL_CONTRACT_ADDR: Item<Addr> = Item::new("pool_contract_addr");
pub const REGISTRY_CONTRACT_ADDR: Item<Addr> = Item::new("registry_contract_addr");
pub const FACTORY_CONTRACT_ADDR: Item<Addr> = Item::new("factory_contract_addr");
pub const PAIR_CONTRACT_ADDR: Item<Addr> = Item::new("pair_contract_addr");

pub const FEE_RATE: Item<u64> = Item::new("fee_rate");

0 comments on commit 53f9eab

Please sign in to comment.