Skip to content

Instantly share code, notes, and snippets.

@ethanfrey
Created November 3, 2021 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethanfrey/f464adb600f189ead2f7a0b07404b9ba to your computer and use it in GitHub Desktop.
Save ethanfrey/f464adb600f189ead2f7a0b07404b9ba to your computer and use it in GitHub Desktop.
IBC Reply Error Handling
#[entry_point]
pub fn ibc_packet_receive(
deps: DepsMut,
_env: Env,
msg: IbcPacketReceiveMsg,
) -> Result<IbcReceiveResponse, Never> {
// other parse code here...
let msg = Cw20ExecuteMsg::Transfer {
recipient,
amount: coin.amount,
};
let exec = WasmMsg::Execute {
contract_addr: coin.address,
msg: to_binary(&msg).unwrap(),
funds: vec![],
};
let sub_msg = SubMsg::reply_on_error(exec, SEND_TOKEN_ID);
// return submessage with pre-emptive success packet in the data field
IbcReceiveResponse::new()
.set_ack(ack_success())
.add_submessage(sub_msg)
.add_attributes(attributes)
}
#[entry_point]
pub fn reply(_deps: DepsMut, _env: Env, reply: Reply) -> Result<Response, ContractError> {
if reply.id != SEND_TOKEN_ID {
return Err(ContractError::UnknownReplyId { id: reply.id });
}
// always return a success... just overwrite the data field (ack packet) on error
let res = match reply.result {
ContractResult::Ok(_) => Response::new(),
ContractResult::Err(err) => {
// encode an acknowledgement error
Response::new().set_data(ack_fail(err))
}
};
Ok(res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment