Skip to content

Commit

Permalink
Merge pull request #53 from oraidex/feat/add-operator-fee
Browse files Browse the repository at this point in the history
Add operator fee
  • Loading branch information
tubackkhoa authored Dec 11, 2024
2 parents 1106ce6 + c8089bf commit 2889273
Show file tree
Hide file tree
Showing 11 changed files with 490 additions and 34 deletions.
15 changes: 13 additions & 2 deletions contracts/oraiswap_factory/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use oraiswap::asset::{pair_key, AssetInfo, PairInfo, PairInfoRaw};
use oraiswap::factory::{
ConfigResponse, ExecuteMsg, InstantiateMsg, MigrateMsg, PairsResponse, QueryMsg,
};
use oraiswap::pair::{InstantiateMsg as PairInstantiateMsg, DEFAULT_COMMISSION_RATE};
use oraiswap::pair::{
InstantiateMsg as PairInstantiateMsg, DEFAULT_COMMISSION_RATE, DEFAULT_OPERATOR_FEE,
};

const INSTANTIATE_REPLY_ID: u64 = 1;

Expand All @@ -36,6 +38,7 @@ pub fn instantiate(
commission_rate: msg
.commission_rate
.unwrap_or(DEFAULT_COMMISSION_RATE.to_string()),
operator_fee: msg.operator_fee.unwrap_or(DEFAULT_OPERATOR_FEE.to_string()),
};

CONFIG.save(deps.storage, &config)?;
Expand All @@ -59,7 +62,8 @@ pub fn execute(
ExecuteMsg::CreatePair {
asset_infos,
pair_admin,
} => execute_create_pair(deps, env, info, asset_infos, pair_admin),
operator,
} => execute_create_pair(deps, env, info, asset_infos, pair_admin, operator),
ExecuteMsg::AddPair { pair_info } => execute_add_pair_manually(deps, env, info, pair_info),
ExecuteMsg::MigrateContract {
contract_addr,
Expand Down Expand Up @@ -134,6 +138,7 @@ pub fn execute_create_pair(
_info: MessageInfo,
asset_infos: [AssetInfo; 2],
pair_admin: Option<String>,
operator: Option<String>,
) -> Result<Response, ContractError> {
let config: Config = CONFIG.load(deps.storage)?;
let raw_infos = [
Expand All @@ -157,10 +162,13 @@ pub fn execute_create_pair(
contract_addr: CanonicalAddr::from(vec![]),
asset_infos: raw_infos,
commission_rate: config.commission_rate.clone(),
operator_fee: config.operator_fee.clone(),
},
)?;
let pair_admin = pair_admin.unwrap_or(env.contract.address.to_string());

let operator_addr = operator.map(|op| deps.api.addr_validate(&op)).transpose()?;

Ok(Response::new()
.add_submessage(SubMsg::reply_on_success(
WasmMsg::Instantiate {
Expand All @@ -174,6 +182,8 @@ pub fn execute_create_pair(
token_code_id: config.token_code_id,
commission_rate: Some(config.commission_rate),
admin: Some(deps.api.addr_validate(&pair_admin)?),
operator_fee: Some(config.operator_fee),
operator: operator_addr,
})?,
},
INSTANTIATE_REPLY_ID,
Expand Down Expand Up @@ -222,6 +232,7 @@ pub fn execute_add_pair_manually(
.addr_canonicalize(pair_info.contract_addr.as_str())?,
asset_infos: raw_infos,
commission_rate: pair_info.commission_rate.clone(),
operator_fee: pair_info.operator_fee,
},
)?;

Expand Down
6 changes: 5 additions & 1 deletion contracts/oraiswap_factory/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Config {
pub pair_code_id: u64,
pub token_code_id: u64,
pub commission_rate: String,
pub operator_fee: String,
}

// put the length bytes at the first for compatibility with legacy singleton store
Expand Down Expand Up @@ -66,7 +67,7 @@ mod test {
bucket, bucket_read, singleton, singleton_read, Bucket, ReadonlyBucket,
};
use oraiswap::asset::pair_key;
use oraiswap::pair::DEFAULT_COMMISSION_RATE;
use oraiswap::pair::{DEFAULT_COMMISSION_RATE, DEFAULT_OPERATOR_FEE};
const KEY_CONFIG: &[u8] = b"config";

pub fn store_config(storage: &mut dyn Storage, config: &Config) -> StdResult<()> {
Expand All @@ -87,6 +88,7 @@ mod test {
pair_code_id: 1,
token_code_id: 1,
commission_rate: DEFAULT_COMMISSION_RATE.to_string(),
operator_fee: DEFAULT_OPERATOR_FEE.to_string(),
},
)
.unwrap();
Expand Down Expand Up @@ -154,6 +156,7 @@ mod test {
contract_addr: deps.api.addr_canonicalize("pair0000").unwrap(),
liquidity_token: deps.api.addr_canonicalize("liquidity0000").unwrap(),
commission_rate: DEFAULT_COMMISSION_RATE.to_string(),
operator_fee: DEFAULT_OPERATOR_FEE.to_string(),
};

let pair_info2 = PairInfoRaw {
Expand All @@ -169,6 +172,7 @@ mod test {
contract_addr: deps.api.addr_canonicalize("pair0001").unwrap(),
liquidity_token: deps.api.addr_canonicalize("liquidity0001").unwrap(),
commission_rate: DEFAULT_COMMISSION_RATE.to_string(),
operator_fee: DEFAULT_OPERATOR_FEE.to_string(),
};

store_pair(&mut deps.storage, &pair_info).unwrap();
Expand Down
6 changes: 4 additions & 2 deletions contracts/oraiswap_factory/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use cosmwasm_std::Addr;
use oraiswap::asset::{AssetInfo, PairInfo};

use oraiswap::create_entry_points_testing;
use oraiswap::pair::DEFAULT_COMMISSION_RATE;
use oraiswap::pair::{DEFAULT_COMMISSION_RATE, DEFAULT_OPERATOR_FEE};
use oraiswap::querier::query_pair_info_from_pair;
use oraiswap::testing::MockApp;

Expand Down Expand Up @@ -48,7 +48,8 @@ fn create_pair() {
liquidity_token: pair_info.liquidity_token,
contract_addr,
asset_infos,
commission_rate: DEFAULT_COMMISSION_RATE.into()
commission_rate: DEFAULT_COMMISSION_RATE.into(),
operator_fee: DEFAULT_OPERATOR_FEE.to_string()
}
);
}
Expand Down Expand Up @@ -85,6 +86,7 @@ fn add_pair() {
contract_addr: Addr::unchecked("contract_addr"),
asset_infos: asset_infos.clone(),
commission_rate: DEFAULT_COMMISSION_RATE.into(),
operator_fee: DEFAULT_OPERATOR_FEE.to_string(),
};

// add pair
Expand Down
12 changes: 6 additions & 6 deletions contracts/oraiswap_mixed_router/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ fn simulate_mixed_swap() {

let res: SimulateSwapOperationsResponse = app.query(router_addr, &msg).unwrap();

assert_eq!(res.amount, Uint128::new(99599));
assert_eq!(res.amount, Uint128::new(99500));
}

#[test]
Expand Down Expand Up @@ -918,7 +918,7 @@ fn execute_mixed_swap_operations() {
},
Coin {
denom: token_x_name.to_string(),
amount: Uint128::new(998499249564)
amount: Uint128::new(998499249465)
}
]
);
Expand Down Expand Up @@ -1089,7 +1089,7 @@ fn test_affiliates() {
vec![
Coin {
denom: token_y_name.to_string(),
amount: Uint128::new(996)
amount: Uint128::new(995)
},
Coin {
denom: token_x_name.to_string(),
Expand All @@ -1104,7 +1104,7 @@ fn test_affiliates() {
vec![
Coin {
denom: token_y_name.to_string(),
amount: Uint128::new(9960)
amount: Uint128::new(9950)
},
Coin {
denom: token_x_name.to_string(),
Expand Down Expand Up @@ -1155,7 +1155,7 @@ fn test_affiliates() {
vec![
Coin {
denom: token_y_name.to_string(),
amount: Uint128::new(996)
amount: Uint128::new(995)
},
Coin {
denom: token_x_name.to_string(),
Expand All @@ -1170,7 +1170,7 @@ fn test_affiliates() {
vec![
Coin {
denom: token_y_name.to_string(),
amount: Uint128::new(9960)
amount: Uint128::new(9950)
},
Coin {
denom: token_x_name.to_string(),
Expand Down
Loading

0 comments on commit 2889273

Please sign in to comment.