@@ -121,7 +121,11 @@ pub fn reply(deps: DepsMut, _env: Env, reply: Reply) -> Result<Response, Contrac
121121 true ,
122122 ) ?;
123123
124- Ok ( Response :: new ( ) . set_data ( ack_fail ( err) ) )
124+ Ok ( Response :: new ( ) . set_data ( ack_fail ( err) ) . add_attributes ( vec ! [
125+ attr( "undo_reduce_channel" , reply_args. channel) ,
126+ attr( "undo_reduce_channel_ibc_denom" , reply_args. denom) ,
127+ attr( "undo_reduce_channel_amount" , reply_args. amount) ,
128+ ] ) )
125129 }
126130 } ,
127131 NATIVE_RECEIVE_ID => match reply. result {
@@ -150,7 +154,12 @@ pub fn reply(deps: DepsMut, _env: Env, reply: Reply) -> Result<Response, Contrac
150154
151155 Ok ( Response :: new ( )
152156 . set_data ( ack_fail ( err. clone ( ) ) )
153- . add_attribute ( "error_transferring_ibc_tokens_to_cw20" , err) )
157+ . add_attribute ( "error_transferring_ibc_tokens_to_cw20" , err)
158+ . add_attributes ( vec ! [
159+ attr( "undo_increase_channel" , reply_args. channel) ,
160+ attr( "undo_increase_channel_ibc_denom" , reply_args. denom) ,
161+ attr( "undo_increase_channel_amount" , reply_args. amount) ,
162+ ] ) )
154163 }
155164 } ,
156165 FOLLOW_UP_FAILURE_ID => match reply. result {
@@ -440,17 +449,10 @@ pub fn get_follow_up_msgs(
440449) -> Result < ( Vec < CosmosMsg > , String ) , ContractError > {
441450 let config = CONFIG . load ( storage) ?;
442451 let mut cosmos_msgs: Vec < CosmosMsg > = vec ! [ ] ;
443- if memo. is_empty ( ) {
444- return Ok ( (
445- vec ! [ send_amount( to_send, receiver. to_string( ) , None ) ] ,
446- "" . to_string ( ) ,
447- ) ) ;
448- }
449452 let destination: DestinationInfo = DestinationInfo :: from_str ( memo) ;
450- // if destination denom, then we simply transfers cw20 to the receiver address.
451- if destination. destination_denom . is_empty ( ) {
453+ if is_follow_up_msgs_only_send_amount ( & memo, & destination. destination_denom ) {
452454 return Ok ( (
453- vec ! [ send_amount( to_send, destination . receiver. clone ( ) , None ) ] ,
455+ vec ! [ send_amount( to_send, receiver. to_string ( ) , None ) ] ,
454456 "" . to_string ( ) ,
455457 ) ) ;
456458 }
@@ -476,22 +478,25 @@ pub fn get_follow_up_msgs(
476478 config. fee_denom . as_str ( ) ,
477479 & destination. destination_denom ,
478480 ) ;
479-
480- let response: SimulateSwapOperationsResponse = querier. query_wasm_smart (
481- config. swap_router_contract . clone ( ) ,
482- & oraiswap:: router:: QueryMsg :: SimulateSwapOperations {
483- offer_amount : to_send. amount ( ) . clone ( ) ,
484- operations : swap_operations. clone ( ) ,
485- } ,
486- ) ?;
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+ }
487492
488493 let ibc_msg = build_ibc_msg (
489494 storage,
490495 env,
491496 receiver_asset_info,
492497 receiver,
493498 packet. dest . channel_id . as_str ( ) ,
494- response . amount . clone ( ) ,
499+ minimum_receive . clone ( ) ,
495500 & sender,
496501 & destination,
497502 config. default_timeout ,
@@ -508,17 +513,35 @@ pub fn get_follow_up_msgs(
508513 ibc_error_msg = ibc_msg. unwrap_err ( ) . to_string ( ) ;
509514 }
510515 build_swap_msgs (
511- response . amount ,
516+ minimum_receive ,
512517 & config. swap_router_contract ,
513518 to_send. amount ( ) ,
514519 initial_receive_asset_info,
515520 to,
516521 & mut cosmos_msgs,
517522 swap_operations,
518523 ) ?;
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+ }
519531 return Ok ( ( cosmos_msgs, ibc_error_msg) ) ;
520532}
521533
534+ pub fn is_follow_up_msgs_only_send_amount ( memo : & str , destination_denom : & str ) -> bool {
535+ if memo. is_empty ( ) {
536+ return true ;
537+ }
538+ // if destination denom, then we simply transfers cw20 to the receiver address.
539+ if destination_denom. is_empty ( ) {
540+ return true ;
541+ }
542+ false
543+ }
544+
522545pub fn build_swap_operations (
523546 receiver_asset_info : AssetInfo ,
524547 initial_receive_asset_info : AssetInfo ,
@@ -529,21 +552,21 @@ pub fn build_swap_operations(
529552 let fee_denom_asset_info = AssetInfo :: NativeToken {
530553 denom : fee_denom. to_string ( ) ,
531554 } ;
532- let mut swap_operations = vec ! [
533- SwapOperation :: OraiSwap {
555+ let mut swap_operations = vec ! [ ] ;
556+ if receiver_asset_info. eq ( & initial_receive_asset_info) {
557+ return vec ! [ ] ;
558+ }
559+ if initial_receive_asset_info. ne ( & fee_denom_asset_info) {
560+ swap_operations. push ( SwapOperation :: OraiSwap {
534561 offer_asset_info : initial_receive_asset_info. clone ( ) ,
535562 ask_asset_info : fee_denom_asset_info. clone ( ) ,
536- } ,
537- SwapOperation :: OraiSwap {
563+ } )
564+ }
565+ if destination_denom. ne ( fee_denom) {
566+ swap_operations. push ( SwapOperation :: OraiSwap {
538567 offer_asset_info : fee_denom_asset_info. clone ( ) ,
539568 ask_asset_info : receiver_asset_info,
540- } ,
541- ] ;
542- if destination_denom. eq ( fee_denom) {
543- swap_operations. pop ( ) ;
544- }
545- if initial_receive_asset_info. eq ( & fee_denom_asset_info) {
546- swap_operations. pop ( ) ;
569+ } ) ;
547570 }
548571 swap_operations
549572}
@@ -710,8 +733,9 @@ pub fn handle_follow_up_failure(
710733 false ,
711734 ) ?;
712735 response = response. add_attributes ( vec ! [
713- attr( "ibc_denom_undo_channel_balance" , ibc_data. ibc_denom) ,
714- attr( "remote_amount_undo_channel_balance" , ibc_data. remote_amount) ,
736+ attr( "undo_reduce_channel" , reply_args. channel) ,
737+ attr( "undo_reduce_channel_ibc_denom" , ibc_data. ibc_denom) ,
738+ attr( "undo_reduce_channel_balance" , ibc_data. remote_amount) ,
715739 ] ) ;
716740 }
717741 let refund_amount = Amount :: from_parts (
0 commit comments