Skip to content

Commit

Permalink
chore: add fee receiver on withdraw fee
Browse files Browse the repository at this point in the history
  • Loading branch information
trung2891 committed Oct 4, 2024
1 parent 4ff5c11 commit 54af593
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
4 changes: 3 additions & 1 deletion contracts/oraiswap-v3/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ pub fn execute(
match msg {
ExecuteMsg::ChangeAdmin { new_admin } => change_admin(deps, info, new_admin),
ExecuteMsg::WithdrawProtocolFee { pool_key } => withdraw_protocol_fee(deps, info, pool_key),
ExecuteMsg::WithdrawAllProtocolFee {} => withdraw_all_protocol_fee(deps, info),
ExecuteMsg::WithdrawAllProtocolFee { receiver } => {
withdraw_all_protocol_fee(deps, info, receiver)
}
ExecuteMsg::ChangeProtocolFee { protocol_fee } => {
change_protocol_fee(deps, info, protocol_fee)
}
Expand Down
9 changes: 7 additions & 2 deletions contracts/oraiswap-v3/src/entrypoints/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ pub fn change_admin(
/// - Reverts the call when the caller is an unauthorized receiver.
pub fn withdraw_all_protocol_fee(
deps: DepsMut,
info: MessageInfo,
mut info: MessageInfo,
receiver: Option<Addr>,
) -> Result<Response, ContractError> {
let pools: Vec<PoolWithPoolKey> = POOLS
.range_raw(deps.storage, None, None, Order::Ascending)
Expand All @@ -81,9 +82,13 @@ pub fn withdraw_all_protocol_fee(
attr("receiver", info.sender.as_str()),
];
let mut msgs = vec![];
let sender = info.sender.clone();
if let Some(receiver) = receiver {
info.sender = receiver;
}

for mut pool_info in pools {
if pool_info.pool.fee_receiver != info.sender {
if pool_info.pool.fee_receiver != sender {
continue;
}
let pool_key_db = pool_info.pool_key.key();
Expand Down
7 changes: 4 additions & 3 deletions contracts/oraiswap-v3/src/tests/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,12 @@ impl MockApp {
&mut self,
sender: &str,
dex: &str,
receiver: Option<Addr>,
) -> MockResult<ExecuteResponse> {
self.execute(
Addr::unchecked(sender),
Addr::unchecked(dex),
&oraiswap_v3_msg::ExecuteMsg::WithdrawAllProtocolFee {},
&oraiswap_v3_msg::ExecuteMsg::WithdrawAllProtocolFee { receiver },
&[],
)
}
Expand Down Expand Up @@ -1208,8 +1209,8 @@ pub mod macros {
pub(crate) use withdraw_protocol_fee;

macro_rules! withdraw_all_protocol_fee {
($app:ident, $dex_address:expr, $caller:tt) => {{
$app.withdraw_all_protocol_fee($caller, $dex_address.as_str())
($app:ident, $dex_address:expr,$receiver:expr, $caller:tt) => {{
$app.withdraw_all_protocol_fee($caller, $dex_address.as_str(), $receiver)
}};
}
pub(crate) use withdraw_all_protocol_fee;
Expand Down
47 changes: 44 additions & 3 deletions contracts/oraiswap-v3/src/tests/protocol_fee.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::coins;
use cosmwasm_std::{coins, Addr};
use decimal::*;

use crate::tests::helper::{macros::*, MockApp, FEE_DENOM};
Expand Down Expand Up @@ -64,7 +64,7 @@ fn test_withdraw_all_protocol_fee() {

let fee_tier = FeeTier::new(Percentage::from_scale(6, 3), 10).unwrap();

withdraw_all_protocol_fee!(app, dex, alice).unwrap();
withdraw_all_protocol_fee!(app, dex, None, alice).unwrap();

let amount_x = balance_of!(app, token_x, alice);
let amount_y = balance_of!(app, token_y, alice);
Expand All @@ -87,6 +87,47 @@ fn test_withdraw_all_protocol_fee() {
);
}

#[test]
fn test_withdraw_all_protocol_fee_with_receiver() {
let (mut app, accounts) = MockApp::new(&[
("alice", &coins(100_000_000_000, FEE_DENOM)),
("bob", &coins(100_000_000_000, FEE_DENOM)),
("charlie", &coins(100_000_000_000, FEE_DENOM)),
]);
let alice = &accounts[0];
let bob = &accounts[1];
let charlie = &accounts[2];

let (dex, token_x, token_y) = init_dex_and_tokens!(app, alice);
init_basic_pool!(app, dex, token_x, token_y, alice);
init_basic_position!(app, dex, token_x, token_y, alice);
init_basic_swap!(app, dex, token_x, token_y, alice, bob);

let fee_tier = FeeTier::new(Percentage::from_scale(6, 3), 10).unwrap();

withdraw_all_protocol_fee!(app, dex, Some(Addr::unchecked(charlie)), alice).unwrap();

let amount_x = balance_of!(app, token_x, charlie);
let amount_y = balance_of!(app, token_y, charlie);
assert_eq!(amount_x, 1);
assert_eq!(amount_y, 0);

let amount_x = balance_of!(app, token_x, dex);
let amount_y = balance_of!(app, token_y, dex);
assert_eq!(amount_x, 1499);
assert_eq!(amount_y, 7);

let pool_after_withdraw = get_pool!(app, dex, token_x, token_y, fee_tier).unwrap();
assert_eq!(
pool_after_withdraw.fee_protocol_token_x,
TokenAmount::new(0)
);
assert_eq!(
pool_after_withdraw.fee_protocol_token_y,
TokenAmount::new(0)
);
}

#[test]
fn test_protocol_fee_not_admin() {
let (mut app, accounts) = MockApp::new(&[
Expand Down Expand Up @@ -131,7 +172,7 @@ fn test_withdraw_all_protocol_fee_not_admin() {
init_basic_swap!(app, dex, token_x, token_y, alice, bob);
let fee_tier = FeeTier::new(Percentage::from_scale(6, 3), 10).unwrap();

withdraw_all_protocol_fee!(app, dex, bob).unwrap();
withdraw_all_protocol_fee!(app, dex, Some(Addr::unchecked(alice)), bob).unwrap();

let amount_x = balance_of!(app, token_x, alice);
let amount_y = balance_of!(app, token_y, alice);
Expand Down
4 changes: 3 additions & 1 deletion packages/oraiswap-v3-common/src/oraiswap_v3_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ pub enum ExecuteMsg {
WithdrawProtocolFee {
pool_key: PoolKey,
},
WithdrawAllProtocolFee {},
WithdrawAllProtocolFee {
receiver: Option<Addr>,
},
ChangeProtocolFee {
protocol_fee: Percentage,
},
Expand Down

0 comments on commit 54af593

Please sign in to comment.