Skip to content

Commit 884b545

Browse files
committed
updated logic no swap when init receiver asset equal dest asset
1 parent a39a69d commit 884b545

File tree

3 files changed

+74
-77
lines changed

3 files changed

+74
-77
lines changed
234 Bytes
Binary file not shown.

contracts/cw-ics20-latest/src/ibc.rs

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,7 @@ pub fn get_follow_up_msgs(
450450
let config = CONFIG.load(storage)?;
451451
let mut cosmos_msgs: Vec<CosmosMsg> = vec![];
452452
let destination: DestinationInfo = DestinationInfo::from_str(memo);
453-
if is_follow_up_msgs_only_send_amount(
454-
&memo,
455-
&destination.destination_denom,
456-
&initial_receive_asset_info.to_string(),
457-
&config.fee_denom,
458-
) {
453+
if is_follow_up_msgs_only_send_amount(&memo, &destination.destination_denom) {
459454
return Ok((
460455
vec![send_amount(to_send, receiver.to_string(), None)],
461456
"".to_string(),
@@ -483,22 +478,25 @@ pub fn get_follow_up_msgs(
483478
config.fee_denom.as_str(),
484479
&destination.destination_denom,
485480
);
486-
487-
let response: SimulateSwapOperationsResponse = querier.query_wasm_smart(
488-
config.swap_router_contract.clone(),
489-
&oraiswap::router::QueryMsg::SimulateSwapOperations {
490-
offer_amount: to_send.amount().clone(),
491-
operations: swap_operations.clone(),
492-
},
493-
)?;
481+
let mut minimum_receive = to_send.amount();
482+
if swap_operations.len() > 0 {
483+
let response: SimulateSwapOperationsResponse = querier.query_wasm_smart(
484+
config.swap_router_contract.clone(),
485+
&oraiswap::router::QueryMsg::SimulateSwapOperations {
486+
offer_amount: to_send.amount().clone(),
487+
operations: swap_operations.clone(),
488+
},
489+
)?;
490+
minimum_receive = response.amount;
491+
}
494492

495493
let ibc_msg = build_ibc_msg(
496494
storage,
497495
env,
498496
receiver_asset_info,
499497
receiver,
500498
packet.dest.channel_id.as_str(),
501-
response.amount.clone(),
499+
minimum_receive.clone(),
502500
&sender,
503501
&destination,
504502
config.default_timeout,
@@ -515,40 +513,32 @@ pub fn get_follow_up_msgs(
515513
ibc_error_msg = ibc_msg.unwrap_err().to_string();
516514
}
517515
build_swap_msgs(
518-
response.amount,
516+
minimum_receive,
519517
&config.swap_router_contract,
520518
to_send.amount(),
521519
initial_receive_asset_info,
522520
to,
523521
&mut cosmos_msgs,
524522
swap_operations,
525523
)?;
524+
// fallback case. If there's no cosmos messages then we return send amount
525+
if cosmos_msgs.is_empty() {
526+
return Ok((
527+
vec![send_amount(to_send, receiver.to_string(), None)],
528+
ibc_error_msg,
529+
));
530+
}
526531
return Ok((cosmos_msgs, ibc_error_msg));
527532
}
528533

529-
pub fn is_follow_up_msgs_only_send_amount(
530-
memo: &str,
531-
destination_denom: &str,
532-
initial_receive_asset_info: &str,
533-
fee_denom: &str,
534-
) -> bool {
534+
pub fn is_follow_up_msgs_only_send_amount(memo: &str, destination_denom: &str) -> bool {
535535
if memo.is_empty() {
536536
return true;
537537
}
538538
// if destination denom, then we simply transfers cw20 to the receiver address.
539539
if destination_denom.is_empty() {
540540
return true;
541541
}
542-
// special case, if inital receiver asset info is native orai (fee denom in this case), and the destination is also orai => we just transfer instead of swapping
543-
// the reason is that orai is the central asset info for swapping. There's no ORAI/ORAI pair
544-
if initial_receive_asset_info.eq(&AssetInfo::NativeToken {
545-
denom: fee_denom.to_string(),
546-
}
547-
.to_string())
548-
&& destination_denom.eq(fee_denom)
549-
{
550-
return true;
551-
}
552542
false
553543
}
554544

@@ -563,6 +553,9 @@ pub fn build_swap_operations(
563553
denom: fee_denom.to_string(),
564554
};
565555
let mut swap_operations = vec![];
556+
if receiver_asset_info.eq(&initial_receive_asset_info) {
557+
return vec![];
558+
}
566559
if initial_receive_asset_info.ne(&fee_denom_asset_info) {
567560
swap_operations.push(SwapOperation::OraiSwap {
568561
offer_asset_info: initial_receive_asset_info.clone(),

contracts/cw-ics20-latest/src/ibc_tests.rs

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,20 @@ mod test {
575575
offer_asset_info: AssetInfo::NativeToken {
576576
denom: fee_denom.clone()
577577
},
578-
ask_asset_info: receiver_asset_info
578+
ask_asset_info: receiver_asset_info.clone()
579579
}
580580
);
581+
582+
// initial = receiver => build swap ops length = 0
583+
let operations = build_swap_operations(
584+
AssetInfo::NativeToken {
585+
denom: "foobar".to_string(),
586+
},
587+
initial_asset_info.clone(),
588+
"not_foo_bar",
589+
&destination.destination_denom,
590+
);
591+
assert_eq!(operations.len(), 0);
581592
}
582593

583594
#[test]
@@ -883,6 +894,40 @@ mod test {
883894
funds: vec![]
884895
})]
885896
);
897+
898+
// 3rd case, cosmos msgs empty case, also send amount
899+
let memo = "cosmosabcd:orai";
900+
let result = get_follow_up_msgs(
901+
deps_mut.storage,
902+
deps_mut.api,
903+
&deps_mut.querier,
904+
env.clone(),
905+
Amount::Cw20(Cw20Coin {
906+
address: "foobar".to_string(),
907+
amount,
908+
}),
909+
AssetInfo::NativeToken {
910+
denom: "orai".to_string(),
911+
},
912+
"foobar",
913+
"foobar",
914+
memo,
915+
&mock_receive_packet_remote_to_local("channel", 1u128, "foobar", "foobar"),
916+
)
917+
.unwrap();
918+
919+
assert_eq!(
920+
result.0,
921+
vec![CosmosMsg::Wasm(WasmMsg::Execute {
922+
contract_addr: env.contract.address.to_string(),
923+
msg: to_binary(&Cw20ExecuteMsg::Transfer {
924+
recipient: receiver.to_string(),
925+
amount: amount.clone()
926+
})
927+
.unwrap(),
928+
funds: vec![]
929+
})]
930+
);
886931
}
887932

888933
#[test]
@@ -963,52 +1008,11 @@ mod test {
9631008

9641009
#[test]
9651010
fn test_is_follow_up_msgs_only_send_amount() {
1011+
assert_eq!(is_follow_up_msgs_only_send_amount("", "dest denom"), true);
1012+
assert_eq!(is_follow_up_msgs_only_send_amount("memo", ""), true);
9661013
assert_eq!(
967-
is_follow_up_msgs_only_send_amount("", "dest denom", "initial info", "fee denom"),
968-
true
969-
);
970-
assert_eq!(
971-
is_follow_up_msgs_only_send_amount("memo", "", "initial info", "fee denom"),
972-
true
973-
);
974-
// dest denom no equal to fee denom, return false
975-
assert_eq!(
976-
is_follow_up_msgs_only_send_amount(
977-
"memo",
978-
"dest denom",
979-
&AssetInfo::NativeToken {
980-
denom: "fee_denom".to_string()
981-
}
982-
.to_string(),
983-
"fee_denom"
984-
),
1014+
is_follow_up_msgs_only_send_amount("memo", "dest denom"),
9851015
false
9861016
);
987-
// initial no equal to fee denom => return false
988-
assert_eq!(
989-
is_follow_up_msgs_only_send_amount(
990-
"memo",
991-
"fee_denom",
992-
&AssetInfo::NativeToken {
993-
denom: "foobar".to_string()
994-
}
995-
.to_string(),
996-
"fee_denom"
997-
),
998-
false
999-
);
1000-
// both dest denom and initial equal fee denom => true
1001-
assert_eq!(
1002-
is_follow_up_msgs_only_send_amount(
1003-
"memo",
1004-
"fee_denom",
1005-
&AssetInfo::NativeToken {
1006-
denom: "fee_denom".to_string()
1007-
}
1008-
.to_string(),
1009-
"fee_denom"
1010-
),
1011-
true
1012-
);
10131017
}
10141018
}

0 commit comments

Comments
 (0)