Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add withdraws func #31

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}]
}))]
);
}
Loading