Skip to content

Commit

Permalink
feat: add withdraws func
Browse files Browse the repository at this point in the history
  • Loading branch information
trung2891 committed Nov 22, 2024
1 parent 90511ad commit ed60246
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ tests/test-data.json
.env

.scannerwork/
clippy-report.json
clippy-report.json
.DS_Store
21 changes: 21 additions & 0 deletions contracts/cw-ics20-latest/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ pub fn execute(
args,
} => ibc_hooks_receive(deps, env, info, func, orai_receiver, args),
ExecuteMsg::RegisterDenom(msg) => register_denom(deps, info, msg),
ExecuteMsg::WithdrawAsset { coin, receiver } => {
execute_withdraw_asset(deps, info, coin, receiver)
}
}
}

Expand All @@ -178,6 +181,24 @@ pub fn is_caller_contract(caller: Addr, contract_addr: Addr) -> StdResult<()> {
Ok(())
}

// withdraw stuck coin and transfer back to user
// only owner can execute
fn execute_withdraw_asset(
deps: DepsMut,
info: MessageInfo,
coin: Amount,
receiver: Option<Addr>,
) -> Result<Response, ContractError> {
ADMIN.assert_admin(deps.as_ref(), &info.sender)?;
let receiver = receiver.unwrap_or(info.sender);

let msg = coin.send_amount(receiver.to_string(), None);

Ok(Response::new()
.add_attributes(vec![("action", "withdraw_asset")])
.add_message(msg))
}

pub fn register_denom(
deps: DepsMut,
info: MessageInfo,
Expand Down
4 changes: 4 additions & 0 deletions contracts/cw-ics20-latest/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ pub enum ExecuteMsg {
args: Binary,
},
RegisterDenom(RegisterDenomMsg),
WithdrawAsset {
coin: Amount,
receiver: Option<Addr>,
},
}

#[cw_serde]
Expand Down
51 changes: 50 additions & 1 deletion contracts/cw-ics20-latest/src/testing/ibc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use cosmwasm_std::{

use crate::error::ContractError;
use crate::state::{
get_key_ics20_ibc_denom, increase_channel_balance, reduce_channel_balance, Config,
get_key_ics20_ibc_denom, increase_channel_balance, reduce_channel_balance, Config, ADMIN,
CHANNEL_REVERSE_STATE, CONFIG, RELAYER_FEE, REPLY_ARGS, TOKEN_FEE,
};
use cw20::{Cw20CoinVerified, Cw20ExecuteMsg, Cw20ReceiveMsg};
Expand Down Expand Up @@ -2487,3 +2487,52 @@ pub fn test_get_follow_up_msg() {
);
// case 4: call universal swap (todo)
}

#[test]
fn test_withdraw_stuck_asset() {
let mut deps = mock_dependencies();
ADMIN
.set(deps.as_mut(), Some(Addr::unchecked("admin")))
.unwrap();

// case 1: unauthorized
let err = execute(
deps.as_mut(),
mock_env(),
mock_info("addr000", &[]),
ExecuteMsg::WithdrawAsset {
coin: Amount::Native(Coin {
denom: "orai".to_string(),
amount: Uint128::new(1000000),
}),
receiver: Some(Addr::unchecked("receiver")),
},
)
.unwrap_err();
assert_eq!(err, ContractError::Admin(AdminError::NotAdmin {}));

// case 2: success
let res = execute(
deps.as_mut(),
mock_env(),
mock_info("admin", &[]),
ExecuteMsg::WithdrawAsset {
coin: Amount::Native(Coin {
denom: "orai".to_string(),
amount: Uint128::new(1000000),
}),
receiver: Some(Addr::unchecked("receiver")),
},
)
.unwrap();
assert_eq!(
res.messages,
vec![SubMsg::new(CosmosMsg::Bank(BankMsg::Send {
to_address: "receiver".to_string(),
amount: vec![Coin {
denom: "orai".to_string(),
amount: Uint128::new(1000000)
}]
}))]
);
}

0 comments on commit ed60246

Please sign in to comment.